Saiyaswanth007 commited on
Commit
0d96746
·
1 Parent(s): a58ada6

Code fixing

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -10,7 +10,7 @@ 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
@@ -613,11 +613,58 @@ def get_status():
613
  return diarization_system.get_status_info()
614
 
615
 
616
- # Setup FastRTC stream handler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
617
  def setup_fastrtc_handler():
618
- """Set up FastRTC audio stream handler"""
619
  handler = DiarizationHandler(diarization_system)
620
- stream = Stream(handler=handler, modality="audio", mode="receive")
 
 
 
 
 
 
 
 
 
 
 
621
  return stream
622
 
623
 
@@ -725,6 +772,13 @@ def create_interface():
725
  This app uses FastRTC for low-latency audio streaming.
726
  For optimal performance, use a modern browser and allow microphone access when prompted.
727
  """)
 
 
 
 
 
 
 
728
 
729
  # Auto-refresh conversation and status
730
  def refresh_display():
 
10
  from scipy.spatial.distance import cosine
11
  from RealtimeSTT import AudioToTextRecorder
12
  from fastapi import FastAPI
13
+ from fastrtc import Stream, AsyncStreamHandler, ReplyOnPause, get_cloudflare_turn_credentials_async, get_cloudflare_turn_credentials
14
  import json
15
  import io
16
  import wave
 
613
  return diarization_system.get_status_info()
614
 
615
 
616
+ # Get Cloudflare TURN credentials for FastRTC
617
+ async def get_cloudflare_credentials():
618
+ # Check if HF_TOKEN is set in environment
619
+ hf_token = os.environ.get("HF_TOKEN")
620
+
621
+ # If not set, use a default Hugging Face token if available
622
+ if not hf_token:
623
+ # Log a warning that user should set their own token
624
+ print("Warning: HF_TOKEN environment variable not set. Please set your own Hugging Face token.")
625
+ # Try to use the Hugging Face token from the environment
626
+ from huggingface_hub import HfApi
627
+ try:
628
+ api = HfApi()
629
+ hf_token = api.token
630
+ if not hf_token:
631
+ print("Error: No Hugging Face token available. TURN relay may not work properly.")
632
+ except:
633
+ print("Error: Failed to get Hugging Face token. TURN relay may not work properly.")
634
+
635
+ # Get Cloudflare TURN credentials using the Hugging Face token
636
+ if hf_token:
637
+ try:
638
+ return await get_cloudflare_turn_credentials_async(hf_token=hf_token)
639
+ except Exception as e:
640
+ print(f"Error getting Cloudflare TURN credentials: {e}")
641
+
642
+ # Fallback to a default configuration that may not work
643
+ return {
644
+ "iceServers": [
645
+ {
646
+ "urls": "stun:stun.l.google.com:19302"
647
+ }
648
+ ]
649
+ }
650
+
651
+
652
+ # Setup FastRTC stream handler with TURN server configuration
653
  def setup_fastrtc_handler():
654
+ """Set up FastRTC audio stream handler with TURN server configuration"""
655
  handler = DiarizationHandler(diarization_system)
656
+
657
+ # Get server-side credentials (longer TTL)
658
+ server_credentials = get_cloudflare_turn_credentials(ttl=360000)
659
+
660
+ stream = Stream(
661
+ handler=handler,
662
+ modality="audio",
663
+ mode="receive",
664
+ rtc_configuration=get_cloudflare_credentials, # Async function for client-side credentials
665
+ server_rtc_configuration=server_credentials # Server-side credentials with longer TTL
666
+ )
667
+
668
  return stream
669
 
670
 
 
772
  This app uses FastRTC for low-latency audio streaming.
773
  For optimal performance, use a modern browser and allow microphone access when prompted.
774
  """)
775
+
776
+ # Hugging Face Token Information
777
+ gr.Markdown("""
778
+ ## 🔑 Hugging Face Token
779
+ This app uses Cloudflare TURN server via Hugging Face integration.
780
+ If audio connection fails, set your HF_TOKEN environment variable in the Space settings.
781
+ """)
782
 
783
  # Auto-refresh conversation and status
784
  def refresh_display():