ahundt commited on
Commit
7bc94a1
Β·
1 Parent(s): d342f59

app.py ui improvements

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -55,13 +55,18 @@ def encode_image(data: np.ndarray) -> dict:
55
  base64_str = str(base64.b64encode(bytes_data), "utf-8")
56
  return {"mime_type": "image/jpeg", "data": base64_str}
57
  except Exception as e:
 
 
58
  logger.error(f"Error encoding image: {e}")
59
  raise
 
 
 
60
 
61
  def check_twilio_availability_sync() -> bool:
62
  """Checks Twilio TURN server availability (synchronous version)."""
63
  global twilio_available
64
- timeout = 10
65
  retries = 3
66
  delay = 2
67
 
@@ -233,9 +238,9 @@ class GeminiHandler(AsyncAudioVideoStreamHandler):
233
 
234
  def update_gemini_status_sync():
235
  """Updates the Gemini status message (synchronous version)."""
236
- status = "Connected" if gemini_connected else "Disconnected"
237
  if 'demo' in locals() and demo.running:
238
- gr.update(value=f"Gemini connection status: {status}")
239
 
240
 
241
 
@@ -265,8 +270,8 @@ with gr.Blocks(css=css) as demo:
265
  </div>
266
  """
267
  )
268
- twilio_status_message = gr.Markdown("")
269
- gemini_status_message = gr.Markdown("")
270
 
271
  with gr.Row() as api_key_row:
272
  api_key = gr.Textbox(
@@ -298,15 +303,16 @@ with gr.Blocks(css=css) as demo:
298
 
299
  def update_twilio_status_ui():
300
  if twilio_available:
301
- message = "Twilio TURN server available."
302
  else:
303
- message = "**Warning:** Twilio TURN server unavailable. Connection might be less reliable."
304
  return gr.update(value=message)
305
 
306
  demo.load(update_twilio_status_ui, [], [twilio_status_message])
307
 
 
308
  webrtc.stream(
309
- GeminiHandler(),
310
  inputs=[webrtc, api_key, image_input],
311
  outputs=[webrtc],
312
  time_limit=90,
@@ -320,13 +326,13 @@ with gr.Blocks(css=css) as demo:
320
  gr.update(visible=True),
321
  gr.update(visible=False),
322
  gr.update(value="Please enter a valid API key"),
323
- gr.update(value=""),
324
  )
325
  return (
326
  gr.update(visible=False),
327
  gr.update(visible=True),
328
  gr.update(value=""),
329
- gr.update(value=""),
330
  )
331
 
332
  api_key.submit(
@@ -335,4 +341,12 @@ with gr.Blocks(css=css) as demo:
335
  [api_key_row, row, twilio_status_message, gemini_status_message],
336
  )
337
 
 
 
 
 
 
 
 
 
338
  demo.launch()
 
55
  base64_str = str(base64.b64encode(bytes_data), "utf-8")
56
  return {"mime_type": "image/jpeg", "data": base64_str}
57
  except Exception as e:
58
+ # log traceback
59
+ logger.error(traceback.format_exc())
60
  logger.error(f"Error encoding image: {e}")
61
  raise
62
+ except ValueError as e: # PIL error
63
+ logger.exception(f"ValueError when creating image: {e}")
64
+ raise
65
 
66
  def check_twilio_availability_sync() -> bool:
67
  """Checks Twilio TURN server availability (synchronous version)."""
68
  global twilio_available
69
+ timeout_seconds = 10
70
  retries = 3
71
  delay = 2
72
 
 
238
 
239
  def update_gemini_status_sync():
240
  """Updates the Gemini status message (synchronous version)."""
241
+ status = "βœ… Gemini: Connected" if gemini_connected else "❌ Gemini: Disconnected"
242
  if 'demo' in locals() and demo.running:
243
+ gr.update(value=status)
244
 
245
 
246
 
 
270
  </div>
271
  """
272
  )
273
+ twilio_status_message = gr.Markdown("❓ Twilio: Checking...")
274
+ gemini_status_message = gr.Markdown("❓ Gemini: Checking...")
275
 
276
  with gr.Row() as api_key_row:
277
  api_key = gr.Textbox(
 
303
 
304
  def update_twilio_status_ui():
305
  if twilio_available:
306
+ message = "βœ… Twilio: Available"
307
  else:
308
+ message = "❌ Twilio: Unavailable (connection may be less reliable)"
309
  return gr.update(value=message)
310
 
311
  demo.load(update_twilio_status_ui, [], [twilio_status_message])
312
 
313
+ handler = GeminiHandler()
314
  webrtc.stream(
315
+ handler,
316
  inputs=[webrtc, api_key, image_input],
317
  outputs=[webrtc],
318
  time_limit=90,
 
326
  gr.update(visible=True),
327
  gr.update(visible=False),
328
  gr.update(value="Please enter a valid API key"),
329
+ gr.update(value="❓ Gemini: Checking..."),
330
  )
331
  return (
332
  gr.update(visible=False),
333
  gr.update(visible=True),
334
  gr.update(value=""),
335
+ gr.update(value="❓ Gemini: Checking..."),
336
  )
337
 
338
  api_key.submit(
 
341
  [api_key_row, row, twilio_status_message, gemini_status_message],
342
  )
343
 
344
+ # If API key is already set via environment variables, hide the API key row and show content
345
+ if os.getenv("GOOGLE_API_KEY"):
346
+ demo.load(
347
+ lambda: (gr.update(visible=False), gr.update(visible=True)),
348
+ None,
349
+ [api_key_row, row],
350
+ )
351
+
352
  demo.launch()