RathodHarish commited on
Commit
98eae58
·
verified ·
1 Parent(s): 5cd70e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -21
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 simple 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
- response = f"Response to query '{query}': For accurate health information, consult a healthcare provider."
143
- return response
 
 
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
- # Check for medication-related queries
167
- if "medicine" in transcription.lower() or "treatment" in transcription.lower():
168
- feedback = "Error: This tool does not provide medication or treatment advice. Please describe symptoms only (e.g., 'I have a fever')."
169
- feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', File Hash = {file_hash}"
170
- feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
171
- return feedback
 
 
 
 
 
172
 
173
- # Analyze symptoms
174
- prediction, score = analyze_symptoms(transcription)
175
- if "Error analyzing" in prediction:
176
- return prediction
177
 
178
- # Generate feedback
179
- if prediction == "No health condition predicted":
180
- feedback = "No significant health indicators detected."
 
 
 
 
 
 
181
  else:
182
- feedback = f"Possible health condition: {prediction} (confidence: {score:.4f}). Consult a doctor."
183
 
184
- feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', Prediction = {prediction}, Confidence = {score:.4f}, File Hash = {file_hash}"
 
 
 
 
 
 
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. Supports WAV, 16kHz audio.
 
206
  **Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice.
207
- **Note**: Text-to-speech is available in the web frontend (Salesforce Sites) using the browser's Web Speech API.
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():