Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,19 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
3 |
import uuid
|
4 |
-
import time
|
5 |
-
import logging
|
6 |
-
from gtts import gTTS, gTTSError
|
7 |
-
from fastapi import FastAPI # Import FastAPI for mounting
|
8 |
|
9 |
-
|
10 |
-
logging.basicConfig(level=logging.INFO)
|
11 |
-
logger = logging.getLogger(__name__)
|
12 |
-
|
13 |
-
# Define a temporary directory for audio files if needed
|
14 |
-
# Gradio often handles temporary files well, but explicit control can be useful.
|
15 |
-
TEMP_DIR = "temp_audio_gradio"
|
16 |
-
os.makedirs(TEMP_DIR, exist_ok=True)
|
17 |
-
|
18 |
-
# Supported languages for the dropdown (add more as needed)
|
19 |
-
# You can find codes here: https://gtts.readthedocs.io/en/latest/module.html#languages-gtts-lang
|
20 |
-
SUPPORTED_LANGUAGES = {
|
21 |
-
"English": "en",
|
22 |
-
"Spanish": "es",
|
23 |
-
"French": "fr",
|
24 |
-
"German": "de",
|
25 |
-
"Italian": "it",
|
26 |
-
"Portuguese": "pt",
|
27 |
-
"Dutch": "nl",
|
28 |
-
"Russian": "ru",
|
29 |
-
"Japanese": "ja",
|
30 |
-
"Korean": "ko",
|
31 |
-
"Chinese (Mandarin/Simplified)": "zh-cn",
|
32 |
-
"Chinese (Mandarin/Traditional)": "zh-tw",
|
33 |
-
"Hindi": "hi",
|
34 |
-
"Arabic": "ar",
|
35 |
-
}
|
36 |
-
LANGUAGE_NAMES = list(SUPPORTED_LANGUAGES.keys())
|
37 |
-
|
38 |
-
# --- Core gTTS Function for Gradio ---
|
39 |
-
def generate_gtts_audio(text_input: str, language_name: str):
|
40 |
-
"""
|
41 |
-
Takes text and language name, generates MP3 using gTTS, saves it temporarily,
|
42 |
-
and returns the filepath for the Gradio Audio component.
|
43 |
-
"""
|
44 |
-
if not text_input or not text_input.strip():
|
45 |
-
# Raise a Gradio-specific error to show in the UI
|
46 |
-
raise gr.Error("Please enter some text to synthesize.")
|
47 |
-
|
48 |
-
if not language_name or language_name not in SUPPORTED_LANGUAGES:
|
49 |
-
raise gr.Error(f"Invalid language selected: {language_name}")
|
50 |
-
|
51 |
-
lang_code = SUPPORTED_LANGUAGES[language_name]
|
52 |
|
53 |
-
|
54 |
-
|
|
|
55 |
|
|
|
|
|
56 |
try:
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Generate a unique filename for the temporary MP3 file
|
61 |
-
filename = f"gtts_speech_{uuid.uuid4()}.mp3"
|
62 |
-
filepath = os.path.join(TEMP_DIR, filename)
|
63 |
-
|
64 |
-
# Save the audio file
|
65 |
-
tts.save(filepath)
|
66 |
-
|
67 |
-
synthesis_time = time.time() - start_synth_time
|
68 |
-
logger.info(f"gTTS audio saved to '{filepath}' in {synthesis_time:.2f} seconds.")
|
69 |
-
|
70 |
-
# Return the path to the generated audio file
|
71 |
-
# Gradio's Audio component with type="filepath" will handle serving this file.
|
72 |
-
return filepath
|
73 |
-
|
74 |
-
except gTTSError as e:
|
75 |
-
logger.error(f"gTTS Error during generation: {e}", exc_info=True)
|
76 |
-
raise gr.Error(f"gTTS failed to generate speech. Error: {e}")
|
77 |
except Exception as e:
|
78 |
-
|
79 |
-
raise gr.Error(f"An unexpected server error occurred. Error: {str(e)}")
|
80 |
-
|
81 |
-
# --- Create Gradio Interface ---
|
82 |
-
iface = gr.Interface(
|
83 |
-
fn=generate_gtts_audio,
|
84 |
-
inputs=[
|
85 |
-
gr.Textbox(
|
86 |
-
label="Text to Synthesize",
|
87 |
-
placeholder="Enter the text you want to convert to speech...",
|
88 |
-
lines=4
|
89 |
-
),
|
90 |
-
gr.Dropdown(
|
91 |
-
label="Language",
|
92 |
-
choices=LANGUAGE_NAMES,
|
93 |
-
value="English", # Default language
|
94 |
-
info="Select the language for the speech."
|
95 |
-
)
|
96 |
-
],
|
97 |
-
outputs=gr.Audio(
|
98 |
-
label="Generated Speech (MP3)",
|
99 |
-
type="filepath" # Gradio handles serving the file from the returned path
|
100 |
-
),
|
101 |
-
title="Text-to-Speech with gTTS",
|
102 |
-
description="Enter text and select a language to generate an MP3 audio file using Google Text-to-Speech.",
|
103 |
-
examples=[
|
104 |
-
["Hello, this is a demonstration of the gTTS library.", "English"],
|
105 |
-
["Bonjour le monde, ceci est un test.", "French"],
|
106 |
-
["Hola mundo, esto es un ejemplo en español.", "Spanish"],
|
107 |
-
],
|
108 |
-
allow_flagging="never", # Disable the flagging feature if not needed
|
109 |
-
# You can add custom CSS or themes here if desired
|
110 |
-
# theme=gr.themes.Default()
|
111 |
-
)
|
112 |
-
|
113 |
-
# --- Setup FastAPI App (Optional, but standard for Spaces) ---
|
114 |
-
# This allows Gradio to be served alongside other potential FastAPI endpoints.
|
115 |
-
app = FastAPI()
|
116 |
-
|
117 |
-
# --- Mount the Gradio Interface onto the FastAPI app ---
|
118 |
-
# The Gradio UI will be available at the '/ ' route of your Space URL
|
119 |
-
app = gr.mount_gradio_app(app, iface, path="/")
|
120 |
-
|
121 |
-
# --- Optional: Add a simple health check for FastAPI ---
|
122 |
-
@app.get("/health", tags=["System"])
|
123 |
-
async def health_check():
|
124 |
-
return {"status": "ok", "message": "Gradio service running"}
|
125 |
-
|
126 |
-
|
127 |
-
# --- How to Run Locally (for testing) ---
|
128 |
-
# if __name__ == "__main__":
|
129 |
-
# # When running locally, Gradio's launch() is often simpler
|
130 |
-
# # iface.launch(server_name="127.0.0.1", server_port=7860)
|
131 |
-
|
132 |
-
# # Or, if you want to test the FastAPI mounting locally:
|
133 |
-
# import uvicorn
|
134 |
-
# uvicorn.run(app, host="127.0.0.1", port=8000)
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
3 |
+
from gtts import gTTS
|
4 |
import uuid
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
@app.get("/")
|
9 |
+
def root():
|
10 |
+
return {"message": "Welcome to gTTS API."}
|
11 |
|
12 |
+
@app.get("/tts")
|
13 |
+
def tts(text: str = Query(..., min_length=1, max_length=500)):
|
14 |
try:
|
15 |
+
filename = f"/tmp/{uuid.uuid4().hex}.mp3"
|
16 |
+
gTTS(text).save(filename)
|
17 |
+
return FileResponse(filename, media_type="audio/mpeg", filename="speech.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
except Exception as e:
|
19 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|