Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import os
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
def text_to_speech(text, language="en"):
|
| 7 |
+
"""
|
| 8 |
+
Convert text to speech using gTTS and return the audio file.
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return "Error: Please enter valid text.", None
|
| 13 |
+
|
| 14 |
+
# Generate the speech using gTTS
|
| 15 |
+
tts = gTTS(text=text, lang=language)
|
| 16 |
+
audio_file = BytesIO()
|
| 17 |
+
tts.write_to_fp(audio_file)
|
| 18 |
+
audio_file.seek(0)
|
| 19 |
+
return "Text-to-Speech conversion successful!", audio_file
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error: {str(e)}", None
|
| 22 |
+
|
| 23 |
+
# Define the Gradio Interface
|
| 24 |
+
language_options = [("English", "en"), ("Spanish", "es"), ("French", "fr"), ("German", "de")]
|
| 25 |
+
|
| 26 |
+
interface = gr.Interface(
|
| 27 |
+
fn=text_to_speech,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Textbox(label="Enter Text", placeholder="Type your text here..."),
|
| 30 |
+
gr.Dropdown(choices=[lang[1] for lang in language_options], label="Select Language", value="en"),
|
| 31 |
+
],
|
| 32 |
+
outputs=[
|
| 33 |
+
gr.Textbox(label="Status"),
|
| 34 |
+
gr.Audio(label="Generated Speech"),
|
| 35 |
+
],
|
| 36 |
+
title="AI Text-to-Speech Web App",
|
| 37 |
+
description="Convert text into natural-sounding speech using AI. Select a language, type text, and download the audio!",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Launch the interface
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
interface.launch()
|