Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def text_to_speech(text, lang="en"):
|
| 6 |
+
"""
|
| 7 |
+
Convert text to speech using gTTS and return the audio file path.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
text (str): The text to convert to speech.
|
| 11 |
+
lang (str): Language code (default is 'en' for English).
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
str: Path to the generated audio file.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
# Create gTTS object
|
| 18 |
+
tts = gTTS(text=text, lang=lang, slow=False)
|
| 19 |
+
|
| 20 |
+
# Save the audio file
|
| 21 |
+
output_file = "output.mp3"
|
| 22 |
+
tts.save(output_file)
|
| 23 |
+
|
| 24 |
+
return output_file
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"An error occurred: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Define Gradio interface
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=text_to_speech,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Textbox(label="Enter text to convert to speech", placeholder="Type your text here..."),
|
| 34 |
+
gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en")
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.Audio(label="Generated Speech"),
|
| 37 |
+
title="Text-to-Speech with gTTS",
|
| 38 |
+
description="Enter text and select a language to generate speech."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the app
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|