Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,31 @@ import base64
|
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI, WebSocket
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
import os
|
| 7 |
from voice_agent import CrewVoiceAgent
|
| 8 |
|
| 9 |
-
load_dotenv()
|
| 10 |
-
agent = CrewVoiceAgent()
|
| 11 |
-
|
| 12 |
app = FastAPI()
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/")
|
| 15 |
async def home():
|
| 16 |
return HTMLResponse("""
|
| 17 |
<html>
|
| 18 |
<body>
|
| 19 |
-
<
|
| 20 |
<button onclick="start()">Start</button>
|
| 21 |
<button onclick="stop()">Stop</button>
|
| 22 |
<p>Status: <span id='status'>Idle</span></p>
|
| 23 |
-
|
| 24 |
<script>
|
| 25 |
let ws;
|
| 26 |
let recorder;
|
| 27 |
-
|
| 28 |
async function start() {
|
| 29 |
-
ws = new WebSocket("
|
|
|
|
| 30 |
ws.onmessage = (event) => {
|
| 31 |
const audio = new Audio("data:audio/wav;base64," + event.data);
|
| 32 |
audio.play();
|
|
@@ -36,12 +36,12 @@ async def home():
|
|
| 36 |
recorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
| 37 |
|
| 38 |
recorder.ondataavailable = async (e) => {
|
| 39 |
-
const
|
| 40 |
-
ws.send(
|
| 41 |
};
|
| 42 |
-
|
| 43 |
-
recorder.start(300);
|
| 44 |
-
document.getElementById("status").textContent = "Listening
|
| 45 |
}
|
| 46 |
|
| 47 |
function stop() {
|
|
@@ -54,15 +54,24 @@ async def home():
|
|
| 54 |
</html>
|
| 55 |
""")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.websocket("/ws")
|
| 58 |
async def websocket_endpoint(ws: WebSocket):
|
| 59 |
await ws.accept()
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
await ws.send_text(b64_audio)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI, WebSocket
|
| 4 |
from fastapi.responses import HTMLResponse
|
|
|
|
|
|
|
| 5 |
from voice_agent import CrewVoiceAgent
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
+
agent = CrewVoiceAgent()
|
| 9 |
|
| 10 |
+
# --------------------------
|
| 11 |
+
# FRONTEND
|
| 12 |
+
# --------------------------
|
| 13 |
@app.get("/")
|
| 14 |
async def home():
|
| 15 |
return HTMLResponse("""
|
| 16 |
<html>
|
| 17 |
<body>
|
| 18 |
+
<h1>🎤 Gemini CrewAI Voice Agent</h1>
|
| 19 |
<button onclick="start()">Start</button>
|
| 20 |
<button onclick="stop()">Stop</button>
|
| 21 |
<p>Status: <span id='status'>Idle</span></p>
|
| 22 |
+
|
| 23 |
<script>
|
| 24 |
let ws;
|
| 25 |
let recorder;
|
| 26 |
+
|
| 27 |
async function start() {
|
| 28 |
+
ws = new WebSocket("ws://" + location.host + "/ws"); // HF Spaces requires ws://
|
| 29 |
+
|
| 30 |
ws.onmessage = (event) => {
|
| 31 |
const audio = new Audio("data:audio/wav;base64," + event.data);
|
| 32 |
audio.play();
|
|
|
|
| 36 |
recorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
| 37 |
|
| 38 |
recorder.ondataavailable = async (e) => {
|
| 39 |
+
const arrayBuf = await e.data.arrayBuffer();
|
| 40 |
+
ws.send(arrayBuf);
|
| 41 |
};
|
| 42 |
+
|
| 43 |
+
recorder.start(300); // send audio chunks
|
| 44 |
+
document.getElementById("status").textContent = "Listening...";
|
| 45 |
}
|
| 46 |
|
| 47 |
function stop() {
|
|
|
|
| 54 |
</html>
|
| 55 |
""")
|
| 56 |
|
| 57 |
+
# --------------------------
|
| 58 |
+
# WEBSOCKET
|
| 59 |
+
# --------------------------
|
| 60 |
@app.websocket("/ws")
|
| 61 |
async def websocket_endpoint(ws: WebSocket):
|
| 62 |
await ws.accept()
|
| 63 |
|
| 64 |
+
while True:
|
| 65 |
+
try:
|
| 66 |
+
audio_bytes = await ws.receive_bytes() # Receive raw binary audio
|
| 67 |
+
audio_reply = await agent.handle_audio(audio_bytes)
|
| 68 |
+
|
| 69 |
+
b64_audio = base64.b64encode(audio_reply).decode()
|
| 70 |
+
await ws.send_text(b64_audio)
|
| 71 |
+
|
| 72 |
+
except Exception:
|
| 73 |
+
break
|
| 74 |
|
|
|
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|