From a5ab5948c9f914edbb63408cc255a5ce4b229a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 30 Aug 2023 17:30:12 +0800 Subject: [PATCH] fixbug: remove aiofile --- metagpt/tools/azure_tts.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/metagpt/tools/azure_tts.py b/metagpt/tools/azure_tts.py index 0dc16d516..6864faf10 100644 --- a/metagpt/tools/azure_tts.py +++ b/metagpt/tools/azure_tts.py @@ -7,18 +7,15 @@ @Desc : azure TTS OAS3 api, which provides text-to-speech functionality """ import asyncio +import base64 from pathlib import Path from uuid import uuid4 -import base64 -import sys + import aiofiles +from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer from metagpt.config import CONFIG, Config - -sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt' from metagpt.logs import logger -from aiofile import async_open -from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer class AzureTTS: @@ -34,18 +31,17 @@ class AzureTTS: # 参数参考:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles async def synthesize_speech(self, lang, voice, text, output_file): - speech_config = SpeechConfig( - subscription=self.subscription_key, region=self.region) + speech_config = SpeechConfig(subscription=self.subscription_key, region=self.region) speech_config.speech_synthesis_voice_name = voice audio_config = AudioConfig(filename=output_file) - synthesizer = SpeechSynthesizer( - speech_config=speech_config, - audio_config=audio_config) + synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) # More detail: https://learn.microsoft.com/en-us/azure/ai-services/speech-service/speech-synthesis-markup-voice - ssml_string = "" \ - f"{text}" + ssml_string = ( + "" + f"{text}" + ) return synthesizer.speak_ssml_async(ssml_string).get() @@ -100,7 +96,7 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti await tts.synthesize_speech(lang=lang, voice=voice, text=xml_value, output_file=str(filename)) async with aiofiles.open(filename, mode="rb") as reader: data = await reader.read() - base64_string = base64.b64encode(data).decode('utf-8') + base64_string = base64.b64encode(data).decode("utf-8") filename.unlink() except Exception as e: logger.error(f"text:{text}, error:{e}")