Musawir19 commited on
Commit
16aac4b
·
verified ·
1 Parent(s): e03bd31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import gradio as gr
3
+ from speechbrain.pretrained import Tacotron2, HIFIGAN
4
+ from scipy.io.wavfile import write
5
+
6
+ # Load TTS and vocoder models
7
+ tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmpdir_tts")
8
+ hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder")
9
+
10
+ # Function to generate speech from text
11
+ def text_to_speech(text):
12
+ mel_output, mel_length, alignment = tacotron2.encode_text(text)
13
+ waveforms, _, _ = hifi_gan.decode_batch(mel_output)
14
+ audio_path = "tts_output.wav"
15
+ write(audio_path, 22050, waveforms.squeeze(1).cpu().numpy())
16
+ return audio_path
17
+
18
+ # Gradio interface for TTS
19
+ def chatbot_response(text):
20
+ audio_path = text_to_speech(text)
21
+ return audio_path
22
+
23
+ gr_interface = gr.Interface(fn=chatbot_response, inputs="text", outputs="audio")
24
+
25
+ # Streamlit app setup
26
+ st.title("Text-to-Speech Chatbot")
27
+ st.write("This application converts text into speech using SpeechBrain.")
28
+
29
+ # Embed Gradio interface in Streamlit
30
+ with st.spinner("Loading Gradio interface..."):
31
+ gr_interface.launch(share=False, inbrowser=True)