Spaces:
Build error
Build error
| import gradio as gr | |
| from gradio import Textbox, Radio, Audio | |
| from gtts import gTTS | |
| import pyttsx3 | |
| # Function to generate MP3 file using gTTS | |
| def generate_mp3(text, language, filename): | |
| tts = gTTS(text=text, lang=language) | |
| tts.save(filename) | |
| # Function to generate MP3 file using pyttsx3 | |
| def generate_mp3_pyttsx3(text, filename, voice): | |
| audio_engine = pyttsx3.init() | |
| audio_engine.setProperty('voice', voice) | |
| audio_engine.save_to_file(text, filename) | |
| audio_engine.runAndWait() | |
| print("Done") | |
| # Main function | |
| def text_to_speech(text, language, english_voice): | |
| filename = "output.mp3" | |
| if language == "English": | |
| print(english_voice) | |
| if english_voice == "Female Voice 1": | |
| print("chk1") | |
| english_voice = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0' | |
| generate_mp3_pyttsx3(text,filename, english_voice) | |
| elif english_voice == "Male Voice": | |
| print("chk2") | |
| english_voice = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0' | |
| generate_mp3_pyttsx3(text,filename, english_voice) | |
| else: | |
| generate_mp3(text, 'en', filename) | |
| else: | |
| generate_mp3(text, 'ur', filename) | |
| return filename | |
| # Gradio Interface | |
| inputs = [ | |
| Textbox(lines=5, label="Enter Text"), | |
| Radio(["English", "Urdu"], label="Select Language"), | |
| Radio(["Female Voice 1", "Male Voice", "Female Voice 2"], label="Select English Voice") | |
| ] | |
| outputs = Audio(label="Download MP3") | |
| gr.Interface(fn=text_to_speech, inputs=inputs, outputs=outputs, title="Text to Speech Converter").launch() | |