Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -59,15 +59,25 @@ class GeminiHandler(StreamHandler):
|
|
59 |
)
|
60 |
|
61 |
def _initialize_websocket(self):
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
}
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
73 |
try:
|
@@ -102,15 +112,23 @@ class GeminiHandler(StreamHandler):
|
|
102 |
def generator(self):
|
103 |
while True:
|
104 |
if not self.ws:
|
|
|
105 |
yield None
|
106 |
continue
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
def emit(self) -> tuple[int, np.ndarray] | None:
|
116 |
if not self.ws:
|
@@ -132,6 +150,15 @@ class GeminiHandler(StreamHandler):
|
|
132 |
if self.ws:
|
133 |
self.ws.close()
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
class GeminiVoiceChat:
|
136 |
def __init__(self):
|
137 |
self.demo = self._create_interface()
|
@@ -145,6 +172,8 @@ class GeminiVoiceChat:
|
|
145 |
</div>
|
146 |
""")
|
147 |
|
|
|
|
|
148 |
webrtc = WebRTC(
|
149 |
label="Conversation",
|
150 |
modality="audio",
|
@@ -154,14 +183,18 @@ class GeminiVoiceChat:
|
|
154 |
webrtc.stream(
|
155 |
GeminiHandler(),
|
156 |
inputs=[webrtc],
|
157 |
-
outputs=[webrtc],
|
158 |
time_limit=90,
|
159 |
concurrency_limit=10
|
160 |
)
|
161 |
return demo
|
162 |
|
163 |
def launch(self):
|
164 |
-
self.demo.launch(
|
|
|
|
|
|
|
|
|
165 |
|
166 |
if __name__ == "__main__":
|
167 |
app = GeminiVoiceChat()
|
|
|
59 |
)
|
60 |
|
61 |
def _initialize_websocket(self):
|
62 |
+
try:
|
63 |
+
self.ws = websockets.sync.client.connect(
|
64 |
+
self.config.ws_url,
|
65 |
+
timeout=30
|
66 |
+
)
|
67 |
+
initial_request = {
|
68 |
+
'setup': {
|
69 |
+
'model': self.config.model,
|
70 |
+
}
|
71 |
}
|
72 |
+
self.ws.send(json.dumps(initial_request))
|
73 |
+
setup_response = json.loads(self.ws.recv())
|
74 |
+
print(f"Setup response: {setup_response}")
|
75 |
+
except websockets.exceptions.WebSocketException as e:
|
76 |
+
print(f"WebSocket connection failed: {str(e)}")
|
77 |
+
self.ws = None
|
78 |
+
except Exception as e:
|
79 |
+
print(f"Setup failed: {str(e)}")
|
80 |
+
self.ws = None
|
81 |
|
82 |
def receive(self, frame: tuple[int, np.ndarray]) -> None:
|
83 |
try:
|
|
|
112 |
def generator(self):
|
113 |
while True:
|
114 |
if not self.ws:
|
115 |
+
print("WebSocket not connected")
|
116 |
yield None
|
117 |
continue
|
118 |
|
119 |
+
try:
|
120 |
+
message = self.ws.recv(timeout=5)
|
121 |
+
msg = json.loads(message)
|
122 |
+
|
123 |
+
if 'serverContent' in msg:
|
124 |
+
content = msg['serverContent'].get('modelTurn', {})
|
125 |
+
yield from self._process_server_content(content)
|
126 |
+
except websockets.exceptions.TimeoutError:
|
127 |
+
print("Timeout waiting for server response")
|
128 |
+
yield None
|
129 |
+
except Exception as e:
|
130 |
+
print(f"Error in generator: {str(e)}")
|
131 |
+
yield None
|
132 |
|
133 |
def emit(self) -> tuple[int, np.ndarray] | None:
|
134 |
if not self.ws:
|
|
|
150 |
if self.ws:
|
151 |
self.ws.close()
|
152 |
|
153 |
+
def check_connection(self):
|
154 |
+
try:
|
155 |
+
if not self.ws or self.ws.closed:
|
156 |
+
self._initialize_websocket()
|
157 |
+
return True
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Connection check failed: {str(e)}")
|
160 |
+
return False
|
161 |
+
|
162 |
class GeminiVoiceChat:
|
163 |
def __init__(self):
|
164 |
self.demo = self._create_interface()
|
|
|
172 |
</div>
|
173 |
""")
|
174 |
|
175 |
+
error_message = gr.Textbox(label="Status", interactive=False)
|
176 |
+
|
177 |
webrtc = WebRTC(
|
178 |
label="Conversation",
|
179 |
modality="audio",
|
|
|
183 |
webrtc.stream(
|
184 |
GeminiHandler(),
|
185 |
inputs=[webrtc],
|
186 |
+
outputs=[webrtc, error_message],
|
187 |
time_limit=90,
|
188 |
concurrency_limit=10
|
189 |
)
|
190 |
return demo
|
191 |
|
192 |
def launch(self):
|
193 |
+
self.demo.launch(
|
194 |
+
server_name="0.0.0.0",
|
195 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
196 |
+
share=True # Only if you want to create a public URL
|
197 |
+
)
|
198 |
|
199 |
if __name__ == "__main__":
|
200 |
app = GeminiVoiceChat()
|