Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,12 @@ import gradio as gr
|
|
9 |
import numpy as np
|
10 |
import websockets.sync.client
|
11 |
from dotenv import load_dotenv
|
12 |
-
from gradio_webrtc import StreamHandler, WebRTC
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
load_dotenv()
|
15 |
|
@@ -192,12 +197,18 @@ Start by introducing yourself and asking how you can help them prepare for their
|
|
192 |
api_key = os.getenv('GEMINI_API_KEY')
|
193 |
if not api_key:
|
194 |
print("Error: GEMINI_API_KEY environment variable not set")
|
|
|
|
|
195 |
return
|
196 |
self.config = GeminiConfig(api_key)
|
197 |
|
198 |
try:
|
199 |
if not self.ws:
|
200 |
self._initialize_websocket()
|
|
|
|
|
|
|
|
|
201 |
|
202 |
_, array = frame
|
203 |
array = array.squeeze()
|
@@ -308,11 +319,20 @@ class PreconsultationApp:
|
|
308 |
|
309 |
with gr.Row():
|
310 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
webrtc = WebRTC(
|
312 |
label="Voice Consultation",
|
313 |
modality="audio",
|
314 |
mode="send-receive",
|
315 |
-
rtc_configuration=
|
316 |
)
|
317 |
|
318 |
with gr.Column(scale=1):
|
@@ -376,14 +396,25 @@ class PreconsultationApp:
|
|
376 |
</div>
|
377 |
""")
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
return demo
|
380 |
|
381 |
def launch(self):
|
382 |
-
# Check if API key is set
|
383 |
if not os.getenv('GEMINI_API_KEY'):
|
384 |
-
print("
|
385 |
print("You can get a Gemini API key from: https://ai.google.dev/gemini-api/docs/api-key")
|
386 |
-
|
387 |
|
388 |
self.demo.launch(
|
389 |
server_name="0.0.0.0",
|
|
|
9 |
import numpy as np
|
10 |
import websockets.sync.client
|
11 |
from dotenv import load_dotenv
|
12 |
+
from gradio_webrtc import StreamHandler, WebRTC
|
13 |
+
try:
|
14 |
+
from gradio_webrtc import get_twilio_turn_credentials
|
15 |
+
TWILIO_AVAILABLE = True
|
16 |
+
except Exception:
|
17 |
+
TWILIO_AVAILABLE = False
|
18 |
|
19 |
load_dotenv()
|
20 |
|
|
|
197 |
api_key = os.getenv('GEMINI_API_KEY')
|
198 |
if not api_key:
|
199 |
print("Error: GEMINI_API_KEY environment variable not set")
|
200 |
+
if self.channel:
|
201 |
+
self.channel.send("API key not configured. Please set GEMINI_API_KEY environment variable.")
|
202 |
return
|
203 |
self.config = GeminiConfig(api_key)
|
204 |
|
205 |
try:
|
206 |
if not self.ws:
|
207 |
self._initialize_websocket()
|
208 |
+
|
209 |
+
if not self.ws:
|
210 |
+
print("Failed to establish WebSocket connection")
|
211 |
+
return
|
212 |
|
213 |
_, array = frame
|
214 |
array = array.squeeze()
|
|
|
319 |
|
320 |
with gr.Row():
|
321 |
with gr.Column(scale=2):
|
322 |
+
# Try to get Twilio credentials, fall back to basic configuration
|
323 |
+
rtc_config = None
|
324 |
+
try:
|
325 |
+
if TWILIO_AVAILABLE:
|
326 |
+
rtc_config = get_twilio_turn_credentials()
|
327 |
+
except Exception as e:
|
328 |
+
print(f"Warning: Could not get Twilio TURN credentials: {e}")
|
329 |
+
print("Using basic WebRTC configuration")
|
330 |
+
|
331 |
webrtc = WebRTC(
|
332 |
label="Voice Consultation",
|
333 |
modality="audio",
|
334 |
mode="send-receive",
|
335 |
+
rtc_configuration=rtc_config,
|
336 |
)
|
337 |
|
338 |
with gr.Column(scale=1):
|
|
|
396 |
</div>
|
397 |
""")
|
398 |
|
399 |
+
# Add environment variable requirements notice
|
400 |
+
if not os.getenv('GEMINI_API_KEY'):
|
401 |
+
gr.HTML("""
|
402 |
+
<div style='text-align: center; margin-top: 10px; padding: 15px; background-color: #fee2e2; border-radius: 8px'>
|
403 |
+
<p style='margin: 0; color: #dc2626'>
|
404 |
+
<strong>Setup Required:</strong> Please set the GEMINI_API_KEY environment variable.
|
405 |
+
<br>Get your key from: <a href="https://ai.google.dev/gemini-api/docs/api-key" target="_blank">Google AI Studio</a>
|
406 |
+
</p>
|
407 |
+
</div>
|
408 |
+
""")
|
409 |
+
|
410 |
return demo
|
411 |
|
412 |
def launch(self):
|
413 |
+
# Check if API key is set but don't block launch
|
414 |
if not os.getenv('GEMINI_API_KEY'):
|
415 |
+
print("Warning: GEMINI_API_KEY environment variable not set")
|
416 |
print("You can get a Gemini API key from: https://ai.google.dev/gemini-api/docs/api-key")
|
417 |
+
print("The interface will launch, but voice functionality won't work without the API key.")
|
418 |
|
419 |
self.demo.launch(
|
420 |
server_name="0.0.0.0",
|