|
import gradio as gr |
|
from gtts import gTTS |
|
import os |
|
|
|
def text_to_speech(text, lang="en"): |
|
""" |
|
Convert text to speech using gTTS and return the audio file path. |
|
|
|
Args: |
|
text (str): The text to convert to speech. |
|
lang (str): Language code (default is 'en' for English). |
|
|
|
Returns: |
|
str: Path to the generated audio file. |
|
""" |
|
try: |
|
|
|
tts = gTTS(text=text, lang=lang, slow=False) |
|
|
|
|
|
output_file = "output.mp3" |
|
tts.save(output_file) |
|
|
|
return output_file |
|
|
|
except Exception as e: |
|
return f"An error occurred: {str(e)}" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=text_to_speech, |
|
inputs=[ |
|
gr.Textbox(label="Enter text to convert to speech", placeholder="Type your text here..."), |
|
gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en") |
|
], |
|
outputs=gr.Audio(label="Generated Speech"), |
|
title="Text-to-Speech with gTTS", |
|
description="Enter text and select a language to generate speech." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |