| import os
|
| from gtts import gTTS
|
|
|
| def text_to_speech_with_gtts_old(input_text, output_filepath, language="en"):
|
|
|
| audioobj= gTTS(
|
| text=input_text,
|
| lang=language,
|
| slow=False
|
| )
|
| audioobj.save(output_filepath)
|
|
|
|
|
|
|
| input_text= "مرحبًا، هذا ذكاء اصطناعي مع حسن لغرض الاختبار!"
|
| text_to_speech_with_gtts_old(input_text=input_text, output_filepath="gtts_testing.mp3", language="ar")
|
|
|
|
|
| import elevenlabs
|
| from elevenlabs.client import ElevenLabs
|
|
|
| ELEVENLABS_API_KEY=os.environ.get("ELEVEN_API_KEY")
|
|
|
| def text_to_speech_with_elevenlabs_old(input_text, output_filepath):
|
| client=ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
| audio=client.generate(
|
| text= input_text,
|
| voice= "Aria",
|
| output_format= "mp3_22050_32",
|
| model= "eleven_turbo_v2"
|
| )
|
| elevenlabs.save(audio, output_filepath)
|
|
|
|
|
|
|
|
|
|
|
| import subprocess
|
| import platform
|
|
|
| def text_to_speech_with_gtts(input_text, output_filepath):
|
| language="en"
|
|
|
| audioobj= gTTS(
|
| text=input_text,
|
| lang=language,
|
| slow=False
|
| )
|
| audioobj.save(output_filepath)
|
| os_name = platform.system()
|
| try:
|
| if os_name == "Darwin":
|
| subprocess.run(['afplay', output_filepath])
|
| elif os_name == "Windows":
|
| subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{output_filepath}").PlaySync();'])
|
| elif os_name == "Linux":
|
| subprocess.run(['aplay', output_filepath])
|
| else:
|
| raise OSError("Unsupported operating system")
|
| except Exception as e:
|
| print(f"An error occurred while trying to play the audio: {e}")
|
|
|
|
|
| input_text="Hi this is Ai with Hassan, autoplay testing!"
|
|
|
|
|
|
|
| def text_to_speech_with_elevenlabs(input_text, output_filepath):
|
| client=ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
| audio=client.generate(
|
| text= input_text,
|
| voice= "Aria",
|
| output_format= "mp3_22050_32",
|
| model= "eleven_turbo_v2"
|
| )
|
| elevenlabs.save(audio, output_filepath)
|
| os_name = platform.system()
|
| try:
|
| if os_name == "Darwin":
|
| subprocess.run(['afplay', output_filepath])
|
| elif os_name == "Windows":
|
| subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{output_filepath}").PlaySync();'])
|
| elif os_name == "Linux":
|
| subprocess.run(['aplay', output_filepath])
|
| else:
|
| raise OSError("Unsupported operating system")
|
| except Exception as e:
|
| print(f"An error occurred while trying to play the audio: {e}")
|
|
|
| |