Satyam-Singh commited on
Commit
e01ec1d
·
verified ·
1 Parent(s): ca74360

Create text_to_speech.py

Browse files
Files changed (1) hide show
  1. text_to_speech.py +34 -0
text_to_speech.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # text_to_speech.py
2
+
3
+ import os
4
+ from elevenlabs import VoiceSettings, ElevenLabs
5
+
6
+ ELEVENLABS_API_KEY = "sk_29eaa30255601541b3a80f470ac8c45987c007b8e2dfb791"
7
+ client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
8
+
9
+ def text_to_speech_file(text: str) -> str:
10
+ # Calling the text-to-speech conversion API with detailed parameters
11
+ response = client.text_to_speech.convert(
12
+ voice_id="XrExE9yKIg1WjnnlVkGX", # Adam pre-made voice
13
+ output_format="mp3_22050_32",
14
+ text=text,
15
+ model_id="eleven_turbo_v2_5", # use the turbo model for low latency
16
+ voice_settings=VoiceSettings(
17
+ stability=0.0,
18
+ similarity_boost=1.0,
19
+ style=0.0,
20
+ use_speaker_boost=True,
21
+ ),
22
+ )
23
+
24
+ # Generating a unique file name for the output MP3 file
25
+ save_file_path = "output.wav"
26
+
27
+ # Writing the audio to a file
28
+ with open(save_file_path, "wb") as f:
29
+ for chunk in response:
30
+ if chunk:
31
+ f.write(chunk)
32
+
33
+ print(f"{save_file_path}: A new audio file was saved successfully!")
34
+ return save_file_path