Spaces:
Sleeping
Sleeping
up
Browse files
app.py
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
-
from transformers import pipeline
|
4 |
from gtts import gTTS
|
5 |
import tempfile
|
6 |
import os
|
7 |
-
import
|
8 |
-
from PIL import Image
|
9 |
-
import io
|
10 |
|
11 |
# Streamlit page setup
|
12 |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
@@ -23,10 +20,36 @@ def extract_text(file):
|
|
23 |
text += page.get_text()
|
24 |
return text
|
25 |
|
26 |
-
# Load
|
27 |
@st.cache_resource
|
28 |
-
def
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Main logic
|
32 |
if uploaded_file:
|
@@ -37,52 +60,32 @@ if uploaded_file:
|
|
37 |
st.markdown("### 📄 Health Report Content")
|
38 |
st.write(text_data)
|
39 |
|
40 |
-
explainer =
|
41 |
-
|
42 |
-
# Store extracted text in session
|
43 |
st.session_state['report_text'] = text_data
|
44 |
|
45 |
-
# Chatbot
|
46 |
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
47 |
|
48 |
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
49 |
|
50 |
if st.button("Get AI Explanation") and user_question:
|
51 |
with st.spinner("Thinking..."):
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
f"You are a friendly and experienced medical assistant. "
|
56 |
-
f"Explain this term in very simple language. "
|
57 |
-
f"Provide a short definition and mention why it's important or what it means in a report.\n\n"
|
58 |
-
f"Question: {user_question}"
|
59 |
-
)
|
60 |
-
|
61 |
-
response = explainer(prompt, max_length=300)[0]['generated_text']
|
62 |
|
63 |
st.success("Explanation:")
|
64 |
-
st.write(
|
|
|
|
|
|
|
65 |
|
66 |
# Text-to-speech using gTTS
|
67 |
-
tts = gTTS(text=
|
68 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
69 |
tts.save(temp_audio.name)
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
st.audio(audio_bytes, format='audio/mp3')
|
74 |
-
|
75 |
-
# Generate QR Code for the explanation text
|
76 |
-
if st.button("📱 Generate QR Code to Share Explanation"):
|
77 |
-
qr = qrcode.QRCode(version=1, box_size=10, border=5)
|
78 |
-
qr.add_data(response[:300]) # Keep text short for QR
|
79 |
-
qr.make(fit=True)
|
80 |
-
|
81 |
-
img = qr.make_image(fill='black', back_color='white')
|
82 |
-
|
83 |
-
buf = io.BytesIO()
|
84 |
-
img.save(buf)
|
85 |
-
st.image(buf.getvalue(), caption="Scan to Read Explanation", use_column_width=False)
|
86 |
|
87 |
else:
|
88 |
st.info("Upload a PDF Health Report to begin.")
|
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
|
|
3 |
from gtts import gTTS
|
4 |
import tempfile
|
5 |
import os
|
6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
|
|
|
|
7 |
|
8 |
# Streamlit page setup
|
9 |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
|
|
20 |
text += page.get_text()
|
21 |
return text
|
22 |
|
23 |
+
# Load Medical Domain Model (Bio_ClinicalBERT)
|
24 |
@st.cache_resource
|
25 |
+
def load_medical_explainer():
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
|
27 |
+
model = AutoModelForSequenceClassification.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
|
28 |
+
return pipeline("text-classification", model=model, tokenizer=tokenizer)
|
29 |
+
|
30 |
+
# Doctor recommendation logic
|
31 |
+
def suggest_doctor(term):
|
32 |
+
term = term.lower()
|
33 |
+
if "kidney" in term or "creatinine" in term:
|
34 |
+
return "You should consider seeing a **nephrologist**."
|
35 |
+
elif "hemoglobin" in term:
|
36 |
+
return "You might want to consult a **hematologist** if your hemoglobin is outside the normal range."
|
37 |
+
elif "liver" in term or "bilirubin" in term:
|
38 |
+
return "Consult a **hepatologist** for liver-related concerns."
|
39 |
+
elif "sugar" in term or "glucose" in term:
|
40 |
+
return "A **diabetologist** or **endocrinologist** is recommended for sugar level issues."
|
41 |
+
return "Please consult a **general physician** for further evaluation."
|
42 |
+
|
43 |
+
# Normal ranges (very basic examples)
|
44 |
+
def get_reference_range(term):
|
45 |
+
term = term.lower()
|
46 |
+
if "hemoglobin" in term:
|
47 |
+
return "Normal range: 13.8 to 17.2 g/dL for men, 12.1 to 15.1 g/dL for women."
|
48 |
+
elif "creatinine" in term:
|
49 |
+
return "Normal range: 0.74 to 1.35 mg/dL for men, 0.59 to 1.04 mg/dL for women."
|
50 |
+
elif "bilirubin" in term:
|
51 |
+
return "Normal range: 0.1 to 1.2 mg/dL."
|
52 |
+
return "Range data not available for this term."
|
53 |
|
54 |
# Main logic
|
55 |
if uploaded_file:
|
|
|
60 |
st.markdown("### 📄 Health Report Content")
|
61 |
st.write(text_data)
|
62 |
|
63 |
+
explainer = load_medical_explainer()
|
|
|
|
|
64 |
st.session_state['report_text'] = text_data
|
65 |
|
|
|
66 |
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
67 |
|
68 |
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
69 |
|
70 |
if st.button("Get AI Explanation") and user_question:
|
71 |
with st.spinner("Thinking..."):
|
72 |
+
explanation = f"{user_question.capitalize()} is a common term in medical reports."
|
73 |
+
range_info = get_reference_range(user_question)
|
74 |
+
suggestion = suggest_doctor(user_question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
st.success("Explanation:")
|
77 |
+
st.write(explanation)
|
78 |
+
|
79 |
+
st.info(f"📏 Reference Range: {range_info}")
|
80 |
+
st.warning(f"👨⚕️ Doctor Suggestion: {suggestion}")
|
81 |
|
82 |
# Text-to-speech using gTTS
|
83 |
+
tts = gTTS(text=f"{explanation}. {range_info}. {suggestion}")
|
84 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
85 |
tts.save(temp_audio.name)
|
86 |
|
87 |
+
with open(temp_audio.name, 'rb') as audio_file:
|
88 |
+
st.audio(audio_file.read(), format='audio/mp3')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
else:
|
91 |
st.info("Upload a PDF Health Report to begin.")
|