app.py improved run performance still some bugs
Browse files
app.py
CHANGED
@@ -31,7 +31,6 @@ gemini_connected = False
|
|
31 |
|
32 |
# --- Helper Functions ---
|
33 |
def encode_audio(data: np.ndarray) -> dict:
|
34 |
-
"""Encode Audio data."""
|
35 |
if not isinstance(data, np.ndarray):
|
36 |
raise TypeError("encode_audio expected a numpy.ndarray")
|
37 |
try:
|
@@ -41,7 +40,6 @@ def encode_audio(data: np.ndarray) -> dict:
|
|
41 |
raise
|
42 |
|
43 |
def encode_image(data: np.ndarray) -> dict:
|
44 |
-
"""Encode Image data."""
|
45 |
if not isinstance(data, np.ndarray):
|
46 |
raise TypeError("encode_image expected a numpy.ndarray")
|
47 |
try:
|
@@ -140,9 +138,9 @@ class GeminiHandler(AsyncAudioVideoStreamHandler):
|
|
140 |
try:
|
141 |
client = genai.Client(api_key=api_key, http_options={"api_version": "v1alpha"})
|
142 |
config = {"response_modalities": ["AUDIO"]}
|
143 |
-
async with client.aio.live.connect(
|
144 |
model="gemini-2.0-flash-exp", config=config
|
145 |
-
) as session:
|
146 |
self.session = session
|
147 |
gemini_connected = True
|
148 |
asyncio.create_task(self.receive_audio())
|
@@ -163,19 +161,22 @@ class GeminiHandler(AsyncAudioVideoStreamHandler):
|
|
163 |
|
164 |
while not self.quit.is_set():
|
165 |
try:
|
166 |
-
|
|
|
|
|
|
|
167 |
async for response in turn:
|
|
|
|
|
168 |
if data := response.data:
|
169 |
yield data
|
170 |
except Exception as e:
|
171 |
logger.error(f"Error receiving from Gemini: {e}")
|
172 |
-
#
|
173 |
-
|
174 |
-
|
175 |
-
# break
|
176 |
async def receive_audio(self):
|
177 |
try:
|
178 |
-
# Correctly use the async generator
|
179 |
async for audio_response in async_aggregate_bytes_to_16bit(self.generator()):
|
180 |
self.audio_queue.put_nowait(audio_response)
|
181 |
except Exception as e:
|
@@ -212,12 +213,16 @@ class GeminiHandler(AsyncAudioVideoStreamHandler):
|
|
212 |
global gemini_connected
|
213 |
gemini_connected = False
|
214 |
logger.info("Shutting down GeminiHandler.")
|
215 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
self.connection = None
|
217 |
self.args_set.clear()
|
218 |
-
|
219 |
-
# No good async close.
|
220 |
-
pass
|
221 |
self.quit.clear()
|
222 |
update_gemini_status_sync()
|
223 |
|
|
|
31 |
|
32 |
# --- Helper Functions ---
|
33 |
def encode_audio(data: np.ndarray) -> dict:
|
|
|
34 |
if not isinstance(data, np.ndarray):
|
35 |
raise TypeError("encode_audio expected a numpy.ndarray")
|
36 |
try:
|
|
|
40 |
raise
|
41 |
|
42 |
def encode_image(data: np.ndarray) -> dict:
|
|
|
43 |
if not isinstance(data, np.ndarray):
|
44 |
raise TypeError("encode_image expected a numpy.ndarray")
|
45 |
try:
|
|
|
138 |
try:
|
139 |
client = genai.Client(api_key=api_key, http_options={"api_version": "v1alpha"})
|
140 |
config = {"response_modalities": ["AUDIO"]}
|
141 |
+
async with client.aio.live.connect(
|
142 |
model="gemini-2.0-flash-exp", config=config
|
143 |
+
) as session:
|
144 |
self.session = session
|
145 |
gemini_connected = True
|
146 |
asyncio.create_task(self.receive_audio())
|
|
|
161 |
|
162 |
while not self.quit.is_set():
|
163 |
try:
|
164 |
+
await asyncio.sleep(0) # Yield to the event loop
|
165 |
+
if self.quit.is_set():
|
166 |
+
break
|
167 |
+
turn = self.session.receive()
|
168 |
async for response in turn:
|
169 |
+
if self.quit.is_set():
|
170 |
+
break # Exit inner loop if quit is set.
|
171 |
if data := response.data:
|
172 |
yield data
|
173 |
except Exception as e:
|
174 |
logger.error(f"Error receiving from Gemini: {e}")
|
175 |
+
self.quit.set() # set quit if we error.
|
176 |
+
break
|
177 |
+
|
|
|
178 |
async def receive_audio(self):
|
179 |
try:
|
|
|
180 |
async for audio_response in async_aggregate_bytes_to_16bit(self.generator()):
|
181 |
self.audio_queue.put_nowait(audio_response)
|
182 |
except Exception as e:
|
|
|
213 |
global gemini_connected
|
214 |
gemini_connected = False
|
215 |
logger.info("Shutting down GeminiHandler.")
|
216 |
+
if self.session:
|
217 |
+
try:
|
218 |
+
# await self.session.close() # There is no async close
|
219 |
+
pass
|
220 |
+
except Exception:
|
221 |
+
pass
|
222 |
+
self.quit.set() # Set quit *after* attempting to close the session
|
223 |
self.connection = None
|
224 |
self.args_set.clear()
|
225 |
+
|
|
|
|
|
226 |
self.quit.clear()
|
227 |
update_gemini_status_sync()
|
228 |
|