EmRa228 commited on
Commit
ac7fa1f
·
verified ·
1 Parent(s): 6b78aa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -3,7 +3,7 @@ import edge_tts
3
  import asyncio
4
  import tempfile
5
 
6
- # 1) Fetch voices once, synchronously at startup
7
  def load_voices():
8
  loop = asyncio.get_event_loop()
9
  voices = loop.run_until_complete(edge_tts.list_voices())
@@ -14,14 +14,14 @@ def load_voices():
14
 
15
  VOICES = load_voices()
16
 
17
- # 2) Async function to drive Edge TTS
18
  async def _text_to_speech(text, short_name, rate_str, pitch_str):
19
- communicate = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
20
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
21
- await communicate.save(tmp.name)
22
  return tmp.name
23
 
24
- # 3) Sync wrapper for the click callback
25
  def tts_interface(text, voice_choice, rate, pitch):
26
  if not text.strip():
27
  return None, "🚨 Please enter some text."
@@ -31,7 +31,6 @@ def tts_interface(text, voice_choice, rate, pitch):
31
  rate_str = f"{rate:+d}%"
32
  pitch_str = f"{pitch:+d}Hz"
33
  try:
34
- # Run the async TTS call synchronously
35
  audio_path = asyncio.get_event_loop().run_until_complete(
36
  _text_to_speech(text, short_name, rate_str, pitch_str)
37
  )
@@ -39,35 +38,34 @@ def tts_interface(text, voice_choice, rate, pitch):
39
  except Exception as e:
40
  return None, f"❌ TTS failed: {e}"
41
 
42
- # 4) Build the Gradio Blocks UI
43
  def create_demo():
44
  with gr.Blocks(analytics_enabled=False) as demo:
45
  gr.Markdown("# 🎙️ Edge TTS on Hugging Face Spaces")
46
 
47
- with gr.Row():
48
- gr.Markdown("""
49
- **Convert your text to speech** using Microsoft Edge's neural voices.
50
- Adjust rate and pitch to fine-tune the output.
51
- """)
52
 
53
  with gr.Row():
54
- txt = gr.Textbox(label="Input Text", lines=5, placeholder="Type something…")
55
- vox = gr.Dropdown(choices=list(VOICES.keys()), label="Voice")
56
- rate = gr.Slider(-50, 50, value=0, label="Rate (%)")
57
  pitch = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
58
 
59
- btn = gr.Button("Generate Speech")
60
  audio_out = gr.Audio(type="filepath", label="Audio Output")
61
- warn_md = gr.Markdown("", label="Warnings / Errors")
62
 
63
- # IMPORTANT: wire queue here so the frontend finds /api/predict
64
  btn.click(
65
  fn=tts_interface,
66
  inputs=[txt, vox, rate, pitch],
67
  outputs=[audio_out, warn_md]
68
  )
69
 
70
- # Enable the queue on the entire app
71
  demo.queue()
72
 
73
  return demo
@@ -75,5 +73,4 @@ Adjust rate and pitch to fine-tune the output.
75
  # 5) Launch
76
  if __name__ == "__main__":
77
  demo = create_demo()
78
- # On Spaces, this will serve on 0.0.0.0:7860 automatically
79
- demo.launch()
 
3
  import asyncio
4
  import tempfile
5
 
6
+ # 1) Load voices once at startup
7
  def load_voices():
8
  loop = asyncio.get_event_loop()
9
  voices = loop.run_until_complete(edge_tts.list_voices())
 
14
 
15
  VOICES = load_voices()
16
 
17
+ # 2) Async TTS worker
18
  async def _text_to_speech(text, short_name, rate_str, pitch_str):
19
+ comm = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
20
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
21
+ await comm.save(tmp.name)
22
  return tmp.name
23
 
24
+ # 3) Synchronous wrapper for Gradio callback
25
  def tts_interface(text, voice_choice, rate, pitch):
26
  if not text.strip():
27
  return None, "🚨 Please enter some text."
 
31
  rate_str = f"{rate:+d}%"
32
  pitch_str = f"{pitch:+d}Hz"
33
  try:
 
34
  audio_path = asyncio.get_event_loop().run_until_complete(
35
  _text_to_speech(text, short_name, rate_str, pitch_str)
36
  )
 
38
  except Exception as e:
39
  return None, f"❌ TTS failed: {e}"
40
 
41
+ # 4) Build Gradio Blocks UI
42
  def create_demo():
43
  with gr.Blocks(analytics_enabled=False) as demo:
44
  gr.Markdown("# 🎙️ Edge TTS on Hugging Face Spaces")
45
 
46
+ gr.Markdown(
47
+ "**Convert your text to speech** using Microsoft Edge's neural voices. "
48
+ "Adjust rate and pitch to fine-tune the output."
49
+ )
 
50
 
51
  with gr.Row():
52
+ txt = gr.Textbox(label="Input Text", lines=5, placeholder="Type something…")
53
+ vox = gr.Dropdown(choices=list(VOICES.keys()), label="Voice")
54
+ rate = gr.Slider(-50, 50, value=0, label="Rate (%)")
55
  pitch = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
56
 
57
+ btn = gr.Button("Generate Speech")
58
  audio_out = gr.Audio(type="filepath", label="Audio Output")
59
+ warn_md = gr.Markdown("", label="Warnings / Errors")
60
 
61
+ # Wire the callback and register the /api endpoints
62
  btn.click(
63
  fn=tts_interface,
64
  inputs=[txt, vox, rate, pitch],
65
  outputs=[audio_out, warn_md]
66
  )
67
 
68
+ # Enable queuing so that Gradio registers its API
69
  demo.queue()
70
 
71
  return demo
 
73
  # 5) Launch
74
  if __name__ == "__main__":
75
  demo = create_demo()
76
+ demo.launch()