Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Urdu Voice Over", page_icon="🔊", layout="centered")
|
| 7 |
+
|
| 8 |
+
st.title("🔊 Urdu Text → Voice")
|
| 9 |
+
st.caption("Type or paste Urdu text and get an MP3 voice over. Works great on Hugging Face Spaces.")
|
| 10 |
+
|
| 11 |
+
# Sidebar options
|
| 12 |
+
with st.sidebar:
|
| 13 |
+
st.header("Options")
|
| 14 |
+
slow = st.checkbox("Slow reading", False)
|
| 15 |
+
file_basename = st.text_input("Output filename (without extension)", "urdu_voice")
|
| 16 |
+
st.markdown("---")
|
| 17 |
+
st.write("Tips")
|
| 18 |
+
st.caption("1) Paste clean Urdu text\n2) Use Slow reading if the voice feels fast\n3) Download the MP3 for editing")
|
| 19 |
+
|
| 20 |
+
# Main input
|
| 21 |
+
default_sample = "یہ ایک سادہ مثال ہے، آپ یہاں اپنا متن لکھیں اور آڈیو حاصل کریں۔"
|
| 22 |
+
text = st.text_area(
|
| 23 |
+
"Urdu text",
|
| 24 |
+
value=default_sample,
|
| 25 |
+
height=200,
|
| 26 |
+
placeholder="یہاں اردو میں ٹیکسٹ لکھیں یا پیسٹ کریں…"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def tts_to_bytes(urdu_text: str, slow_read: bool = False) -> bytes:
|
| 30 |
+
# gTTS automatically chunks long text
|
| 31 |
+
tts = gTTS(text=urdu_text, lang="ur", slow=slow_read)
|
| 32 |
+
buf = io.BytesIO()
|
| 33 |
+
tts.write_to_fp(buf)
|
| 34 |
+
buf.seek(0)
|
| 35 |
+
return buf.read()
|
| 36 |
+
|
| 37 |
+
col1, col2 = st.columns([1,1])
|
| 38 |
+
with col1:
|
| 39 |
+
make_audio = st.button("🎙️ Generate Voice", use_container_width=True)
|
| 40 |
+
with col2:
|
| 41 |
+
clear = st.button("🧹 Clear", use_container_width=True)
|
| 42 |
+
|
| 43 |
+
if clear:
|
| 44 |
+
st.session_state.pop("audio_bytes", None)
|
| 45 |
+
st.session_state.pop("last_text", None)
|
| 46 |
+
st.experimental_rerun()
|
| 47 |
+
|
| 48 |
+
if make_audio:
|
| 49 |
+
if not text.strip():
|
| 50 |
+
st.warning("براہ کرم آڈیو بنانے کے لیے اردو متن درج کریں")
|
| 51 |
+
else:
|
| 52 |
+
try:
|
| 53 |
+
audio_bytes = tts_to_bytes(text.strip(), slow_read=slow)
|
| 54 |
+
st.session_state["audio_bytes"] = audio_bytes
|
| 55 |
+
st.session_state["last_text"] = text.strip()
|
| 56 |
+
st.success("آڈیو تیار ہے")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
st.error(f"کچھ مسئلہ آیا: {e}")
|
| 59 |
+
|
| 60 |
+
# Playback and download
|
| 61 |
+
if "audio_bytes" in st.session_state:
|
| 62 |
+
st.markdown("### ▶️ Preview")
|
| 63 |
+
st.audio(st.session_state["audio_bytes"], format="audio/mp3")
|
| 64 |
+
|
| 65 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 66 |
+
base = file_basename.strip() or "urdu_voice"
|
| 67 |
+
fname = f"{base}_{timestamp}.mp3"
|
| 68 |
+
|
| 69 |
+
st.download_button(
|
| 70 |
+
label="⬇️ Download MP3",
|
| 71 |
+
data=st.session_state["audio_bytes"],
|
| 72 |
+
file_name=fname,
|
| 73 |
+
mime="audio/mpeg",
|
| 74 |
+
use_container_width=True
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
st.markdown("---")
|
| 78 |
+
st.caption(
|
| 79 |
+
"Note: This app uses gTTS for Urdu speech synthesis. If your Space is very busy or internet is restricted, synthesis can fail. Try again after a short while."
|
| 80 |
+
)
|