Ruchir456 commited on
Commit
089cafd
·
verified ·
1 Parent(s): a0333d6

Update voice_of_the_doctor.py

Browse files
Files changed (1) hide show
  1. voice_of_the_doctor.py +12 -34
voice_of_the_doctor.py CHANGED
@@ -1,35 +1,13 @@
1
- import os
2
- from gtts import gTTS
3
- from elevenlabs.client import ElevenLabs
4
-
5
- # Load ElevenLabs API key from environment
6
- ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
7
-
8
-
9
- def text_to_speech_with_gtts(input_text, output_filepath="doctor_voice.mp3"):
10
- """Generate voice using gTTS (free) and save as MP3"""
11
- tts = gTTS(text=input_text, lang="en", slow=False)
12
- tts.save(output_filepath)
13
- return output_filepath # Return file for frontend to play
14
-
15
-
16
- def text_to_speech_with_elevenlabs(input_text, output_filepath="doctor_voice.mp3"):
17
- """Generate voice using ElevenLabs (paid API)"""
18
- if not ELEVENLABS_API_KEY:
19
- raise ValueError("ELEVENLABS_API_KEY not set in environment variables")
20
-
21
- client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
22
-
23
- # This returns a generator of audio chunks
24
- audio_stream = client.text_to_speech.convert(
25
- voice_id="EXAVITQu4vr4xnSDxMaL", # Example voice_id (replace with yours)
26
- model_id="eleven_turbo_v2",
27
- text=input_text
28
- )
29
-
30
- # Collect chunks into a file
31
- with open(output_filepath, "wb") as f:
32
- for chunk in audio_stream:
33
- f.write(chunk)
34
-
35
  return output_filepath
 
1
+ import soundfile as sf
2
+ from transformers import pipeline
3
+
4
+ # Load Hugging Face TTS pipeline
5
+ pipe = pipeline("text-to-speech", model="maya-research/Veena")
6
+
7
+ def text_to_speech_with_veena(input_text, output_filepath="doctor_voice.wav"):
8
+ """Generate speech using Hugging Face Veena TTS model"""
9
+ output = pipe(input_text)
10
+ audio_array = output["audio"]
11
+ sampling_rate = output["sampling_rate"]
12
+ sf.write(output_filepath, audio_array, sampling_rate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  return output_filepath