Spaces:
Running
Running
Commit
Β·
b9d6018
1
Parent(s):
ed08f62
Revert
Browse files
ui.py
CHANGED
@@ -1,339 +1,506 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
import threading
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
|
70 |
-
|
71 |
-
|
72 |
-
loop.run_until_complete(self.connect_async())
|
73 |
-
loop.close()
|
74 |
|
75 |
-
|
76 |
-
|
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 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
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 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
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 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
.
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
conversation_display = gr.HTML("<div class='conversation-container'><p>No conversation yet. Start speaking to begin transcription.</p></div>")
|
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 |
-
|
260 |
-
|
261 |
-
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
266 |
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
[
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
|
303 |
-
|
304 |
-
|
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 |
-
|
318 |
|
319 |
-
#
|
320 |
-
|
321 |
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
interface.launch()
|
326 |
|
327 |
-
#
|
328 |
-
|
329 |
-
|
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
|
|
|
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__)
|
5 |
+
# Connection configuration (separate signaling server from model server)
|
6 |
+
# These will be replaced at deployment time with the correct URLs
|
7 |
+
RENDER_SIGNALING_URL = "wss://render-signal-audio.onrender.com/stream"
|
8 |
+
HF_SPACE_URL = "https://androidguy-speaker-diarization.hf.space"
|
|
|
9 |
|
10 |
+
def build_ui():
|
11 |
+
"""Build Gradio UI for speaker diarization"""
|
12 |
+
with gr.Blocks(title="Real-time Speaker Diarization", theme=gr.themes.Soft()) as demo:
|
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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Header and description
|
25 |
+
gr.Markdown("# π€ Live Speaker Diarization")
|
26 |
+
gr.Markdown(f"Real-time speech recognition with automatic speaker identification")
|
|
|
|
|
27 |
|
28 |
+
# Add transcription model info
|
29 |
+
gr.Markdown(f"**Using Models:** Final: {FINAL_TRANSCRIPTION_MODEL}, Realtime: {REALTIME_TRANSCRIPTION_MODEL}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Status indicator
|
32 |
+
connection_status = gr.HTML(
|
33 |
+
"""<div class="status-indicator">
|
34 |
+
<span id="status-text" style="color:#888;">Waiting to connect...</span>
|
35 |
+
<span id="status-icon" style="width:10px; height:10px; display:inline-block;
|
36 |
+
background-color:#888; border-radius:50%; margin-left:5px;"></span>
|
37 |
+
</div>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column(scale=2):
|
42 |
+
# Conversation display with embedded JavaScript for WebRTC and audio handling
|
43 |
+
conversation_display = gr.HTML(
|
44 |
+
"""
|
45 |
+
<div class='output' id="conversation" style='padding:20px; background:#111; border-radius:10px;
|
46 |
+
min-height:400px; font-family:Arial; font-size:16px; line-height:1.5; overflow-y:auto;'>
|
47 |
+
<i>Click 'Start Listening' to begin...</i>
|
48 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
<script>
|
51 |
+
// Global variables
|
52 |
+
let rtcConnection;
|
53 |
+
let mediaStream;
|
54 |
+
let wsConnection;
|
55 |
+
let statusUpdateInterval;
|
56 |
+
|
57 |
+
// Check connection to HF space
|
58 |
+
async function checkHfConnection() {
|
59 |
+
try {
|
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);
|
96 |
+
}
|
97 |
+
}
|
98 |
+
|
99 |
+
// Set up WebRTC connection to Render signaling server
|
100 |
+
async function setupWebRTC() {
|
101 |
+
try {
|
102 |
+
if (rtcConnection) {
|
103 |
+
rtcConnection.close();
|
104 |
+
}
|
105 |
+
|
106 |
+
// Use FastRTC's connection approach
|
107 |
+
const pc = new RTCPeerConnection({
|
108 |
+
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
109 |
+
});
|
110 |
+
|
111 |
+
// Add audio track
|
112 |
+
mediaStream.getAudioTracks().forEach(track => {
|
113 |
+
pc.addTrack(track, mediaStream);
|
114 |
+
});
|
115 |
+
|
116 |
+
// Connect to FastRTC signaling via WebSocket
|
117 |
+
const signalWs = new WebSocket(window.RENDER_SIGNALING_URL.replace('wss://', 'wss://'));
|
118 |
+
|
119 |
+
// Handle signaling messages
|
120 |
+
signalWs.onmessage = async (event) => {
|
121 |
+
const message = JSON.parse(event.data);
|
122 |
+
|
123 |
+
if (message.type === 'offer') {
|
124 |
+
await pc.setRemoteDescription(new RTCSessionDescription(message));
|
125 |
+
const answer = await pc.createAnswer();
|
126 |
+
await pc.setLocalDescription(answer);
|
127 |
+
signalWs.send(JSON.stringify(pc.localDescription));
|
128 |
+
} else if (message.type === 'candidate') {
|
129 |
+
if (message.candidate) {
|
130 |
+
await pc.addIceCandidate(new RTCIceCandidate(message));
|
131 |
+
}
|
132 |
+
}
|
133 |
+
};
|
134 |
+
|
135 |
+
// Send ICE candidates
|
136 |
+
pc.onicecandidate = (event) => {
|
137 |
+
if (event.candidate) {
|
138 |
+
signalWs.send(JSON.stringify({
|
139 |
+
type: 'candidate',
|
140 |
+
candidate: event.candidate
|
141 |
+
}));
|
142 |
+
}
|
143 |
+
};
|
144 |
+
|
145 |
+
// Keep connection reference
|
146 |
+
rtcConnection = pc;
|
147 |
+
|
148 |
+
// Wait for connection to be established
|
149 |
+
await new Promise((resolve, reject) => {
|
150 |
+
const timeout = setTimeout(() => reject(new Error("WebRTC connection timeout")), 10000);
|
151 |
+
pc.onconnectionstatechange = () => {
|
152 |
+
if (pc.connectionState === 'connected') {
|
153 |
+
clearTimeout(timeout);
|
154 |
+
resolve();
|
155 |
+
} else if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') {
|
156 |
+
clearTimeout(timeout);
|
157 |
+
reject(new Error("WebRTC connection failed"));
|
158 |
+
}
|
159 |
+
};
|
160 |
+
});
|
161 |
+
|
162 |
+
updateStatus('connected');
|
163 |
+
} catch (err) {
|
164 |
+
console.error('WebRTC setup error:', err);
|
165 |
+
updateStatus('error', 'WebRTC setup failed: ' + err.message);
|
166 |
+
}
|
167 |
+
}
|
168 |
+
|
169 |
+
// Set up WebSocket connection to HF Space for conversation updates
|
170 |
+
function setupWebSocket() {
|
171 |
+
const wsUrl = window.RENDER_SIGNALING_URL.replace('stream', 'ws_relay');
|
172 |
+
wsConnection = new WebSocket(wsUrl);
|
173 |
+
|
174 |
+
wsConnection.onopen = () => {
|
175 |
+
console.log('WebSocket connection established');
|
176 |
+
};
|
177 |
+
|
178 |
+
wsConnection.onmessage = (event) => {
|
179 |
+
try {
|
180 |
+
// Parse the JSON message
|
181 |
+
const message = JSON.parse(event.data);
|
182 |
+
|
183 |
+
// Process different message types
|
184 |
+
switch(message.type) {
|
185 |
+
case 'transcription':
|
186 |
+
// Handle transcription data
|
187 |
+
if (message && message.data && typeof message.data === 'object') {
|
188 |
+
document.getElementById("conversation").innerHTML = message.data.conversation_html ||
|
189 |
+
JSON.stringify(message.data);
|
190 |
+
}
|
191 |
+
break;
|
192 |
+
|
193 |
+
case 'processing_result':
|
194 |
+
// Handle individual audio chunk processing result
|
195 |
+
console.log('Processing result:', message.data);
|
196 |
+
|
197 |
+
// Update status info if needed
|
198 |
+
if (message.data && message.data.status === "processed") {
|
199 |
+
const statusElem = document.getElementById('status-text');
|
200 |
+
if (statusElem) {
|
201 |
+
const speakerId = message.data.speaker_id !== undefined ?
|
202 |
+
`Speaker ${message.data.speaker_id + 1}` : '';
|
203 |
+
|
204 |
+
if (speakerId) {
|
205 |
+
statusElem.textContent = `Connected - ${speakerId} active`;
|
206 |
+
}
|
207 |
+
}
|
208 |
+
} else if (message.data && message.data.status === "error") {
|
209 |
+
updateStatus('error', message.data.message || 'Processing error');
|
210 |
+
}
|
211 |
+
break;
|
212 |
+
|
213 |
+
case 'connection':
|
214 |
+
console.log('Connection status:', message.status);
|
215 |
+
updateStatus(message.status === 'connected' ? 'connected' : 'warning');
|
216 |
+
break;
|
217 |
+
|
218 |
+
case 'connection_established':
|
219 |
+
console.log('Connection established:', message);
|
220 |
+
updateStatus('connected');
|
221 |
+
|
222 |
+
// If initial conversation is provided, display it
|
223 |
+
if (message.conversation) {
|
224 |
+
document.getElementById("conversation").innerHTML = message.conversation;
|
225 |
+
}
|
226 |
+
break;
|
227 |
+
|
228 |
+
case 'conversation_update':
|
229 |
+
if (message.conversation_html) {
|
230 |
+
document.getElementById("conversation").innerHTML = message.conversation_html;
|
231 |
+
}
|
232 |
+
break;
|
233 |
+
|
234 |
+
case 'conversation_cleared':
|
235 |
+
document.getElementById("conversation").innerHTML =
|
236 |
+
"<i>Conversation cleared. Start speaking again...</i>";
|
237 |
+
break;
|
238 |
+
|
239 |
+
case 'error':
|
240 |
+
console.error('Error message from server:', message.message);
|
241 |
+
updateStatus('warning', message.message);
|
242 |
+
break;
|
243 |
+
|
244 |
+
default:
|
245 |
+
// If it's just HTML content without proper JSON structure (legacy format)
|
246 |
+
document.getElementById("conversation").innerHTML = event.data;
|
247 |
+
}
|
248 |
+
|
249 |
+
// Auto-scroll to bottom
|
250 |
+
const container = document.getElementById("conversation");
|
251 |
+
container.scrollTop = container.scrollHeight;
|
252 |
+
} catch (e) {
|
253 |
+
// Fallback for non-JSON messages (legacy format)
|
254 |
+
document.getElementById("conversation").innerHTML = event.data;
|
255 |
+
|
256 |
+
// Auto-scroll to bottom
|
257 |
+
const container = document.getElementById("conversation");
|
258 |
+
container.scrollTop = container.scrollHeight;
|
259 |
+
}
|
260 |
+
};
|
261 |
+
|
262 |
+
wsConnection.onerror = (error) => {
|
263 |
+
console.error('WebSocket error:', error);
|
264 |
+
updateStatus('warning', 'WebSocket error');
|
265 |
+
};
|
266 |
+
|
267 |
+
wsConnection.onclose = () => {
|
268 |
+
console.log('WebSocket connection closed');
|
269 |
+
// Try to reconnect after a delay
|
270 |
+
setTimeout(setupWebSocket, 3000);
|
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 |
+
|
291 |
+
// Update status indicator
|
292 |
+
function updateStatus(status, message = '') {
|
293 |
+
const statusText = document.getElementById('status-text');
|
294 |
+
const statusIcon = document.getElementById('status-icon');
|
295 |
+
|
296 |
+
switch(status) {
|
297 |
+
case 'connected':
|
298 |
+
statusText.textContent = 'Connected';
|
299 |
+
statusIcon.style.backgroundColor = '#4CAF50';
|
300 |
+
break;
|
301 |
+
case 'connecting':
|
302 |
+
statusText.textContent = 'Connecting...';
|
303 |
+
statusIcon.style.backgroundColor = '#FFC107';
|
304 |
+
break;
|
305 |
+
case 'disconnected':
|
306 |
+
statusText.textContent = 'Disconnected';
|
307 |
+
statusIcon.style.backgroundColor = '#9E9E9E';
|
308 |
+
break;
|
309 |
+
case 'error':
|
310 |
+
statusText.textContent = 'Error: ' + message;
|
311 |
+
statusIcon.style.backgroundColor = '#F44336';
|
312 |
+
break;
|
313 |
+
case 'warning':
|
314 |
+
statusText.textContent = 'Warning: ' + message;
|
315 |
+
statusIcon.style.backgroundColor = '#FF9800';
|
316 |
+
break;
|
317 |
+
default:
|
318 |
+
statusText.textContent = 'Unknown';
|
319 |
+
statusIcon.style.backgroundColor = '#9E9E9E';
|
320 |
+
}
|
321 |
+
}
|
322 |
|
323 |
+
// Stop streaming and clean up
|
324 |
+
function stopStreaming() {
|
325 |
+
// Close WebRTC connection
|
326 |
+
if (rtcConnection) {
|
327 |
+
rtcConnection.close();
|
328 |
+
rtcConnection = null;
|
329 |
+
}
|
330 |
+
|
331 |
+
// Close WebSocket
|
332 |
+
if (wsConnection) {
|
333 |
+
wsConnection.close();
|
334 |
+
wsConnection = null;
|
335 |
+
}
|
336 |
+
|
337 |
+
// Stop all tracks in media stream
|
338 |
+
if (mediaStream) {
|
339 |
+
mediaStream.getTracks().forEach(track => track.stop());
|
340 |
+
mediaStream = null;
|
341 |
+
}
|
342 |
+
|
343 |
+
// Clear interval
|
344 |
+
if (statusUpdateInterval) {
|
345 |
+
clearInterval(statusUpdateInterval);
|
346 |
+
statusUpdateInterval = null;
|
347 |
+
}
|
348 |
+
|
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):
|
380 |
+
# Settings
|
381 |
+
gr.Markdown("## βοΈ Settings")
|
382 |
+
|
383 |
+
threshold_slider = gr.Slider(
|
384 |
+
minimum=0.3,
|
385 |
+
maximum=0.9,
|
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(
|
393 |
+
minimum=2,
|
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("""
|
404 |
+
## π Instructions
|
405 |
+
1. **Start Listening** - allows browser to access microphone
|
406 |
+
2. **Speak** - system will transcribe and identify speakers
|
407 |
+
3. **Stop** when finished
|
408 |
+
4. **Clear** to reset conversation
|
409 |
+
|
410 |
+
## π¨ Speaker Colors
|
411 |
+
- π΄ Speaker 1 (Red)
|
412 |
+
- π’ Speaker 2 (Teal)
|
413 |
+
- π΅ Speaker 3 (Blue)
|
414 |
+
- π‘ Speaker 4 (Green)
|
415 |
+
- β Speaker 5 (Yellow)
|
416 |
+
- π£ Speaker 6 (Plum)
|
417 |
+
- π€ Speaker 7 (Mint)
|
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():
|
481 |
+
"""API call to get system status - called periodically"""
|
482 |
+
import requests
|
483 |
+
try:
|
484 |
+
resp = requests.get(f"{HF_SPACE_URL}/status")
|
485 |
+
if resp.status_code == 200:
|
486 |
+
return resp.json().get('status', 'No status information')
|
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
|
496 |
|
497 |
+
# Create Gradio interface
|
498 |
+
demo = build_ui()
|
499 |
|
500 |
+
def mount_ui(app: FastAPI):
|
501 |
+
"""Mount Gradio app to FastAPI"""
|
502 |
+
app.mount("/ui", demo.app)
|
|
|
503 |
|
504 |
+
# For standalone testing
|
505 |
+
if __name__ == "__main__":
|
506 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|