EngrMuhammadBilal commited on
Commit
47f59e0
·
verified ·
1 Parent(s): 6bb76af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -8,12 +8,12 @@ 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. If you have a custom OpenAI voice ID, you can use it.")
12
 
13
- # ---- API key ----
14
- API_KEY = os.getenv("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") or st.secrets.get("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")
15
  if not API_KEY:
16
- st.error("Missing OPENAI_API_KEY. In your Space go to Settings → Secrets and add it.")
17
  st.stop()
18
 
19
  client = OpenAI(api_key=API_KEY)
@@ -21,45 +21,34 @@ client = OpenAI(api_key=API_KEY)
21
  # ---- Sidebar options ----
22
  with st.sidebar:
23
  st.header("Options")
24
- st.caption("Pick a built-in voice or provide your own custom voice ID if you have access.")
25
- voice_presets = ["alloy", "verse", "aria", "ballad", "cove", "luna", "sage"]
26
  use_custom = st.checkbox("Use custom OpenAI voice ID", False)
27
  custom_voice = ""
28
  if use_custom:
29
- custom_voice = st.text_input("Custom voice_id", value="", help="Requires Voice access in your OpenAI account")
30
  else:
31
- preset = st.selectbox("Built-in voice", options=voice_presets, index=0)
32
  out_name = st.text_input("Output filename (no extension)", "urdu_tts")
33
- fmt = st.selectbox("Audio format", options=["mp3_44100_128", "wav"], index=0)
34
 
35
- sample = "یہ ایک سادہ مثال ہے۔ یہاں اپنا متن لکھیں اور آڈیو حاصل کریں۔"
36
- text = st.text_area("Urdu text", value=sample, height=200, placeholder="یہاں اردو میں ٹیکسٹ لکھیں یا پیسٹ کریں")
 
37
 
38
  col1, col2 = st.columns(2)
39
  with col1:
40
- make_audio = st.button("🎙️ Generate", use_container_width=True)
41
  with col2:
42
- clear = st.button("🧹 Clear", use_container_width=True)
43
 
44
- if clear:
45
  st.session_state.pop("audio_bytes", None)
46
  st.experimental_rerun()
47
 
48
- def tts_openai(urdu_text: str, voice: str, output_format: str) -> bytes:
49
- """
50
- Uses OpenAI TTS (gpt-4o-mini-tts) to synthesize speech.
51
- We write to a temp file using streaming response, then return bytes.
52
- """
53
  model = "gpt-4o-mini-tts"
54
- # Map friendly dropdown to OpenAI format
55
- # mp3_44100_128 is recommended for quality-size balance
56
- if output_format == "wav":
57
- audio_format = "wav"
58
- ext = "wav"
59
- else:
60
- audio_format = "mp3"
61
- ext = "mp3"
62
-
63
  tmp_path = Path(f"/tmp/tts_{datetime.now().strftime('%H%M%S')}.{ext}")
64
  with client.audio.speech.with_streaming_response.create(
65
  model=model,
@@ -68,7 +57,6 @@ def tts_openai(urdu_text: str, voice: str, output_format: str) -> bytes:
68
  format=audio_format
69
  ) as resp:
70
  resp.stream_to_file(tmp_path)
71
-
72
  data = tmp_path.read_bytes()
73
  try:
74
  tmp_path.unlink(missing_ok=True)
@@ -76,7 +64,8 @@ def tts_openai(urdu_text: str, voice: str, output_format: str) -> bytes:
76
  pass
77
  return data, ext
78
 
79
- if make_audio:
 
80
  if not text.strip():
81
  st.warning("براہ کرم اردو متن درج کریں")
82
  else:
@@ -85,7 +74,7 @@ if make_audio:
85
  if not voice_to_use:
86
  st.warning("Custom voice ID is empty. Either uncheck custom voice or provide a valid voice_id.")
87
  else:
88
- st.info("Generating speech with OpenAI TTS…")
89
  audio_bytes, ext = tts_openai(text.strip(), voice_to_use, fmt)
90
  st.session_state["audio_bytes"] = audio_bytes
91
  st.session_state["ext"] = ext
@@ -93,7 +82,7 @@ if make_audio:
93
  except Exception as e:
94
  st.error(f"کچھ مسئلہ آیا: {e}")
95
 
96
- # ---- Preview and download ----
97
  if "audio_bytes" in st.session_state:
98
  ext = st.session_state.get("ext", "mp3")
99
  st.markdown("### ▶️ Preview")
@@ -109,6 +98,6 @@ if "audio_bytes" in st.session_state:
109
 
110
  st.markdown("---")
111
  st.caption(
112
- "Notes: Built-in voices are not your personal voice. For an exact match you need OpenAI Voice access with a custom voice_id. "
113
- "Urdu is supported by gpt-4o-mini-tts. If the audio sounds too fast or slow, try the WAV format then adjust speed in an editor."
114
  )
 
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. If you have a custom voice ID, you can use it.")
12
 
13
+ # ---- API key from Hugging Face Secret ----
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
  # ---- Sidebar options ----
22
  with st.sidebar:
23
  st.header("Options")
24
+ voices = ["alloy", "verse", "aria", "ballad", "cove", "luna", "sage"]
 
25
  use_custom = st.checkbox("Use custom OpenAI voice ID", False)
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
+ # ---- Main input ----
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
+ run_btn = st.button("🎙️ Generate", use_container_width=True)
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
+ # ---- Helper function ----
49
+ def tts_openai(urdu_text: str, voice: str, audio_format: str) -> bytes:
 
 
 
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,
 
57
  format=audio_format
58
  ) as resp:
59
  resp.stream_to_file(tmp_path)
 
60
  data = tmp_path.read_bytes()
61
  try:
62
  tmp_path.unlink(missing_ok=True)
 
64
  pass
65
  return data, ext
66
 
67
+ # ---- Generate speech ----
68
+ if run_btn:
69
  if not text.strip():
70
  st.warning("براہ کرم اردو متن درج کریں")
71
  else:
 
74
  if not voice_to_use:
75
  st.warning("Custom voice ID is empty. Either uncheck custom voice or provide a valid voice_id.")
76
  else:
77
+ st.info("Generating Urdu speech with OpenAI TTS…")
78
  audio_bytes, ext = tts_openai(text.strip(), voice_to_use, fmt)
79
  st.session_state["audio_bytes"] = audio_bytes
80
  st.session_state["ext"] = ext
 
82
  except Exception as e:
83
  st.error(f"کچھ مسئلہ آیا: {e}")
84
 
85
+ # ---- Preview & download ----
86
  if "audio_bytes" in st.session_state:
87
  ext = st.session_state.get("ext", "mp3")
88
  st.markdown("### ▶️ Preview")
 
98
 
99
  st.markdown("---")
100
  st.caption(
101
+ "Notes: Built-in voices are not your personal voice. For your own cloned voice, you need access to custom OpenAI Voice IDs. "
102
+ "Urdu is supported by gpt-4o-mini-tts."
103
  )