Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Initialize TTS pipeline
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def get_tts_pipeline():
|
| 9 |
+
return pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
|
| 10 |
+
|
| 11 |
+
tts = get_tts_pipeline()
|
| 12 |
+
|
| 13 |
+
# Streamlit app
|
| 14 |
+
st.title("Text-to-Speech (TTS) Application")
|
| 15 |
+
st.write("Convert text to speech using Hugging Face models!")
|
| 16 |
+
|
| 17 |
+
# Input text
|
| 18 |
+
input_text = st.text_area("Enter text below:", "Hello! Welcome to our TTS application.")
|
| 19 |
+
|
| 20 |
+
if st.button("Generate Speech"):
|
| 21 |
+
if input_text.strip():
|
| 22 |
+
# Generate speech
|
| 23 |
+
with st.spinner("Generating speech..."):
|
| 24 |
+
speech = tts(input_text)
|
| 25 |
+
|
| 26 |
+
# Convert numpy array to audio file
|
| 27 |
+
audio_data = io.BytesIO()
|
| 28 |
+
sf.write(audio_data, speech["audio"], samplerate=speech["sampling_rate"], format="wav")
|
| 29 |
+
audio_data.seek(0)
|
| 30 |
+
|
| 31 |
+
# Play audio
|
| 32 |
+
st.audio(audio_data, format="audio/wav")
|
| 33 |
+
else:
|
| 34 |
+
st.error("Please enter some text!")
|
| 35 |
+
|
| 36 |
+
st.markdown("**Created by [Your Name]**")
|