Spaces:
Running
Running
Create app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,36 @@
|
|
|
|
1 |
from speechbrain.pretrained import Tacotron2, HIFIGAN
|
2 |
from scipy.io.wavfile import write
|
3 |
-
import gradio as gr
|
4 |
|
5 |
-
# Load TTS and
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
10 |
def text_to_speech(text):
|
11 |
mel_output, mel_length, alignment = tacotron2.encode_text(text)
|
12 |
waveforms, _, _ = hifi_gan.decode_batch(mel_output)
|
13 |
-
audio_path = "
|
14 |
write(audio_path, 22050, waveforms.squeeze(1).cpu().numpy())
|
15 |
return audio_path
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
from speechbrain.pretrained import Tacotron2, HIFIGAN
|
3 |
from scipy.io.wavfile import write
|
|
|
4 |
|
5 |
+
# Load the TTS and Vocoder models
|
6 |
+
@st.cache_resource
|
7 |
+
def load_models():
|
8 |
+
tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmpdir_tts")
|
9 |
+
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
|
10 |
+
return tacotron2, hifi_gan
|
11 |
|
12 |
+
tacotron2, hifi_gan = load_models()
|
13 |
+
|
14 |
+
# Text-to-Speech function
|
15 |
def text_to_speech(text):
|
16 |
mel_output, mel_length, alignment = tacotron2.encode_text(text)
|
17 |
waveforms, _, _ = hifi_gan.decode_batch(mel_output)
|
18 |
+
audio_path = "output.wav"
|
19 |
write(audio_path, 22050, waveforms.squeeze(1).cpu().numpy())
|
20 |
return audio_path
|
21 |
|
22 |
+
# Streamlit App UI
|
23 |
+
st.title("Text-to-Speech Chatbot")
|
24 |
+
|
25 |
+
# Input text box
|
26 |
+
text = st.text_input("Enter text to convert to speech:", "")
|
27 |
+
|
28 |
+
if st.button("Generate Speech"):
|
29 |
+
if text.strip():
|
30 |
+
st.write("Generating speech...")
|
31 |
+
audio_file = text_to_speech(text)
|
32 |
+
st.audio(audio_file, format="audio/wav")
|
33 |
+
else:
|
34 |
+
st.warning("Please enter some text.")
|
35 |
|
36 |
+
st.write("Powered by SpeechBrain and Streamlit.")
|