Update voice_of_the_doctor.py
Browse files- voice_of_the_doctor.py +49 -66
voice_of_the_doctor.py
CHANGED
@@ -1,66 +1,49 @@
|
|
1 |
-
import os
|
2 |
-
from gtts import gTTS
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
|
5 |
-
load_dotenv()
|
6 |
-
|
7 |
-
|
8 |
-
def text_to_speech_with_gtts(input_text, output_filepath):
|
9 |
-
"""
|
10 |
-
Convert input text to speech and save it as an MP3 file.
|
11 |
-
|
12 |
-
Args:
|
13 |
-
input_text (str): The text to be converted to speech.
|
14 |
-
output_filepath (str): The path to save the generated MP3 file.
|
15 |
-
"""
|
16 |
-
audioobj = gTTS(text=input_text, lang='en',slow= False)
|
17 |
-
audioobj.save(output_filepath)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
abs_filepath
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
os_name = platform.system()
|
52 |
-
try:
|
53 |
-
if os_name == "Darwin": # macOS
|
54 |
-
subprocess.run(['afplay', abs_filepath])
|
55 |
-
elif os_name == "Windows": # Windows
|
56 |
-
playsound(abs_filepath)
|
57 |
-
elif os_name == "Linux": # Linux
|
58 |
-
subprocess.run(['aplay', abs_filepath])
|
59 |
-
else:
|
60 |
-
raise OSError("Unsupported operating system")
|
61 |
-
except Exception as e:
|
62 |
-
print(f"An error occurred while trying to play the audio: {e}")
|
63 |
-
|
64 |
-
return abs_filepath
|
65 |
-
|
66 |
-
text_to_speech_with_gtts_autoplay(input_text=input_text, output_filepath="gtts_testing.mp3")
|
|
|
1 |
+
import os
|
2 |
+
from gtts import gTTS
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
|
8 |
+
def text_to_speech_with_gtts(input_text, output_filepath):
|
9 |
+
"""
|
10 |
+
Convert input text to speech and save it as an MP3 file.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
input_text (str): The text to be converted to speech.
|
14 |
+
output_filepath (str): The path to save the generated MP3 file.
|
15 |
+
"""
|
16 |
+
audioobj = gTTS(text=input_text, lang='en',slow= False)
|
17 |
+
audioobj.save(output_filepath)
|
18 |
+
|
19 |
+
import elevenlabs
|
20 |
+
from elevenlabs.client import ElevenLabs
|
21 |
+
|
22 |
+
ELEVENLABS_API_KEY = os.environ.get('ELEVENLABS_API_KEY')
|
23 |
+
|
24 |
+
def text_to_speech_with_elevanlabs(input_text,output_filepath):
|
25 |
+
|
26 |
+
client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
27 |
+
audio = client.generate(
|
28 |
+
text = input_text,
|
29 |
+
voice = "Aria",
|
30 |
+
output_format = "mp3_22050_32",
|
31 |
+
model = "eleven_turbo_v2"
|
32 |
+
)
|
33 |
+
elevenlabs.save(audio,output_filepath)
|
34 |
+
|
35 |
+
# text_to_speech_with_elevanlabs(input_text=input_text,output_filepath="elevenlabs_testing.mp3")
|
36 |
+
|
37 |
+
import subprocess
|
38 |
+
import platform
|
39 |
+
from playsound import playsound
|
40 |
+
|
41 |
+
def text_to_speech_with_gtts_autoplay(input_text, output_filepath):
|
42 |
+
|
43 |
+
abs_filepath = os.path.abspath(output_filepath)
|
44 |
+
audioobj = gTTS(text=input_text, lang='en', slow=False)
|
45 |
+
audioobj.save(abs_filepath)
|
46 |
+
|
47 |
+
return abs_filepath
|
48 |
+
|
49 |
+
# text_to_speech_with_gtts_autoplay(input_text=input_text, output_filepath="gtts_testing.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|