Spaces:
Sleeping
Sleeping
Nirav Madhani
commited on
Commit
·
9b731f8
1
Parent(s):
73922ff
Disconnect logic and UI fix
Browse files- handler.py +18 -17
- index.html +1 -1
- webapp.py +3 -0
handler.py
CHANGED
|
@@ -96,25 +96,26 @@ class AudioLoop:
|
|
| 96 |
self.ws = ws
|
| 97 |
await self.startup(tools)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
except asyncio.CancelledError:
|
| 105 |
-
print("[AudioLoop]
|
| 106 |
-
# Cancel our tasks if they're still running
|
| 107 |
-
if 'send_task' in locals():
|
| 108 |
-
send_task.cancel()
|
| 109 |
-
if 'receive_task' in locals():
|
| 110 |
-
receive_task.cancel()
|
| 111 |
-
# Close Gemini connection if we have one
|
| 112 |
-
if self.ws and not self.ws.closed:
|
| 113 |
-
try:
|
| 114 |
-
await self.ws.close()
|
| 115 |
-
except Exception as e:
|
| 116 |
-
print(f"[AudioLoop] Error closing Gemini connection: {e}")
|
| 117 |
-
print("[AudioLoop] Cleanup complete")
|
| 118 |
except Exception as e:
|
| 119 |
traceback.print_exc()
|
| 120 |
raise
|
|
|
|
| 96 |
self.ws = ws
|
| 97 |
await self.startup(tools)
|
| 98 |
|
| 99 |
+
try:
|
| 100 |
+
async with asyncio.TaskGroup() as tg:
|
| 101 |
+
send_task = tg.create_task(self.send_realtime())
|
| 102 |
+
receive_task = tg.create_task(self.receive_audio())
|
| 103 |
+
await asyncio.Future() # Keep running until canceled
|
| 104 |
+
finally:
|
| 105 |
+
# Clean up tasks and connection
|
| 106 |
+
if 'send_task' in locals():
|
| 107 |
+
send_task.cancel()
|
| 108 |
+
if 'receive_task' in locals():
|
| 109 |
+
receive_task.cancel()
|
| 110 |
+
try:
|
| 111 |
+
await self.ws.close()
|
| 112 |
+
print("[AudioLoop] Closed WebSocket connection")
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"[AudioLoop] Error closing Gemini connection: {e}")
|
| 115 |
+
print("[AudioLoop] Cleanup complete")
|
| 116 |
|
| 117 |
except asyncio.CancelledError:
|
| 118 |
+
print("[AudioLoop] Cancelled")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
except Exception as e:
|
| 120 |
traceback.print_exc()
|
| 121 |
raise
|
index.html
CHANGED
|
@@ -117,7 +117,7 @@
|
|
| 117 |
<button id="connectButton" onclick="toggleConnection()">
|
| 118 |
<span style="margin-right: 0.3em;">🔌</span> Connect to Server
|
| 119 |
</button>
|
| 120 |
-
<button id="voiceStartButton" class="voice-start" onclick="startCapture()">
|
| 121 |
<span style="margin-right: 0.3em;">🎤</span> Start Voice Chat
|
| 122 |
</button>
|
| 123 |
<button id="voiceStopButton" class="voice-stop" onclick="stopCapture()" style="display: none;">
|
|
|
|
| 117 |
<button id="connectButton" onclick="toggleConnection()">
|
| 118 |
<span style="margin-right: 0.3em;">🔌</span> Connect to Server
|
| 119 |
</button>
|
| 120 |
+
<button id="voiceStartButton" class="voice-start" onclick="startCapture()" style="display: none;">
|
| 121 |
<span style="margin-right: 0.3em;">🎤</span> Start Voice Chat
|
| 122 |
</button>
|
| 123 |
<button id="voiceStopButton" class="voice-stop" onclick="stopCapture()" style="display: none;">
|
webapp.py
CHANGED
|
@@ -102,6 +102,8 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 102 |
|
| 103 |
except WebSocketDisconnect:
|
| 104 |
print("[from_client_to_gemini] Client disconnected.")
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
print("[from_client_to_gemini] Error:", e)
|
| 107 |
|
|
@@ -121,6 +123,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 121 |
|
| 122 |
except WebSocketDisconnect:
|
| 123 |
print("[from_gemini_to_client] Client disconnected.")
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
print("[from_gemini_to_client] Error:", e)
|
| 126 |
|
|
|
|
| 102 |
|
| 103 |
except WebSocketDisconnect:
|
| 104 |
print("[from_client_to_gemini] Client disconnected.")
|
| 105 |
+
#del audio_loop
|
| 106 |
+
loop_task.cancel()
|
| 107 |
except Exception as e:
|
| 108 |
print("[from_client_to_gemini] Error:", e)
|
| 109 |
|
|
|
|
| 123 |
|
| 124 |
except WebSocketDisconnect:
|
| 125 |
print("[from_gemini_to_client] Client disconnected.")
|
| 126 |
+
audio_loop.stop()
|
| 127 |
except Exception as e:
|
| 128 |
print("[from_gemini_to_client] Error:", e)
|
| 129 |
|