Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,39 +4,28 @@ from PIL import Image
|
|
4 |
import PyPDF2
|
5 |
import tempfile
|
6 |
import os
|
|
|
7 |
from dotenv import load_dotenv
|
8 |
import time
|
9 |
from gtts import gTTS
|
10 |
import base64
|
11 |
-
import requests # Added for Hugging Face API requests
|
12 |
-
from google.api_core import exceptions
|
13 |
|
14 |
load_dotenv()
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
if not
|
19 |
st.error("Gemini API key not found. Please set the GEMINI_API_KEY environment variable.")
|
20 |
st.stop()
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
# Configure the Hugging Face model for image analysis
|
26 |
-
huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
|
27 |
-
if not huggingface_api_key:
|
28 |
-
st.error("Hugging Face API key not found. Please set the HUGGINGFACE_API_KEY environment variable.")
|
29 |
-
st.stop()
|
30 |
-
|
31 |
-
HUGGINGFACE_API_URL = os.getenv("HUGGINGFACE_API_URL")
|
32 |
-
if not HUGGINGFACE_API_URL:
|
33 |
-
st.error("Hugging Face API URL not found. Please set the HUGGINGFACE_API_URL environment variable.")
|
34 |
-
st.stop()
|
35 |
|
36 |
MAX_RETRIES = 3
|
37 |
RETRY_DELAY = 2 # seconds
|
38 |
|
39 |
-
#
|
40 |
LANGUAGES = {
|
41 |
"English": "en",
|
42 |
"Spanish": "es",
|
@@ -44,10 +33,11 @@ LANGUAGES = {
|
|
44 |
"German": "de",
|
45 |
"Italian": "it",
|
46 |
"Portuguese": "pt",
|
47 |
-
"Urdu": "ur"
|
|
|
48 |
}
|
49 |
|
50 |
-
def
|
51 |
prompt = "Analyze this medical report concisely. Provide key findings, diagnoses, and recommendations:"
|
52 |
|
53 |
# Adjust prompt language if not English
|
@@ -58,13 +48,18 @@ def analyze_text_report(content, lang):
|
|
58 |
"de": "Analysieren Sie diesen medizinischen Bericht kurz und prägnant. Geben Sie wichtige Ergebnisse, Diagnosen und Empfehlungen an:",
|
59 |
"it": "Analizza questo rapporto medico in modo conciso. Fornisci risultati chiave, diagnosi e raccomandazioni:",
|
60 |
"pt": "Analise este relatório médico de forma concisa. Forneça os principais resultados, diagnósticos e recomendações:",
|
61 |
-
"ur": "اس طبی رپورٹ کا مختصر تجزیہ کریں۔ اہم نتائج، تشخیصات، اور سفارشات فراہم کریں:"
|
|
|
62 |
}
|
63 |
prompt = translations.get(lang, prompt)
|
64 |
|
65 |
for attempt in range(MAX_RETRIES):
|
66 |
try:
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
return response.text
|
69 |
except exceptions.GoogleAPIError as e:
|
70 |
if attempt < MAX_RETRIES - 1:
|
@@ -72,104 +67,40 @@ def analyze_text_report(content, lang):
|
|
72 |
time.sleep(RETRY_DELAY)
|
73 |
else:
|
74 |
st.error(f"Failed to analyze the report after {MAX_RETRIES} attempts. Error: {str(e)}")
|
75 |
-
return fallback_analysis(content,
|
76 |
|
77 |
-
def
|
78 |
-
|
79 |
-
|
80 |
-
"Content-Type": "application/octet-stream"
|
81 |
-
}
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
for condition in result:
|
95 |
-
label = condition.get('label', 'Unknown')
|
96 |
-
score = condition.get('score', 0)
|
97 |
-
analysis += f"{label}: {score:.2f}\n"
|
98 |
-
elif isinstance(result, dict):
|
99 |
-
for key, value in result.items():
|
100 |
-
analysis += f"{key}: {value:.2f}\n"
|
101 |
-
else:
|
102 |
-
st.warning("Unexpected response format from Hugging Face API.")
|
103 |
-
return fallback_analysis(None, "image")
|
104 |
-
return analysis
|
105 |
-
elif response.status_code == 503:
|
106 |
-
# Model is loading
|
107 |
-
st.warning("Model is loading. Waiting for 30 seconds before retrying...")
|
108 |
-
time.sleep(30)
|
109 |
-
continue
|
110 |
-
else:
|
111 |
-
st.warning(f"Hugging Face API returned status code {response.status_code}: {response.text}")
|
112 |
-
if attempt < MAX_RETRIES - 1:
|
113 |
-
st.warning(f"Retrying in {RETRY_DELAY} seconds... (Attempt {attempt + 1}/{MAX_RETRIES})")
|
114 |
-
time.sleep(RETRY_DELAY)
|
115 |
-
else:
|
116 |
-
st.error(f"Failed to analyze the image after {MAX_RETRIES} attempts.")
|
117 |
-
return fallback_analysis(None, "image")
|
118 |
-
except Exception as e:
|
119 |
-
if attempt < MAX_RETRIES - 1:
|
120 |
-
st.warning(f"An error occurred: {str(e)}. Retrying in {RETRY_DELAY} seconds... (Attempt {attempt + 1}/{MAX_RETRIES})")
|
121 |
-
time.sleep(RETRY_DELAY)
|
122 |
-
else:
|
123 |
-
st.error(f"Failed to analyze the image after {MAX_RETRIES} attempts. Error: {str(e)}")
|
124 |
-
return fallback_analysis(None, "image")
|
125 |
-
|
126 |
-
def fallback_analysis(content, content_type):
|
127 |
-
st.warning("Using fallback analysis method due to API issues.")
|
128 |
-
if content_type == "image":
|
129 |
-
return "Unable to analyze the image due to API issues. Please try again later or consult a medical professional for accurate interpretation."
|
130 |
-
else: # text
|
131 |
-
word_count = len(content.split()) if content else 0
|
132 |
-
return f"""
|
133 |
-
**Fallback Analysis:**
|
134 |
-
1. **Document Type:** Text-based medical report
|
135 |
-
2. **Word Count:** Approximately {word_count} words
|
136 |
-
3. **Content:** The document appears to contain medical information, but detailed analysis is unavailable due to technical issues.
|
137 |
-
4. **Recommendation:** Please review the document manually or consult with a healthcare professional for accurate interpretation.
|
138 |
-
5. **Note:** This is a simplified analysis due to temporary unavailability of the AI service. For a comprehensive analysis, please try again later.
|
139 |
-
"""
|
140 |
|
141 |
def extract_text_from_pdf(pdf_file):
|
|
|
142 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
|
|
|
143 |
text = ""
|
144 |
-
for
|
145 |
-
|
146 |
-
|
147 |
-
text += page_text
|
148 |
-
return text
|
149 |
|
150 |
-
|
151 |
-
tts = gTTS(text=text, lang=lang_code)
|
152 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
|
153 |
-
tts.save(tmp_file.name)
|
154 |
-
return tmp_file.name
|
155 |
-
|
156 |
-
def audio_player(audio_file_path):
|
157 |
-
with open(audio_file_path, "rb") as audio_file:
|
158 |
-
audio_bytes = audio_file.read()
|
159 |
-
b64_audio = base64.b64encode(audio_bytes).decode()
|
160 |
-
audio_html = f"""
|
161 |
-
<audio controls>
|
162 |
-
<source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3">
|
163 |
-
Your browser does not support the audio element.
|
164 |
-
</audio>
|
165 |
-
"""
|
166 |
-
st.markdown(audio_html, unsafe_allow_html=True)
|
167 |
|
168 |
def main():
|
169 |
st.title("AI-driven Medical Report Analyzer with Multilingual Audio Feedback")
|
170 |
st.write("Upload a medical report (image or PDF) for analysis")
|
171 |
|
172 |
-
# Language selection
|
173 |
language = st.selectbox("Select language for analysis and audio feedback:", list(LANGUAGES.keys()))
|
174 |
lang_code = LANGUAGES[language]
|
175 |
|
@@ -187,11 +118,11 @@ def main():
|
|
187 |
|
188 |
if st.button("Analyze Image Report"):
|
189 |
with st.spinner("Analyzing the medical report image..."):
|
190 |
-
analysis =
|
191 |
st.subheader("Analysis Results:")
|
192 |
st.write(analysis)
|
193 |
|
194 |
-
# Generate
|
195 |
audio_path = generate_tts_audio(analysis, lang_code)
|
196 |
st.write("Listen to the analysis:")
|
197 |
audio_player(audio_path)
|
@@ -212,16 +143,20 @@ def main():
|
|
212 |
with open(tmp_file_path, 'rb') as pdf_file:
|
213 |
pdf_text = extract_text_from_pdf(pdf_file)
|
214 |
|
215 |
-
analysis =
|
216 |
st.subheader("Analysis Results:")
|
217 |
st.write(analysis)
|
218 |
|
219 |
-
# Generate
|
220 |
audio_path = generate_tts_audio(analysis, lang_code)
|
221 |
st.write("Listen to the analysis:")
|
222 |
audio_player(audio_path)
|
223 |
|
224 |
os.unlink(tmp_file_path)
|
225 |
|
|
|
|
|
|
|
|
|
226 |
if __name__ == "__main__":
|
227 |
main()
|
|
|
4 |
import PyPDF2
|
5 |
import tempfile
|
6 |
import os
|
7 |
+
from google.api_core import exceptions
|
8 |
from dotenv import load_dotenv
|
9 |
import time
|
10 |
from gtts import gTTS
|
11 |
import base64
|
|
|
|
|
12 |
|
13 |
load_dotenv()
|
14 |
|
15 |
+
# Load the API key from the environment variable
|
16 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
17 |
+
if not api_key:
|
18 |
st.error("Gemini API key not found. Please set the GEMINI_API_KEY environment variable.")
|
19 |
st.stop()
|
20 |
|
21 |
+
# Configure the Gemini API
|
22 |
+
genai.configure(api_key=api_key)
|
23 |
+
model = genai.GenerativeModel('gemini-1.5-flash') # Initialize the model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
MAX_RETRIES = 3
|
26 |
RETRY_DELAY = 2 # seconds
|
27 |
|
28 |
+
# Add Chinese (Simplified) to language support
|
29 |
LANGUAGES = {
|
30 |
"English": "en",
|
31 |
"Spanish": "es",
|
|
|
33 |
"German": "de",
|
34 |
"Italian": "it",
|
35 |
"Portuguese": "pt",
|
36 |
+
"Urdu": "ur",
|
37 |
+
"Chinese (Simplified)": "zh-cn"
|
38 |
}
|
39 |
|
40 |
+
def analyze_medical_report(content, content_type, lang):
|
41 |
prompt = "Analyze this medical report concisely. Provide key findings, diagnoses, and recommendations:"
|
42 |
|
43 |
# Adjust prompt language if not English
|
|
|
48 |
"de": "Analysieren Sie diesen medizinischen Bericht kurz und prägnant. Geben Sie wichtige Ergebnisse, Diagnosen und Empfehlungen an:",
|
49 |
"it": "Analizza questo rapporto medico in modo conciso. Fornisci risultati chiave, diagnosi e raccomandazioni:",
|
50 |
"pt": "Analise este relatório médico de forma concisa. Forneça os principais resultados, diagnósticos e recomendações:",
|
51 |
+
"ur": "اس طبی رپورٹ کا مختصر تجزیہ کریں۔ اہم نتائج، تشخیصات، اور سفارشات فراہم کریں:",
|
52 |
+
"zh-cn": "简明分析此医疗报告。提供关键发现、诊断和建议:"
|
53 |
}
|
54 |
prompt = translations.get(lang, prompt)
|
55 |
|
56 |
for attempt in range(MAX_RETRIES):
|
57 |
try:
|
58 |
+
if content_type == "image":
|
59 |
+
response = model.generate_content([prompt, content])
|
60 |
+
else: # text
|
61 |
+
response = model.generate_content(f"{prompt}\n\n{content}")
|
62 |
+
|
63 |
return response.text
|
64 |
except exceptions.GoogleAPIError as e:
|
65 |
if attempt < MAX_RETRIES - 1:
|
|
|
67 |
time.sleep(RETRY_DELAY)
|
68 |
else:
|
69 |
st.error(f"Failed to analyze the report after {MAX_RETRIES} attempts. Error: {str(e)}")
|
70 |
+
return fallback_analysis(content, content_type)
|
71 |
|
72 |
+
def generate_tts_audio(text, lang_code):
|
73 |
+
# Generate TTS audio from the provided text and language code
|
74 |
+
tts = gTTS(text=text, lang=lang_code)
|
|
|
|
|
75 |
|
76 |
+
# Save the audio to a temporary file
|
77 |
+
audio_path = "audio_output.mp3"
|
78 |
+
tts.save(audio_path)
|
79 |
+
|
80 |
+
return audio_path
|
81 |
+
|
82 |
+
def audio_player(audio_path):
|
83 |
+
# Display an audio player in Streamlit
|
84 |
+
audio_file = open(audio_path, "rb")
|
85 |
+
audio_bytes = audio_file.read()
|
86 |
+
st.audio(audio_bytes, format="audio/mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
def extract_text_from_pdf(pdf_file):
|
89 |
+
# Create a PDF reader object
|
90 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
91 |
+
|
92 |
+
# Extract text from each page
|
93 |
text = ""
|
94 |
+
for page_num in range(len(pdf_reader.pages)):
|
95 |
+
page = pdf_reader.pages[page_num]
|
96 |
+
text += page.extract_text()
|
|
|
|
|
97 |
|
98 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
def main():
|
101 |
st.title("AI-driven Medical Report Analyzer with Multilingual Audio Feedback")
|
102 |
st.write("Upload a medical report (image or PDF) for analysis")
|
103 |
|
|
|
104 |
language = st.selectbox("Select language for analysis and audio feedback:", list(LANGUAGES.keys()))
|
105 |
lang_code = LANGUAGES[language]
|
106 |
|
|
|
118 |
|
119 |
if st.button("Analyze Image Report"):
|
120 |
with st.spinner("Analyzing the medical report image..."):
|
121 |
+
analysis = analyze_medical_report(image, "image", lang_code)
|
122 |
st.subheader("Analysis Results:")
|
123 |
st.write(analysis)
|
124 |
|
125 |
+
# Generate audio of the analysis
|
126 |
audio_path = generate_tts_audio(analysis, lang_code)
|
127 |
st.write("Listen to the analysis:")
|
128 |
audio_player(audio_path)
|
|
|
143 |
with open(tmp_file_path, 'rb') as pdf_file:
|
144 |
pdf_text = extract_text_from_pdf(pdf_file)
|
145 |
|
146 |
+
analysis = analyze_medical_report(pdf_text, "text", lang_code)
|
147 |
st.subheader("Analysis Results:")
|
148 |
st.write(analysis)
|
149 |
|
150 |
+
# Generate audio of the analysis
|
151 |
audio_path = generate_tts_audio(analysis, lang_code)
|
152 |
st.write("Listen to the analysis:")
|
153 |
audio_player(audio_path)
|
154 |
|
155 |
os.unlink(tmp_file_path)
|
156 |
|
157 |
+
# Footer with "Made by Shan"
|
158 |
+
st.markdown("---")
|
159 |
+
st.markdown("<p style='text-align: center;'>😎Made by Shan-Ul-Haq😎</p>", unsafe_allow_html=True)
|
160 |
+
|
161 |
if __name__ == "__main__":
|
162 |
main()
|