YashuSinghal commited on
Commit
5b832d0
·
verified ·
1 Parent(s): a01a104

Update voice_of_the_doctor.py

Browse files
Files changed (1) hide show
  1. 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
- input_text = "Hello, how can i help you today, how are you feeling ?"
20
- text_to_speech_with_gtts(input_text=input_text, output_filepath="gtts_testing.mp3")
21
-
22
-
23
- import elevenlabs
24
- from elevenlabs.client import ElevenLabs
25
-
26
- ELEVENLABS_API_KEY = os.environ.get('ELEVENLABS_API_KEY')
27
-
28
- def text_to_speech_with_elevanlabs(input_text,output_filepath):
29
-
30
- client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
31
- audio = client.generate(
32
- text = input_text,
33
- voice = "Aria",
34
- output_format = "mp3_22050_32",
35
- model = "eleven_turbo_v2"
36
- )
37
- elevenlabs.save(audio,output_filepath)
38
-
39
- # text_to_speech_with_elevanlabs(input_text=input_text,output_filepath="elevenlabs_testing.mp3")
40
-
41
- import subprocess
42
- import platform
43
- from playsound import playsound
44
-
45
- def text_to_speech_with_gtts_autoplay(input_text, output_filepath):
46
-
47
- abs_filepath = os.path.abspath(output_filepath)
48
- audioobj = gTTS(text=input_text, lang='en', slow=False)
49
- audioobj.save(abs_filepath)
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")