import gradio as gr from gtts import gTTS import io import os import time from gtts.lang import _main_langs from docx import Document # Directory for audio files AUDIO_DIR = 'audio_files' # Maximum file age in seconds (24 hours) MAX_FILE_AGE = 24 * 60 * 60 # Function to convert text to speech using gTTS def text_to_speech(text, lang, tld): lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()} lang_code = lang_codes[lang] tts = gTTS(text, lang=lang_code, tld=tld) fp = io.BytesIO() tts.write_to_fp(fp) fp.seek(0) os.makedirs(AUDIO_DIR, exist_ok=True) file_name = str(time.time()) + '.mp3' file_path = os.path.join(AUDIO_DIR, file_name) with open(file_path, 'wb') as f: f.write(fp.read()) delete_old_audio_files() return file_path, file_path # Function to delete old audio files def delete_old_audio_files(): now = time.time() for file_name in os.listdir(AUDIO_DIR): file_path = os.path.join(AUDIO_DIR, file_name) if now - os.path.getmtime(file_path) > MAX_FILE_AGE: os.remove(file_path) # Function to convert TXT file to speech def txt_to_speech(file, lang, tld): with open(file.name, 'r') as f: text = f.read() return text_to_speech(text, lang, tld) # Function to convert DOCX file to speech def docx_to_speech(file, lang, tld): doc = Document(file.name) text = "\n".join([para.text for para in doc.paragraphs]) return text_to_speech(text, lang, tld) # Create beautiful Gradio interface with gr.Blocks(css=""" .gradio-container { background: linear-gradient(to right, #f6f8fa, #e9ecef); } .tabs { border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .input-container { background: white; padding: 20px; border-radius: 8px; margin: 10px 0; } .button { background: #007bff; color: white; border-radius: 5px; padding: 10px 20px; transition: all 0.3s ease; } .button:hover { background: #0056b3; transform: translateY(-2px); } """) as iface: gr.Markdown( """ # 🎙️ Text to Speech Converter By LiaqatEagle ### Convert your text into natural-sounding speech """ ) with gr.Tabs(elem_classes="tabs") as tabs: with gr.Tab("✍️ Text to Speech"): with gr.Column(elem_classes="input-container"): text_input = gr.Textbox( lines=10, label="Enter your text here:", placeholder="Type or paste your text here..." ) with gr.Row(): lang_input = gr.Dropdown( choices=list(_main_langs().values()), label="Select Language:", value="English" ) tld_input = gr.Dropdown( choices=["com", "co.uk", "ca"], label="Select TLD:", value="com" ) convert_btn = gr.Button("Convert", elem_classes="button") audio_output = gr.Audio(label="Audio Output") file_output = gr.File(label="Download Audio") convert_btn.click( fn=text_to_speech, inputs=[text_input, lang_input, tld_input], outputs=[audio_output, file_output] ) with gr.Tab("📄 TXT to Speech"): with gr.Column(elem_classes="input-container"): file_input = gr.File(label="Upload your TXT file") with gr.Row(): lang_input_file = gr.Dropdown( choices=list(_main_langs().values()), label="Select Language:", value="English" ) tld_input_file = gr.Dropdown( choices=["com", "co.uk", "ca"], label="Select TLD:", value="com" ) convert_btn_file = gr.Button("Convert", elem_classes="button") audio_output_file = gr.Audio(label="Audio Output") file_output_file = gr.File(label="Download Audio") convert_btn_file.click( fn=txt_to_speech, inputs=[file_input, lang_input_file, tld_input_file], outputs=[audio_output_file, file_output_file] ) with gr.Tab("📝 DOCX to Speech"): with gr.Column(elem_classes="input-container"): docx_file_input = gr.File(label="Upload your DOCX file") with gr.Row(): lang_input_docx = gr.Dropdown( choices=list(_main_langs().values()), label="Select Language:", value="English" ) tld_input_docx = gr.Dropdown( choices=["com", "co.uk", "ca"], label="Select TLD:", value="com" ) convert_btn_docx = gr.Button("Convert", elem_classes="button") audio_output_docx = gr.Audio(label="Audio Output") file_output_docx = gr.File(label="Download Audio") convert_btn_docx.click( fn=docx_to_speech, inputs=[docx_file_input, lang_input_docx, tld_input_docx], outputs=[audio_output_docx, file_output_docx] ) # Enable queue and launch the interface iface.queue() iface.launch()