Saiyaswanth007 commited on
Commit
a58ada6
·
1 Parent(s): 7bc4c94

Code fixing

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -9,11 +9,13 @@ import urllib.request
9
  import torchaudio
10
  from scipy.spatial.distance import cosine
11
  from RealtimeSTT import AudioToTextRecorder
 
12
  from fastrtc import Stream, AsyncStreamHandler, ReplyOnPause
13
  import json
14
  import io
15
  import wave
16
  import asyncio
 
17
 
18
  # Simplified configuration parameters
19
  SILENCE_THRESHS = [0, 0.4]
@@ -621,7 +623,7 @@ def setup_fastrtc_handler():
621
 
622
  # Create Gradio interface
623
  def create_interface():
624
- with gr.Blocks(title="Real-time Speaker Diarization", theme=gr.themes.Monochrome()) as app:
625
  gr.Markdown("# 🎤 Real-time Speech Recognition with Speaker Diarization")
626
  gr.Markdown("This app performs real-time speech recognition with automatic speaker identification and color-coding.")
627
 
@@ -639,7 +641,15 @@ def create_interface():
639
  <script>
640
  document.getElementById('start-fastrtc').addEventListener('click', function() {
641
  document.getElementById('fastrtc-status').textContent = 'Connecting...';
642
- // FastRTC will be initialized here by the middleware
 
 
 
 
 
 
 
 
643
  });
644
  </script>
645
  </div>
@@ -790,29 +800,25 @@ def create_interface():
790
  outputs=[conversation_output, status_output]
791
  )
792
 
793
- return app
794
 
795
 
796
- async def main():
797
- # Setup FastRTC stream
798
- stream = setup_fastrtc_handler()
799
-
800
- # Create Gradio app
801
- app = create_interface()
802
-
803
- # Mount FastRTC stream to the Gradio app
804
- # stream.mount(app)
805
- # Mount FastRTC stream to the underlying FastAPI app
806
- stream.mount(app.app)
807
-
808
- # Launch the app
809
- app.launch(
810
- server_name="0.0.0.0",
811
- server_port=7860,
812
- share=True
813
- )
814
 
 
 
 
 
815
 
816
  if __name__ == "__main__":
817
- # Run the async application
818
- asyncio.run(main())
 
9
  import torchaudio
10
  from scipy.spatial.distance import cosine
11
  from RealtimeSTT import AudioToTextRecorder
12
+ from fastapi import FastAPI
13
  from fastrtc import Stream, AsyncStreamHandler, ReplyOnPause
14
  import json
15
  import io
16
  import wave
17
  import asyncio
18
+ import uvicorn
19
 
20
  # Simplified configuration parameters
21
  SILENCE_THRESHS = [0, 0.4]
 
623
 
624
  # Create Gradio interface
625
  def create_interface():
626
+ with gr.Blocks(title="Real-time Speaker Diarization", theme=gr.themes.Monochrome()) as interface:
627
  gr.Markdown("# 🎤 Real-time Speech Recognition with Speaker Diarization")
628
  gr.Markdown("This app performs real-time speech recognition with automatic speaker identification and color-coding.")
629
 
 
641
  <script>
642
  document.getElementById('start-fastrtc').addEventListener('click', function() {
643
  document.getElementById('fastrtc-status').textContent = 'Connecting...';
644
+ // FastRTC will initialize the connection
645
+ fetch('/start-rtc', { method: 'POST' })
646
+ .then(response => response.text())
647
+ .then(data => {
648
+ document.getElementById('fastrtc-status').textContent = 'Connected! Speak now...';
649
+ })
650
+ .catch(error => {
651
+ document.getElementById('fastrtc-status').textContent = 'Connection error: ' + error;
652
+ });
653
  });
654
  </script>
655
  </div>
 
800
  outputs=[conversation_output, status_output]
801
  )
802
 
803
+ return interface
804
 
805
 
806
+ # Create FastAPI app and set up routes
807
+ app = FastAPI()
808
+
809
+ # Initialize FastRTC stream
810
+ rtc_stream = setup_fastrtc_handler()
811
+ rtc_stream.mount(app)
812
+
813
+ # Create Gradio interface
814
+ gradio_interface = create_interface()
815
+ app = gr.mount_gradio_app(app, gradio_interface, path="/")
 
 
 
 
 
 
 
 
816
 
817
+ # Add endpoint to start RTC connection
818
+ @app.post("/start-rtc")
819
+ def start_rtc():
820
+ return {"status": "success"}
821
 
822
  if __name__ == "__main__":
823
+ # Run with uvicorn
824
+ uvicorn.run(app, host="0.0.0.0", port=7860)