Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -1,15 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
from transformers import pipeline
|
4 |
-
|
5 |
-
import
|
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="
|
12 |
-
st.title("
|
13 |
|
14 |
# Upload PDF
|
15 |
uploaded_file = st.file_uploader("Upload a Health Report (PDF only)", type="pdf")
|
@@ -25,34 +23,16 @@ def extract_text(file):
|
|
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-
|
29 |
-
|
30 |
-
# Text-to-
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
with
|
35 |
-
|
36 |
-
|
37 |
-
|
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
|
@@ -60,60 +40,42 @@ 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
|
78 |
-
f"
|
79 |
-
f"
|
80 |
-
f"
|
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 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
os.remove(
|
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.")
|
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
from transformers import pipeline
|
4 |
+
import pyttsx3
|
5 |
+
import tempfile
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
# Streamlit page setup
|
9 |
+
st.set_page_config(page_title="Health Report Analyzer", page_icon="")
|
10 |
+
st.title(" Health Report Analyzer")
|
11 |
|
12 |
# Upload PDF
|
13 |
uploaded_file = st.file_uploader("Upload a Health Report (PDF only)", type="pdf")
|
|
|
23 |
# Load Hugging Face model for medical explanation
|
24 |
@st.cache_resource
|
25 |
def load_explainer():
|
26 |
+
return pipeline("text2text-generation", model="google/flan-t5-large")
|
27 |
+
|
28 |
+
# Text-to-speech function
|
29 |
+
def speak_text(text):
|
30 |
+
engine = pyttsx3.init()
|
31 |
+
engine.setProperty('rate', 150)
|
32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
33 |
+
filename = f.name
|
34 |
+
engine.save_to_file(text, filename)
|
35 |
+
engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
return filename
|
37 |
|
38 |
# Main logic
|
|
|
40 |
text_data = extract_text(uploaded_file)
|
41 |
st.success("Health Report Uploaded Successfully!")
|
42 |
|
43 |
+
# Display the report text
|
44 |
st.markdown("### 📄 Health Report Content")
|
45 |
st.write(text_data)
|
46 |
|
47 |
explainer = load_explainer()
|
48 |
+
|
49 |
+
# Store extracted text in session
|
50 |
st.session_state['report_text'] = text_data
|
51 |
|
52 |
+
# Chatbot
|
53 |
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
54 |
|
55 |
+
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
56 |
|
57 |
if st.button("Get AI Explanation") and user_question:
|
58 |
with st.spinner("Thinking..."):
|
59 |
|
60 |
+
# Enhanced prompt for better answers
|
61 |
prompt = (
|
62 |
+
f"You are a friendly and experienced medical assistant. "
|
63 |
+
f"Explain this term in very simple language. "
|
64 |
+
f"Provide a short definition and mention why it's important or what it means in a report.\n\n"
|
65 |
+
f"Question: {user_question}"
|
|
|
|
|
66 |
)
|
67 |
|
68 |
+
response = explainer(prompt, max_length=300)[0]['generated_text']
|
69 |
|
70 |
st.success("Explanation:")
|
71 |
st.write(response)
|
72 |
|
73 |
+
# Play text as speech
|
74 |
+
if st.button("🎧 Listen to Explanation"):
|
75 |
+
audio_file = speak_text(response)
|
76 |
+
audio_bytes = open(audio_file, 'rb').read()
|
77 |
+
st.audio(audio_bytes, format='audio/mp3')
|
78 |
+
os.remove(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
else:
|
81 |
st.info("Upload a PDF Health Report to begin.")
|