Saiyaswanth007 commited on
Commit
cba237d
·
1 Parent(s): 4562675

Check point 4

Browse files
Files changed (1) hide show
  1. app.py +49 -12
app.py CHANGED
@@ -635,11 +635,12 @@ audio_handler = None
635
 
636
  def initialize_system():
637
  """Initialize the diarization system"""
638
- global audio_handler
639
  try:
640
  success = diarization_system.initialize_models()
641
  if success:
642
- audio_handler = DiarizationHandler(diarization_system)
 
643
  return "✅ System initialized successfully!"
644
  else:
645
  return "❌ Failed to initialize system. Check logs for details."
@@ -651,11 +652,14 @@ def start_recording():
651
  """Start recording and transcription"""
652
  try:
653
  result = diarization_system.start_recording()
654
- # FastRTC connection is handled through the mounted stream
655
  return result
656
  except Exception as e:
657
  return f"❌ Failed to start recording: {str(e)}"
658
 
 
 
 
 
659
  def stop_recording():
660
  """Stop recording and transcription"""
661
  try:
@@ -702,11 +706,11 @@ def create_interface():
702
 
703
  with gr.Row():
704
  with gr.Column(scale=2):
705
- # Add WebRTC component for audio streaming
706
- audio_webrtc = WebRTC(
707
  label="Audio Input",
708
- source_url="/stream", # Connect to the FastRTC stream endpoint
709
- rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
710
  )
711
 
712
  # Conversation display
@@ -782,7 +786,6 @@ def create_interface():
782
 
783
  def on_start():
784
  result = start_recording()
785
- # FastRTC connection is handled through the mounted stream
786
  return result, gr.update(interactive=False), gr.update(interactive=True)
787
 
788
  def on_stop():
@@ -838,16 +841,50 @@ def create_interface():
838
  status_timer = gr.Timer(2)
839
  status_timer.tick(refresh_status, outputs=[status_output])
840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
841
  return interface
842
 
843
 
844
  # FastAPI setup for FastRTC integration
845
  app = FastAPI()
846
 
847
- # Initialize audio handler for FastRTC
848
- audio_handler = DiarizationHandler(diarization_system)
849
- stream = Stream(handler=audio_handler, modality="audio", mode="send-receive")
850
- stream.mount(app) # Mount the Stream to the FastAPI app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
 
852
  @app.get("/")
853
  async def root():
 
635
 
636
  def initialize_system():
637
  """Initialize the diarization system"""
638
+ global stream
639
  try:
640
  success = diarization_system.initialize_models()
641
  if success:
642
+ # Update the Stream's handler to use our DiarizationHandler
643
+ stream.handler = DiarizationHandler(diarization_system)
644
  return "✅ System initialized successfully!"
645
  else:
646
  return "❌ Failed to initialize system. Check logs for details."
 
652
  """Start recording and transcription"""
653
  try:
654
  result = diarization_system.start_recording()
 
655
  return result
656
  except Exception as e:
657
  return f"❌ Failed to start recording: {str(e)}"
658
 
659
+ def on_start():
660
+ result = start_recording()
661
+ return result, gr.update(interactive=False), gr.update(interactive=True)
662
+
663
  def stop_recording():
664
  """Stop recording and transcription"""
665
  try:
 
706
 
707
  with gr.Row():
708
  with gr.Column(scale=2):
709
+ # Replace WebRTC with standard Gradio audio component
710
+ audio_component = gr.Audio(
711
  label="Audio Input",
712
+ sources=["microphone"],
713
+ streaming=True
714
  )
715
 
716
  # Conversation display
 
786
 
787
  def on_start():
788
  result = start_recording()
 
789
  return result, gr.update(interactive=False), gr.update(interactive=True)
790
 
791
  def on_stop():
 
841
  status_timer = gr.Timer(2)
842
  status_timer.tick(refresh_status, outputs=[status_output])
843
 
844
+ # Process audio from Gradio component
845
+ def process_audio_input(audio_data):
846
+ if audio_data is not None and diarization_system.is_running:
847
+ # Extract audio data
848
+ if isinstance(audio_data, tuple) and len(audio_data) >= 2:
849
+ sample_rate, audio_array = audio_data[0], audio_data[1]
850
+ diarization_system.process_audio_chunk(audio_array, sample_rate)
851
+ return get_conversation()
852
+
853
+ # Connect audio component to processing function
854
+ audio_component.stream(
855
+ fn=process_audio_input,
856
+ outputs=[conversation_output]
857
+ )
858
+
859
  return interface
860
 
861
 
862
  # FastAPI setup for FastRTC integration
863
  app = FastAPI()
864
 
865
+ # Create a placeholder handler - will be properly initialized later
866
+ class DefaultHandler(AsyncStreamHandler):
867
+ def __init__(self):
868
+ super().__init__()
869
+
870
+ async def receive(self, frame):
871
+ pass
872
+
873
+ async def emit(self):
874
+ return None
875
+
876
+ def copy(self):
877
+ return DefaultHandler()
878
+
879
+ async def shutdown(self):
880
+ pass
881
+
882
+ async def start_up(self):
883
+ pass
884
+
885
+ # Initialize with placeholder handler
886
+ stream = Stream(handler=DefaultHandler(), modality="audio", mode="send-receive")
887
+ stream.mount(app)
888
 
889
  @app.get("/")
890
  async def root():