Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -135,15 +135,17 @@ def analyze_symptoms(text):
|
|
135 |
return f"Error analyzing symptoms: {str(e)}", 0.0
|
136 |
|
137 |
def handle_health_query(query, language="en"):
|
138 |
-
"""Handle health-related queries with a
|
139 |
if not query:
|
140 |
return "Please provide a valid health query."
|
141 |
# Placeholder for Q&A logic (could integrate a model like BERT for Q&A)
|
142 |
-
|
143 |
-
|
|
|
|
|
144 |
|
145 |
def analyze_voice(audio_file, language="en"):
|
146 |
-
"""Analyze voice for health indicators."""
|
147 |
try:
|
148 |
# Ensure unique file name
|
149 |
unique_path = f"/tmp/gradio/{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
@@ -163,25 +165,39 @@ def analyze_voice(audio_file, language="en"):
|
|
163 |
if "Error transcribing" in transcription:
|
164 |
return transcription
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
|
174 |
-
prediction, score = analyze_symptoms(transcription)
|
175 |
-
if "Error analyzing" in prediction:
|
176 |
-
return prediction
|
177 |
|
178 |
-
#
|
179 |
-
if
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
else:
|
182 |
-
feedback
|
183 |
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
186 |
|
187 |
# Clean up temporary audio file
|
@@ -202,9 +218,10 @@ def create_gradio_interface():
|
|
202 |
"""
|
203 |
# Health Voice Analyzer
|
204 |
Record or upload a voice sample describing symptoms in English, Spanish, Hindi, or Mandarin (e.g., 'I have a fever').
|
205 |
-
Ask health questions in the text box below.
|
|
|
206 |
**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice.
|
207 |
-
**
|
208 |
"""
|
209 |
)
|
210 |
with gr.Row():
|
|
|
135 |
return f"Error analyzing symptoms: {str(e)}", 0.0
|
136 |
|
137 |
def handle_health_query(query, language="en"):
|
138 |
+
"""Handle health-related queries with a general response."""
|
139 |
if not query:
|
140 |
return "Please provide a valid health query."
|
141 |
# Placeholder for Q&A logic (could integrate a model like BERT for Q&A)
|
142 |
+
restricted_terms = ["medicine", "treatment", "drug", "prescription"]
|
143 |
+
if any(term in query.lower() for term in restricted_terms):
|
144 |
+
return "This tool does not provide medication or treatment advice. Please ask about symptoms or general health information (e.g., 'What are symptoms of asthma?')."
|
145 |
+
return f"Response to query '{query}': For accurate health information, consult a healthcare provider."
|
146 |
|
147 |
def analyze_voice(audio_file, language="en"):
|
148 |
+
"""Analyze voice for health indicators and handle queries."""
|
149 |
try:
|
150 |
# Ensure unique file name
|
151 |
unique_path = f"/tmp/gradio/{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
|
|
165 |
if "Error transcribing" in transcription:
|
166 |
return transcription
|
167 |
|
168 |
+
# Split transcription into symptom and query parts
|
169 |
+
symptom_text = transcription
|
170 |
+
query_text = None
|
171 |
+
restricted_terms = ["medicine", "treatment", "drug", "prescription"]
|
172 |
+
for term in restricted_terms:
|
173 |
+
if term in transcription.lower():
|
174 |
+
# Split at the first restricted term
|
175 |
+
split_index = transcription.lower().find(term)
|
176 |
+
symptom_text = transcription[:split_index].strip()
|
177 |
+
query_text = transcription[split_index:].strip()
|
178 |
+
break
|
179 |
|
180 |
+
feedback = ""
|
|
|
|
|
|
|
181 |
|
182 |
+
# Analyze symptoms if present
|
183 |
+
if symptom_text:
|
184 |
+
prediction, score = analyze_symptoms(symptom_text)
|
185 |
+
if "Error analyzing" in prediction:
|
186 |
+
feedback += prediction + "\n"
|
187 |
+
elif prediction == "No health condition predicted":
|
188 |
+
feedback += "No significant health indicators detected.\n"
|
189 |
+
else:
|
190 |
+
feedback += f"Possible health condition: {prediction} (confidence: {score:.4f}). Consult a doctor.\n"
|
191 |
else:
|
192 |
+
feedback += "No symptoms detected in the audio.\n"
|
193 |
|
194 |
+
# Handle query if present
|
195 |
+
if query_text:
|
196 |
+
feedback += f"\nQuery detected: '{query_text}'\n"
|
197 |
+
feedback += handle_health_query(query_text, language) + "\n"
|
198 |
+
|
199 |
+
# Add debug info and disclaimer
|
200 |
+
feedback += f"\n**Debug Info**: Transcription = '{transcription}', File Hash = {file_hash}"
|
201 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
202 |
|
203 |
# Clean up temporary audio file
|
|
|
218 |
"""
|
219 |
# Health Voice Analyzer
|
220 |
Record or upload a voice sample describing symptoms in English, Spanish, Hindi, or Mandarin (e.g., 'I have a fever').
|
221 |
+
Ask health questions in the text box below (e.g., 'What are symptoms of asthma?').
|
222 |
+
**Note**: Do not ask for medication or treatment advice; focus on symptoms or general health questions.
|
223 |
**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice.
|
224 |
+
**Text-to-Speech**: Available in the web frontend (Salesforce Sites) using the browser's Web Speech API.
|
225 |
"""
|
226 |
)
|
227 |
with gr.Row():
|