diff --git a/xiaogpt/cli.py b/xiaogpt/cli.py index 965d3713..125169f5 100644 --- a/xiaogpt/cli.py +++ b/xiaogpt/cli.py @@ -111,9 +111,10 @@ def main(): ) parser.add_argument( "--verbose", + "-v", dest="verbose", - action="store_true", - default=None, + action="count", + default=0, help="show info", ) parser.add_argument( diff --git a/xiaogpt/config.py b/xiaogpt/config.py index 556bf6fe..5c12c32e 100644 --- a/xiaogpt/config.py +++ b/xiaogpt/config.py @@ -77,7 +77,7 @@ class Config: api_base: str | None = None deployment_id: str | None = None use_command: bool = False - verbose: bool = False + verbose: int = 0 start_conversation: str = "开始持续对话" end_conversation: str = "结束持续对话" stream: bool = False diff --git a/xiaogpt/tts/live.py b/xiaogpt/tts/live.py index ec1dddfb..64a905f1 100644 --- a/xiaogpt/tts/live.py +++ b/xiaogpt/tts/live.py @@ -27,11 +27,15 @@ def do_GET(self): self.end_headers() key = self.path.split("/")[-1] queue = get_queue(key) + chunks: list[bytes] = [] while True: chunk = queue.get() + chunks.append(chunk) if chunk == b"": break self.wfile.write(chunk) + for chunk in chunks: + queue.put_nowait(chunk) def log_message(self, format, *args): logger.debug(f"{self.address_string()} - {format}", *args) @@ -76,6 +80,7 @@ async def worker(): while True: if await self.get_if_xiaoai_is_playing(): + logger.debug("Xiaoai is playing, waiting") await asyncio.sleep(1) else: break diff --git a/xiaogpt/xiaogpt.py b/xiaogpt/xiaogpt.py index 7ec49767..f9a81d0e 100644 --- a/xiaogpt/xiaogpt.py +++ b/xiaogpt/xiaogpt.py @@ -57,15 +57,20 @@ async def close(self): async def poll_latest_ask(self): async with ClientSession() as session: session._cookie_jar = self.cookie_jar + log_polling = int(self.config.verbose) > 1 while True: - self.log.debug( - "Listening new message, timestamp: %s", self.last_timestamp - ) + if log_polling: + self.log.debug( + "Listening new message, timestamp: %s", self.last_timestamp + ) new_record = await self.get_latest_ask_from_xiaoai(session) start = time.perf_counter() - self.log.debug( - "Polling_event, timestamp: %s %s", self.last_timestamp, new_record - ) + if log_polling: + self.log.debug( + "Polling_event, timestamp: %s %s", + self.last_timestamp, + new_record, + ) await self.polling_event.wait() if ( self.config.mute_xiaoai @@ -75,7 +80,10 @@ async def poll_latest_ask(self): await self.stop_if_xiaoai_is_playing() if (d := time.perf_counter() - start) < 1: # sleep to avoid too many request - self.log.debug("Sleep %f, timestamp: %s", d, self.last_timestamp) + if log_polling: + self.log.debug( + "Sleep %f, timestamp: %s", d, self.last_timestamp + ) # if you want force mute xiaoai, comment this line below. await asyncio.sleep(1 - d) @@ -334,6 +342,7 @@ async def get_if_xiaoai_is_playing(self): async def stop_if_xiaoai_is_playing(self): is_playing = await self.get_if_xiaoai_is_playing() if is_playing: + self.log.debug("Muting xiaoai") # stop it await self.mina_service.player_pause(self.device_id)