Spaces:
Sleeping
Sleeping
up
Browse files
app.py
CHANGED
@@ -1,7 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
from transformers import pipeline
|
4 |
-
from
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Streamlit page setup
|
7 |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
@@ -18,64 +22,98 @@ def extract_text(file):
|
|
18 |
text += page.get_text()
|
19 |
return text
|
20 |
|
21 |
-
# Load
|
22 |
@st.cache_resource
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Main logic
|
29 |
if uploaded_file:
|
30 |
text_data = extract_text(uploaded_file)
|
31 |
-
st.success("
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
else:
|
81 |
st.info("Upload a PDF Health Report to begin.")
|
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
from transformers import pipeline
|
4 |
+
from gtts import gTTS
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
from fpdf import FPDF
|
8 |
+
import urllib.parse
|
9 |
|
10 |
# Streamlit page setup
|
11 |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
|
|
22 |
text += page.get_text()
|
23 |
return text
|
24 |
|
25 |
+
# Load Hugging Face model for medical explanation
|
26 |
@st.cache_resource
|
27 |
+
def load_explainer():
|
28 |
+
return pipeline("text2text-generation", model="google/flan-t5-base")
|
29 |
+
|
30 |
+
# Text-to-Speech helper
|
31 |
+
def text_to_speech_base64(text):
|
32 |
+
tts = gTTS(text)
|
33 |
+
tts.save("response.mp3")
|
34 |
+
with open("response.mp3", "rb") as audio_file:
|
35 |
+
audio_bytes = audio_file.read()
|
36 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
37 |
+
audio_html = f"""
|
38 |
+
<audio controls autoplay>
|
39 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
40 |
+
Your browser does not support the audio element.
|
41 |
+
</audio>
|
42 |
+
"""
|
43 |
+
return audio_html
|
44 |
+
|
45 |
+
# PDF Export helper
|
46 |
+
def generate_pdf(name, explanation_text):
|
47 |
+
pdf = FPDF()
|
48 |
+
pdf.add_page()
|
49 |
+
pdf.set_font("Arial", "B", 16)
|
50 |
+
pdf.cell(200, 10, "Medical Explanation", ln=True, align="C")
|
51 |
+
pdf.ln(10)
|
52 |
+
pdf.set_font("Arial", size=12)
|
53 |
+
pdf.multi_cell(0, 10, explanation_text)
|
54 |
+
filename = f"{name.replace(' ', '_')}_Explanation.pdf"
|
55 |
+
pdf.output(filename)
|
56 |
+
return filename
|
57 |
|
58 |
# Main logic
|
59 |
if uploaded_file:
|
60 |
text_data = extract_text(uploaded_file)
|
61 |
+
st.success("Health Report Uploaded Successfully!")
|
62 |
+
|
63 |
+
st.markdown("### 📄 Health Report Content")
|
64 |
+
st.write(text_data)
|
65 |
+
|
66 |
+
explainer = load_explainer()
|
67 |
+
st.session_state['report_text'] = text_data
|
68 |
+
|
69 |
+
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
70 |
+
|
71 |
+
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
72 |
+
|
73 |
+
if st.button("Get AI Explanation") and user_question:
|
74 |
+
with st.spinner("Thinking..."):
|
75 |
+
|
76 |
+
prompt = (
|
77 |
+
f"You are a helpful medical assistant. "
|
78 |
+
f"Please explain the following medical term or phrase in very simple words, "
|
79 |
+
f"as if you're talking to someone with no medical background.\n\n"
|
80 |
+
f"Health Report Content:\n{st.session_state['report_text']}\n\n"
|
81 |
+
f"Term or Question: {user_question}\n\n"
|
82 |
+
f"Give a clear, beginner-friendly explanation."
|
83 |
+
)
|
84 |
+
|
85 |
+
response = explainer(prompt)[0]['generated_text']
|
86 |
+
|
87 |
+
st.success("Explanation:")
|
88 |
+
st.write(response)
|
89 |
+
|
90 |
+
st.markdown("🔊 **Hear the Explanation**")
|
91 |
+
audio_html = text_to_speech_base64(response)
|
92 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
93 |
+
|
94 |
+
if os.path.exists("response.mp3"):
|
95 |
+
os.remove("response.mp3")
|
96 |
+
|
97 |
+
st.markdown("---")
|
98 |
+
st.subheader("📤 Export or Share This Explanation")
|
99 |
+
|
100 |
+
user_name = st.text_input("Enter your name to personalize export", value="Patient")
|
101 |
+
|
102 |
+
# PDF Export
|
103 |
+
if st.button("📄 Download as PDF"):
|
104 |
+
pdf_file = generate_pdf(user_name, response)
|
105 |
+
with open(pdf_file, "rb") as f:
|
106 |
+
st.download_button("Download PDF", f, file_name=pdf_file, mime="application/pdf")
|
107 |
+
os.remove(pdf_file)
|
108 |
+
|
109 |
+
# WhatsApp Share
|
110 |
+
whatsapp_msg = urllib.parse.quote(f"Hey! I just got this medical explanation:\n\n{response}")
|
111 |
+
whatsapp_url = f"https://wa.me/?text={whatsapp_msg}"
|
112 |
+
st.markdown(f"[💬 Share via WhatsApp]({whatsapp_url})", unsafe_allow_html=True)
|
113 |
+
|
114 |
+
# Email Copy
|
115 |
+
st.markdown("📧 Copy this text for email:")
|
116 |
+
st.code(f"Subject: Medical Report Explanation\n\nHello,\n\nI wanted to share this:\n\n{response}", language="text")
|
117 |
|
118 |
else:
|
119 |
st.info("Upload a PDF Health Report to begin.")
|