Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
from io import BytesIO
|
5 |
+
import base64
|
6 |
+
|
7 |
+
# Define a function to generate text-to-speech audio
|
8 |
+
def generate_tts(text, voice_type, language):
|
9 |
+
# Hugging Face pipeline for text-to-speech in English
|
10 |
+
if language == "English":
|
11 |
+
model_id = "facebook/fastspeech2-en-ljspeech"
|
12 |
+
tts_pipeline = pipeline("text-to-speech", model=model_id)
|
13 |
+
audio_data = tts_pipeline(text, return_tensors=True).audio["array"]
|
14 |
+
return audio_data
|
15 |
+
# Google Text-to-Speech (gTTS) for Urdu
|
16 |
+
elif language == "Urdu":
|
17 |
+
tts = gTTS(text=text, lang="ur")
|
18 |
+
mp3_fp = BytesIO()
|
19 |
+
tts.write_to_fp(mp3_fp)
|
20 |
+
mp3_fp.seek(0)
|
21 |
+
return mp3_fp.read()
|
22 |
+
|
23 |
+
# Configure Streamlit app
|
24 |
+
st.title("Multilingual Text-to-Speech Application")
|
25 |
+
st.write("Generate speech from text in American English and Urdu with different voice styles.")
|
26 |
+
|
27 |
+
# Input fields for text and options
|
28 |
+
text = st.text_area("Enter your text:", "Hello, welcome to the Text-to-Speech app!")
|
29 |
+
voice_type = st.selectbox(
|
30 |
+
"Select voice style:", ["Adult", "Child", "Cartoon"]
|
31 |
+
)
|
32 |
+
language = st.selectbox("Select language:", ["English", "Urdu"])
|
33 |
+
|
34 |
+
if st.button("Generate Audio"):
|
35 |
+
with st.spinner("Generating audio..."):
|
36 |
+
try:
|
37 |
+
audio_data = generate_tts(text, voice_type, language)
|
38 |
+
if language == "English":
|
39 |
+
# Provide the audio as a downloadable file
|
40 |
+
st.audio(audio_data, format="audio/wav")
|
41 |
+
elif language == "Urdu":
|
42 |
+
b64 = base64.b64encode(audio_data).decode()
|
43 |
+
href = f'<a href="data:audio/mpeg;base64,{b64}" download="output.mp3">Download Urdu Audio</a>'
|
44 |
+
st.markdown(href, unsafe_allow_html=True)
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"Error: {e}")
|
47 |
+
|
48 |
+
st.caption("Powered by Hugging Face Transformers and Google TTS")
|