Saiyaswanth007 commited on
Commit
b45f016
·
1 Parent(s): 7f7357a
Files changed (1) hide show
  1. ui.py +45 -34
ui.py CHANGED
@@ -81,40 +81,21 @@ def create_gradio_app():
81
  this.baseUrl = 'https://{config.hf_space_url}';
82
  this.wsUrl = 'wss://{config.hf_space_url}/ws';
83
  this.renderUrl = 'wss://{config.render_url}/stream';
 
84
  }}
85
 
86
  async startRecording() {{
87
  try {{
88
- // Request microphone access
89
- this.mediaStream = await navigator.mediaDevices.getUserMedia({{
90
- audio: {{
91
- echoCancellation: true,
92
- noiseSuppression: true,
93
- autoGainControl: true,
94
- sampleRate: 16000
95
- }}
96
- }});
97
 
98
- // Set up WebSocket connection
99
  await this.connectWebSocket();
100
 
101
- // Set up MediaRecorder for audio chunks
102
- this.mediaRecorder = new MediaRecorder(this.mediaStream, {{
103
- mimeType: 'audio/webm;codecs=opus'
104
- }});
105
-
106
- this.mediaRecorder.ondataavailable = (event) => {{
107
- if (event.data.size > 0 && this.ws && this.ws.readyState === WebSocket.OPEN) {{
108
- // Send audio chunk to server
109
- this.ws.send(event.data);
110
- }}
111
- }};
112
-
113
- // Start recording with chunks every 1 second
114
- this.mediaRecorder.start(1000);
115
- this.isRecording = true;
116
 
117
- this.updateStatus('connected', 'Recording started');
118
 
119
  }} catch (error) {{
120
  console.error('Error starting recording:', error);
@@ -124,10 +105,11 @@ def create_gradio_app():
124
 
125
  async connectWebSocket() {{
126
  return new Promise((resolve, reject) => {{
127
- this.ws = new WebSocket(this.wsUrl);
 
128
 
129
  this.ws.onopen = () => {{
130
- console.log('WebSocket connected');
131
  resolve();
132
  }};
133
 
@@ -312,7 +294,9 @@ def create_gradio_app():
312
  streaming=False,
313
  modality="audio",
314
  mode="send-receive",
315
- visible=False # Hidden but functional
 
 
316
  )
317
 
318
  # Control buttons
@@ -498,13 +482,26 @@ async def process_audio_chunk(audio_data: bytes) -> dict:
498
  fastapi_app = create_fastapi_app()
499
  gradio_app = create_gradio_app()
500
 
501
- # Root redirect
502
  @fastapi_app.get("/")
503
- async def root():
504
- return RedirectResponse(url="/ui")
 
505
 
506
- # Mount Gradio app to FastAPI at /ui
507
- fastapi_app.mount("/ui", gradio_app.app)
 
 
 
 
 
 
 
 
 
 
 
 
508
 
509
  # Add diagnostic endpoints to check connections
510
  @fastapi_app.get("/check-backend")
@@ -542,6 +539,20 @@ async def log_configuration():
542
  logger.info(f"- WebRTC URL: wss://{config.render_url}/stream")
543
  logger.info(f"- WebSocket URL: wss://{config.hf_space_url}/ws_inference")
544
  logger.info("Note: Audio will be streamed through the Render backend using WebRTC")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
545
 
546
  if __name__ == "__main__":
547
  import uvicorn
 
81
  this.baseUrl = 'https://{config.hf_space_url}';
82
  this.wsUrl = 'wss://{config.hf_space_url}/ws';
83
  this.renderUrl = 'wss://{config.render_url}/stream';
84
+ this.rtcComponentSynced = false;
85
  }}
86
 
87
  async startRecording() {{
88
  try {{
89
+ this.isRecording = true;
90
+ this.updateStatus('connecting', 'Connecting to server...');
 
 
 
 
 
 
 
91
 
92
+ // Connect to WebSocket for transcription updates
93
  await this.connectWebSocket();
94
 
95
+ // Let the RTCComponent handle the audio streaming
96
+ // This will be handled by Gradio's WebRTC component
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ this.updateStatus('connected', 'Connected and listening');
99
 
100
  }} catch (error) {{
101
  console.error('Error starting recording:', error);
 
105
 
106
  async connectWebSocket() {{
107
  return new Promise((resolve, reject) => {{
108
+ // Only connect to the conversation updates WebSocket
109
+ this.ws = new WebSocket('wss://{config.hf_space_url}/ws_transcription');
110
 
111
  this.ws.onopen = () => {{
112
+ console.log('WebSocket connected for transcription updates');
113
  resolve();
114
  }};
115
 
 
294
  streaming=False,
295
  modality="audio",
296
  mode="send-receive",
297
+ audio_html_attrs="style='display:none;'", # Hide the audio element
298
+ visible=True, # Make component visible but hide audio element
299
+ elements=["video", "start", "stop"] # Don't include audio element
300
  )
301
 
302
  # Control buttons
 
482
  fastapi_app = create_fastapi_app()
483
  gradio_app = create_gradio_app()
484
 
485
+ # Root redirect - keep this simple
486
  @fastapi_app.get("/")
487
+ def root():
488
+ """Redirect root to the Gradio UI"""
489
+ return RedirectResponse(url="/ui/") # Note the trailing slash is important
490
 
491
+ # Mount Gradio app to FastAPI - use correct mounting method for Gradio
492
+ try:
493
+ # For newer Gradio versions
494
+ fastapi_app.mount("/ui", gradio_app)
495
+ except Exception as e:
496
+ # Try alternative mounting method
497
+ try:
498
+ from gradio.routes import mount_gradio_app
499
+ app = mount_gradio_app(fastapi_app, gradio_app, path="/ui")
500
+ logger.info("Mounted Gradio app using mount_gradio_app")
501
+ except Exception as e2:
502
+ logger.error(f"Failed to mount Gradio app: {e2}")
503
+ # As a last resort, try the simplest mounting
504
+ fastapi_app.mount("/ui", gradio_app.app)
505
 
506
  # Add diagnostic endpoints to check connections
507
  @fastapi_app.get("/check-backend")
 
539
  logger.info(f"- WebRTC URL: wss://{config.render_url}/stream")
540
  logger.info(f"- WebSocket URL: wss://{config.hf_space_url}/ws_inference")
541
  logger.info("Note: Audio will be streamed through the Render backend using WebRTC")
542
+
543
+ # Test connection to Render backend
544
+ try:
545
+ async with websockets.connect(f"wss://{config.render_url}/stream", ping_interval=None, ping_timeout=None) as ws:
546
+ logger.info("Successfully connected to Render backend WebSocket")
547
+ except Exception as e:
548
+ logger.error(f"Failed to connect to Render backend: {e}")
549
+
550
+ # Test connection to HF Space backend
551
+ try:
552
+ async with websockets.connect(f"wss://{config.hf_space_url}/ws_inference", ping_interval=None, ping_timeout=None) as ws:
553
+ logger.info("Successfully connected to HF Space WebSocket")
554
+ except Exception as e:
555
+ logger.error(f"Failed to connect to HF Space: {e}")
556
 
557
  if __name__ == "__main__":
558
  import uvicorn