Spaces:
Sleeping
Sleeping
Commit
·
375c386
1
Parent(s):
97a4ae5
Code splitting
Browse files
ui.py
CHANGED
@@ -13,11 +13,51 @@ def build_ui():
|
|
13 |
|
14 |
with gr.Row():
|
15 |
with gr.Column(scale=2):
|
16 |
-
# Conversation display
|
17 |
output = gr.HTML(
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
|
23 |
# Control buttons
|
@@ -71,96 +111,6 @@ def build_ui():
|
|
71 |
- 🟡 Speaker 4 (Green)
|
72 |
""")
|
73 |
|
74 |
-
# JavaScript for browser audio capture and WebRTC
|
75 |
-
js = f"""
|
76 |
-
<script>
|
77 |
-
let ws, recorder, mediaStream;
|
78 |
-
|
79 |
-
async function startStream() {{
|
80 |
-
try {{
|
81 |
-
// Get user media
|
82 |
-
mediaStream = await navigator.mediaDevices.getUserMedia({{audio: true}});
|
83 |
-
|
84 |
-
// Connect to WebSocket
|
85 |
-
ws = new WebSocket('{API_WS}');
|
86 |
-
|
87 |
-
ws.onopen = () => {{
|
88 |
-
console.log('WebSocket connected');
|
89 |
-
document.getElementById('status').textContent = 'Connected to server';
|
90 |
-
|
91 |
-
// Create MediaRecorder
|
92 |
-
recorder = new MediaRecorder(mediaStream, {{mimeType: 'audio/webm'}});
|
93 |
-
|
94 |
-
// Send audio chunks to WebSocket
|
95 |
-
recorder.ondataavailable = (e) => {{
|
96 |
-
if (ws && ws.readyState === 1 && e.data.size > 0) {{
|
97 |
-
ws.send(e.data);
|
98 |
-
}}
|
99 |
-
}};
|
100 |
-
|
101 |
-
// Start recording
|
102 |
-
recorder.start(200); // Send chunks every 200ms
|
103 |
-
}};
|
104 |
-
|
105 |
-
ws.onmessage = (evt) => {{
|
106 |
-
// Update the HTML output with conversation
|
107 |
-
document.querySelector('.output').innerHTML = evt.data;
|
108 |
-
}};
|
109 |
-
|
110 |
-
ws.onerror = (error) => {{
|
111 |
-
console.error('WebSocket error:', error);
|
112 |
-
document.getElementById('status').textContent = 'WebSocket error';
|
113 |
-
}};
|
114 |
-
|
115 |
-
ws.onclose = () => {{
|
116 |
-
console.log('WebSocket closed');
|
117 |
-
document.getElementById('status').textContent = 'Disconnected';
|
118 |
-
stopStream();
|
119 |
-
}};
|
120 |
-
|
121 |
-
}} catch (error) {{
|
122 |
-
console.error('Error starting stream:', error);
|
123 |
-
document.getElementById('status').textContent = `Error: ${{error.message}}`;
|
124 |
-
}}
|
125 |
-
}}
|
126 |
-
|
127 |
-
function stopStream() {{
|
128 |
-
// Stop the MediaRecorder
|
129 |
-
if (recorder && recorder.state !== 'inactive') {{
|
130 |
-
recorder.stop();
|
131 |
-
}}
|
132 |
-
|
133 |
-
// Stop all audio tracks
|
134 |
-
if (mediaStream) {{
|
135 |
-
mediaStream.getTracks().forEach(track => track.stop());
|
136 |
-
}}
|
137 |
-
|
138 |
-
// Close the WebSocket
|
139 |
-
if (ws) {{
|
140 |
-
ws.close();
|
141 |
-
}}
|
142 |
-
|
143 |
-
document.getElementById('status').textContent = 'Stopped';
|
144 |
-
}}
|
145 |
-
|
146 |
-
// Attach event listeners to buttons
|
147 |
-
document.addEventListener('DOMContentLoaded', function() {{
|
148 |
-
// Add hidden status element
|
149 |
-
const statusElem = document.createElement('div');
|
150 |
-
statusElem.id = 'status';
|
151 |
-
statusElem.style.display = 'none';
|
152 |
-
document.body.appendChild(statusElem);
|
153 |
-
}});
|
154 |
-
|
155 |
-
// Connect buttons to functions
|
156 |
-
document.querySelector('button[aria-label="Start Listening"]').onclick = startStream;
|
157 |
-
document.querySelector('button[aria-label="Stop"]').onclick = stopStream;
|
158 |
-
</script>
|
159 |
-
"""
|
160 |
-
|
161 |
-
# Inject JavaScript
|
162 |
-
demo.load(js, outputs=[output, status_output])
|
163 |
-
|
164 |
return demo
|
165 |
|
166 |
# Create Gradio interface
|
|
|
13 |
|
14 |
with gr.Row():
|
15 |
with gr.Column(scale=2):
|
16 |
+
# Conversation display with embedded JavaScript
|
17 |
output = gr.HTML(
|
18 |
+
"""
|
19 |
+
<div class='output' style='padding:20px; background:#f8f9fa; border-radius:10px; min-height:300px;'>
|
20 |
+
<i>Click 'Start Listening' to begin…</i>
|
21 |
+
</div>
|
22 |
+
<script>
|
23 |
+
const API_WS = 'wss://your-space.hf.space/ws_inference';
|
24 |
+
let ws, recorder, mediaStream;
|
25 |
+
|
26 |
+
async function startStream() {
|
27 |
+
try {
|
28 |
+
mediaStream = await navigator.mediaDevices.getUserMedia({audio:true});
|
29 |
+
ws = new WebSocket(API_WS);
|
30 |
+
ws.onopen = () => {
|
31 |
+
recorder = new MediaRecorder(mediaStream, {mimeType:'audio/webm'});
|
32 |
+
recorder.ondataavailable = e => {
|
33 |
+
if (ws.readyState===1 && e.data.size>0) ws.send(e.data);
|
34 |
+
};
|
35 |
+
recorder.start(200);
|
36 |
+
};
|
37 |
+
ws.onmessage = evt => {
|
38 |
+
document.querySelector('.output').innerHTML = evt.data;
|
39 |
+
};
|
40 |
+
ws.onerror = err => console.error('WebSocket error:', err);
|
41 |
+
ws.onclose = () => stopStream();
|
42 |
+
} catch (err) {
|
43 |
+
console.error('Error starting stream:', err);
|
44 |
+
alert(`Error: ${err.message}`);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
|
48 |
+
function stopStream() {
|
49 |
+
recorder?.state!=='inactive' && recorder.stop();
|
50 |
+
mediaStream?.getTracks().forEach(t=>t.stop());
|
51 |
+
ws?.close();
|
52 |
+
}
|
53 |
+
|
54 |
+
document.addEventListener('DOMContentLoaded', () => {
|
55 |
+
document.querySelector('button[aria-label="Start Listening"]').onclick = startStream;
|
56 |
+
document.querySelector('button[aria-label="Stop"]').onclick = stopStream;
|
57 |
+
});
|
58 |
+
</script>
|
59 |
+
""",
|
60 |
+
label="Live Conversation"
|
61 |
)
|
62 |
|
63 |
# Control buttons
|
|
|
111 |
- 🟡 Speaker 4 (Green)
|
112 |
""")
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
return demo
|
115 |
|
116 |
# Create Gradio interface
|