mprateek commited on
Commit
74ac3a9
1 Parent(s): ad1f4e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,53 +1,48 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
- import os
4
 
5
- os.system("pip uninstall -y gradio")
6
- os.system("pip install gradio==2.6.4")
7
 
8
- # Initialize the Text-to-Speech models
9
- tts_engine = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
10
-
11
- # Function to convert text to speech
12
- def convert_text_to_speech(input_text: str, audio_source, audio_file_path, language: str = "en"):
13
- output_audio_path = "generated_speech.wav"
14
- if audio_file_path and not audio_source:
15
- audio_source = audio_file_path
16
-
17
- # Generate speech using the TTS engine
18
- if audio_source is not None:
19
- tts_engine.tts_to_file(input_text, speaker_wav=audio_source, language=language, file_path=output_audio_path)
20
  else:
21
- tts_engine.tts_to_file(input_text, speaker=tts_engine.speakers[0], language=language, file_path=output_audio_path)
22
-
23
- return output_audio_path
24
 
25
- # Gradio interface setup
26
- title = "Enhanced Voice-Cloning Interface"
27
 
28
- def audio_input_toggle(choice):
29
- """Toggle audio input method based on user choice."""
30
  if choice == "mic":
31
  return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
32
  else:
33
  return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
34
 
35
- with gr.Blocks() as enhanced_demo:
 
36
  with gr.Row():
37
  with gr.Column():
38
- text_input = gr.Textbox(label="Enter your text here", value="", max_lines=3)
39
- gr.Markdown("### Note: This demo currently supports English only.")
40
- audio_method = gr.Radio(["mic", "file"], value="mic", label="Select your audio input method:")
 
41
  audio_input_mic = gr.Audio(label="Record your voice", source="microphone", type="filepath", visible=True)
42
- audio_input_file = gr.Audio(label="Upload your voice file", type="filepath", visible=False)
43
 
44
  with gr.Row():
45
- btn_clear = gr.Button("Clear")
46
- btn_submit = gr.Button("Generate Speech", variant="primary")
 
 
47
  with gr.Column():
48
  audio_output = gr.Audio(label="Generated Speech")
49
 
50
- btn_submit.click(convert_text_to_speech, inputs=[text_input, audio_input_mic, audio_input_file], outputs=audio_output)
51
- audio_method.change(audio_input_toggle, audio_method, [audio_input_mic, audio_input_file])
 
52
 
53
- enhanced_demo.launch(enable_queue=True)
 
1
  import gradio as gr
2
  from TTS.api import TTS
 
3
 
4
+ # Initialize TTS models
5
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
6
 
7
+ def text_to_speech(text: str, speaker_wav, speaker_wav_file, language: str):
8
+ if speaker_wav_file and not speaker_wav:
9
+ speaker_wav = speaker_wav_file
10
+ file_path = "output.wav"
11
+ if speaker_wav is not None:
12
+ tts.tts_to_file(text, speaker_wav=speaker_wav, language=language, file_path=file_path)
 
 
 
 
 
 
13
  else:
14
+ tts.tts_to_file(text, speaker=tts.speakers[0], language=language, file_path=file_path)
15
+ return file_path
 
16
 
17
+ title = "Prateek's Voice Cloning Demo"
 
18
 
19
+ def toggle(choice):
 
20
  if choice == "mic":
21
  return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
22
  else:
23
  return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
24
 
25
+ with gr.Blocks() as demo:
26
+ gr.Markdown("# 🎙️ Prateek's Voice Cloning Demo")
27
  with gr.Row():
28
  with gr.Column():
29
+ text_input = gr.Textbox(label="Enter the text you want to convert to speech", value="", max_lines=3)
30
+ lan_input = gr.Radio(label="Language", choices=["en"], value="en", visible=False)
31
+ radio = gr.Radio(["Record your voice", "Upload an audio file"], value="mic",
32
+ label="How would you like to provide your voice sample?")
33
  audio_input_mic = gr.Audio(label="Record your voice", source="microphone", type="filepath", visible=True)
34
+ audio_input_file = gr.Audio(label="Upload an audio file", type="filepath", visible=False)
35
 
36
  with gr.Row():
37
+ with gr.Column():
38
+ btn_clear = gr.Button("Clear")
39
+ with gr.Column():
40
+ btn = gr.Button("Generate Speech", variant="primary")
41
  with gr.Column():
42
  audio_output = gr.Audio(label="Generated Speech")
43
 
44
+ btn.click(text_to_speech, inputs=[text_input, audio_input_mic,
45
+ audio_input_file, lan_input], outputs=audio_output)
46
+ radio.change(toggle, radio, [audio_input_mic, audio_input_file])
47
 
48
+ demo.launch(enable_queue=True)