NLPV commited on
Commit
4940f5b
·
verified ·
1 Parent(s): 704d2e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -1,33 +1,50 @@
1
  import gradio as gr
2
- from gtts import gTTS
3
  import tempfile
 
 
4
 
5
- def text_to_speech(text, voice_sample=None):
6
- """
7
- Generate Hindi speech from input text using gTTS.
8
- Note: The voice_sample is accepted but not used, since voice cloning for Hindi is not supported.
9
- """
10
- # Generate Hindi speech using gTTS (lang='hi')
11
- tts = gTTS(text=text, lang='hi')
12
 
13
- # Save to a temporary MP3 file
14
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
15
- tts.save(fp.name)
16
- audio_file = fp.name
17
- return audio_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  iface = gr.Interface(
20
- fn=text_to_speech,
21
  inputs=[
22
  gr.Textbox(lines=5, placeholder="हिंदी में टेक्स्ट दर्ज करें...", label="Text"),
23
- gr.Audio(type="filepath", label="Voice Sample (ignored)")
24
  ],
25
- outputs=gr.Audio(type="filepath", label="Generated Speech"),
26
- title="Hindi Text-to-Speech",
27
  description=(
28
- "Generate Hindi speech from text using gTTS. Note: "
29
- "Voice cloning is not available for Hindi; the voice sample input is ignored."
30
  )
31
  )
32
 
33
  iface.launch()
 
 
1
  import gradio as gr
2
+ from TTS.api import TTS
3
  import tempfile
4
+ import librosa
5
+ import soundfile as sf
6
 
7
+ # Initialize Coqui TTS model for Hindi (IndicTTS)
8
+ tts_model_name = "tts_models/multilingual/multi-dataset/xtts_v2"
9
+ tts = TTS(tts_model_name)
10
+
11
+ def text_to_speech_clone(text, voice_sample):
12
+ if voice_sample is None:
13
+ return "Please provide a voice sample audio.", None
14
 
15
+ # Load voice sample
16
+ sample_wav, sample_rate = librosa.load(voice_sample, sr=22050)
17
+
18
+ # Save voice sample as wav temporarily
19
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_sample:
20
+ sf.write(tmp_sample.name, sample_wav, sample_rate)
21
+ voice_sample_path = tmp_sample.name
22
+
23
+ # Generate cloned speech in Hindi
24
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_output:
25
+ tts.tts_to_file(
26
+ text=text,
27
+ file_path=tmp_output.name,
28
+ speaker_wav=voice_sample_path,
29
+ language="hi"
30
+ )
31
+ output_path = tmp_output.name
32
+
33
+ return output_path
34
 
35
  iface = gr.Interface(
36
+ fn=text_to_speech_clone,
37
  inputs=[
38
  gr.Textbox(lines=5, placeholder="हिंदी में टेक्स्ट दर्ज करें...", label="Text"),
39
+ gr.Audio(type="filepath", label="Voice Sample (Hindi speech)")
40
  ],
41
+ outputs=gr.Audio(type="filepath", label="Generated Cloned Speech"),
42
+ title="Hindi Text-to-Speech with Voice Cloning",
43
  description=(
44
+ "Generate Hindi speech from text with voice cloning capability. "
45
+ "Provide a Hindi voice sample to clone its voice characteristics."
46
  )
47
  )
48
 
49
  iface.launch()
50
+