Saiyaswanth007 commited on
Commit
6e5c27e
·
1 Parent(s): 2b2608e

experiment

Browse files
Files changed (1) hide show
  1. ui.py +196 -84
ui.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  from fastapi import FastAPI
3
  from shared import DEFAULT_CHANGE_THRESHOLD, DEFAULT_MAX_SPEAKERS, ABSOLUTE_MAX_SPEAKERS, FINAL_TRANSCRIPTION_MODEL, REALTIME_TRANSCRIPTION_MODEL
4
  print(gr.__version__)
@@ -13,10 +14,11 @@ def build_ui():
13
  # Add configuration variables to page using custom component
14
  gr.HTML(
15
  f"""
16
- <!-- Configuration parameters -->
17
  <script>
18
  window.RENDER_SIGNALING_URL = "{RENDER_SIGNALING_URL}";
19
  window.HF_SPACE_URL = "{HF_SPACE_URL}";
 
 
20
  </script>
21
  """
22
  )
@@ -60,36 +62,55 @@ def build_ui():
60
  let response = await fetch(`${window.HF_SPACE_URL}/health`);
61
  return response.ok;
62
  } catch (err) {
 
63
  return false;
64
  }
65
  }
66
 
67
- // Start the connection and audio streaming
68
  async function startStreaming() {
69
  try {
70
  // Update status
71
  updateStatus('connecting');
72
 
73
- // Request microphone access
74
  mediaStream = await navigator.mediaDevices.getUserMedia({audio: {
75
  echoCancellation: true,
76
  noiseSuppression: true,
77
  autoGainControl: true
78
  }});
79
 
80
- // Set up WebRTC connection to Render signaling server
81
- await setupWebRTC();
82
 
83
- // Also connect WebSocket directly to HF Space for conversation updates
84
- setupWebSocket();
 
 
 
 
 
85
 
86
- // Start status update interval
87
- statusUpdateInterval = setInterval(updateConnectionInfo, 5000);
 
 
 
 
 
88
 
89
- // Update status
90
- updateStatus('connected');
91
 
92
- document.getElementById("conversation").innerHTML = "<i>Connected! Start speaking...</i>";
 
 
 
 
 
 
 
 
93
  } catch (err) {
94
  console.error('Error starting stream:', err);
95
  updateStatus('error', err.message);
@@ -271,20 +292,41 @@ def build_ui():
271
  };
272
  }
273
 
274
- // Update connection info in the UI
275
  async function updateConnectionInfo() {
276
  try {
277
  const hfConnected = await checkHfConnection();
 
278
  if (!hfConnected) {
279
- updateStatus('warning', 'HF Space connection issue');
 
 
 
 
 
 
 
 
 
 
280
  } else if (rtcConnection?.connectionState === 'connected' ||
281
  rtcConnection?.iceConnectionState === 'connected') {
282
  updateStatus('connected');
283
  } else {
284
  updateStatus('warning', 'Connection unstable');
 
 
 
 
 
 
 
 
 
285
  }
286
  } catch (err) {
287
  console.error('Error updating connection info:', err);
 
288
  }
289
  }
290
 
@@ -349,31 +391,157 @@ def build_ui():
349
  // Update status
350
  updateStatus('disconnected');
351
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
 
353
  // Set up event listeners when the DOM is loaded
354
  document.addEventListener('DOMContentLoaded', () => {
355
  updateStatus('disconnected');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  });
357
  </script>
358
  """,
359
  label="Live Conversation"
360
  )
361
 
362
- # Control buttons
363
  with gr.Row():
364
- start_btn = gr.Button("▶️ Start Listening", variant="primary", size="lg")
365
- stop_btn = gr.Button("⏹️ Stop", variant="stop", size="lg")
366
- clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg")
367
 
368
- # Status display
369
  status_output = gr.Markdown(
370
  """
371
  ## System Status
372
- Waiting to connect...
373
 
374
  *Click Start Listening to begin*
375
  """,
376
- label="Status Information"
 
377
  )
378
 
379
  with gr.Column(scale=1):
@@ -386,7 +554,8 @@ def build_ui():
386
  step=0.05,
387
  value=DEFAULT_CHANGE_THRESHOLD,
388
  label="Speaker Change Sensitivity",
389
- info="Lower = more sensitive (more speaker changes)"
 
390
  )
391
 
392
  max_speakers_slider = gr.Slider(
@@ -394,10 +563,11 @@ def build_ui():
394
  maximum=ABSOLUTE_MAX_SPEAKERS,
395
  step=1,
396
  value=DEFAULT_MAX_SPEAKERS,
397
- label="Maximum Speakers"
 
398
  )
399
 
400
- update_btn = gr.Button("Update Settings", variant="secondary")
401
 
402
  # Instructions
403
  gr.Markdown("""
@@ -418,63 +588,8 @@ def build_ui():
418
  - 🟠 Speaker 8 (Gold)
419
  """)
420
 
421
- # JavaScript to connect buttons to the script functions
422
- gr.HTML("""
423
- <script>
424
- // Wait for Gradio to fully load
425
- document.addEventListener('DOMContentLoaded', () => {
426
- // Wait a bit for Gradio buttons to be created
427
- setTimeout(() => {
428
- // Get the buttons
429
- const startBtn = document.querySelector('button[aria-label="Start Listening"]');
430
- const stopBtn = document.querySelector('button[aria-label="Stop"]');
431
- const clearBtn = document.querySelector('button[aria-label="Clear"]');
432
-
433
- if (startBtn) startBtn.onclick = () => startStreaming();
434
- if (stopBtn) stopBtn.onclick = () => stopStreaming();
435
- if (clearBtn) clearBtn.onclick = () => {
436
- // Make API call to clear conversation
437
- fetch(`${window.HF_SPACE_URL}/clear`, {
438
- method: 'POST'
439
- }).then(resp => resp.json())
440
- .then(data => {
441
- document.getElementById("conversation").innerHTML =
442
- "<i>Conversation cleared. Start speaking again...</i>";
443
- });
444
- }
445
-
446
- // Set up settings update
447
- const updateBtn = document.querySelector('button[aria-label="Update Settings"]');
448
- if (updateBtn) updateBtn.onclick = () => {
449
- const threshold = document.querySelector('input[aria-label="Speaker Change Sensitivity"]').value;
450
- const maxSpeakers = document.querySelector('input[aria-label="Maximum Speakers"]').value;
451
-
452
- fetch(`${window.HF_SPACE_URL}/settings?threshold=${threshold}&max_speakers=${maxSpeakers}`, {
453
- method: 'POST'
454
- }).then(resp => resp.json())
455
- .then(data => {
456
- const statusOutput = document.querySelector('.prose');
457
- if (statusOutput) {
458
- statusOutput.innerHTML = `
459
- <h2>System Status</h2>
460
- <p>Settings updated:</p>
461
- <ul>
462
- <li>Threshold: ${threshold}</li>
463
- <li>Max Speakers: ${maxSpeakers}</li>
464
- </ul>
465
- <p>Transcription Models:</p>
466
- <ul>
467
- <li>Final: ${window.FINAL_TRANSCRIPTION_MODEL || "distil-large-v3"}</li>
468
- <li>Realtime: ${window.REALTIME_TRANSCRIPTION_MODEL || "distil-small.en"}</li>
469
- </ul>
470
- `;
471
- }
472
- });
473
- }
474
- }, 1000);
475
- });
476
- </script>
477
- """)
478
 
479
  # Set up periodic status updates
480
  def get_status():
@@ -487,9 +602,6 @@ def build_ui():
487
  return "Error getting status"
488
  except Exception as e:
489
  return f"Connection error: {str(e)}"
490
-
491
- status_timer = gr.Timer(5)
492
- status_timer.tick(fn=get_status, outputs=status_output)
493
 
494
 
495
  return demo
 
1
  import gradio as gr
2
+ import requests
3
  from fastapi import FastAPI
4
  from shared import DEFAULT_CHANGE_THRESHOLD, DEFAULT_MAX_SPEAKERS, ABSOLUTE_MAX_SPEAKERS, FINAL_TRANSCRIPTION_MODEL, REALTIME_TRANSCRIPTION_MODEL
5
  print(gr.__version__)
 
14
  # Add configuration variables to page using custom component
15
  gr.HTML(
16
  f"""
 
17
  <script>
18
  window.RENDER_SIGNALING_URL = "{RENDER_SIGNALING_URL}";
19
  window.HF_SPACE_URL = "{HF_SPACE_URL}";
20
+ window.FINAL_TRANSCRIPTION_MODEL = "{FINAL_TRANSCRIPTION_MODEL}"; <!-- ADDED -->
21
+ window.REALTIME_TRANSCRIPTION_MODEL = "{REALTIME_TRANSCRIPTION_MODEL}"; <!-- ADDED -->
22
  </script>
23
  """
24
  )
 
62
  let response = await fetch(`${window.HF_SPACE_URL}/health`);
63
  return response.ok;
64
  } catch (err) {
65
+ console.warn("HF Space connection failed:", err);
66
  return false;
67
  }
68
  }
69
 
70
+ // Improved start streaming function with offline mode support
71
  async function startStreaming() {
72
  try {
73
  // Update status
74
  updateStatus('connecting');
75
 
76
+ // Request microphone access - this will work even offline
77
  mediaStream = await navigator.mediaDevices.getUserMedia({audio: {
78
  echoCancellation: true,
79
  noiseSuppression: true,
80
  autoGainControl: true
81
  }});
82
 
83
+ // Check if we can connect to backend
84
+ let backendAvailable = await checkHfConnection();
85
 
86
+ try {
87
+ // Try WebRTC connection, might fail if backend is down
88
+ await setupWebRTC();
89
+ } catch (rtcErr) {
90
+ console.error("WebRTC setup failed:", rtcErr);
91
+ // Continue even if WebRTC fails
92
+ }
93
 
94
+ try {
95
+ // Try WebSocket connection, might fail if backend is down
96
+ setupWebSocket();
97
+ } catch (wsErr) {
98
+ console.error("WebSocket setup failed:", wsErr);
99
+ // Continue even if WebSocket fails
100
+ }
101
 
102
+ // Start status update interval regardless
103
+ statusUpdateInterval = setInterval(updateConnectionInfo, 5000);
104
 
105
+ // Update status - either connected or offline mode
106
+ if (backendAvailable) {
107
+ updateStatus('connected');
108
+ document.getElementById("conversation").innerHTML = "<i>Connected! Start speaking...</i>";
109
+ } else {
110
+ updateStatus('warning', 'Running in offline mode - limited functionality');
111
+ document.getElementById("conversation").innerHTML =
112
+ "<i>Backend connection failed. Microphone active but transcription unavailable.</i>";
113
+ }
114
  } catch (err) {
115
  console.error('Error starting stream:', err);
116
  updateStatus('error', err.message);
 
292
  };
293
  }
294
 
295
+ // Update connection info in the UI with better error handling
296
  async function updateConnectionInfo() {
297
  try {
298
  const hfConnected = await checkHfConnection();
299
+
300
  if (!hfConnected) {
301
+ // If backend is unavailable, update status but don't break functionality
302
+ updateStatus('warning', 'Backend unavailable - limited functionality');
303
+
304
+ // Try to reconnect WebSocket if it was disconnected
305
+ if (!wsConnection || wsConnection.readyState !== WebSocket.OPEN) {
306
+ try {
307
+ setupWebSocket();
308
+ } catch (e) {
309
+ console.warn("Failed to reconnect WebSocket:", e);
310
+ }
311
+ }
312
  } else if (rtcConnection?.connectionState === 'connected' ||
313
  rtcConnection?.iceConnectionState === 'connected') {
314
  updateStatus('connected');
315
  } else {
316
  updateStatus('warning', 'Connection unstable');
317
+
318
+ // Try to reconnect if needed
319
+ if (!rtcConnection || rtcConnection.connectionState === 'failed') {
320
+ try {
321
+ await setupWebRTC();
322
+ } catch (e) {
323
+ console.warn("Failed to reconnect WebRTC:", e);
324
+ }
325
+ }
326
  }
327
  } catch (err) {
328
  console.error('Error updating connection info:', err);
329
+ // Don't update status here to avoid flickering
330
  }
331
  }
332
 
 
391
  // Update status
392
  updateStatus('disconnected');
393
  }
394
+
395
+ // Clear conversation with better error handling and offline mode support
396
+ function clearConversation() {
397
+ // First update the UI immediately regardless of backend availability
398
+ document.getElementById("conversation").innerHTML =
399
+ "<i>Conversation cleared. Start speaking again...</i>";
400
+
401
+ // Then try to update on the backend if available
402
+ checkHfConnection().then(isConnected => {
403
+ if (isConnected) {
404
+ return fetch(`${window.HF_SPACE_URL}/clear`, {
405
+ method: 'POST'
406
+ });
407
+ } else {
408
+ throw new Error("Backend unavailable");
409
+ }
410
+ })
411
+ .then(resp => resp.json())
412
+ .then(data => {
413
+ console.log("Backend conversation cleared successfully");
414
+ })
415
+ .catch(err => {
416
+ console.warn("Backend clear API failed:", err);
417
+ // No need to update UI again as we already did it above
418
+ });
419
+ }
420
+
421
+ // Update settings with better error handling and offline mode support
422
+ function updateSettings() {
423
+ const threshold = document.querySelector('input[data-testid="threshold-slider"]').value;
424
+ const maxSpeakers = document.querySelector('input[data-testid="speakers-slider"]').value;
425
+
426
+ // First update the UI immediately regardless of API success
427
+ const statusOutput = document.getElementById('status-output');
428
+ if (statusOutput) {
429
+ statusOutput.innerHTML = `
430
+ <h2>System Status</h2>
431
+ <p>Settings updated:</p>
432
+ <ul>
433
+ <li>Threshold: ${threshold}</li>
434
+ <li>Max Speakers: ${maxSpeakers}</li>
435
+ </ul>
436
+ <p>Transcription Models:</p>
437
+ <ul>
438
+ <li>Final: ${window.FINAL_TRANSCRIPTION_MODEL || "distil-large-v3"}</li>
439
+ <li>Realtime: ${window.REALTIME_TRANSCRIPTION_MODEL || "distil-small.en"}</li>
440
+ </ul>
441
+ `;
442
+ }
443
+
444
+ // Then try to update on the backend if available
445
+ checkHfConnection().then(isConnected => {
446
+ if (isConnected) {
447
+ return fetch(`${window.HF_SPACE_URL}/settings?threshold=${threshold}&max_speakers=${maxSpeakers}`, {
448
+ method: 'POST'
449
+ });
450
+ } else {
451
+ throw new Error("Backend unavailable");
452
+ }
453
+ })
454
+ .then(resp => resp.json())
455
+ .then(data => {
456
+ console.log("Backend settings updated successfully:", data);
457
+ })
458
+ .catch(err => {
459
+ console.warn("Backend settings update failed:", err);
460
+ // No need to update UI again as we already did it above
461
+ });
462
+ }
463
 
464
  // Set up event listeners when the DOM is loaded
465
  document.addEventListener('DOMContentLoaded', () => {
466
  updateStatus('disconnected');
467
+
468
+ // Function to find and bind buttons with retries
469
+ function findAndBindButtons() {
470
+ // Try to find buttons by ID first (most reliable)
471
+ let startBtn = document.getElementById('btn-start');
472
+ let stopBtn = document.getElementById('btn-stop');
473
+ let clearBtn = document.getElementById('btn-clear');
474
+ let updateBtn = document.getElementById('btn-update');
475
+
476
+ // Fallback to aria-label if IDs aren't found
477
+ if (!startBtn) startBtn = document.querySelector('button[aria-label="Start Listening"]');
478
+ if (!stopBtn) stopBtn = document.querySelector('button[aria-label="Stop"]');
479
+ if (!clearBtn) clearBtn = document.querySelector('button[aria-label="Clear"]');
480
+ if (!updateBtn) updateBtn = document.querySelector('button[aria-label="Update Settings"]');
481
+
482
+ // Check if all buttons are found
483
+ const buttonsFound = startBtn && stopBtn && clearBtn && updateBtn;
484
+
485
+ if (buttonsFound) {
486
+ console.log("All buttons found, binding events");
487
+
488
+ // Bind event handlers
489
+ startBtn.onclick = () => startStreaming();
490
+ stopBtn.onclick = () => stopStreaming();
491
+ clearBtn.onclick = () => clearConversation();
492
+ updateBtn.onclick = () => updateSettings();
493
+
494
+ // Add data attributes to make it clear these are bound
495
+ startBtn.setAttribute('data-bound', 'true');
496
+ stopBtn.setAttribute('data-bound', 'true');
497
+ clearBtn.setAttribute('data-bound', 'true');
498
+ updateBtn.setAttribute('data-bound', 'true');
499
+
500
+ return true;
501
+ } else {
502
+ console.log("Not all buttons found, will retry");
503
+ return false;
504
+ }
505
+ }
506
+
507
+ // Try to bind immediately
508
+ if (!findAndBindButtons()) {
509
+ // If not successful, set up a retry mechanism
510
+ let retryCount = 0;
511
+ const maxRetries = 10;
512
+ const retryInterval = 500; // 500ms between retries
513
+
514
+ const retryBinding = setInterval(() => {
515
+ if (findAndBindButtons() || ++retryCount >= maxRetries) {
516
+ clearInterval(retryBinding);
517
+ if (retryCount >= maxRetries) {
518
+ console.warn("Failed to find all buttons after maximum retries");
519
+ }
520
+ }
521
+ }, retryInterval);
522
+ }
523
  });
524
  </script>
525
  """,
526
  label="Live Conversation"
527
  )
528
 
529
+ # Control buttons with elem_id for reliable selection
530
  with gr.Row():
531
+ start_btn = gr.Button("▶️ Start Listening", variant="primary", size="lg", elem_id="btn-start")
532
+ stop_btn = gr.Button("⏹️ Stop", variant="stop", size="lg", elem_id="btn-stop")
533
+ clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg", elem_id="btn-clear")
534
 
535
+ # Status display with elem_id for reliable selection
536
  status_output = gr.Markdown(
537
  """
538
  ## System Status
539
+ "Waiting to connect...\n\n*Click Start Listening to begin*"
540
 
541
  *Click Start Listening to begin*
542
  """,
543
+ label="Status Information",
544
+ elem_id="status-output"
545
  )
546
 
547
  with gr.Column(scale=1):
 
554
  step=0.05,
555
  value=DEFAULT_CHANGE_THRESHOLD,
556
  label="Speaker Change Sensitivity",
557
+ info="Lower = more sensitive (more speaker changes)",
558
+ elem_id="threshold-slider"
559
  )
560
 
561
  max_speakers_slider = gr.Slider(
 
563
  maximum=ABSOLUTE_MAX_SPEAKERS,
564
  step=1,
565
  value=DEFAULT_MAX_SPEAKERS,
566
+ label="Maximum Speakers",
567
+ elem_id="speakers-slider"
568
  )
569
 
570
+ update_btn = gr.Button("Update Settings", variant="secondary", elem_id="btn-update")
571
 
572
  # Instructions
573
  gr.Markdown("""
 
588
  - 🟠 Speaker 8 (Gold)
589
  """)
590
 
591
+ # JavaScript to connect buttons to the script functions is now embedded directly in the conversation_display
592
+ # We removed the separate gr.HTML for button bindings since it's now handled with more robust code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
 
594
  # Set up periodic status updates
595
  def get_status():
 
602
  return "Error getting status"
603
  except Exception as e:
604
  return f"Connection error: {str(e)}"
 
 
 
605
 
606
 
607
  return demo