Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import edge_tts | |
| import asyncio | |
| import tempfile | |
| import os | |
| # Custom CSS for clean, modern appearance | |
| CUSTOM_CSS = """ | |
| body { | |
| background-color: #ffffff; /* White background */ | |
| color: #333333; /* Dark text */ | |
| font-family: 'Poppins', sans-serif; | |
| } | |
| .gradio-container { | |
| background-color: #ffffff; | |
| border-radius: 15px; | |
| padding: 20px; | |
| border: 1px solid #e0e0e0; | |
| } | |
| .gr-button { | |
| background-color: #FFA500 !important; /* Orange buttons */ | |
| border: none !important; | |
| color: #ffffff !important; /* White text */ | |
| font-size: 16px; | |
| font-weight: bold; | |
| border-radius: 8px; | |
| padding: 10px 20px; | |
| transition: all 0.3s ease-in-out; | |
| } | |
| .gr-button:hover { | |
| background-color: #ff8c00 !important; /* Darker orange on hover */ | |
| } | |
| .gr-slider, .gr-dropdown, .gr-textbox { | |
| background-color: #f9f9f9 !important; /* Light input background */ | |
| border-color: #d1d1d1 !important; | |
| color: #333333 !important; | |
| } | |
| .gr-slider .gr-label, .gr-dropdown .gr-label, .gr-textbox .gr-label { | |
| color: #333333 !important; /* Darker labels */ | |
| } | |
| h1, h2, h3, p, label { | |
| color: #333333 !important; | |
| } | |
| a { | |
| color: #FFA500; | |
| text-decoration: none; | |
| font-weight: bold; | |
| } | |
| a:hover { | |
| color: #ff8c00; | |
| } | |
| footer { | |
| display: none; /* Hide default footer */ | |
| } | |
| """ | |
| async def get_voices(): | |
| voices = await edge_tts.list_voices() | |
| voice_dict = {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices} | |
| return voice_dict | |
| async def text_to_speech(text, voice, rate, pitch): | |
| if not text.strip(): | |
| return None, "Please enter text to convert." | |
| if not voice: | |
| return None, "Please select a voice." | |
| voice_short_name = voice.split(" - ")[0] | |
| rate_str = f"{rate:+d}%" | |
| pitch_str = f"{pitch:+d}Hz" | |
| communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
| tmp_path = tmp_file.name | |
| await communicate.save(tmp_path) | |
| return tmp_path, None | |
| async def tts_interface(text, voice, rate, pitch): | |
| audio, warning = await text_to_speech(text, voice, rate, pitch) | |
| if warning: | |
| return audio, gr.Warning(warning) | |
| return audio, None | |
| async def create_demo(): | |
| voices = await get_voices() | |
| default_voice = "en-US-AvaNeural - en-US (Female)" | |
| description = """ | |
| Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease. | |
| Take your content creation to the next level with our cutting-edge Text-to-Video Converter! | |
| Transform your words into stunning, professional-quality videos in just a few clicks. | |
| β¨ Features: | |
| β’ Convert text to engaging videos with customizable visuals | |
| β’ Perfect for creating audiobooks, storytelling, and language learning materials | |
| β’ Ideal for educators, content creators, and language enthusiasts | |
| For mor information [Click here to try our blog now!](https://ruslanmv.com/) | |
| """ | |
| demo = gr.Interface( | |
| fn=tts_interface, | |
| inputs=[ | |
| gr.Textbox(label="Input Text", lines=5, placeholder="Type your text here..."), | |
| gr.Dropdown(choices=["", default_voice] + list(voices.keys()), label="Select Voice", value=default_voice), | |
| gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1), | |
| gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1) | |
| ], | |
| outputs=[ | |
| gr.Audio(label="Generated Audio", type="filepath"), | |
| gr.Markdown(label="Warning", visible=False) | |
| ], | |
| title="π Edge TTS Text-to-Speech Converter", | |
| description=description, | |
| article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!", | |
| analytics_enabled=False, | |
| allow_flagging="manual", | |
| api_name=None, | |
| theme="default" | |
| ) | |
| demo.css = CUSTOM_CSS | |
| return demo | |
| async def main(): | |
| demo = await create_demo() | |
| demo.queue(default_concurrency_limit=5) | |
| demo.launch(show_api=False) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |