sanjaystarc commited on
Commit
a90d301
·
verified ·
1 Parent(s): 3b5e44c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -42
app.py CHANGED
@@ -11,60 +11,91 @@ agent = CrewVoiceAgent()
11
  async def home():
12
  return HTMLResponse("""
13
  <html>
 
 
 
14
  <body>
15
- <h1>🎤 Gemini CrewAI Voice Agent</h1>
16
- <button onclick="start()">Start</button>
17
- <button onclick="stop()">Stop</button>
18
- <p>Status: <span id='status'>Idle</span></p>
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);
29
- audio.play();
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__":
 
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__":