Spaces:
Sleeping
Sleeping
up
Browse files
app.py
CHANGED
@@ -4,26 +4,11 @@ from transformers import pipeline
|
|
4 |
from gtts import gTTS
|
5 |
import tempfile
|
6 |
import os
|
7 |
-
from googletrans import Translator
|
8 |
|
9 |
# Streamlit page setup
|
10 |
st.set_page_config(page_title="Health Report Analyzer", page_icon="🩺")
|
11 |
st.title("🩺 Health Report Analyzer")
|
12 |
|
13 |
-
# Language selection
|
14 |
-
lang_map = {
|
15 |
-
"English": "en",
|
16 |
-
"Hindi (हिंदी)": "hi",
|
17 |
-
"Punjabi (ਪੰਜਾਬੀ)": "pa",
|
18 |
-
"Bengali (বাংলা)": "bn",
|
19 |
-
"Tamil (தமிழ்)": "ta",
|
20 |
-
"Telugu (తెలుగు)": "te",
|
21 |
-
"Gujarati (ગુજરાતી)": "gu",
|
22 |
-
"Marathi (मराठी)": "mr"
|
23 |
-
}
|
24 |
-
language_choice = st.selectbox("Choose output language:", list(lang_map.keys()))
|
25 |
-
target_lang = lang_map[language_choice]
|
26 |
-
|
27 |
# Upload PDF
|
28 |
uploaded_file = st.file_uploader("Upload a Health Report (PDF only)", type="pdf")
|
29 |
|
@@ -35,31 +20,26 @@ def extract_text(file):
|
|
35 |
text += page.get_text()
|
36 |
return text
|
37 |
|
38 |
-
# Load
|
39 |
@st.cache_resource
|
40 |
def load_explainer():
|
41 |
return pipeline("text2text-generation", model="google/flan-t5-large")
|
42 |
|
43 |
-
# Translate text using Google Translate
|
44 |
-
def translate_text(text, target_language):
|
45 |
-
if target_language == "en":
|
46 |
-
return text # No translation needed
|
47 |
-
translator = Translator()
|
48 |
-
translated = translator.translate(text, dest=target_language)
|
49 |
-
return translated.text
|
50 |
-
|
51 |
# Main logic
|
52 |
if uploaded_file:
|
53 |
text_data = extract_text(uploaded_file)
|
54 |
-
st.success("
|
55 |
|
56 |
# Display the report text
|
57 |
st.markdown("### 📄 Health Report Content")
|
58 |
st.write(text_data)
|
59 |
|
60 |
explainer = load_explainer()
|
|
|
|
|
61 |
st.session_state['report_text'] = text_data
|
62 |
|
|
|
63 |
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
64 |
|
65 |
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
@@ -67,7 +47,7 @@ if uploaded_file:
|
|
67 |
if st.button("Get AI Explanation") and user_question:
|
68 |
with st.spinner("Thinking..."):
|
69 |
|
70 |
-
#
|
71 |
prompt = (
|
72 |
f"You are a friendly and experienced medical assistant. "
|
73 |
f"Explain this term in very simple language. "
|
@@ -77,19 +57,17 @@ if uploaded_file:
|
|
77 |
|
78 |
response = explainer(prompt, max_length=300)[0]['generated_text']
|
79 |
|
80 |
-
# Translate if selected language is not English
|
81 |
-
translated_response = translate_text(response, target_lang)
|
82 |
-
|
83 |
st.success("Explanation:")
|
84 |
-
st.write(
|
85 |
|
86 |
-
# Text-to-speech
|
87 |
-
tts = gTTS(text=
|
88 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
89 |
tts.save(temp_audio.name)
|
90 |
|
91 |
audio_file = open(temp_audio.name, 'rb')
|
92 |
-
|
|
|
93 |
|
94 |
else:
|
95 |
-
st.info("Upload a PDF Health Report to begin.")
|
|
|
4 |
from gtts import gTTS
|
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")
|
14 |
|
|
|
20 |
text += page.get_text()
|
21 |
return text
|
22 |
|
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 |
# Main logic
|
29 |
if uploaded_file:
|
30 |
text_data = extract_text(uploaded_file)
|
31 |
+
st.success("Health Report Uploaded Successfully!")
|
32 |
|
33 |
# Display the report text
|
34 |
st.markdown("### 📄 Health Report Content")
|
35 |
st.write(text_data)
|
36 |
|
37 |
explainer = load_explainer()
|
38 |
+
|
39 |
+
# Store extracted text in session
|
40 |
st.session_state['report_text'] = text_data
|
41 |
|
42 |
+
# Chatbot
|
43 |
st.subheader("💬 Ask About Any Medical Term or Part of the Report")
|
44 |
|
45 |
user_question = st.text_input("Enter a medical term or question (e.g. 'CT scan', 'Explain creatinine'):")
|
|
|
47 |
if st.button("Get AI Explanation") and user_question:
|
48 |
with st.spinner("Thinking..."):
|
49 |
|
50 |
+
# Enhanced prompt for better answers
|
51 |
prompt = (
|
52 |
f"You are a friendly and experienced medical assistant. "
|
53 |
f"Explain this term in very simple language. "
|
|
|
57 |
|
58 |
response = explainer(prompt, max_length=300)[0]['generated_text']
|
59 |
|
|
|
|
|
|
|
60 |
st.success("Explanation:")
|
61 |
+
st.write(response)
|
62 |
|
63 |
+
# Text-to-speech using gTTS
|
64 |
+
tts = gTTS(text=response)
|
65 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
66 |
tts.save(temp_audio.name)
|
67 |
|
68 |
audio_file = open(temp_audio.name, 'rb')
|
69 |
+
audio_bytes = audio_file.read()
|
70 |
+
st.audio(audio_bytes, format='audio/mp3')
|
71 |
|
72 |
else:
|
73 |
+
st.info("Upload a PDF Health Report to begin.")
|