|
2 | 2 | import json |
3 | 3 | import logging |
4 | 4 | import threading |
5 | | -import requests |
| 5 | +import asyncio |
| 6 | +import aiohttp |
6 | 7 |
|
7 | 8 | from wyoming.asr import Transcript, Transcribe |
8 | 9 | from wyoming.tts import Synthesize |
@@ -236,40 +237,40 @@ async def _HandleDescribe(self) -> bool: |
236 | 237 |
|
237 | 238 | # Since this is important, we have some retry logic. |
238 | 239 | attempt = 0 |
239 | | - while True: |
240 | | - attempt += 1 |
241 | | - |
242 | | - # Attempt getting a valid response. |
243 | | - serviceInfo:Info = None |
244 | | - try: |
245 | | - self.Logger.debug(f"Sage - Starting Info Service Request - {url}") |
246 | | - # Attempt to get and parse the info object. |
247 | | - response = requests.get(url, timeout=10) |
248 | | - if response.status_code == 200: |
249 | | - serviceInfo = self._BuildInfoEvent(response.json()) |
250 | | - if serviceInfo is None: |
251 | | - raise Exception("Failed to build info event.") |
252 | | - else: |
253 | | - self.Logger.warning(f"Sage - Failed to get models from service. Attempt: {attempt} - {response.status_code}") |
254 | | - except Exception as e: |
255 | | - self.Logger.warning(f"Sage - Failed to get models from service. Attempt: {attempt} - {e}") |
256 | | - |
257 | | - # If we got service info, try to write it to the client. |
258 | | - if serviceInfo is not None: |
259 | | - self.Logger.debug("Sage - Service request successful, sending to Wyoming protocol.") |
| 240 | + async with aiohttp.ClientSession() as session: |
| 241 | + while True: |
| 242 | + attempt += 1 |
| 243 | + serviceInfo:Info = None |
260 | 244 | try: |
261 | | - await self._CacheAndWriteInfoEvent(serviceInfo) |
262 | | - # Success |
263 | | - return True |
| 245 | + self.Logger.debug(f"Sage - Starting Info Service Request - {url}") |
| 246 | + # Perform the async GET request with a timeout of 10 seconds. |
| 247 | + async with session.get(url, timeout=10) as response: |
| 248 | + if response.status == 200: |
| 249 | + data = await response.json() |
| 250 | + serviceInfo = self._BuildInfoEvent(data) |
| 251 | + if serviceInfo is None: |
| 252 | + raise Exception("Failed to build info event.") |
| 253 | + else: |
| 254 | + self.Logger.warning(f"Sage - Failed to get models from service. Attempt: {attempt} - {response.status}") |
264 | 255 | except Exception as e: |
265 | | - self.Logger.warning(f"Sage - Failed to send info to wyoming protocol. Attempt: {attempt} - {e}") |
266 | | - |
267 | | - # If we fail, try a few times. Throw when we hit the limit. |
268 | | - if attempt > 3: |
269 | | - raise Exception("Failed to get models from service after 3 attempts.") |
270 | | - |
271 | | - # Sleep before trying again. |
272 | | - time.sleep(5) |
| 256 | + self.Logger.warning(f"Sage - Failed to get models from service. Attempt: {attempt} - {e}") |
| 257 | + |
| 258 | + # If we got valid serviceInfo, try to process it. |
| 259 | + if serviceInfo is not None: |
| 260 | + self.Logger.debug("Sage - Service request successful, sending to Wyoming protocol.") |
| 261 | + try: |
| 262 | + await self._CacheAndWriteInfoEvent(serviceInfo) |
| 263 | + # Success, exit the function. |
| 264 | + return True |
| 265 | + except Exception as e: |
| 266 | + self.Logger.warning(f"Sage - Failed to send info to wyoming protocol. Attempt: {attempt} - {e}") |
| 267 | + |
| 268 | + # After 3 attempts, raise an exception. |
| 269 | + if attempt > 3: |
| 270 | + raise Exception("Failed to get models from service after 3 attempts.") |
| 271 | + |
| 272 | + # Wait 5 seconds before retrying. |
| 273 | + await asyncio.sleep(5) |
273 | 274 |
|
274 | 275 | except Exception as e: |
275 | 276 | Sentry.Exception("Sage - Failed get info from service.", e) |
|
0 commit comments