Joffrey Thomas commited on
Commit
f67b151
·
1 Parent(s): 68d5702

error handling

Browse files
Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -333,6 +333,13 @@ def start_websocket(session):
333
  # Cleanup happens in websocket_handler's finally block
334
 
335
 
 
 
 
 
 
 
 
336
  def auto_start_recording(session):
337
  """Automatically start the transcription service when audio begins."""
338
  # Protect against startup races: Gradio can call `process_audio` concurrently.
@@ -361,6 +368,7 @@ def auto_start_recording(session):
361
 
362
  def clear_history(session):
363
  """Stop the websocket connection and clear all history."""
 
364
  session.is_running = False
365
  session.last_audio_time = None
366
 
@@ -387,6 +395,7 @@ def clear_history(session):
387
 
388
  def process_audio(audio, session):
389
  """Process incoming audio and queue for streaming."""
 
390
  try:
391
  # Quick return if audio is None
392
  if audio is None:
@@ -439,7 +448,11 @@ def process_audio(audio, session):
439
  return get_transcription_html(session.transcription_text, session.status_message, session.current_wpm)
440
  except Exception as e:
441
  print(f"Error processing audio: {e}")
442
- return get_transcription_html(session.transcription_text, session.status_message, session.current_wpm)
 
 
 
 
443
 
444
 
445
  def _safe_queue_put(q, item):
 
333
  # Cleanup happens in websocket_handler's finally block
334
 
335
 
336
+ def ensure_session(session):
337
+ """Ensure we have a valid UserSession instance (not the lambda factory)."""
338
+ if session is None or callable(session):
339
+ return UserSession()
340
+ return session
341
+
342
+
343
  def auto_start_recording(session):
344
  """Automatically start the transcription service when audio begins."""
345
  # Protect against startup races: Gradio can call `process_audio` concurrently.
 
368
 
369
  def clear_history(session):
370
  """Stop the websocket connection and clear all history."""
371
+ session = ensure_session(session)
372
  session.is_running = False
373
  session.last_audio_time = None
374
 
 
395
 
396
  def process_audio(audio, session):
397
  """Process incoming audio and queue for streaming."""
398
+ session = ensure_session(session)
399
  try:
400
  # Quick return if audio is None
401
  if audio is None:
 
448
  return get_transcription_html(session.transcription_text, session.status_message, session.current_wpm)
449
  except Exception as e:
450
  print(f"Error processing audio: {e}")
451
+ # Safely get session attributes with fallbacks
452
+ text = getattr(session, 'transcription_text', '') if not callable(session) else ''
453
+ status = getattr(session, 'status_message', 'error') if not callable(session) else 'error'
454
+ wpm = getattr(session, 'current_wpm', '') if not callable(session) else ''
455
+ return get_transcription_html(text, status, wpm)
456
 
457
 
458
  def _safe_queue_put(q, item):