Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -1,25 +1,80 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
else:
|
25 |
-
st.info("
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
from transformers import pipeline
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
7 |
+
st.title("🩺 Health Report Analyzer")
|
8 |
+
|
9 |
+
# Upload PDF
|
10 |
+
uploaded_file = st.file_uploader("Upload a Health Report (PDF only)", type="pdf")
|
11 |
+
|
12 |
+
# Extract text from PDF
|
13 |
+
def extract_text(file):
|
14 |
+
doc = fitz.open(stream=file.read(), filetype="pdf")
|
15 |
+
text = ""
|
16 |
+
for page in doc:
|
17 |
+
text += page.get_text()
|
18 |
+
return text
|
19 |
+
|
20 |
+
# Load Hugging Face Models
|
21 |
+
@st.cache_resource
|
22 |
+
def load_models():
|
23 |
+
summarizer = pipeline("summarization", model="philschmid/bart-large-cnn-samsum")
|
24 |
+
explainer = pipeline("text2text-generation", model="google/flan-t5-base")
|
25 |
+
return summarizer, explainer
|
26 |
+
|
27 |
+
# MAIN LOGIC
|
28 |
+
if uploaded_file:
|
29 |
+
text_data = extract_text(uploaded_file)
|
30 |
+
st.success("PDF Text Extracted Successfully!")
|
31 |
+
st.text_area("Extracted Text", text_data, height=300)
|
32 |
+
|
33 |
+
summarizer, explainer = load_models()
|
34 |
+
|
35 |
+
if st.button("Summarize & Explain"):
|
36 |
+
with st.spinner("Generating Summary & Explanation..."):
|
37 |
+
chunks = [text_data[i:i+1000] for i in range(0, len(text_data), 1000)]
|
38 |
+
|
39 |
+
summary = ""
|
40 |
+
for chunk in chunks:
|
41 |
+
result = summarizer(chunk, max_length=300, min_length=80, do_sample=False)
|
42 |
+
summary += result[0]['summary_text'] + " "
|
43 |
+
|
44 |
+
explanation = explainer(f"Explain in simple words: {summary}")[0]['generated_text']
|
45 |
+
|
46 |
+
st.subheader("Summary")
|
47 |
+
st.write(summary)
|
48 |
+
|
49 |
+
st.subheader("Explanation")
|
50 |
+
st.write(explanation)
|
51 |
+
|
52 |
+
st.session_state['summary'] = summary
|
53 |
+
st.session_state['explanation'] = explanation
|
54 |
+
|
55 |
+
# Chatbot Section
|
56 |
+
st.subheader("AI Chatbot: Ask Anything About Your Report or Medical Terms")
|
57 |
+
|
58 |
+
if 'summary' in st.session_state and 'explanation' in st.session_state:
|
59 |
+
user_question = st.text_input("Ask a question (e.g. 'meaning of CT scan', 'explain cholecystectomy'):")
|
60 |
+
|
61 |
+
if st.button("Get AI Answer") and user_question:
|
62 |
+
with st.spinner("Thinking..."):
|
63 |
+
context = st.session_state['summary'] + "\n" + st.session_state['explanation']
|
64 |
+
prompt = (
|
65 |
+
f"You are a helpful medical assistant. "
|
66 |
+
f"If the report context below doesn't help, use your general medical knowledge.\n\n"
|
67 |
+
f"Context:\n{context}\n\n"
|
68 |
+
f"Question: What is the meaning of '{user_question}'?\n"
|
69 |
+
f"Explain it simply as if talking to a non-medical person."
|
70 |
+
)
|
71 |
+
response = explainer(prompt)[0]['generated_text']
|
72 |
+
|
73 |
+
st.success("AI's Response:")
|
74 |
+
st.write(response)
|
75 |
+
|
76 |
+
else:
|
77 |
+
st.info("Generate Summary & Explanation to activate chatbot.")
|
78 |
+
|
79 |
else:
|
80 |
+
st.info("Upload a PDF Health Report to begin.")
|