Testys commited on
Commit
6b8d232
·
verified ·
1 Parent(s): 45edd80

Update pages/1_Live_Detection.py

Browse files
Files changed (1) hide show
  1. pages/1_Live_Detection.py +21 -11
pages/1_Live_Detection.py CHANGED
@@ -37,7 +37,9 @@ def load_app_config():
37
  secrets = {
38
  "gemini_api_key": os.getenv("GEMINI_API_KEY"),
39
  "turn_username": os.getenv("TURN_USERNAME"),
40
- "turn_credential": os.getenv("TURN_CREDENTIAL")
 
 
41
  }
42
  return config, secrets
43
 
@@ -91,16 +93,24 @@ st.title("📹 Live Drowsiness Detection")
91
  st.info("Press 'START' to activate your camera and begin monitoring.")
92
 
93
  # --- Dynamically Build RTC Configuration ---
94
- ice_servers = [{'urls': 'stun:global.stun.twilio.com:3478'}]
95
- if secrets["turn_username"] and secrets["turn_credential"]:
96
- turn_servers = [
97
- {'urls': 'turn:global.turn.twilio.com:3478?transport=udp', 'username': secrets["turn_username"], 'credential': secrets["turn_credential"]},
98
- {'urls': 'turn:global.turn.twilio.com:3478?transport=tcp', 'username': secrets["turn_username"], 'credential': secrets["turn_credential"]}
99
- ]
100
- ice_servers.extend(turn_servers)
101
-
102
- RTC_CONFIGURATION = RTCConfiguration({"iceServers": ice_servers})
103
-
 
 
 
 
 
 
 
 
104
 
105
  col1, col2 = st.columns([3, 1])
106
 
 
37
  secrets = {
38
  "gemini_api_key": os.getenv("GEMINI_API_KEY"),
39
  "turn_username": os.getenv("TURN_USERNAME"),
40
+ "turn_credential": os.getenv("TURN_CREDENTIAL"),
41
+ "metered_domain": os.getenv("METERED_DOMAIN"),
42
+ "metered_api_key": os.getenv("METERED_KEY")
43
  }
44
  return config, secrets
45
 
 
93
  st.info("Press 'START' to activate your camera and begin monitoring.")
94
 
95
  # --- Dynamically Build RTC Configuration ---
96
+ @st.cache_data(ttl=3600) # Cache the credentials for 1 hour
97
+ def get_ice_servers():
98
+ """Build the list of ICE servers, including STUN and TURN from Metered."""
99
+ if not secrets["metered_domain"] or not secrets["metered_api_key"]:
100
+ print("No Metered credentials found. Using public STUN servers only.")
101
+ return [{"urls": ["stun:stun.l.google.com:19302"]}]
102
+
103
+ try:
104
+ url = f"https://{secrets['metered_domain']}/api/v1/turn/credentials?apiKey={secrets['metered_api_key']}"
105
+ response = requests.get(url)
106
+ response.raise_for_status()
107
+ print("Successfully fetched ICE servers from Metered.ca")
108
+ return response.json()
109
+ except requests.exceptions.RequestException as e:
110
+ print(f"Warning: Could not fetch TURN credentials from Metered.ca: {e}")
111
+ return [{"urls": ["stun:stun.l.google.com:19302"]}]
112
+
113
+ RTC_CONFIGURATION = RTCConfiguration({"iceServers": get_ice_servers()})
114
 
115
  col1, col2 = st.columns([3, 1])
116