Sunysss commited on
Commit
3da6ab0
·
verified ·
1 Parent(s): fdb768c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install speechbrain torchaudio gradio")
3
+
4
+ import torchaudio
5
+ from speechbrain.pretrained import Tacotron2, HIFIGAN
6
+ import gradio as gr
7
+
8
+ # Load SpeechBrain models
9
+ print("Loading SpeechBrain models...")
10
+ tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="models/tacotron2")
11
+ hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="models/hifigan")
12
+ print("Models loaded! Ready to generate speech.")
13
+
14
+ # Generate speech from text
15
+ def text_to_speech(text):
16
+ mel_output, _, _ = tacotron2.encode_text(text)
17
+ waveforms = hifi_gan.decode_batch(mel_output)
18
+ output_file = "output_speech.wav"
19
+ torchaudio.save(output_file, waveforms.squeeze(1), 22050)
20
+ return output_file # Return the audio file
21
+
22
+ # Create Gradio UI
23
+ iface = gr.Interface(
24
+ fn=text_to_speech,
25
+ inputs=gr.Textbox(label="Text to speak"),
26
+ outputs=gr.Audio(type="file", label="Generated Speech"),
27
+ title="SpeechBrain TTS Demo",
28
+ description="Enter text and get an AI-generated voice output!"
29
+ )
30
+
31
+ # Launch the app
32
+ iface.launch()