Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,6 @@ from voice_agent import CrewVoiceAgent
|
|
| 7 |
app = FastAPI()
|
| 8 |
agent = CrewVoiceAgent()
|
| 9 |
|
| 10 |
-
# --------------------------
|
| 11 |
-
# FRONTEND
|
| 12 |
-
# --------------------------
|
| 13 |
@app.get("/")
|
| 14 |
async def home():
|
| 15 |
return HTMLResponse("""
|
|
@@ -22,10 +19,10 @@ async def home():
|
|
| 22 |
|
| 23 |
<script>
|
| 24 |
let ws;
|
| 25 |
-
let
|
| 26 |
|
| 27 |
async function start() {
|
| 28 |
-
ws = new WebSocket("ws://" + location.host + "/ws");
|
| 29 |
|
| 30 |
ws.onmessage = (event) => {
|
| 31 |
const audio = new Audio("data:audio/wav;base64," + event.data);
|
|
@@ -33,45 +30,42 @@ async def home():
|
|
| 33 |
};
|
| 34 |
|
| 35 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
ws.send(arrayBuf);
|
| 41 |
};
|
| 42 |
|
| 43 |
-
|
| 44 |
-
document.getElementById("status").
|
| 45 |
}
|
| 46 |
|
| 47 |
function stop() {
|
| 48 |
-
|
| 49 |
ws.close();
|
| 50 |
-
document.getElementById("status").
|
| 51 |
}
|
| 52 |
</script>
|
| 53 |
</body>
|
| 54 |
</html>
|
| 55 |
""")
|
| 56 |
|
| 57 |
-
# --------------------------
|
| 58 |
-
# WEBSOCKET
|
| 59 |
-
# --------------------------
|
| 60 |
@app.websocket("/ws")
|
| 61 |
-
async def
|
| 62 |
await ws.accept()
|
| 63 |
-
|
| 64 |
while True:
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
await ws.send_text(
|
| 71 |
|
| 72 |
except Exception:
|
| 73 |
break
|
| 74 |
|
| 75 |
-
|
| 76 |
if __name__ == "__main__":
|
| 77 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
agent = CrewVoiceAgent()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.get("/")
|
| 11 |
async def home():
|
| 12 |
return HTMLResponse("""
|
|
|
|
| 19 |
|
| 20 |
<script>
|
| 21 |
let ws;
|
| 22 |
+
let rec;
|
| 23 |
|
| 24 |
async function start() {
|
| 25 |
+
ws = new WebSocket("ws://" + location.host + "/ws"); // HF requires ws://
|
| 26 |
|
| 27 |
ws.onmessage = (event) => {
|
| 28 |
const audio = new Audio("data:audio/wav;base64," + event.data);
|
|
|
|
| 30 |
};
|
| 31 |
|
| 32 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 33 |
+
rec = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
| 34 |
|
| 35 |
+
rec.ondataavailable = async (e) => {
|
| 36 |
+
ws.send(await e.data.arrayBuffer());
|
|
|
|
| 37 |
};
|
| 38 |
|
| 39 |
+
rec.start(500);
|
| 40 |
+
document.getElementById("status").innerText = "Listening…";
|
| 41 |
}
|
| 42 |
|
| 43 |
function stop() {
|
| 44 |
+
rec.stop();
|
| 45 |
ws.close();
|
| 46 |
+
document.getElementById("status").innerText = "Stopped";
|
| 47 |
}
|
| 48 |
</script>
|
| 49 |
</body>
|
| 50 |
</html>
|
| 51 |
""")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
@app.websocket("/ws")
|
| 54 |
+
async def websocket(ws: WebSocket):
|
| 55 |
await ws.accept()
|
|
|
|
| 56 |
while True:
|
| 57 |
try:
|
| 58 |
+
data = await ws.receive_bytes()
|
| 59 |
+
audio = await agent.handle_audio(data)
|
| 60 |
+
|
| 61 |
+
if not audio:
|
| 62 |
+
continue
|
| 63 |
|
| 64 |
+
b64 = base64.b64encode(audio).decode()
|
| 65 |
+
await ws.send_text(b64)
|
| 66 |
|
| 67 |
except Exception:
|
| 68 |
break
|
| 69 |
|
|
|
|
| 70 |
if __name__ == "__main__":
|
| 71 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|