pr0fixi commited on
Commit
18c6b7a
·
verified ·
1 Parent(s): f714f5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -1,33 +1,37 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
 
4
- # Initialize Coqui TTS with XTTS-v2
5
- tts = TTS(model_name="coqui/XTTS-v2", progress_bar=False)
6
 
7
- def generate_speech(text, speaker_wav, language):
8
- output_file = "/tmp/output.wav"
 
 
 
 
9
  try:
10
- tts.tts_to_file(
11
- text=text,
12
- file_path=output_file,
13
- speaker_wav=speaker_wav, # Path to reference audio for voice cloning
14
- language=language # Language code (e.g., "en" for English)
15
- )
16
  except Exception as e:
17
  return f"Error generating speech: {str(e)}"
18
- return output_file
19
 
20
  # Create a Gradio interface
21
  with gr.Blocks() as demo:
22
- gr.Markdown("# Coqui XTTS-v2 Demo")
23
  gr.Markdown("Type some text and hear it spoken!")
24
 
25
- input_text = gr.Textbox(label="Enter Text", placeholder="Type something...")
26
- speaker_wav = gr.File(label="Upload Reference Audio for Voice Cloning")
27
- language = gr.Dropdown(choices=["en", "es", "fr", "de"], label="Language", value="en")
28
- output_audio = gr.Audio(label="Generated Speech", type="filepath")
29
 
30
- submit_button = gr.Button("Generate Speech")
31
- submit_button.click(generate_speech, inputs=[input_text, speaker_wav, language], outputs=output_audio)
 
 
 
 
 
 
32
 
 
33
  demo.launch(share=True)
 
1
  import gradio as gr
2
  from TTS.api import TTS
3
 
4
+ # Initialize Coqui TTS with a pre-trained model
5
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
6
 
7
+ def generate_speech(text):
8
+ """
9
+ Generate speech from the input text.
10
+ Saves the audio to a temporary file and returns the file path.
11
+ """
12
+ output_file = "/tmp/output.wav" # Temporary file for the generated audio
13
  try:
14
+ tts.tts_to_file(text=text, file_path=output_file)
15
+ return output_file # Return the path to the generated audio
 
 
 
 
16
  except Exception as e:
17
  return f"Error generating speech: {str(e)}"
 
18
 
19
  # Create a Gradio interface
20
  with gr.Blocks() as demo:
21
+ gr.Markdown("# Coqui TTS Demo")
22
  gr.Markdown("Type some text and hear it spoken!")
23
 
24
+ with gr.Row():
25
+ input_text = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=3)
 
 
26
 
27
+ with gr.Row():
28
+ submit_button = gr.Button("Generate Speech")
29
+
30
+ with gr.Row():
31
+ output_audio = gr.Audio(label="Generated Speech", type="filepath")
32
+
33
+ # Connect the button click to the generate_speech function
34
+ submit_button.click(generate_speech, inputs=input_text, outputs=output_audio)
35
 
36
+ # Launch the app
37
  demo.launch(share=True)