EmRa228 commited on
Commit
100a302
·
verified ·
1 Parent(s): 5f6b453

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -58
app.py CHANGED
@@ -16,79 +16,63 @@ async def text_to_speech(text, voice, rate, pitch):
16
  if not voice:
17
  return None, "Please select a voice."
18
 
19
- voice_short_name = voice.split(" - ")[0]
20
  rate_str = f"{rate:+d}%"
21
  pitch_str = f"{pitch:+d}Hz"
22
- communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
23
 
24
- # Save directly to mp3 file
25
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
26
- await communicate.save(tmp_file.name)
27
- return tmp_file.name, ""
28
 
29
- async def tts_interface(text, voice, rate, pitch):
30
- audio_path, warning_text = await text_to_speech(text, voice, rate, pitch)
31
- # audio_path: str|None; warning_text: str
32
- # Return audio_path, and a Gradio update dict for the Markdown
33
- return audio_path, gr.update(value=warning_text)
 
 
 
 
 
34
 
35
- async def create_demo():
36
- voices = await get_voices()
37
-
38
  with gr.Blocks(analytics_enabled=False) as demo:
39
  gr.Markdown("# 🎙️ Edge TTS Text-to-Speech")
40
 
41
- with gr.Row():
42
- with gr.Column(scale=1):
43
- gr.Markdown("## Text-to-Speech with Microsoft Edge TTS")
44
- gr.Markdown("""
45
- Convert text to speech using Microsoft Edge TTS.
46
- Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
47
- """)
48
- # (Promotional HTML omitted for brevity)
49
-
50
- with gr.Column(scale=1):
51
- # (Second promotional HTML omitted for brevity)
52
- pass
53
-
54
  with gr.Row():
55
  with gr.Column():
56
- text_input = gr.Textbox(label="Input Text", lines=5)
57
- voice_dropdown = gr.Dropdown(
58
- choices=[""] + list(voices.keys()),
59
- label="Select Voice",
60
- value=""
61
- )
62
- rate_slider = gr.Slider(
63
- minimum=-50, maximum=50, value=0,
64
- label="Speech Rate Adjustment (%)", step=1
65
- )
66
- pitch_slider = gr.Slider(
67
- minimum=-20, maximum=20, value=0,
68
- label="Pitch Adjustment (Hz)", step=1
69
- )
70
-
71
- generate_btn = gr.Button("Generate Speech", variant="primary")
72
- audio_output = gr.Audio(label="Generated Audio", type="filepath")
73
- warning_md = gr.Markdown("", label="Warning")
74
-
75
- generate_btn.click(
76
- fn=tts_interface,
77
- inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
78
- outputs=[audio_output, warning_md]
79
  )
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  gr.Markdown(
82
- "Experience the power of Edge TTS for text-to-speech conversion, "
83
- "and explore our advanced Text-to-Video Converter for even more creative possibilities!"
84
  )
85
 
86
  return demo
87
 
88
- async def main():
89
- demo = await create_demo()
90
- demo.queue(default_concurrency_limit=50)
91
- demo.launch(show_api=False)
92
-
93
  if __name__ == "__main__":
94
- asyncio.run(main())
 
 
 
16
  if not voice:
17
  return None, "Please select a voice."
18
 
19
+ short_name = voice.split(" - ")[0]
20
  rate_str = f"{rate:+d}%"
21
  pitch_str = f"{pitch:+d}Hz"
22
+ comm = edge_tts.Communicate(text, short_name, rate=rate_str, pitch=pitch_str)
23
 
24
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
25
+ await comm.save(tmp.name)
26
+ return tmp.name, ""
 
27
 
28
+ def tts_interface(text, voice, rate, pitch):
29
+ # Run asyncio in sync function
30
+ audio_path, warning_text = asyncio.get_event_loop().run_until_complete(
31
+ text_to_speech(text, voice, rate, pitch)
32
+ )
33
+ # Return the file path (or None) and the warning string
34
+ return audio_path, warning_text
35
+
36
+ def build_ui():
37
+ voices = asyncio.get_event_loop().run_until_complete(get_voices())
38
 
 
 
 
39
  with gr.Blocks(analytics_enabled=False) as demo:
40
  gr.Markdown("# 🎙️ Edge TTS Text-to-Speech")
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  with gr.Row():
43
  with gr.Column():
44
+ gr.Markdown("## Text-to-Speech with Microsoft Edge TTS")
45
+ gr.Markdown(
46
+ "Convert text to speech using Microsoft Edge TTS. "
47
+ "Adjust rate/pitch: 0 is default, +/− to change."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  )
49
 
50
+ with gr.Row():
51
+ text_input = gr.Textbox(label="Input Text", lines=5)
52
+ voice_dropdown = gr.Dropdown(
53
+ choices=list(voices.keys()),
54
+ label="Select Voice"
55
+ )
56
+ rate_slider = gr.Slider(-50, 50, value=0, label="Rate (%)")
57
+ pitch_slider = gr.Slider(-20, 20, value=0, label="Pitch (Hz)")
58
+
59
+ generate_btn = gr.Button("Generate Speech")
60
+ audio_out = gr.Audio(type="filepath", label="Output Audio")
61
+ warning_md = gr.Markdown("", label="Warning")
62
+
63
+ generate_btn.click(
64
+ fn=tts_interface,
65
+ inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
66
+ outputs=[audio_out, warning_md]
67
+ )
68
+
69
  gr.Markdown(
70
+ "Try our Text-to-Video converter, or tweak rate/pitch for the perfect delivery!"
 
71
  )
72
 
73
  return demo
74
 
 
 
 
 
 
75
  if __name__ == "__main__":
76
+ ui = build_ui()
77
+ # Launch synchronously—no .queue(), so no internal API schema error
78
+ ui.launch()