arthrod commited on
Commit
97198a5
Β·
verified Β·
1 Parent(s): b6b57b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -20
app.py CHANGED
@@ -16,7 +16,7 @@ Features:
16
  - Enhanced error handling and recovery
17
  - 300s timeout for real-time behavior
18
  """
19
-
20
  import asyncio
21
  import os
22
  import time
@@ -436,6 +436,39 @@ def get_connection_status():
436
  else:
437
  return f"πŸ”΄ Disconnected | Status: {app_state['last_status']}"
438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
  def create_interface():
440
  """PREMIUM: Enhanced interface with complete real-time integration"""
441
  # Initialize premium stream
@@ -487,20 +520,6 @@ def create_interface():
487
  mic_test_btn = gr.Button("πŸŽ™οΈ Test Microphone", variant="secondary")
488
  screen_share_btn = gr.Button("πŸ–₯️ Share Screen", variant="secondary")
489
 
490
- # --- Backend logic for mic test and screen sharing ---
491
- def backend_mic_test():
492
- # Simulate a backend mic test (could be extended to record/playback)
493
- if app_state.get("handler") and app_state.get("connected"):
494
- return "πŸŽ™οΈ Microphone is active and streaming to backend."
495
- return "⚠️ Please connect first to test microphone."
496
-
497
- def backend_screen_share():
498
- # Simulate backend screen sharing trigger
499
- if app_state.get("handler") and app_state.get("connected"):
500
- # In a real implementation, you might set a flag or trigger a backend event
501
- return "πŸ–₯️ Screen sharing is active and streaming to backend."
502
- return "⚠️ Please connect first to share your screen."
503
-
504
  # PREMIUM: Real-time streaming interface
505
  gr.Markdown("### πŸ“‘ Premium Real-Time Stream")
506
 
@@ -520,9 +539,9 @@ def create_interface():
520
  interactive=True
521
  )
522
 
523
- # PREMIUM: Connect streaming handlers
524
  audio_stream.stream(
525
- fn=lambda audio: app_state["handler"].receive(audio) if app_state["handler"] and app_state["connected"] else None,
526
  inputs=[audio_stream],
527
  outputs=[],
528
  time_limit=300, # Real-time optimized
@@ -530,7 +549,7 @@ def create_interface():
530
  )
531
 
532
  video_stream.stream(
533
- fn=lambda frame: app_state["handler"].video_receive(frame) if app_state["handler"] and app_state["connected"] else None,
534
  inputs=[video_stream],
535
  outputs=[],
536
  time_limit=300, # Real-time optimized
@@ -552,9 +571,9 @@ def create_interface():
552
  streaming=True
553
  )
554
 
555
- # Connect AI response handlers
556
  ai_audio_output.stream(
557
- fn=lambda: app_state["handler"].emit() if app_state["handler"] and app_state["connected"] else None,
558
  inputs=[],
559
  outputs=[ai_audio_output],
560
  time_limit=300
 
16
  - Enhanced error handling and recovery
17
  - 300s timeout for real-time behavior
18
  """
19
+ import gradio as gr
20
  import asyncio
21
  import os
22
  import time
 
436
  else:
437
  return f"πŸ”΄ Disconnected | Status: {app_state['last_status']}"
438
 
439
+
440
+
441
+
442
+
443
+ # --- Async helpers for streaming handlers ---
444
+
445
+ async def audio_stream_handler(audio):
446
+ if app_state.get("handler") and app_state.get("connected"):
447
+ return await app_state["handler"].receive(audio)
448
+ return None
449
+
450
+ async def video_stream_handler(frame):
451
+ if app_state.get("handler") and app_state.get("connected"):
452
+ return await app_state["handler"].video_receive(frame)
453
+ return None
454
+
455
+ async def ai_audio_output_handler():
456
+ if app_state.get("handler") and app_state.get("connected"):
457
+ return await app_state["handler"].emit()
458
+ return None
459
+
460
+ # --- Backend logic for mic test and screen sharing ---
461
+
462
+ def backend_mic_test():
463
+ if app_state.get("handler") and app_state.get("connected"):
464
+ return "πŸŽ™οΈ Microphone is active and streaming to backend."
465
+ return "⚠️ Please connect first to test microphone."
466
+
467
+ def backend_screen_share():
468
+ if app_state.get("handler") and app_state.get("connected"):
469
+ return "πŸ–₯️ Screen sharing is active and streaming to backend."
470
+ return "⚠️ Please connect first to share your screen."
471
+
472
  def create_interface():
473
  """PREMIUM: Enhanced interface with complete real-time integration"""
474
  # Initialize premium stream
 
520
  mic_test_btn = gr.Button("πŸŽ™οΈ Test Microphone", variant="secondary")
521
  screen_share_btn = gr.Button("πŸ–₯️ Share Screen", variant="secondary")
522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523
  # PREMIUM: Real-time streaming interface
524
  gr.Markdown("### πŸ“‘ Premium Real-Time Stream")
525
 
 
539
  interactive=True
540
  )
541
 
542
+ # PREMIUM: Connect streaming handlers with async functions
543
  audio_stream.stream(
544
+ fn=audio_stream_handler,
545
  inputs=[audio_stream],
546
  outputs=[],
547
  time_limit=300, # Real-time optimized
 
549
  )
550
 
551
  video_stream.stream(
552
+ fn=video_stream_handler,
553
  inputs=[video_stream],
554
  outputs=[],
555
  time_limit=300, # Real-time optimized
 
571
  streaming=True
572
  )
573
 
574
+ # Connect AI response handlers with async function
575
  ai_audio_output.stream(
576
+ fn=ai_audio_output_handler,
577
  inputs=[],
578
  outputs=[ai_audio_output],
579
  time_limit=300