Spaces:
Running
Running
Update doctorvoice.py
Browse files- doctorvoice.py +20 -101
doctorvoice.py
CHANGED
@@ -1,112 +1,31 @@
|
|
1 |
-
# if you dont use pipenv uncomment the following:
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
load_dotenv()
|
4 |
-
|
5 |
-
#Step1a: Setup Text to Speech–TTS–model with gTTS
|
6 |
import os
|
7 |
from gtts import gTTS
|
8 |
-
|
9 |
-
def text_to_speech_with_gtts_old(input_text, output_filepath):
|
10 |
-
language="en"
|
11 |
-
|
12 |
-
audioobj= gTTS(
|
13 |
-
text=input_text,
|
14 |
-
lang=language,
|
15 |
-
slow=False
|
16 |
-
)
|
17 |
-
audioobj.save(output_filepath)
|
18 |
-
|
19 |
-
|
20 |
-
# input_text="Hi"
|
21 |
-
# text_to_speech_with_gtts_old(input_text=input_text, output_filepath="gtts_testing.mp3")
|
22 |
-
|
23 |
-
#Step1b: Setup Text to Speech–TTS–model with ElevenLabs
|
24 |
-
import elevenlabs
|
25 |
-
from elevenlabs.client import ElevenLabs
|
26 |
-
|
27 |
-
ELEVENLABS_API_KEY=os.environ.get("ELEVENLABS_API_KEY")
|
28 |
-
|
29 |
-
def text_to_speech_with_elevenlabs_old(input_text, output_filepath):
|
30 |
-
client=ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
31 |
-
audio=client.generate(
|
32 |
-
text= input_text,
|
33 |
-
voice= "Emily",
|
34 |
-
output_format= "mp3_22050_32",
|
35 |
-
model= "eleven_turbo_v2"
|
36 |
-
)
|
37 |
-
elevenlabs.save(audio, output_filepath)
|
38 |
-
|
39 |
-
# text_to_speech_with_elevenlabs_old(input_text, output_filepath="elevenlabs_testing.mp3")
|
40 |
-
|
41 |
-
# #Step2: Use Model for Text output to Voice
|
42 |
-
# when the files of the doctor gets saved, they dont play automatically so we have to do this step 2 in order to automatically run the audio files.
|
43 |
-
import subprocess
|
44 |
-
import platform
|
45 |
from pydub import AudioSegment
|
46 |
-
from pydub.playback import play
|
47 |
import tempfile
|
48 |
|
49 |
def text_to_speech_with_gtts(input_text, output_filepath):
|
50 |
-
|
|
|
51 |
|
52 |
-
|
53 |
-
text
|
54 |
-
|
55 |
-
slow=False
|
56 |
-
)
|
57 |
-
audioobj.save(output_filepath)
|
58 |
-
os_name = platform.system()
|
59 |
-
try:
|
60 |
-
if os_name == "Darwin": # macOS
|
61 |
-
subprocess.run(['afplay', output_filepath])
|
62 |
-
elif os_name == "Windows": # Windows
|
63 |
-
subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{output_filepath}").PlaySync();'])
|
64 |
-
elif os_name == "Linux": # Linux
|
65 |
-
subprocess.run(['aplay', output_filepath]) # Alternative: use 'mpg123' or 'ffplay'
|
66 |
-
else:
|
67 |
-
raise OSError("Unsupported operating system")
|
68 |
-
except Exception as e:
|
69 |
-
print(f"An error occurred while trying to play the audio: {e}")
|
70 |
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
os_name = platform.system()
|
77 |
try:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
audio.export(wav_path, format='wav')
|
87 |
-
# Play the WAV file
|
88 |
-
subprocess.run(['powershell', '-c', f'(New-Object Media.SoundPlayer "{wav_path}").PlaySync();'])
|
89 |
-
# Clean up temporary file
|
90 |
-
os.unlink(wav_path)
|
91 |
-
elif os_name == "Linux": # Linux
|
92 |
-
subprocess.run(['mpg123', file_path]) # Using mpg123 for MP3 playback
|
93 |
-
else:
|
94 |
-
raise OSError("Unsupported operating system")
|
95 |
except Exception as e:
|
96 |
-
|
97 |
-
|
98 |
-
def text_to_speech_with_elevenlabs(input_text, output_filepath):
|
99 |
-
client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
100 |
-
audio = client.generate(
|
101 |
-
text=input_text,
|
102 |
-
voice="Aria",
|
103 |
-
output_format="mp3_22050_32",
|
104 |
-
model="eleven_turbo_v2"
|
105 |
-
)
|
106 |
-
elevenlabs.save(audio, output_filepath)
|
107 |
-
|
108 |
-
# Play the audio
|
109 |
-
play_audio(output_filepath)
|
110 |
-
return output_filepath
|
111 |
-
|
112 |
-
# text_to_speech_with_elevenlabs(input_text, output_filepath="elevenlabs_testing_autoplay.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from gtts import gTTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from pydub import AudioSegment
|
|
|
4 |
import tempfile
|
5 |
|
6 |
def text_to_speech_with_gtts(input_text, output_filepath):
|
7 |
+
"""
|
8 |
+
Convert text to speech using gTTS and save as an MP3 file.
|
9 |
|
10 |
+
Args:
|
11 |
+
input_text (str): The text to convert to speech.
|
12 |
+
output_filepath (str): Path to save the output MP3 file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
Returns:
|
15 |
+
str: Path to the generated MP3 file (for Gradio playback in Spaces).
|
16 |
|
17 |
+
Raises:
|
18 |
+
Exception: If text-to-speech or file saving fails.
|
19 |
+
"""
|
20 |
+
language = "en"
|
|
|
21 |
try:
|
22 |
+
# Generate MP3 using gTTS
|
23 |
+
audio_obj = gTTS(
|
24 |
+
text=input_text,
|
25 |
+
lang=language,
|
26 |
+
slow=False
|
27 |
+
)
|
28 |
+
audio_obj.save(output_filepath)
|
29 |
+
return output_filepath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
except Exception as e:
|
31 |
+
raise Exception(f"Error generating text-to-speech: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|