Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,15 @@
|
|
| 1 |
-
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from datetime import datetime
|
| 5 |
import streamlit as st
|
|
@@ -8,12 +18,16 @@ from openai import OpenAI
|
|
| 8 |
st.set_page_config(page_title="Urdu TTS - OpenAI", page_icon="🔊", layout="centered")
|
| 9 |
|
| 10 |
st.title("🔊 Urdu Text → Speech (OpenAI)")
|
| 11 |
-
st.caption("Type Urdu text and generate natural Urdu speech with OpenAI TTS.
|
| 12 |
|
| 13 |
-
# ---- API key
|
| 14 |
API_KEY = os.getenv("Key_1") or st.secrets.get("Key_1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if not API_KEY:
|
| 16 |
-
st.error("Missing Key_1. Go to Settings → Secrets in your Space and add it.")
|
| 17 |
st.stop()
|
| 18 |
|
| 19 |
client = OpenAI(api_key=API_KEY)
|
|
@@ -21,40 +35,39 @@ client = OpenAI(api_key=API_KEY)
|
|
| 21 |
# ---- Sidebar options ----
|
| 22 |
with st.sidebar:
|
| 23 |
st.header("Options")
|
| 24 |
-
|
| 25 |
-
use_custom = st.checkbox("Use custom OpenAI
|
| 26 |
-
custom_voice = ""
|
| 27 |
-
if use_custom
|
| 28 |
-
custom_voice = st.text_input("Custom voice_id", value="", help="Only works if your account has custom voices enabled")
|
| 29 |
-
else:
|
| 30 |
-
preset = st.selectbox("Built-in voice", options=voices, index=0)
|
| 31 |
out_name = st.text_input("Output filename (no extension)", "urdu_tts")
|
| 32 |
fmt = st.selectbox("Audio format", options=["mp3", "wav"], index=0)
|
| 33 |
|
| 34 |
-
# ----
|
| 35 |
default_text = "یہ ایک سادہ مثال ہے۔ یہاں اپنا متن لکھیں اور آڈیو حاصل کریں۔"
|
| 36 |
text = st.text_area("Urdu text", value=default_text, height=200, placeholder="یہاں اردو میں ٹیکسٹ لکھیں یا پیسٹ کریں…")
|
| 37 |
|
| 38 |
col1, col2 = st.columns(2)
|
| 39 |
with col1:
|
| 40 |
-
|
| 41 |
with col2:
|
| 42 |
clear_btn = st.button("🧹 Clear", use_container_width=True)
|
| 43 |
|
| 44 |
if clear_btn:
|
| 45 |
st.session_state.pop("audio_bytes", None)
|
|
|
|
| 46 |
st.experimental_rerun()
|
| 47 |
|
| 48 |
-
# ----
|
| 49 |
-
def
|
| 50 |
model = "gpt-4o-mini-tts"
|
| 51 |
ext = "mp3" if audio_format == "mp3" else "wav"
|
| 52 |
tmp_path = Path(f"/tmp/tts_{datetime.now().strftime('%H%M%S')}.{ext}")
|
|
|
|
| 53 |
with client.audio.speech.with_streaming_response.create(
|
| 54 |
model=model,
|
| 55 |
voice=voice,
|
| 56 |
input=urdu_text,
|
| 57 |
-
format=audio_format
|
| 58 |
) as resp:
|
| 59 |
resp.stream_to_file(tmp_path)
|
| 60 |
data = tmp_path.read_bytes()
|
|
@@ -64,25 +77,25 @@ def tts_openai(urdu_text: str, voice: str, audio_format: str) -> bytes:
|
|
| 64 |
pass
|
| 65 |
return data, ext
|
| 66 |
|
| 67 |
-
# ---- Generate
|
| 68 |
-
if
|
| 69 |
if not text.strip():
|
| 70 |
st.warning("براہ کرم اردو متن درج کریں")
|
| 71 |
else:
|
| 72 |
try:
|
| 73 |
-
voice_to_use =
|
| 74 |
if not voice_to_use:
|
| 75 |
-
st.warning("
|
| 76 |
else:
|
| 77 |
st.info("Generating Urdu speech with OpenAI TTS…")
|
| 78 |
-
audio_bytes, ext =
|
| 79 |
st.session_state["audio_bytes"] = audio_bytes
|
| 80 |
st.session_state["ext"] = ext
|
| 81 |
st.success("آڈیو تیار ہے")
|
| 82 |
except Exception as e:
|
| 83 |
st.error(f"کچھ مسئلہ آیا: {e}")
|
| 84 |
|
| 85 |
-
# ---- Preview &
|
| 86 |
if "audio_bytes" in st.session_state:
|
| 87 |
ext = st.session_state.get("ext", "mp3")
|
| 88 |
st.markdown("### ▶️ Preview")
|
|
@@ -92,12 +105,10 @@ if "audio_bytes" in st.session_state:
|
|
| 92 |
"⬇️ Download",
|
| 93 |
data=st.session_state["audio_bytes"],
|
| 94 |
file_name=fname,
|
| 95 |
-
mime="audio/mpeg" if ext == "mp3" else "audio/wav",
|
| 96 |
-
use_container_width=True
|
| 97 |
)
|
| 98 |
|
| 99 |
st.markdown("---")
|
| 100 |
-
st.caption(
|
| 101 |
-
|
| 102 |
-
"Urdu is supported by gpt-4o-mini-tts."
|
| 103 |
-
)
|
|
|
|
| 1 |
+
# --- Fix Streamlit permission issue BEFORE importing streamlit ---
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
+
# Put Streamlit state/config under /tmp (writeable on Spaces)
|
| 5 |
+
os.environ.setdefault("STREAMLIT_HOME", "/tmp/streamlit")
|
| 6 |
+
os.environ.setdefault("XDG_STATE_HOME", "/tmp")
|
| 7 |
+
os.environ.setdefault("XDG_CONFIG_HOME", "/tmp")
|
| 8 |
+
os.makedirs(os.environ["STREAMLIT_HOME"], exist_ok=True)
|
| 9 |
+
|
| 10 |
+
# Disable telemetry to avoid writes under root paths
|
| 11 |
+
os.environ.setdefault("STREAMLIT_BROWSER_GATHER_USAGE_STATS", "false")
|
| 12 |
+
|
| 13 |
from pathlib import Path
|
| 14 |
from datetime import datetime
|
| 15 |
import streamlit as st
|
|
|
|
| 18 |
st.set_page_config(page_title="Urdu TTS - OpenAI", page_icon="🔊", layout="centered")
|
| 19 |
|
| 20 |
st.title("🔊 Urdu Text → Speech (OpenAI)")
|
| 21 |
+
st.caption("Type Urdu text and generate natural Urdu speech with OpenAI TTS. Use a custom voice_id if your account has Voice access.")
|
| 22 |
|
| 23 |
+
# ---- Get API key (your secret name is Key_1). Fallback to manual entry. ----
|
| 24 |
API_KEY = os.getenv("Key_1") or st.secrets.get("Key_1")
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
st.header("API")
|
| 27 |
+
if not API_KEY:
|
| 28 |
+
st.warning("Secret `Key_1` not found. Paste your key temporarily (will not be saved to repo).")
|
| 29 |
+
API_KEY = st.text_input("OpenAI API key (sk-...)", type="password")
|
| 30 |
if not API_KEY:
|
|
|
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
client = OpenAI(api_key=API_KEY)
|
|
|
|
| 35 |
# ---- Sidebar options ----
|
| 36 |
with st.sidebar:
|
| 37 |
st.header("Options")
|
| 38 |
+
builtin_voices = ["alloy", "verse", "aria", "ballad", "cove", "luna", "sage"]
|
| 39 |
+
use_custom = st.checkbox("Use custom OpenAI voice_id", False)
|
| 40 |
+
custom_voice = st.text_input("Custom voice_id", value="", help="Requires access to custom voices") if use_custom else ""
|
| 41 |
+
preset_voice = st.selectbox("Built-in voice", builtin_voices, index=0) if not use_custom else None
|
|
|
|
|
|
|
|
|
|
| 42 |
out_name = st.text_input("Output filename (no extension)", "urdu_tts")
|
| 43 |
fmt = st.selectbox("Audio format", options=["mp3", "wav"], index=0)
|
| 44 |
|
| 45 |
+
# ---- Input ----
|
| 46 |
default_text = "یہ ایک سادہ مثال ہے۔ یہاں اپنا متن لکھیں اور آڈیو حاصل کریں۔"
|
| 47 |
text = st.text_area("Urdu text", value=default_text, height=200, placeholder="یہاں اردو میں ٹیکسٹ لکھیں یا پیسٹ کریں…")
|
| 48 |
|
| 49 |
col1, col2 = st.columns(2)
|
| 50 |
with col1:
|
| 51 |
+
make_audio = st.button("🎙️ Generate", use_container_width=True)
|
| 52 |
with col2:
|
| 53 |
clear_btn = st.button("🧹 Clear", use_container_width=True)
|
| 54 |
|
| 55 |
if clear_btn:
|
| 56 |
st.session_state.pop("audio_bytes", None)
|
| 57 |
+
st.session_state.pop("ext", None)
|
| 58 |
st.experimental_rerun()
|
| 59 |
|
| 60 |
+
# ---- TTS helper ----
|
| 61 |
+
def synth_openai(urdu_text: str, voice: str, audio_format: str):
|
| 62 |
model = "gpt-4o-mini-tts"
|
| 63 |
ext = "mp3" if audio_format == "mp3" else "wav"
|
| 64 |
tmp_path = Path(f"/tmp/tts_{datetime.now().strftime('%H%M%S')}.{ext}")
|
| 65 |
+
# Stream to file to avoid loading all in memory
|
| 66 |
with client.audio.speech.with_streaming_response.create(
|
| 67 |
model=model,
|
| 68 |
voice=voice,
|
| 69 |
input=urdu_text,
|
| 70 |
+
format=("mp3" if audio_format == "mp3" else "wav"),
|
| 71 |
) as resp:
|
| 72 |
resp.stream_to_file(tmp_path)
|
| 73 |
data = tmp_path.read_bytes()
|
|
|
|
| 77 |
pass
|
| 78 |
return data, ext
|
| 79 |
|
| 80 |
+
# ---- Generate ----
|
| 81 |
+
if make_audio:
|
| 82 |
if not text.strip():
|
| 83 |
st.warning("براہ کرم اردو متن درج کریں")
|
| 84 |
else:
|
| 85 |
try:
|
| 86 |
+
voice_to_use = (custom_voice.strip() if use_custom else preset_voice)
|
| 87 |
if not voice_to_use:
|
| 88 |
+
st.warning("Please choose a built-in voice or provide a custom voice_id.")
|
| 89 |
else:
|
| 90 |
st.info("Generating Urdu speech with OpenAI TTS…")
|
| 91 |
+
audio_bytes, ext = synth_openai(text.strip(), voice_to_use, fmt)
|
| 92 |
st.session_state["audio_bytes"] = audio_bytes
|
| 93 |
st.session_state["ext"] = ext
|
| 94 |
st.success("آڈیو تیار ہے")
|
| 95 |
except Exception as e:
|
| 96 |
st.error(f"کچھ مسئلہ آیا: {e}")
|
| 97 |
|
| 98 |
+
# ---- Preview & Download ----
|
| 99 |
if "audio_bytes" in st.session_state:
|
| 100 |
ext = st.session_state.get("ext", "mp3")
|
| 101 |
st.markdown("### ▶️ Preview")
|
|
|
|
| 105 |
"⬇️ Download",
|
| 106 |
data=st.session_state["audio_bytes"],
|
| 107 |
file_name=fname,
|
| 108 |
+
mime=("audio/mpeg" if ext == "mp3" else "audio/wav"),
|
| 109 |
+
use_container_width=True,
|
| 110 |
)
|
| 111 |
|
| 112 |
st.markdown("---")
|
| 113 |
+
st.caption("If your secret is named `Key_1`, keep it in Settings → Variables and secrets → Secrets. "
|
| 114 |
+
"This app stores Streamlit state under /tmp to avoid permission issues on Spaces.")
|
|
|
|
|
|