Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- app.py +11 -12
- index.html +43 -1
app.py
CHANGED
|
@@ -38,30 +38,29 @@ def response(
|
|
| 38 |
):
|
| 39 |
gradio_chatbot = gradio_chatbot or []
|
| 40 |
conversation_state = conversation_state or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
sample_rate, array = audio
|
| 44 |
-
gradio_chatbot.append(
|
| 45 |
-
{"role": "user", "content": gr.Audio((sample_rate, array.squeeze()))}
|
| 46 |
-
)
|
| 47 |
-
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 48 |
-
|
| 49 |
-
conversation_state.append({"role": "user", "content": text})
|
| 50 |
|
| 51 |
-
try:
|
| 52 |
request = client.chat.completions.create(
|
| 53 |
model="Meta-Llama-3.2-3B-Instruct",
|
| 54 |
-
messages=conversation_state,
|
| 55 |
temperature=0.1,
|
| 56 |
top_p=0.1,
|
| 57 |
)
|
| 58 |
response = {"role": "assistant", "content": request.choices[0].message.content}
|
| 59 |
|
| 60 |
-
except Exception:
|
| 61 |
import traceback
|
| 62 |
|
| 63 |
traceback.print_exc()
|
| 64 |
-
raise WebRTCError(traceback.format_exc())
|
| 65 |
|
| 66 |
conversation_state.append(response)
|
| 67 |
gradio_chatbot.append(response)
|
|
|
|
| 38 |
):
|
| 39 |
gradio_chatbot = gradio_chatbot or []
|
| 40 |
conversation_state = conversation_state or []
|
| 41 |
+
try:
|
| 42 |
+
text = stt(audio)
|
| 43 |
+
sample_rate, array = audio
|
| 44 |
+
gradio_chatbot.append(
|
| 45 |
+
{"role": "user", "content": gr.Audio((sample_rate, array.squeeze()))}
|
| 46 |
+
)
|
| 47 |
+
yield AdditionalOutputs(gradio_chatbot, conversation_state)
|
| 48 |
|
| 49 |
+
conversation_state.append({"role": "user", "content": text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
| 51 |
request = client.chat.completions.create(
|
| 52 |
model="Meta-Llama-3.2-3B-Instruct",
|
| 53 |
+
messages=conversation_state, # type: ignore
|
| 54 |
temperature=0.1,
|
| 55 |
top_p=0.1,
|
| 56 |
)
|
| 57 |
response = {"role": "assistant", "content": request.choices[0].message.content}
|
| 58 |
|
| 59 |
+
except Exception as e:
|
| 60 |
import traceback
|
| 61 |
|
| 62 |
traceback.print_exc()
|
| 63 |
+
raise WebRTCError(str(e) + "\n" + traceback.format_exc())
|
| 64 |
|
| 65 |
conversation_state.append(response)
|
| 66 |
gradio_chatbot.append(response)
|
index.html
CHANGED
|
@@ -176,10 +176,28 @@
|
|
| 176 |
transform: scale(1.2);
|
| 177 |
}
|
| 178 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
</style>
|
| 180 |
</head>
|
| 181 |
|
| 182 |
<body>
|
|
|
|
|
|
|
| 183 |
<div class="container">
|
| 184 |
<div class="logo">
|
| 185 |
<h1>Hello Computer 💻</h1>
|
|
@@ -257,11 +275,24 @@
|
|
| 257 |
updateAudioLevel();
|
| 258 |
}
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
function handleMessage(event) {
|
| 261 |
const eventJson = JSON.parse(event.data);
|
| 262 |
const typingIndicator = document.getElementById('typing-indicator');
|
| 263 |
|
| 264 |
-
if (eventJson.type === "
|
|
|
|
|
|
|
| 265 |
fetch('/input_hook', {
|
| 266 |
method: 'POST',
|
| 267 |
headers: {
|
|
@@ -336,6 +367,15 @@
|
|
| 336 |
});
|
| 337 |
|
| 338 |
const serverResponse = await response.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
await peerConnection.setRemoteDescription(serverResponse);
|
| 340 |
|
| 341 |
eventSource = new EventSource('/outputs?webrtc_id=' + webrtc_id);
|
|
@@ -347,6 +387,8 @@
|
|
| 347 |
});
|
| 348 |
} catch (err) {
|
| 349 |
console.error('Error setting up WebRTC:', err);
|
|
|
|
|
|
|
| 350 |
}
|
| 351 |
}
|
| 352 |
|
|
|
|
| 176 |
transform: scale(1.2);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
+
|
| 180 |
+
/* Add styles for toast notifications */
|
| 181 |
+
.toast {
|
| 182 |
+
position: fixed;
|
| 183 |
+
top: 20px;
|
| 184 |
+
left: 50%;
|
| 185 |
+
transform: translateX(-50%);
|
| 186 |
+
background-color: #f44336;
|
| 187 |
+
color: white;
|
| 188 |
+
padding: 16px 24px;
|
| 189 |
+
border-radius: 4px;
|
| 190 |
+
font-size: 14px;
|
| 191 |
+
z-index: 1000;
|
| 192 |
+
display: none;
|
| 193 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
|
| 194 |
+
}
|
| 195 |
</style>
|
| 196 |
</head>
|
| 197 |
|
| 198 |
<body>
|
| 199 |
+
<!-- Add toast element after body opening tag -->
|
| 200 |
+
<div id="error-toast" class="toast"></div>
|
| 201 |
<div class="container">
|
| 202 |
<div class="logo">
|
| 203 |
<h1>Hello Computer 💻</h1>
|
|
|
|
| 275 |
updateAudioLevel();
|
| 276 |
}
|
| 277 |
|
| 278 |
+
function showError(message) {
|
| 279 |
+
const toast = document.getElementById('error-toast');
|
| 280 |
+
toast.textContent = message;
|
| 281 |
+
toast.style.display = 'block';
|
| 282 |
+
|
| 283 |
+
// Hide toast after 5 seconds
|
| 284 |
+
setTimeout(() => {
|
| 285 |
+
toast.style.display = 'none';
|
| 286 |
+
}, 5000);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
function handleMessage(event) {
|
| 290 |
const eventJson = JSON.parse(event.data);
|
| 291 |
const typingIndicator = document.getElementById('typing-indicator');
|
| 292 |
|
| 293 |
+
if (eventJson.type === "error") {
|
| 294 |
+
showError(eventJson.message);
|
| 295 |
+
} else if (eventJson.type === "send_input") {
|
| 296 |
fetch('/input_hook', {
|
| 297 |
method: 'POST',
|
| 298 |
headers: {
|
|
|
|
| 367 |
});
|
| 368 |
|
| 369 |
const serverResponse = await response.json();
|
| 370 |
+
|
| 371 |
+
if (serverResponse.status === 'failed') {
|
| 372 |
+
showError(serverResponse.meta.error === 'concurrency_limit_reached'
|
| 373 |
+
? `Too many connections. Maximum limit is ${serverResponse.meta.limit}`
|
| 374 |
+
: serverResponse.meta.error);
|
| 375 |
+
stop();
|
| 376 |
+
return;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
await peerConnection.setRemoteDescription(serverResponse);
|
| 380 |
|
| 381 |
eventSource = new EventSource('/outputs?webrtc_id=' + webrtc_id);
|
|
|
|
| 387 |
});
|
| 388 |
} catch (err) {
|
| 389 |
console.error('Error setting up WebRTC:', err);
|
| 390 |
+
showError('Failed to establish connection. Please try again.');
|
| 391 |
+
stop();
|
| 392 |
}
|
| 393 |
}
|
| 394 |
|