gpaasch commited on
Commit
95321db
·
1 Parent(s): 65e09c8

taking out type

Browse files
Files changed (1) hide show
  1. src/app.py +44 -38
src/app.py CHANGED
@@ -175,59 +175,38 @@ def process_speech(audio_path, history):
175
  if not audio_path:
176
  return []
177
 
178
- # The audio_path now contains the transcribed text directly from Gradio
179
- transcript = audio_path
180
 
181
  # Query the symptom index
182
  diagnosis_query = f"""
183
  Given these symptoms: '{transcript}'
184
-
185
- Identify the most likely ICD-10 diagnoses and key questions to differentiate between them.
186
- Focus only on symptoms mentioned and their clinical implications.
187
  """
188
 
189
  response = symptom_index.as_query_engine().query(diagnosis_query)
190
 
191
- # Format response
192
- formatted_response = {
193
- "diagnoses": [],
194
- "confidences": [],
195
- "follow_up": str(response)
196
- }
197
-
198
  return [
199
  {"role": "user", "content": transcript},
200
- {"role": "assistant", "content": json.dumps(formatted_response)}
 
 
 
 
201
  ]
202
 
203
  except Exception as e:
204
  print(f"Error processing speech: {e}")
205
  return []
206
 
207
- def text_to_speech(text):
208
- """Convert text to speech and return audio HTML element."""
209
- tts = gTTS(text=text, lang='en')
210
- audio_fp = io.BytesIO()
211
- tts.write_to_fp(audio_fp)
212
- audio_b64 = base64.b64encode(audio_fp.getvalue()).decode()
213
- return f'<audio src="data:audio/mp3;base64,{audio_b64}" autoplay></audio>'
214
-
215
- def format_response_for_user(response_dict):
216
- """Convert JSON response to user-friendly format."""
217
- diagnoses = response_dict.get("diagnoses", [])
218
- confidences = response_dict.get("confidences", [])
219
- follow_up = response_dict.get("follow_up", "")
220
-
221
- message = ""
222
- if diagnoses and confidences:
223
- for d, c in zip(diagnoses, confidences):
224
- conf_percent = int(c * 100)
225
- message += f"Possible diagnosis ({conf_percent}% confidence): {d}\n"
226
-
227
- if follow_up:
228
- message += f"\n{follow_up}"
229
-
230
- return message
231
 
232
  # Build enhanced Gradio interface
233
  with gr.Blocks(
@@ -261,7 +240,6 @@ with gr.Blocks(
261
  # Moved microphone row above chatbot
262
  with gr.Row():
263
  microphone = gr.Audio(
264
- type="filepath", # Use filepath to get the audio file path
265
  label="Describe your symptoms",
266
  streaming=True
267
  )
@@ -302,6 +280,21 @@ with gr.Blocks(
302
  # Event handlers
303
  clear_btn.click(lambda: None, None, chatbot, queue=False)
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  def enhanced_process_speech(audio_path, history, api_key=None, model_tier="small", temp=0.7):
306
  """Handle speech processing and chat formatting."""
307
  if not audio_path:
@@ -341,6 +334,19 @@ with gr.Blocks(
341
  show_progress="hidden"
342
  )
343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  # Add footer with social links
345
  gr.Markdown("""
346
  ---
 
175
  if not audio_path:
176
  return []
177
 
178
+ # Extract just the transcribed text if it's a tuple
179
+ transcript = audio_path[1] if isinstance(audio_path, tuple) else audio_path
180
 
181
  # Query the symptom index
182
  diagnosis_query = f"""
183
  Given these symptoms: '{transcript}'
184
+ Identify the most likely ICD-10 diagnoses and key questions.
185
+ Focus on clinical implications.
 
186
  """
187
 
188
  response = symptom_index.as_query_engine().query(diagnosis_query)
189
 
 
 
 
 
 
 
 
190
  return [
191
  {"role": "user", "content": transcript},
192
+ {"role": "assistant", "content": json.dumps({
193
+ "diagnoses": [],
194
+ "confidences": [],
195
+ "follow_up": str(response)
196
+ })}
197
  ]
198
 
199
  except Exception as e:
200
  print(f"Error processing speech: {e}")
201
  return []
202
 
203
+ def update_transcription(audio_path):
204
+ """Update transcription box with speech recognition results."""
205
+ if not audio_path:
206
+ return ""
207
+ # Extract transcription from audio result
208
+ transcript = audio_path[1] if isinstance(audio_path, tuple) else audio_path
209
+ return transcript
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  # Build enhanced Gradio interface
212
  with gr.Blocks(
 
240
  # Moved microphone row above chatbot
241
  with gr.Row():
242
  microphone = gr.Audio(
 
243
  label="Describe your symptoms",
244
  streaming=True
245
  )
 
280
  # Event handlers
281
  clear_btn.click(lambda: None, None, chatbot, queue=False)
282
 
283
+ def format_response_for_user(response_dict):
284
+ """Format the assistant's response dictionary into a user-friendly string."""
285
+ diagnoses = response_dict.get("diagnoses", [])
286
+ confidences = response_dict.get("confidences", [])
287
+ follow_up = response_dict.get("follow_up", "")
288
+ result = ""
289
+ if diagnoses:
290
+ result += "Possible Diagnoses:\n"
291
+ for i, diag in enumerate(diagnoses):
292
+ conf = f" ({confidences[i]*100:.1f}%)" if i < len(confidences) else ""
293
+ result += f"- {diag}{conf}\n"
294
+ if follow_up:
295
+ result += f"\nFollow-up: {follow_up}"
296
+ return result.strip()
297
+
298
  def enhanced_process_speech(audio_path, history, api_key=None, model_tier="small", temp=0.7):
299
  """Handle speech processing and chat formatting."""
300
  if not audio_path:
 
334
  show_progress="hidden"
335
  )
336
 
337
+ microphone.stream( # Add real-time transcription updates
338
+ fn=update_transcription,
339
+ inputs=[microphone],
340
+ outputs=transcript_box,
341
+ show_progress="hidden"
342
+ )
343
+
344
+ clear_btn.click(
345
+ fn=lambda: (None, ""), # Clear both chat and transcription
346
+ outputs=[chatbot, transcript_box],
347
+ queue=False
348
+ )
349
+
350
  # Add footer with social links
351
  gr.Markdown("""
352
  ---