Peter Michael Gits Claude commited on
Commit
8527d70
Β·
1 Parent(s): a312290

fix: Switch to Gradio HTTP API fallback for WebSocket connectivity issues

Browse files

- Use /api/predict endpoint instead of WebSocket due to Gradio routing conflicts
- Maintain WebRTC audio capture with WebM/Opus format
- Add fallback mechanism for reliable STT integration
- Compatible with new standalone WebSocket STT service v1.0.0

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. streamlit_websocket_app.py +53 -36
streamlit_websocket_app.py CHANGED
@@ -104,22 +104,15 @@ def main():
104
  this.updateStatus('πŸ”— Testing STT service connection...');
105
 
106
  try {
107
- // Test connection to STT service
108
- const testWebSocket = new WebSocket(this.sttWebSocketUrl);
109
 
110
- testWebSocket.onopen = () => {
111
- this.updateStatus('βœ… STT service connection successful!');
112
- testWebSocket.close();
113
- };
114
-
115
- testWebSocket.onerror = (error) => {
116
- this.updateStatus('❌ STT service connection failed');
117
- console.error('STT connection test failed:', error);
118
- };
119
-
120
- testWebSocket.onclose = () => {
121
- console.log('STT test connection closed');
122
- };
123
 
124
  } catch (error) {
125
  this.updateStatus('❌ Failed to test STT connection');
@@ -250,16 +243,13 @@ def main():
250
  }
251
 
252
  try {
 
 
253
  // Combine all audio chunks (unmute.sh pattern)
254
  const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' });
255
 
256
- // Convert to base64 for WebSocket transmission
257
- const reader = new FileReader();
258
- reader.onload = () => {
259
- const audioBase64 = btoa(String.fromCharCode(...new Uint8Array(reader.result)));
260
- this.sendAudioToSTT(audioBase64);
261
- };
262
- reader.readAsArrayBuffer(audioBlob);
263
 
264
  } catch (error) {
265
  console.error('Audio processing failed:', error);
@@ -267,21 +257,48 @@ def main():
267
  }
268
  }
269
 
270
- sendAudioToSTT(audioBase64) {
271
- if (!this.sttWebSocket || this.sttWebSocket.readyState !== WebSocket.OPEN) {
272
- this.updateStatus('❌ STT service not connected');
273
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  }
275
-
276
- const message = {
277
- type: 'stt_audio_chunk',
278
- audio_data: audioBase64,
279
- language: 'auto',
280
- model_size: 'base'
281
- };
282
-
283
- this.sttWebSocket.send(JSON.stringify(message));
284
- this.updateStatus('πŸ“€ Sending audio to STT service...');
285
  }
286
 
287
  updateStatus(message) {
 
104
  this.updateStatus('πŸ”— Testing STT service connection...');
105
 
106
  try {
107
+ // Since WebSocket is not working due to Gradio routing, test HTTP API instead
108
+ const response = await fetch('https://pgits-stt-gpu-service.hf.space/');
109
 
110
+ if (response.ok) {
111
+ this.updateStatus('βœ… STT service HTTP connection successful!');
112
+ console.log('STT service is online - will use HTTP API fallback');
113
+ } else {
114
+ this.updateStatus('❌ STT service HTTP connection failed');
115
+ }
 
 
 
 
 
 
 
116
 
117
  } catch (error) {
118
  this.updateStatus('❌ Failed to test STT connection');
 
243
  }
244
 
245
  try {
246
+ this.updateStatus('βš™οΈ Processing audio with Gradio API...');
247
+
248
  // Combine all audio chunks (unmute.sh pattern)
249
  const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' });
250
 
251
+ // Send to STT service using Gradio HTTP API
252
+ await this.sendAudioToSTTAPI(audioBlob);
 
 
 
 
 
253
 
254
  } catch (error) {
255
  console.error('Audio processing failed:', error);
 
257
  }
258
  }
259
 
260
+ async sendAudioToSTTAPI(audioBlob) {
261
+ try {
262
+ this.updateStatus('πŸ“€ Sending audio to STT via Gradio API...');
263
+
264
+ // Create FormData for Gradio API
265
+ const formData = new FormData();
266
+ formData.append('data', audioBlob, 'audio.webm');
267
+
268
+ // Gradio API expects this format: data: ["auto", "base", true]
269
+ formData.append('data', JSON.stringify(["auto", "base", true]));
270
+
271
+ // Send to Gradio API
272
+ const response = await fetch('https://pgits-stt-gpu-service.hf.space/api/predict', {
273
+ method: 'POST',
274
+ body: formData
275
+ });
276
+
277
+ if (response.ok) {
278
+ const result = await response.json();
279
+ console.log('STT API Response:', result);
280
+
281
+ // Extract transcription from Gradio response format
282
+ if (result && result.data && result.data.length > 1) {
283
+ const transcription = result.data[1]; // [status, transcription, timestamps]
284
+ if (transcription && transcription.trim()) {
285
+ this.updateTranscription(transcription);
286
+ this.updateStatus('βœ… Transcription completed via Gradio API');
287
+ } else {
288
+ this.updateStatus('⚠️ No transcription received');
289
+ }
290
+ } else {
291
+ this.updateStatus('❌ Unexpected API response format');
292
+ console.error('Unexpected response:', result);
293
+ }
294
+ } else {
295
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
296
+ }
297
+
298
+ } catch (error) {
299
+ console.error('STT API request failed:', error);
300
+ this.updateStatus('❌ STT API request failed: ' + error.message);
301
  }
 
 
 
 
 
 
 
 
 
 
302
  }
303
 
304
  updateStatus(message) {