我正在使用微软语音翻译模型。我在windows下用过。效果很好。当我在 Linux 上运行它时,它给了我多个错误。其中大部分是由于我在 Linux 上安装的依赖项造成的。然而,我仍然面临着问题。
错误:无法从部分初始化的模块“gi”导入名称“_gi”(很可能是由于循环导入)(/usr/lib/python3/dist-packages/gi/init.py)
注意:我使用的是python 3.11.5
感谢期待!
from dotenv import load_dotenv
import os
import azure.cognitiveservices.speech as speech_sdk
from playsound import playsound
def main():
try:
global speech_config
global translation_config
# Get Configuration Settings
load_dotenv()
cog_key = os.getenv('COG_SERVICE_KEY')
cog_region = os.getenv('COG_SERVICE_REGION')
# Configure translation
translation_config = speech_sdk.translation.SpeechTranslationConfig(cog_key, cog_region)
translation_config.speech_recognition_language = 'en-US'
translation_config.add_target_language('fr')
translation_config.add_target_language('es')
translation_config.add_target_language('ko')
translation_config.add_target_language('de')
print('Ready to translate from',translation_config.speech_recognition_language)
# Configure speech
speech_config = speech_sdk.SpeechConfig(cog_key, cog_region)
# Get user input
targetLanguage = ''
while targetLanguage != 'quit':
targetLanguage = input('\nEnter a target language\n de = German\n fr = French\n es = Spanish\n ko = Korean\n Enter anything else to stop\n').lower()
if targetLanguage in translation_config.target_languages:
Translate(targetLanguage)
else:
targetLanguage = 'quit'
except Exception as ex:
print(ex)
def Translate(targetLanguage):
translation = ''
# Translate speech
audioFile = 'harvard.wav'
playsound(audioFile)
audio_config = speech_sdk.AudioConfig(filename=audioFile)
translator = speech_sdk.translation.TranslationRecognizer(translation_config, audio_config = audio_config)
print("Getting speech from file...")
result = translator.recognize_once_async().get()
print('Translating "{}"'.format(f"+++++++++++++{result.text}"))
translation = result.translations[targetLanguage]
print( f"{translation}")
with open('just_a_dummy.txt', 'w', encoding='utf-8') as file:
file.write(translation)
# with open('just_a_dummy.txt' 'r', encoding='utf-8') as file:
# print(f"**********************************************************{ file.read()}")
# Synthesize translation
voices = {
"fr": {"male": "fr-FR-DeniseNeural", "female": "fr-FR-HenriNeural"},
"es": {"male": "es-ES-PedroNeural", "female": "es-ES-ElviraNeural"},
"ko": {"male": "ko-KR-InJoonNeural", "female": "ko-KR-SunHiNeural"},
"de": {"male": "de-DE-ConradNeural", "female": "de-DE-KatjaNeural"},
"ms": {"male": "ms-MY-OsmanNeural", "female": "ms-MY-YasminNeural"},
"wuu": {"male": "wuu-CN-YunzheNeural", "female": "wuu-CN-XiaotongNeural"}
}
# Prompt the user to choose the voice gender
while True:
voice_gender = input(f"Choose voice gender for {targetLanguage} (male/female): ").strip().lower()
if voice_gender in ("male", "female"):
break
else:
print("Invalid input. Please enter 'male' or 'female'.")
selected_voice = voices[targetLanguage][voice_gender]
speech_config.speech_synthesis_voice_name = selected_voice
speech_synthesizer = speech_sdk.SpeechSynthesizer(speech_config)
speak = speech_synthesizer.speak_text_async(translation).get()
if speak.reason != speech_sdk.ResultReason.SynthesizingAudioCompleted:
print(speak.reason)
else:
output_audio_file = f"output_{targetLanguage}_{voice_gender}.wav"
with open(output_audio_file, "wb") as file:
file.write(speak.audio_data)
print(f"Saved synthesized audio to {output_audio_file}")
if __name__ == "__main__":
main()