Spaces:
Runtime error
Runtime error
Commit ·
ed08f62
1
Parent(s): a905808
Experiment 4
Browse files
ui.py
CHANGED
|
@@ -1,820 +1,339 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
-
import requests
|
| 4 |
import json
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
HF_SPACE_URL = "https://androidguy-speaker-diarization.hf.space"
|
| 13 |
|
| 14 |
-
class
|
| 15 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __init__(self):
|
| 17 |
-
self.
|
| 18 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=2):
|
| 76 |
-
|
| 77 |
-
conversation_display = gr.HTML(
|
| 78 |
-
"""
|
| 79 |
-
<div class='output' id="conversation" style='padding:20px; background:#111; border-radius:10px;
|
| 80 |
-
min-height:400px; font-family:Arial; font-size:16px; line-height:1.5; overflow-y:auto;'>
|
| 81 |
-
<i>Click 'Start Listening' to begin...</i>
|
| 82 |
-
</div>
|
| 83 |
-
|
| 84 |
-
<script>
|
| 85 |
-
// Global state management
|
| 86 |
-
const AppState = {
|
| 87 |
-
rtcConnection: null,
|
| 88 |
-
mediaStream: null,
|
| 89 |
-
wsConnection: null,
|
| 90 |
-
statusUpdateInterval: null,
|
| 91 |
-
wsReconnectAttempts: 0,
|
| 92 |
-
maxReconnectAttempts: 5,
|
| 93 |
-
isConnecting: false,
|
| 94 |
-
isCleaningUp: false
|
| 95 |
-
};
|
| 96 |
-
|
| 97 |
-
// Utility functions
|
| 98 |
-
const Utils = {
|
| 99 |
-
// Check connection to HF space with timeout
|
| 100 |
-
async checkHfConnection(timeout = 5000) {
|
| 101 |
-
try {
|
| 102 |
-
const controller = new AbortController();
|
| 103 |
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
| 104 |
-
|
| 105 |
-
const response = await fetch(`${window.HF_SPACE_URL}/health`, {
|
| 106 |
-
signal: controller.signal,
|
| 107 |
-
method: 'GET',
|
| 108 |
-
cache: 'no-cache'
|
| 109 |
-
});
|
| 110 |
-
|
| 111 |
-
clearTimeout(timeoutId);
|
| 112 |
-
return response.ok;
|
| 113 |
-
} catch (err) {
|
| 114 |
-
console.warn("HF Space connection failed:", err);
|
| 115 |
-
return false;
|
| 116 |
-
}
|
| 117 |
-
},
|
| 118 |
-
|
| 119 |
-
// Safe JSON parse
|
| 120 |
-
safeJsonParse(str) {
|
| 121 |
-
try {
|
| 122 |
-
return JSON.parse(str);
|
| 123 |
-
} catch (e) {
|
| 124 |
-
return null;
|
| 125 |
-
}
|
| 126 |
-
},
|
| 127 |
-
|
| 128 |
-
// Debounce function
|
| 129 |
-
debounce(func, wait) {
|
| 130 |
-
let timeout;
|
| 131 |
-
return function executedFunction(...args) {
|
| 132 |
-
const later = () => {
|
| 133 |
-
clearTimeout(timeout);
|
| 134 |
-
func(...args);
|
| 135 |
-
};
|
| 136 |
-
clearTimeout(timeout);
|
| 137 |
-
timeout = setTimeout(later, wait);
|
| 138 |
-
};
|
| 139 |
-
}
|
| 140 |
-
};
|
| 141 |
-
|
| 142 |
-
// Main streaming control
|
| 143 |
-
const StreamController = {
|
| 144 |
-
async start() {
|
| 145 |
-
if (AppState.isConnecting || AppState.isCleaningUp) {
|
| 146 |
-
console.log("Already connecting or cleaning up, ignoring start request");
|
| 147 |
-
return;
|
| 148 |
-
}
|
| 149 |
-
|
| 150 |
-
AppState.isConnecting = true;
|
| 151 |
-
|
| 152 |
-
try {
|
| 153 |
-
// Update status
|
| 154 |
-
StatusManager.update('connecting');
|
| 155 |
-
|
| 156 |
-
// Request microphone access with proper error handling
|
| 157 |
-
await this.setupMediaStream();
|
| 158 |
-
|
| 159 |
-
// Check backend availability
|
| 160 |
-
const backendAvailable = await Utils.checkHfConnection();
|
| 161 |
-
|
| 162 |
-
// Setup connections
|
| 163 |
-
if (backendAvailable) {
|
| 164 |
-
await Promise.allSettled([
|
| 165 |
-
this.setupWebRTC(),
|
| 166 |
-
this.setupWebSocket()
|
| 167 |
-
]);
|
| 168 |
-
|
| 169 |
-
StatusManager.update('connected');
|
| 170 |
-
document.getElementById("conversation").innerHTML = "<i>Connected! Start speaking...</i>";
|
| 171 |
-
} else {
|
| 172 |
-
StatusManager.update('warning', 'Backend unavailable - limited functionality');
|
| 173 |
-
document.getElementById("conversation").innerHTML =
|
| 174 |
-
"<i>Backend connection failed. Microphone active but transcription unavailable.</i>";
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
// Start status monitoring
|
| 178 |
-
AppState.statusUpdateInterval = setInterval(() => {
|
| 179 |
-
ConnectionMonitor.updateConnectionInfo();
|
| 180 |
-
}, 5000);
|
| 181 |
-
|
| 182 |
-
} catch (err) {
|
| 183 |
-
console.error('Error starting stream:', err);
|
| 184 |
-
StatusManager.update('error', err.message);
|
| 185 |
-
this.cleanup();
|
| 186 |
-
} finally {
|
| 187 |
-
AppState.isConnecting = false;
|
| 188 |
-
}
|
| 189 |
-
},
|
| 190 |
-
|
| 191 |
-
async setupMediaStream() {
|
| 192 |
-
try {
|
| 193 |
-
AppState.mediaStream = await navigator.mediaDevices.getUserMedia({
|
| 194 |
-
audio: {
|
| 195 |
-
echoCancellation: true,
|
| 196 |
-
noiseSuppression: true,
|
| 197 |
-
autoGainControl: true,
|
| 198 |
-
sampleRate: 16000 // Specify sample rate for consistency
|
| 199 |
-
}
|
| 200 |
-
});
|
| 201 |
-
} catch (err) {
|
| 202 |
-
let errorMessage;
|
| 203 |
-
switch (err.name) {
|
| 204 |
-
case 'NotAllowedError':
|
| 205 |
-
errorMessage = 'Microphone access denied. Please allow microphone access and try again.';
|
| 206 |
-
break;
|
| 207 |
-
case 'NotFoundError':
|
| 208 |
-
errorMessage = 'No microphone found. Please connect a microphone and try again.';
|
| 209 |
-
break;
|
| 210 |
-
case 'NotReadableError':
|
| 211 |
-
errorMessage = 'Microphone is being used by another application.';
|
| 212 |
-
break;
|
| 213 |
-
case 'OverconstrainedError':
|
| 214 |
-
errorMessage = 'Microphone constraints cannot be satisfied.';
|
| 215 |
-
break;
|
| 216 |
-
default:
|
| 217 |
-
errorMessage = `Microphone error: ${err.message}`;
|
| 218 |
-
}
|
| 219 |
-
throw new Error(errorMessage);
|
| 220 |
-
}
|
| 221 |
-
},
|
| 222 |
-
|
| 223 |
-
async setupWebRTC() {
|
| 224 |
-
try {
|
| 225 |
-
// Close existing connection
|
| 226 |
-
if (AppState.rtcConnection) {
|
| 227 |
-
AppState.rtcConnection.close();
|
| 228 |
-
}
|
| 229 |
-
|
| 230 |
-
const pc = new RTCPeerConnection({
|
| 231 |
-
iceServers: [
|
| 232 |
-
{ urls: 'stun:stun.l.google.com:19302' },
|
| 233 |
-
{ urls: 'stun:stun1.l.google.com:19302' }
|
| 234 |
-
]
|
| 235 |
-
});
|
| 236 |
-
|
| 237 |
-
// Add audio track
|
| 238 |
-
if (AppState.mediaStream) {
|
| 239 |
-
AppState.mediaStream.getAudioTracks().forEach(track => {
|
| 240 |
-
pc.addTrack(track, AppState.mediaStream);
|
| 241 |
-
});
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
// Connect to signaling server
|
| 245 |
-
const signalWs = new WebSocket(window.RENDER_SIGNALING_URL);
|
| 246 |
-
|
| 247 |
-
// Handle signaling messages
|
| 248 |
-
signalWs.onmessage = async (event) => {
|
| 249 |
-
const message = Utils.safeJsonParse(event.data);
|
| 250 |
-
if (!message) return;
|
| 251 |
-
|
| 252 |
-
try {
|
| 253 |
-
if (message.type === 'offer') {
|
| 254 |
-
await pc.setRemoteDescription(new RTCSessionDescription(message));
|
| 255 |
-
const answer = await pc.createAnswer();
|
| 256 |
-
await pc.setLocalDescription(answer);
|
| 257 |
-
signalWs.send(JSON.stringify(pc.localDescription));
|
| 258 |
-
} else if (message.type === 'candidate' && message.candidate) {
|
| 259 |
-
await pc.addIceCandidate(new RTCIceCandidate(message));
|
| 260 |
-
}
|
| 261 |
-
} catch (err) {
|
| 262 |
-
console.error('Error handling signaling message:', err);
|
| 263 |
-
}
|
| 264 |
-
};
|
| 265 |
-
|
| 266 |
-
// Send ICE candidates
|
| 267 |
-
pc.onicecandidate = (event) => {
|
| 268 |
-
if (event.candidate && signalWs.readyState === WebSocket.OPEN) {
|
| 269 |
-
signalWs.send(JSON.stringify({
|
| 270 |
-
type: 'candidate',
|
| 271 |
-
candidate: event.candidate
|
| 272 |
-
}));
|
| 273 |
-
}
|
| 274 |
-
};
|
| 275 |
-
|
| 276 |
-
// Handle connection state changes
|
| 277 |
-
pc.onconnectionstatechange = () => {
|
| 278 |
-
console.log('WebRTC connection state:', pc.connectionState);
|
| 279 |
-
if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') {
|
| 280 |
-
StatusManager.update('warning', 'WebRTC connection lost');
|
| 281 |
-
}
|
| 282 |
-
};
|
| 283 |
-
|
| 284 |
-
AppState.rtcConnection = pc;
|
| 285 |
-
|
| 286 |
-
// Wait for connection with timeout
|
| 287 |
-
await new Promise((resolve, reject) => {
|
| 288 |
-
const timeout = setTimeout(() => {
|
| 289 |
-
reject(new Error("WebRTC connection timeout (15s)"));
|
| 290 |
-
}, 15000);
|
| 291 |
-
|
| 292 |
-
pc.onconnectionstatechange = () => {
|
| 293 |
-
if (pc.connectionState === 'connected') {
|
| 294 |
-
clearTimeout(timeout);
|
| 295 |
-
resolve();
|
| 296 |
-
} else if (pc.connectionState === 'failed') {
|
| 297 |
-
clearTimeout(timeout);
|
| 298 |
-
reject(new Error("WebRTC connection failed"));
|
| 299 |
-
}
|
| 300 |
-
};
|
| 301 |
-
});
|
| 302 |
-
|
| 303 |
-
} catch (err) {
|
| 304 |
-
console.error('WebRTC setup error:', err);
|
| 305 |
-
throw new Error(`WebRTC setup failed: ${err.message}`);
|
| 306 |
-
}
|
| 307 |
-
},
|
| 308 |
-
|
| 309 |
-
setupWebSocket() {
|
| 310 |
-
try {
|
| 311 |
-
// Close existing connection
|
| 312 |
-
if (AppState.wsConnection) {
|
| 313 |
-
AppState.wsConnection.close();
|
| 314 |
-
}
|
| 315 |
-
|
| 316 |
-
const wsUrl = window.RENDER_SIGNALING_URL.replace('/stream', '/ws_relay');
|
| 317 |
-
AppState.wsConnection = new WebSocket(wsUrl);
|
| 318 |
-
|
| 319 |
-
AppState.wsConnection.onopen = () => {
|
| 320 |
-
console.log('WebSocket connection established');
|
| 321 |
-
AppState.wsReconnectAttempts = 0; // Reset on successful connection
|
| 322 |
-
};
|
| 323 |
-
|
| 324 |
-
AppState.wsConnection.onmessage = (event) => {
|
| 325 |
-
MessageHandler.process(event.data);
|
| 326 |
-
};
|
| 327 |
-
|
| 328 |
-
AppState.wsConnection.onerror = (error) => {
|
| 329 |
-
console.error('WebSocket error:', error);
|
| 330 |
-
StatusManager.update('warning', 'WebSocket error');
|
| 331 |
-
};
|
| 332 |
-
|
| 333 |
-
AppState.wsConnection.onclose = () => {
|
| 334 |
-
console.log('WebSocket connection closed');
|
| 335 |
-
|
| 336 |
-
// Only attempt reconnection if not cleaning up and under limit
|
| 337 |
-
if (!AppState.isCleaningUp &&
|
| 338 |
-
AppState.wsReconnectAttempts < AppState.maxReconnectAttempts) {
|
| 339 |
-
|
| 340 |
-
AppState.wsReconnectAttempts++;
|
| 341 |
-
const delay = Math.min(3000 * AppState.wsReconnectAttempts, 30000); // Max 30s delay
|
| 342 |
-
|
| 343 |
-
console.log(`Attempting WebSocket reconnection ${AppState.wsReconnectAttempts}/${AppState.maxReconnectAttempts} in ${delay}ms`);
|
| 344 |
-
|
| 345 |
-
setTimeout(() => {
|
| 346 |
-
if (!AppState.isCleaningUp) {
|
| 347 |
-
this.setupWebSocket();
|
| 348 |
-
}
|
| 349 |
-
}, delay);
|
| 350 |
-
} else if (AppState.wsReconnectAttempts >= AppState.maxReconnectAttempts) {
|
| 351 |
-
StatusManager.update('error', 'Max WebSocket reconnection attempts reached');
|
| 352 |
-
}
|
| 353 |
-
};
|
| 354 |
-
|
| 355 |
-
} catch (err) {
|
| 356 |
-
console.error('WebSocket setup error:', err);
|
| 357 |
-
throw new Error(`WebSocket setup failed: ${err.message}`);
|
| 358 |
-
}
|
| 359 |
-
},
|
| 360 |
-
|
| 361 |
-
stop() {
|
| 362 |
-
AppState.isCleaningUp = true;
|
| 363 |
-
this.cleanup();
|
| 364 |
-
StatusManager.update('disconnected');
|
| 365 |
-
AppState.isCleaningUp = false;
|
| 366 |
-
},
|
| 367 |
-
|
| 368 |
-
cleanup() {
|
| 369 |
-
// Close WebRTC connection
|
| 370 |
-
if (AppState.rtcConnection) {
|
| 371 |
-
AppState.rtcConnection.close();
|
| 372 |
-
AppState.rtcConnection = null;
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
// Close WebSocket
|
| 376 |
-
if (AppState.wsConnection) {
|
| 377 |
-
AppState.wsConnection.close();
|
| 378 |
-
AppState.wsConnection = null;
|
| 379 |
-
}
|
| 380 |
-
|
| 381 |
-
// Stop media stream
|
| 382 |
-
if (AppState.mediaStream) {
|
| 383 |
-
AppState.mediaStream.getTracks().forEach(track => track.stop());
|
| 384 |
-
AppState.mediaStream = null;
|
| 385 |
-
}
|
| 386 |
-
|
| 387 |
-
// Clear intervals
|
| 388 |
-
if (AppState.statusUpdateInterval) {
|
| 389 |
-
clearInterval(AppState.statusUpdateInterval);
|
| 390 |
-
AppState.statusUpdateInterval = null;
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
// Reset reconnection attempts
|
| 394 |
-
AppState.wsReconnectAttempts = 0;
|
| 395 |
-
}
|
| 396 |
-
};
|
| 397 |
-
|
| 398 |
-
// Message handling
|
| 399 |
-
const MessageHandler = {
|
| 400 |
-
process(data) {
|
| 401 |
-
try {
|
| 402 |
-
const message = Utils.safeJsonParse(data);
|
| 403 |
-
|
| 404 |
-
if (message) {
|
| 405 |
-
this.handleStructuredMessage(message);
|
| 406 |
-
} else {
|
| 407 |
-
// Fallback for plain HTML content
|
| 408 |
-
this.updateConversationDisplay(data);
|
| 409 |
-
}
|
| 410 |
-
|
| 411 |
-
this.autoScroll();
|
| 412 |
-
} catch (e) {
|
| 413 |
-
console.error('Error processing message:', e);
|
| 414 |
-
this.updateConversationDisplay(data);
|
| 415 |
-
this.autoScroll();
|
| 416 |
-
}
|
| 417 |
-
},
|
| 418 |
-
|
| 419 |
-
handleStructuredMessage(message) {
|
| 420 |
-
switch(message.type) {
|
| 421 |
-
case 'transcription':
|
| 422 |
-
if (message.data && message.data.conversation_html) {
|
| 423 |
-
this.updateConversationDisplay(message.data.conversation_html);
|
| 424 |
-
}
|
| 425 |
-
break;
|
| 426 |
-
|
| 427 |
-
case 'processing_result':
|
| 428 |
-
this.handleProcessingResult(message.data);
|
| 429 |
-
break;
|
| 430 |
-
|
| 431 |
-
case 'connection':
|
| 432 |
-
StatusManager.update(message.status === 'connected' ? 'connected' : 'warning');
|
| 433 |
-
break;
|
| 434 |
-
|
| 435 |
-
case 'connection_established':
|
| 436 |
-
StatusManager.update('connected');
|
| 437 |
-
if (message.conversation) {
|
| 438 |
-
this.updateConversationDisplay(message.conversation);
|
| 439 |
-
}
|
| 440 |
-
break;
|
| 441 |
-
|
| 442 |
-
case 'conversation_update':
|
| 443 |
-
if (message.conversation_html) {
|
| 444 |
-
this.updateConversationDisplay(message.conversation_html);
|
| 445 |
-
}
|
| 446 |
-
break;
|
| 447 |
-
|
| 448 |
-
case 'conversation_cleared':
|
| 449 |
-
this.updateConversationDisplay("<i>Conversation cleared. Start speaking again...</i>");
|
| 450 |
-
break;
|
| 451 |
-
|
| 452 |
-
case 'error':
|
| 453 |
-
console.error('Server error:', message.message);
|
| 454 |
-
StatusManager.update('warning', message.message);
|
| 455 |
-
break;
|
| 456 |
-
|
| 457 |
-
default:
|
| 458 |
-
console.log('Unknown message type:', message.type);
|
| 459 |
-
}
|
| 460 |
-
},
|
| 461 |
-
|
| 462 |
-
handleProcessingResult(data) {
|
| 463 |
-
if (!data) return;
|
| 464 |
-
|
| 465 |
-
if (data.status === "processed" && data.speaker_id !== undefined) {
|
| 466 |
-
const statusElem = document.getElementById('status-text');
|
| 467 |
-
if (statusElem) {
|
| 468 |
-
const speakerId = `Speaker ${data.speaker_id + 1}`;
|
| 469 |
-
statusElem.textContent = `Connected - ${speakerId} active`;
|
| 470 |
-
}
|
| 471 |
-
} else if (data.status === "error") {
|
| 472 |
-
StatusManager.update('error', data.message || 'Processing error');
|
| 473 |
-
}
|
| 474 |
-
},
|
| 475 |
-
|
| 476 |
-
updateConversationDisplay(content) {
|
| 477 |
-
const container = document.getElementById("conversation");
|
| 478 |
-
if (container) {
|
| 479 |
-
container.innerHTML = content;
|
| 480 |
-
}
|
| 481 |
-
},
|
| 482 |
-
|
| 483 |
-
autoScroll() {
|
| 484 |
-
const container = document.getElementById("conversation");
|
| 485 |
-
if (container) {
|
| 486 |
-
container.scrollTop = container.scrollHeight;
|
| 487 |
-
}
|
| 488 |
-
}
|
| 489 |
-
};
|
| 490 |
-
|
| 491 |
-
// Connection monitoring
|
| 492 |
-
const ConnectionMonitor = {
|
| 493 |
-
async updateConnectionInfo() {
|
| 494 |
-
try {
|
| 495 |
-
const hfConnected = await Utils.checkHfConnection(3000);
|
| 496 |
-
|
| 497 |
-
if (!hfConnected) {
|
| 498 |
-
StatusManager.update('warning', 'Backend unavailable');
|
| 499 |
-
|
| 500 |
-
// Try to reconnect WebSocket only if not already trying
|
| 501 |
-
if (!AppState.wsConnection || AppState.wsConnection.readyState !== WebSocket.OPEN) {
|
| 502 |
-
if (AppState.wsReconnectAttempts < AppState.maxReconnectAttempts) {
|
| 503 |
-
StreamController.setupWebSocket();
|
| 504 |
-
}
|
| 505 |
-
}
|
| 506 |
-
} else if (AppState.rtcConnection?.connectionState === 'connected' ||
|
| 507 |
-
AppState.rtcConnection?.iceConnectionState === 'connected') {
|
| 508 |
-
StatusManager.update('connected');
|
| 509 |
-
} else {
|
| 510 |
-
StatusManager.update('warning', 'Connection unstable');
|
| 511 |
-
}
|
| 512 |
-
} catch (err) {
|
| 513 |
-
console.error('Error updating connection info:', err);
|
| 514 |
-
}
|
| 515 |
-
}
|
| 516 |
-
};
|
| 517 |
-
|
| 518 |
-
// Status management
|
| 519 |
-
const StatusManager = {
|
| 520 |
-
update(status, message = '') {
|
| 521 |
-
const statusText = document.getElementById('status-text');
|
| 522 |
-
const statusIcon = document.getElementById('status-icon');
|
| 523 |
-
|
| 524 |
-
if (!statusText || !statusIcon) return;
|
| 525 |
-
|
| 526 |
-
switch(status) {
|
| 527 |
-
case 'connected':
|
| 528 |
-
statusText.textContent = message || 'Connected';
|
| 529 |
-
statusIcon.style.backgroundColor = '#4CAF50';
|
| 530 |
-
break;
|
| 531 |
-
case 'connecting':
|
| 532 |
-
statusText.textContent = 'Connecting...';
|
| 533 |
-
statusIcon.style.backgroundColor = '#FFC107';
|
| 534 |
-
break;
|
| 535 |
-
case 'disconnected':
|
| 536 |
-
statusText.textContent = 'Disconnected';
|
| 537 |
-
statusIcon.style.backgroundColor = '#9E9E9E';
|
| 538 |
-
break;
|
| 539 |
-
case 'error':
|
| 540 |
-
statusText.textContent = `Error: ${message}`;
|
| 541 |
-
statusIcon.style.backgroundColor = '#F44336';
|
| 542 |
-
break;
|
| 543 |
-
case 'warning':
|
| 544 |
-
statusText.textContent = `Warning: ${message}`;
|
| 545 |
-
statusIcon.style.backgroundColor = '#FF9800';
|
| 546 |
-
break;
|
| 547 |
-
default:
|
| 548 |
-
statusText.textContent = 'Unknown';
|
| 549 |
-
statusIcon.style.backgroundColor = '#9E9E9E';
|
| 550 |
-
}
|
| 551 |
-
}
|
| 552 |
-
};
|
| 553 |
-
|
| 554 |
-
// API functions
|
| 555 |
-
const ApiManager = {
|
| 556 |
-
async clearConversation() {
|
| 557 |
-
// Update UI immediately
|
| 558 |
-
document.getElementById("conversation").innerHTML =
|
| 559 |
-
"<i>Conversation cleared. Start speaking again...</i>";
|
| 560 |
-
|
| 561 |
-
// Try backend API
|
| 562 |
-
try {
|
| 563 |
-
const isConnected = await Utils.checkHfConnection();
|
| 564 |
-
if (isConnected) {
|
| 565 |
-
const response = await fetch(`${window.HF_SPACE_URL}/clear`, {
|
| 566 |
-
method: 'POST',
|
| 567 |
-
headers: {
|
| 568 |
-
'Content-Type': 'application/json'
|
| 569 |
-
}
|
| 570 |
-
});
|
| 571 |
-
|
| 572 |
-
if (!response.ok) {
|
| 573 |
-
throw new Error(`HTTP ${response.status}`);
|
| 574 |
-
}
|
| 575 |
-
|
| 576 |
-
console.log("Backend conversation cleared successfully");
|
| 577 |
-
}
|
| 578 |
-
} catch (err) {
|
| 579 |
-
console.warn("Backend clear API failed:", err);
|
| 580 |
-
}
|
| 581 |
-
},
|
| 582 |
-
|
| 583 |
-
async updateSettings() {
|
| 584 |
-
const threshold = document.querySelector('input[data-testid="threshold-slider"]')?.value || 0.7;
|
| 585 |
-
const maxSpeakers = document.querySelector('input[data-testid="speakers-slider"]')?.value || 4;
|
| 586 |
-
|
| 587 |
-
// Update UI immediately
|
| 588 |
-
const statusOutput = document.getElementById('status-output');
|
| 589 |
-
if (statusOutput) {
|
| 590 |
-
statusOutput.innerHTML = `
|
| 591 |
-
<h2>System Status</h2>
|
| 592 |
-
<p>Settings updated:</p>
|
| 593 |
-
<ul>
|
| 594 |
-
<li>Threshold: ${threshold}</li>
|
| 595 |
-
<li>Max Speakers: ${maxSpeakers}</li>
|
| 596 |
-
</ul>
|
| 597 |
-
<p>Transcription Models:</p>
|
| 598 |
-
<ul>
|
| 599 |
-
<li>Final: ${window.FINAL_TRANSCRIPTION_MODEL || "distil-large-v3"}</li>
|
| 600 |
-
<li>Realtime: ${window.REALTIME_TRANSCRIPTION_MODEL || "distil-small.en"}</li>
|
| 601 |
-
</ul>
|
| 602 |
-
`;
|
| 603 |
-
}
|
| 604 |
-
|
| 605 |
-
// Try backend API
|
| 606 |
-
try {
|
| 607 |
-
const isConnected = await Utils.checkHfConnection();
|
| 608 |
-
if (isConnected) {
|
| 609 |
-
const response = await fetch(
|
| 610 |
-
`${window.HF_SPACE_URL}/settings?threshold=${threshold}&max_speakers=${maxSpeakers}`,
|
| 611 |
-
{
|
| 612 |
-
method: 'POST',
|
| 613 |
-
headers: {
|
| 614 |
-
'Content-Type': 'application/json'
|
| 615 |
-
}
|
| 616 |
-
}
|
| 617 |
-
);
|
| 618 |
-
|
| 619 |
-
if (!response.ok) {
|
| 620 |
-
throw new Error(`HTTP ${response.status}`);
|
| 621 |
-
}
|
| 622 |
-
|
| 623 |
-
console.log("Backend settings updated successfully");
|
| 624 |
-
}
|
| 625 |
-
} catch (err) {
|
| 626 |
-
console.warn("Backend settings update failed:", err);
|
| 627 |
-
}
|
| 628 |
-
}
|
| 629 |
-
};
|
| 630 |
-
|
| 631 |
-
// DOM initialization
|
| 632 |
-
const DOMManager = {
|
| 633 |
-
init() {
|
| 634 |
-
StatusManager.update('disconnected');
|
| 635 |
-
|
| 636 |
-
// Use MutationObserver for reliable button detection
|
| 637 |
-
const observer = new MutationObserver(Utils.debounce(() => {
|
| 638 |
-
if (this.bindButtons()) {
|
| 639 |
-
observer.disconnect();
|
| 640 |
-
}
|
| 641 |
-
}, 100));
|
| 642 |
-
|
| 643 |
-
observer.observe(document.body, {
|
| 644 |
-
childList: true,
|
| 645 |
-
subtree: true
|
| 646 |
-
});
|
| 647 |
-
|
| 648 |
-
// Fallback: try binding immediately
|
| 649 |
-
this.bindButtons();
|
| 650 |
-
|
| 651 |
-
// Cleanup on page unload
|
| 652 |
-
window.addEventListener('beforeunload', () => {
|
| 653 |
-
StreamController.cleanup();
|
| 654 |
-
});
|
| 655 |
-
},
|
| 656 |
-
|
| 657 |
-
bindButtons() {
|
| 658 |
-
const buttons = {
|
| 659 |
-
start: document.getElementById('btn-start') ||
|
| 660 |
-
document.querySelector('button[aria-label="Start Listening"]'),
|
| 661 |
-
stop: document.getElementById('btn-stop') ||
|
| 662 |
-
document.querySelector('button[aria-label="Stop"]'),
|
| 663 |
-
clear: document.getElementById('btn-clear') ||
|
| 664 |
-
document.querySelector('button[aria-label="Clear"]'),
|
| 665 |
-
update: document.getElementById('btn-update') ||
|
| 666 |
-
document.querySelector('button[aria-label="Update Settings"]')
|
| 667 |
-
};
|
| 668 |
-
|
| 669 |
-
const allFound = Object.values(buttons).every(btn => btn !== null);
|
| 670 |
-
|
| 671 |
-
if (allFound) {
|
| 672 |
-
// Remove existing listeners to prevent duplicates
|
| 673 |
-
Object.values(buttons).forEach(btn => {
|
| 674 |
-
if (btn.dataset.bound !== 'true') {
|
| 675 |
-
btn.onclick = null;
|
| 676 |
-
}
|
| 677 |
-
});
|
| 678 |
-
|
| 679 |
-
// Bind new listeners
|
| 680 |
-
buttons.start.onclick = () => StreamController.start();
|
| 681 |
-
buttons.stop.onclick = () => StreamController.stop();
|
| 682 |
-
buttons.clear.onclick = () => ApiManager.clearConversation();
|
| 683 |
-
buttons.update.onclick = () => ApiManager.updateSettings();
|
| 684 |
-
|
| 685 |
-
// Mark as bound
|
| 686 |
-
Object.values(buttons).forEach(btn => {
|
| 687 |
-
btn.dataset.bound = 'true';
|
| 688 |
-
});
|
| 689 |
-
|
| 690 |
-
console.log("All buttons bound successfully");
|
| 691 |
-
return true;
|
| 692 |
-
}
|
| 693 |
-
|
| 694 |
-
return false;
|
| 695 |
-
}
|
| 696 |
-
};
|
| 697 |
-
|
| 698 |
-
// Initialize when DOM is ready
|
| 699 |
-
if (document.readyState === 'loading') {
|
| 700 |
-
document.addEventListener('DOMContentLoaded', () => DOMManager.init());
|
| 701 |
-
} else {
|
| 702 |
-
DOMManager.init();
|
| 703 |
-
}
|
| 704 |
-
</script>
|
| 705 |
-
""",
|
| 706 |
-
label="Live Conversation"
|
| 707 |
-
)
|
| 708 |
-
|
| 709 |
-
# Control buttons with elem_id for reliable selection
|
| 710 |
-
with gr.Row():
|
| 711 |
-
start_btn = gr.Button("▶️ Start Listening", variant="primary", size="lg", elem_id="btn-start")
|
| 712 |
-
stop_btn = gr.Button("⏹️ Stop", variant="stop", size="lg", elem_id="btn-stop")
|
| 713 |
-
clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="lg", elem_id="btn-clear")
|
| 714 |
-
|
| 715 |
-
# Status display with elem_id for reliable selection
|
| 716 |
-
status_output = gr.Markdown(
|
| 717 |
-
"""
|
| 718 |
-
## System Status
|
| 719 |
-
Waiting to connect...
|
| 720 |
-
|
| 721 |
-
*Click Start Listening to begin*
|
| 722 |
-
""",
|
| 723 |
-
label="Status Information",
|
| 724 |
-
elem_id="status-output"
|
| 725 |
-
)
|
| 726 |
|
| 727 |
with gr.Column(scale=1):
|
| 728 |
-
|
| 729 |
-
|
| 730 |
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
value=DEFAULT_CHANGE_THRESHOLD,
|
| 736 |
-
label="Speaker Change Sensitivity",
|
| 737 |
-
info="Lower = more sensitive (more speaker changes)",
|
| 738 |
-
elem_id="threshold-slider"
|
| 739 |
-
)
|
| 740 |
-
|
| 741 |
-
max_speakers_slider = gr.Slider(
|
| 742 |
-
minimum=2,
|
| 743 |
-
maximum=ABSOLUTE_MAX_SPEAKERS,
|
| 744 |
-
step=1,
|
| 745 |
-
value=DEFAULT_MAX_SPEAKERS,
|
| 746 |
-
label="Maximum Speakers",
|
| 747 |
-
elem_id="speakers-slider"
|
| 748 |
-
)
|
| 749 |
-
|
| 750 |
-
update_btn = gr.Button("Update Settings", variant="secondary", elem_id="btn-update")
|
| 751 |
-
|
| 752 |
-
# Instructions
|
| 753 |
-
gr.Markdown("""
|
| 754 |
-
## 📋 Instructions
|
| 755 |
-
1. **Start Listening** - allows browser to access microphone
|
| 756 |
-
2. **Speak** - system will transcribe and identify speakers
|
| 757 |
-
3. **Stop** when finished
|
| 758 |
-
4. **Clear** to reset conversation
|
| 759 |
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
|
| 771 |
-
# Set up
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
data = resp.json()
|
| 778 |
-
return data.get('status', 'No status information')
|
| 779 |
-
return f"HTTP {resp.status_code}"
|
| 780 |
-
except requests.exceptions.Timeout:
|
| 781 |
-
return "Connection timeout"
|
| 782 |
-
except requests.exceptions.ConnectionError:
|
| 783 |
-
return "Connection error - backend unavailable"
|
| 784 |
-
except Exception as e:
|
| 785 |
-
return f"Error: {str(e)}"
|
| 786 |
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
|
|
|
| 791 |
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
resource_manager.cleanup()
|
| 804 |
|
| 805 |
-
#
|
| 806 |
if __name__ == "__main__":
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
import asyncio
|
| 6 |
+
import websockets
|
| 7 |
+
import logging
|
| 8 |
+
from fastrtc import RTCComponent
|
| 9 |
+
import threading
|
| 10 |
|
| 11 |
+
# Configure logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
RENDER_SIGNALING_URL = os.getenv("RENDER_SIGNALING_URL", "wss://render-signal-audio.onrender.com/stream")
|
| 16 |
+
HF_SPACE_URL = os.getenv("HF_SPACE_URL", "androidguy-speaker-diarization.hf.space")
|
| 17 |
+
WS_TRANSCRIPTION_URL = f"wss://{HF_SPACE_URL}/ws_transcription"
|
|
|
|
| 18 |
|
| 19 |
+
class TranscriptionClient:
|
| 20 |
+
"""Client to handle WebSocket connection to transcription service"""
|
| 21 |
+
|
| 22 |
+
def __init__(self, url, on_message, on_error=None, on_close=None):
|
| 23 |
+
self.url = url
|
| 24 |
+
self.on_message = on_message
|
| 25 |
+
self.on_error = on_error or (lambda e: logger.error(f"WebSocket error: {e}"))
|
| 26 |
+
self.on_close = on_close or (lambda: logger.info("WebSocket closed"))
|
| 27 |
+
self.ws = None
|
| 28 |
+
self.running = False
|
| 29 |
+
self.connected = False
|
| 30 |
+
self.reconnect_task = None
|
| 31 |
+
self.thread = None
|
| 32 |
+
|
| 33 |
+
async def connect_async(self):
|
| 34 |
+
"""Connect to WebSocket server asynchronously"""
|
| 35 |
+
try:
|
| 36 |
+
self.ws = await websockets.connect(self.url)
|
| 37 |
+
self.connected = True
|
| 38 |
+
logger.info(f"Connected to {self.url}")
|
| 39 |
+
|
| 40 |
+
# Start listening for messages
|
| 41 |
+
while self.running:
|
| 42 |
+
try:
|
| 43 |
+
message = await self.ws.recv()
|
| 44 |
+
self.on_message(message)
|
| 45 |
+
except websockets.exceptions.ConnectionClosed:
|
| 46 |
+
logger.warning("Connection closed")
|
| 47 |
+
self.connected = False
|
| 48 |
+
break
|
| 49 |
+
except Exception as e:
|
| 50 |
+
self.on_error(e)
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Handle connection closed
|
| 54 |
+
self.connected = False
|
| 55 |
+
self.on_close()
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
logger.error(f"Connection error: {e}")
|
| 59 |
+
self.connected = False
|
| 60 |
+
self.on_error(e)
|
| 61 |
+
|
| 62 |
+
def connect(self):
|
| 63 |
+
"""Start connection in a separate thread"""
|
| 64 |
+
if self.running:
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
self.running = True
|
| 68 |
+
|
| 69 |
+
def run_async_loop():
|
| 70 |
+
loop = asyncio.new_event_loop()
|
| 71 |
+
asyncio.set_event_loop(loop)
|
| 72 |
+
loop.run_until_complete(self.connect_async())
|
| 73 |
+
loop.close()
|
| 74 |
+
|
| 75 |
+
self.thread = threading.Thread(target=run_async_loop, daemon=True)
|
| 76 |
+
self.thread.start()
|
| 77 |
+
|
| 78 |
+
def disconnect(self):
|
| 79 |
+
"""Disconnect from WebSocket server"""
|
| 80 |
+
self.running = False
|
| 81 |
+
if self.ws:
|
| 82 |
+
asyncio.run(self.ws.close())
|
| 83 |
+
self.ws = None
|
| 84 |
+
self.connected = False
|
| 85 |
+
|
| 86 |
+
class SpeakerDiarizationUI:
|
| 87 |
+
"""Main UI for speaker diarization"""
|
| 88 |
+
|
| 89 |
def __init__(self):
|
| 90 |
+
self.transcription_client = None
|
| 91 |
+
self.conversation_html = ""
|
| 92 |
+
self.system_status = {"status": "disconnected"}
|
| 93 |
+
self.webrtc_state = "stopped"
|
| 94 |
+
|
| 95 |
+
def handle_transcription_message(self, message):
|
| 96 |
+
"""Handle incoming transcription messages"""
|
| 97 |
+
try:
|
| 98 |
+
data = json.loads(message)
|
| 99 |
+
message_type = data.get("type", "unknown")
|
| 100 |
+
|
| 101 |
+
if message_type == "conversation_update":
|
| 102 |
+
# Update the conversation display
|
| 103 |
+
self.conversation_html = data.get("conversation_html", "")
|
| 104 |
+
# Update system status if available
|
| 105 |
+
if "status" in data:
|
| 106 |
+
self.system_status = data.get("status", {})
|
| 107 |
+
|
| 108 |
+
elif message_type == "connection":
|
| 109 |
+
# Handle connection status update
|
| 110 |
+
status = data.get("status", "unknown")
|
| 111 |
+
logger.info(f"Connection status: {status}")
|
| 112 |
+
if "hf_space_status" in data:
|
| 113 |
+
self.system_status["hf_space_status"] = data["hf_space_status"]
|
| 114 |
+
|
| 115 |
+
elif message_type == "error":
|
| 116 |
+
# Handle error message
|
| 117 |
+
error_msg = data.get("message", "Unknown error")
|
| 118 |
+
logger.error(f"Error from server: {error_msg}")
|
| 119 |
+
|
| 120 |
+
except json.JSONDecodeError:
|
| 121 |
+
logger.warning(f"Received invalid JSON: {message}")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
logger.error(f"Error handling message: {e}")
|
| 124 |
|
| 125 |
+
def handle_transcription_error(self, error):
|
| 126 |
+
"""Handle WebSocket errors"""
|
| 127 |
+
logger.error(f"WebSocket error: {error}")
|
| 128 |
|
| 129 |
+
def handle_transcription_close(self):
|
| 130 |
+
"""Handle WebSocket connection closure"""
|
| 131 |
+
logger.info("WebSocket connection closed")
|
| 132 |
+
self.system_status["status"] = "disconnected"
|
| 133 |
|
| 134 |
+
def connect_to_transcription(self):
|
| 135 |
+
"""Connect to transcription WebSocket"""
|
| 136 |
+
if self.transcription_client and self.transcription_client.connected:
|
| 137 |
+
return
|
| 138 |
+
|
| 139 |
+
self.transcription_client = TranscriptionClient(
|
| 140 |
+
url=WS_TRANSCRIPTION_URL,
|
| 141 |
+
on_message=self.handle_transcription_message,
|
| 142 |
+
on_error=self.handle_transcription_error,
|
| 143 |
+
on_close=self.handle_transcription_close
|
| 144 |
+
)
|
| 145 |
+
self.transcription_client.connect()
|
| 146 |
+
|
| 147 |
+
def disconnect_from_transcription(self):
|
| 148 |
+
"""Disconnect from transcription WebSocket"""
|
| 149 |
+
if self.transcription_client:
|
| 150 |
+
self.transcription_client.disconnect()
|
| 151 |
+
self.transcription_client = None
|
| 152 |
+
|
| 153 |
+
def start_listening(self):
|
| 154 |
+
"""Start listening to audio and connect to services"""
|
| 155 |
+
self.connect_to_transcription()
|
| 156 |
+
self.webrtc_state = "started"
|
| 157 |
+
return {
|
| 158 |
+
webrtc: gr.update(streaming=True),
|
| 159 |
+
status_display: gr.update(value=f"Status: Connected and listening"),
|
| 160 |
+
start_button: gr.update(visible=False),
|
| 161 |
+
stop_button: gr.update(visible=True),
|
| 162 |
+
clear_button: gr.update(visible=True)
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
def stop_listening(self):
|
| 166 |
+
"""Stop listening to audio and disconnect from services"""
|
| 167 |
+
self.disconnect_from_transcription()
|
| 168 |
+
self.webrtc_state = "stopped"
|
| 169 |
+
return {
|
| 170 |
+
webrtc: gr.update(streaming=False),
|
| 171 |
+
status_display: gr.update(value=f"Status: Disconnected"),
|
| 172 |
+
start_button: gr.update(visible=True),
|
| 173 |
+
stop_button: gr.update(visible=False),
|
| 174 |
+
clear_button: gr.update(visible=True)
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
def clear_conversation(self):
|
| 178 |
+
"""Clear the conversation display"""
|
| 179 |
+
# Call API to clear conversation
|
| 180 |
+
import requests
|
| 181 |
+
try:
|
| 182 |
+
response = requests.post(f"https://{HF_SPACE_URL}/clear")
|
| 183 |
+
if response.status_code == 200:
|
| 184 |
+
logger.info("Conversation cleared")
|
| 185 |
+
else:
|
| 186 |
+
logger.error(f"Failed to clear conversation: {response.status_code}")
|
| 187 |
+
except Exception as e:
|
| 188 |
+
logger.error(f"Error clearing conversation: {e}")
|
| 189 |
|
| 190 |
+
# Clear local display
|
| 191 |
+
self.conversation_html = ""
|
| 192 |
+
return {
|
| 193 |
+
conversation_display: gr.update(value="<div class='conversation-container'><p>Conversation cleared</p></div>")
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
def update_display(self):
|
| 197 |
+
"""Update conversation display - called periodically"""
|
| 198 |
+
status_text = f"Status: "
|
| 199 |
+
if self.webrtc_state == "started":
|
| 200 |
+
status_text += "Connected and listening"
|
| 201 |
+
else:
|
| 202 |
+
status_text += "Disconnected"
|
| 203 |
+
|
| 204 |
+
if self.system_status.get("hf_space_status"):
|
| 205 |
+
status_text += f" | HF Space: {self.system_status['hf_space_status']}"
|
| 206 |
+
|
| 207 |
+
return {
|
| 208 |
+
conversation_display: gr.update(value=self.conversation_html if self.conversation_html else "<div class='conversation-container'><p>No conversation yet. Start speaking to begin transcription.</p></div>"),
|
| 209 |
+
status_display: gr.update(value=status_text)
|
| 210 |
+
}
|
| 211 |
|
| 212 |
+
# Create UI instance
|
| 213 |
+
ui = SpeakerDiarizationUI()
|
| 214 |
|
| 215 |
+
# Custom CSS for better styling
|
| 216 |
+
css = """
|
| 217 |
+
.conversation-container {
|
| 218 |
+
border: 1px solid #ddd;
|
| 219 |
+
border-radius: 10px;
|
| 220 |
+
padding: 15px;
|
| 221 |
+
margin-bottom: 10px;
|
| 222 |
+
max-height: 500px;
|
| 223 |
+
overflow-y: auto;
|
| 224 |
+
background-color: white;
|
| 225 |
+
}
|
| 226 |
+
.speaker {
|
| 227 |
+
margin-bottom: 12px;
|
| 228 |
+
border-radius: 8px;
|
| 229 |
+
padding: 8px 12px;
|
| 230 |
+
}
|
| 231 |
+
.speaker-label {
|
| 232 |
+
font-weight: bold;
|
| 233 |
+
margin-bottom: 5px;
|
| 234 |
+
}
|
| 235 |
+
.status-display {
|
| 236 |
+
margin-top: 10px;
|
| 237 |
+
padding: 5px 10px;
|
| 238 |
+
background-color: #f0f0f0;
|
| 239 |
+
border-radius: 5px;
|
| 240 |
+
font-size: 0.9rem;
|
| 241 |
+
}
|
| 242 |
+
"""
|
| 243 |
+
|
| 244 |
+
# Create Gradio interface as a function to avoid clashing
|
| 245 |
+
def create_interface():
|
| 246 |
+
with gr.Blocks(css=css) as interface:
|
| 247 |
+
gr.Markdown("# Real-Time Speaker Diarization")
|
| 248 |
+
gr.Markdown("This app performs real-time speaker diarization on your audio. It automatically transcribes speech and identifies different speakers.")
|
| 249 |
|
| 250 |
with gr.Row():
|
| 251 |
with gr.Column(scale=2):
|
| 252 |
+
conversation_display = gr.HTML("<div class='conversation-container'><p>No conversation yet. Start speaking to begin transcription.</p></div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
with gr.Column(scale=1):
|
| 255 |
+
status_display = gr.Markdown("Status: Disconnected", elem_classes=["status-display"])
|
| 256 |
+
webrtc = RTCComponent(url=RENDER_SIGNALING_URL, streaming=False, modality="audio", mode="send-receive")
|
| 257 |
|
| 258 |
+
with gr.Row():
|
| 259 |
+
start_button = gr.Button("Start Listening", variant="primary")
|
| 260 |
+
stop_button = gr.Button("Stop Listening", variant="secondary", visible=False)
|
| 261 |
+
clear_button = gr.Button("Clear Conversation", visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 264 |
+
speaker_threshold = gr.Slider(0.5, 0.9, value=0.65, label="Speaker Change Threshold")
|
| 265 |
+
max_speakers = gr.Slider(2, 8, value=4, step=1, label="Maximum Number of Speakers")
|
| 266 |
+
|
| 267 |
+
def update_settings(threshold, speakers):
|
| 268 |
+
import requests
|
| 269 |
+
try:
|
| 270 |
+
response = requests.post(
|
| 271 |
+
f"https://{HF_SPACE_URL}/settings",
|
| 272 |
+
params={"threshold": threshold, "max_speakers": speakers}
|
| 273 |
+
)
|
| 274 |
+
if response.status_code == 200:
|
| 275 |
+
return gr.update(value=f"Settings updated: Threshold={threshold}, Max Speakers={speakers}")
|
| 276 |
+
else:
|
| 277 |
+
return gr.update(value=f"Failed to update settings: {response.status_code}")
|
| 278 |
+
except Exception as e:
|
| 279 |
+
return gr.update(value=f"Error updating settings: {e}")
|
| 280 |
+
|
| 281 |
+
settings_button = gr.Button("Update Settings")
|
| 282 |
+
settings_status = gr.Markdown("", elem_classes=["status-display"])
|
| 283 |
+
|
| 284 |
+
settings_button.click(
|
| 285 |
+
update_settings,
|
| 286 |
+
[speaker_threshold, max_speakers],
|
| 287 |
+
[settings_status]
|
| 288 |
+
)
|
| 289 |
|
| 290 |
+
# Set up event handlers
|
| 291 |
+
start_button.click(
|
| 292 |
+
ui.start_listening,
|
| 293 |
+
[],
|
| 294 |
+
[webrtc, status_display, start_button, stop_button, clear_button]
|
| 295 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
+
stop_button.click(
|
| 298 |
+
ui.stop_listening,
|
| 299 |
+
[],
|
| 300 |
+
[webrtc, status_display, start_button, stop_button, clear_button]
|
| 301 |
+
)
|
| 302 |
|
| 303 |
+
clear_button.click(
|
| 304 |
+
ui.clear_conversation,
|
| 305 |
+
[],
|
| 306 |
+
[conversation_display]
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
# Periodic update every 0.5 seconds
|
| 310 |
+
interface.load(
|
| 311 |
+
ui.update_display,
|
| 312 |
+
[],
|
| 313 |
+
[conversation_display, status_display],
|
| 314 |
+
every=0.5
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
return interface
|
| 318 |
|
| 319 |
+
# Global interface instance
|
| 320 |
+
interface = None
|
|
|
|
| 321 |
|
| 322 |
+
# Launch the app
|
| 323 |
if __name__ == "__main__":
|
| 324 |
+
interface = create_interface()
|
| 325 |
+
interface.launch()
|
| 326 |
+
|
| 327 |
+
# Add mount_ui function for integration with FastAPI
|
| 328 |
+
def mount_ui(app):
|
| 329 |
+
"""Mount the Gradio interface at /ui path of the FastAPI app"""
|
| 330 |
+
global interface
|
| 331 |
+
|
| 332 |
+
# Create interface if it doesn't exist yet
|
| 333 |
+
if interface is None:
|
| 334 |
+
interface = create_interface()
|
| 335 |
+
|
| 336 |
+
# Mount Gradio app at /ui path
|
| 337 |
+
interface.mount_in_app(app, path="/ui")
|
| 338 |
+
|
| 339 |
+
return app
|