Saiyaswanth007 commited on
Commit
54ccef4
·
1 Parent(s): e1de00e

Removed complex code

Browse files
Files changed (1) hide show
  1. ui.py +45 -18
ui.py CHANGED
@@ -9,12 +9,12 @@ import os
9
  from datetime import datetime
10
  import httpx
11
  import websockets
 
12
 
13
- # Configuration - use environment variables for deployment
14
  class Config:
15
  def __init__(self):
16
- self.hf_space_url = os.getenv("HF_SPACE_URL", "https://your-space.hf.space")
17
- self.render_url = os.getenv("RENDER_URL", "https://your-app.onrender.com")
18
  self.default_threshold = float(os.getenv("DEFAULT_THRESHOLD", "0.7"))
19
  self.default_max_speakers = int(os.getenv("DEFAULT_MAX_SPEAKERS", "4"))
20
  self.max_speakers_limit = int(os.getenv("MAX_SPEAKERS_LIMIT", "8"))
@@ -74,8 +74,9 @@ def create_gradio_app():
74
  this.mediaStream = null;
75
  this.mediaRecorder = null;
76
  this.isRecording = false;
77
- this.baseUrl = '{config.hf_space_url}';
78
- this.wsUrl = this.baseUrl.replace('https://', 'wss://').replace('http://', 'ws://') + '/ws';
 
79
  }}
80
 
81
  async startRecording() {{
@@ -301,28 +302,53 @@ def create_gradio_app():
301
  </div>
302
  """)
303
 
 
 
 
 
 
 
 
 
 
304
  # Control buttons
305
  with gr.Row():
306
- gr.Button(
307
  "▶️ Start Listening",
308
  variant="primary",
309
  size="lg",
310
  elem_id="start-btn"
311
- ).click(fn=None, js="startListening()")
312
 
313
- gr.Button(
314
  "⏹️ Stop",
315
  variant="stop",
316
  size="lg",
317
  elem_id="stop-btn"
318
- ).click(fn=None, js="stopListening()")
319
 
320
- gr.Button(
321
  "🗑️ Clear",
322
  variant="secondary",
323
  size="lg",
324
  elem_id="clear-btn"
325
- ).click(fn=None, js="clearConversation()")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
 
327
  with gr.Column(scale=1):
328
  gr.Markdown("## ⚙️ Settings")
@@ -418,21 +444,22 @@ def create_fastapi_app():
418
 
419
  async def process_audio_chunk(audio_data: bytes) -> dict:
420
  """
421
- Process audio chunk and return diarization result by sending it to the Speaker Diarization backend
 
 
 
 
422
  """
423
  try:
424
- # Convert WebM audio to appropriate format if needed
425
- # This step may require additional processing depending on your backend requirements
426
-
427
  # Connect to the Speaker Diarization backend via WebSocket
428
- websocket_url = f"wss://{config.hf_space_url.replace('https://', '').replace('http://', '')}/ws_inference"
429
- logger.info(f"Connecting to diarization backend at {websocket_url}")
430
 
431
  async with websockets.connect(websocket_url) as websocket:
432
  # Send audio data
433
  await websocket.send(audio_data)
434
 
435
- # Receive the response (may need to handle multiple messages)
436
  response = await websocket.recv()
437
 
438
  # Parse the response
 
9
  from datetime import datetime
10
  import httpx
11
  import websockets
12
+ from fastrtc import RTCComponent
13
 
 
14
  class Config:
15
  def __init__(self):
16
+ self.hf_space_url = os.getenv("HF_SPACE_URL", "androidguy-speaker-diarization.hf.space")
17
+ self.render_url = os.getenv("RENDER_URL", "render-signal-audio.onrender.com")
18
  self.default_threshold = float(os.getenv("DEFAULT_THRESHOLD", "0.7"))
19
  self.default_max_speakers = int(os.getenv("DEFAULT_MAX_SPEAKERS", "4"))
20
  self.max_speakers_limit = int(os.getenv("MAX_SPEAKERS_LIMIT", "8"))
 
74
  this.mediaStream = null;
75
  this.mediaRecorder = null;
76
  this.isRecording = false;
77
+ this.baseUrl = 'https://{config.hf_space_url}';
78
+ this.wsUrl = 'wss://{config.hf_space_url}/ws';
79
+ this.renderUrl = 'wss://{config.render_url}/stream';
80
  }}
81
 
82
  async startRecording() {{
 
302
  </div>
303
  """)
304
 
305
+ # WebRTC component (hidden, but functional)
306
+ webrtc = RTCComponent(
307
+ url=f"wss://{config.render_url}/stream",
308
+ streaming=False,
309
+ modality="audio",
310
+ mode="send-receive",
311
+ visible=False # Hidden but functional
312
+ )
313
+
314
  # Control buttons
315
  with gr.Row():
316
+ start_btn = gr.Button(
317
  "▶️ Start Listening",
318
  variant="primary",
319
  size="lg",
320
  elem_id="start-btn"
321
+ )
322
 
323
+ stop_btn = gr.Button(
324
  "⏹️ Stop",
325
  variant="stop",
326
  size="lg",
327
  elem_id="stop-btn"
328
+ )
329
 
330
+ clear_btn = gr.Button(
331
  "🗑️ Clear",
332
  variant="secondary",
333
  size="lg",
334
  elem_id="clear-btn"
335
+ )
336
+
337
+ # WebRTC control functions
338
+ def start_webrtc():
339
+ return {
340
+ webrtc: gr.update(streaming=True)
341
+ }
342
+
343
+ def stop_webrtc():
344
+ return {
345
+ webrtc: gr.update(streaming=False)
346
+ }
347
+
348
+ # Connect buttons to both WebRTC and JavaScript functions
349
+ start_btn.click(fn=start_webrtc, outputs=[webrtc], js="startListening()")
350
+ stop_btn.click(fn=stop_webrtc, outputs=[webrtc], js="stopListening()")
351
+ clear_btn.click(fn=None, js="clearConversation()")
352
 
353
  with gr.Column(scale=1):
354
  gr.Markdown("## ⚙️ Settings")
 
444
 
445
  async def process_audio_chunk(audio_data: bytes) -> dict:
446
  """
447
+ Process audio chunk by forwarding to the backend.
448
+ This function is only used for the direct WebSocket API, not for the WebRTC component.
449
+
450
+ Note: In production, you should primarily use the WebRTC component which has its own
451
+ audio processing flow through the Render backend.
452
  """
453
  try:
 
 
 
454
  # Connect to the Speaker Diarization backend via WebSocket
455
+ websocket_url = f"wss://{config.hf_space_url}/ws_inference"
456
+ logger.info(f"Forwarding audio to diarization backend at {websocket_url}")
457
 
458
  async with websockets.connect(websocket_url) as websocket:
459
  # Send audio data
460
  await websocket.send(audio_data)
461
 
462
+ # Receive the response
463
  response = await websocket.recv()
464
 
465
  # Parse the response