Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,60 +11,91 @@ agent = CrewVoiceAgent()
|
|
| 11 |
async def home():
|
| 12 |
return HTMLResponse("""
|
| 13 |
<html>
|
|
|
|
|
|
|
|
|
|
| 14 |
<body>
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
</body>
|
| 50 |
</html>
|
| 51 |
""")
|
| 52 |
|
| 53 |
@app.websocket("/ws")
|
| 54 |
-
async def
|
| 55 |
await ws.accept()
|
|
|
|
| 56 |
while True:
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
audio = await agent.handle_audio(data)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
continue
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
except Exception:
|
|
|
|
| 68 |
break
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 11 |
async def home():
|
| 12 |
return HTMLResponse("""
|
| 13 |
<html>
|
| 14 |
+
<head>
|
| 15 |
+
<title>Gemini CrewAI Voice Agent</title>
|
| 16 |
+
</head>
|
| 17 |
<body>
|
| 18 |
+
<h1>🎤 Gemini CrewAI Voice Agent</h1>
|
| 19 |
+
|
| 20 |
+
<button onclick="start()">Start</button>
|
| 21 |
+
<button onclick="stop()">Stop</button>
|
| 22 |
+
|
| 23 |
+
<p>Status: <span id='status'>Idle</span></p>
|
| 24 |
+
|
| 25 |
+
<script>
|
| 26 |
+
let ws;
|
| 27 |
+
let rec;
|
| 28 |
+
|
| 29 |
+
async function start() {
|
| 30 |
+
|
| 31 |
+
// HuggingFace Spaces REQUIRE wss:// for embedded iframe
|
| 32 |
+
const wsUrl = "wss://" + window.location.host + "/ws";
|
| 33 |
+
console.log("Connecting to:", wsUrl);
|
| 34 |
+
|
| 35 |
+
ws = new WebSocket(wsUrl);
|
| 36 |
+
|
| 37 |
+
ws.onopen = () => {
|
| 38 |
+
console.log("WebSocket connected!");
|
| 39 |
+
document.getElementById("status").innerText = "Connected. Listening…";
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
ws.onerror = (e) => {
|
| 43 |
+
console.error("WebSocket error:", e);
|
| 44 |
+
document.getElementById("status").innerText = "WebSocket Error!";
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
ws.onclose = () => {
|
| 48 |
+
console.log("WebSocket closed.");
|
| 49 |
+
document.getElementById("status").innerText = "Disconnected";
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
ws.onmessage = (event) => {
|
| 53 |
+
console.log("Received audio reply from backend.");
|
| 54 |
+
|
| 55 |
+
const audio = new Audio("data:audio/wav;base64," + event.data);
|
| 56 |
+
audio.play();
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
// Microphone access
|
| 60 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 61 |
+
|
| 62 |
+
rec = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
| 63 |
+
|
| 64 |
+
rec.ondataavailable = async (e) => {
|
| 65 |
+
ws.send(await e.data.arrayBuffer()); // send audio to backend
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
rec.start(300); // send audio every 300ms
|
| 69 |
+
document.getElementById("status").innerText = "Listening…";
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
function stop() {
|
| 73 |
+
if (rec) rec.stop();
|
| 74 |
+
if (ws) ws.close();
|
| 75 |
+
document.getElementById("status").innerText = "Stopped";
|
| 76 |
+
}
|
| 77 |
+
</script>
|
| 78 |
+
|
| 79 |
</body>
|
| 80 |
</html>
|
| 81 |
""")
|
| 82 |
|
| 83 |
@app.websocket("/ws")
|
| 84 |
+
async def websocket_endpoint(ws: WebSocket):
|
| 85 |
await ws.accept()
|
| 86 |
+
|
| 87 |
while True:
|
| 88 |
try:
|
| 89 |
+
audio_bytes = await ws.receive_bytes()
|
|
|
|
| 90 |
|
| 91 |
+
reply_audio = await agent.handle_audio(audio_bytes)
|
|
|
|
| 92 |
|
| 93 |
+
if reply_audio:
|
| 94 |
+
b64 = base64.b64encode(reply_audio).decode()
|
| 95 |
+
await ws.send_text(b64)
|
| 96 |
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print("WebSocket error:", e)
|
| 99 |
break
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|