Saiyaswanth007 commited on
Commit
55a6464
·
1 Parent(s): b6d6c89

Revert portg

Browse files
Files changed (1) hide show
  1. app.py +65 -11
app.py CHANGED
@@ -16,6 +16,7 @@ import io
16
  import wave
17
  import asyncio
18
  import uvicorn
 
19
 
20
  # Simplified configuration parameters
21
  SILENCE_THRESHS = [0, 0.4]
@@ -866,17 +867,48 @@ def create_app():
866
  }
867
  else:
868
  # Get Cloudflare TURN credentials
869
- turn_credentials = get_cloudflare_turn_credentials(hf_token)
870
- rtc_config = {
871
- "iceServers": [
872
- {"urls": "stun:stun.l.google.com:19302"},
873
- {
874
- "urls": f"turn:{turn_credentials['urls'][0]}",
875
- "username": turn_credentials["username"],
876
- "credential": turn_credentials["credential"]
877
- }
878
- ]
879
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
880
 
881
  # Create FastRTC stream
882
  stream = Stream(
@@ -898,6 +930,22 @@ def create_app():
898
  return app
899
 
900
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
901
  # Main entry point
902
  if __name__ == "__main__":
903
  # Create the app
@@ -907,6 +955,12 @@ if __name__ == "__main__":
907
  host = os.environ.get("HOST", "0.0.0.0")
908
  port = int(os.environ.get("PORT", 7860))
909
 
 
 
 
 
 
 
910
  print(f"""
911
  🎤 Real-time Speaker Diarization Server
912
  =====================================
 
16
  import wave
17
  import asyncio
18
  import uvicorn
19
+ import socket
20
 
21
  # Simplified configuration parameters
22
  SILENCE_THRESHS = [0, 0.4]
 
867
  }
868
  else:
869
  # Get Cloudflare TURN credentials
870
+ try:
871
+ turn_credentials = get_cloudflare_turn_credentials(hf_token)
872
+
873
+ # Safely extract credentials from the response
874
+ ice_servers = []
875
+
876
+ # Always add STUN server
877
+ ice_servers.append({"urls": "stun:stun.l.google.com:19302"})
878
+
879
+ # Check for and add TURN server if available
880
+ if turn_credentials and isinstance(turn_credentials, dict):
881
+ # Handle different possible structures
882
+ if 'iceServers' in turn_credentials:
883
+ # If credentials already have iceServers, use them directly
884
+ rtc_config = turn_credentials
885
+ elif 'urls' in turn_credentials and isinstance(turn_credentials['urls'], list) and turn_credentials['urls']:
886
+ # Structure: {urls: [...], username: "...", credential: "..."}
887
+ ice_servers.append({
888
+ "urls": [f"turn:{url}" for url in turn_credentials["urls"]],
889
+ "username": turn_credentials.get("username", ""),
890
+ "credential": turn_credentials.get("credential", "")
891
+ })
892
+ rtc_config = {"iceServers": ice_servers}
893
+ elif 'url' in turn_credentials:
894
+ # Structure with single URL
895
+ ice_servers.append({
896
+ "urls": f"turn:{turn_credentials['url']}",
897
+ "username": turn_credentials.get("username", ""),
898
+ "credential": turn_credentials.get("credential", "")
899
+ })
900
+ rtc_config = {"iceServers": ice_servers}
901
+ else:
902
+ print("Warning: Unexpected TURN credentials format. Using STUN only.")
903
+ rtc_config = {"iceServers": ice_servers}
904
+ else:
905
+ print("Warning: Could not get TURN credentials. Using STUN only.")
906
+ rtc_config = {"iceServers": ice_servers}
907
+ except Exception as e:
908
+ print(f"Warning: Error getting TURN credentials: {e}. Using STUN only.")
909
+ rtc_config = {
910
+ "iceServers": [{"urls": "stun:stun.l.google.com:19302"}]
911
+ }
912
 
913
  # Create FastRTC stream
914
  stream = Stream(
 
930
  return app
931
 
932
 
933
+ # Function to find an available port
934
+ def find_available_port(start_port=7860, max_tries=10):
935
+ """Find an available port starting from start_port"""
936
+ for port_offset in range(max_tries):
937
+ port = start_port + port_offset
938
+ try:
939
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
940
+ sock.bind(('0.0.0.0', port))
941
+ sock.close()
942
+ return port
943
+ except OSError:
944
+ continue
945
+ # If no ports are available, return a default and let the server handle the error
946
+ return start_port
947
+
948
+
949
  # Main entry point
950
  if __name__ == "__main__":
951
  # Create the app
 
955
  host = os.environ.get("HOST", "0.0.0.0")
956
  port = int(os.environ.get("PORT", 7860))
957
 
958
+ # Find available port if specified port is in use
959
+ available_port = find_available_port(port)
960
+ if available_port != port:
961
+ print(f"Port {port} is in use, using port {available_port} instead.")
962
+ port = available_port
963
+
964
  print(f"""
965
  🎤 Real-time Speaker Diarization Server
966
  =====================================