Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,189 +1,111 @@
|
|
1 |
-
import os
|
2 |
-
import uuid
|
3 |
-
import time
|
4 |
-
from pathlib import Path
|
5 |
import io
|
6 |
import logging
|
|
|
7 |
|
8 |
-
import
|
9 |
-
from
|
10 |
-
import
|
11 |
-
import
|
12 |
-
from fastapi import FastAPI, HTTPException, Body, BackgroundTasks
|
13 |
-
from fastapi.responses import StreamingResponse # To send binary audio data
|
14 |
-
from pydantic import BaseModel
|
15 |
|
16 |
# --- Configuration ---
|
17 |
-
# Choose a TTS model from the Hugging Face Hub
|
18 |
-
MODEL_NAME = "espnet/kan-bayashi_ljspeech_vits" # Example model
|
19 |
-
# MODEL_NAME = "suno/bark-small"
|
20 |
-
|
21 |
-
# Directories
|
22 |
-
BASE_DIR = Path(__file__).parent
|
23 |
-
TEMP_AUDIO_DIR = BASE_DIR / "temp_audio" # For temporary storage before sending
|
24 |
-
|
25 |
-
# Ensure temporary audio directory exists
|
26 |
-
TEMP_AUDIO_DIR.mkdir(parents=True, exist_ok=True)
|
27 |
-
|
28 |
-
# Configure Logging
|
29 |
logging.basicConfig(level=logging.INFO)
|
30 |
logger = logging.getLogger(__name__)
|
31 |
|
32 |
# --- Pydantic Model for Request Body ---
|
33 |
class TTSRequest(BaseModel):
|
34 |
-
text: str
|
35 |
-
|
36 |
-
#
|
37 |
-
|
38 |
-
start_load_time = time.time()
|
39 |
-
tts_pipeline = None
|
40 |
-
try:
|
41 |
-
# Use GPU if available
|
42 |
-
if torch.cuda.is_available():
|
43 |
-
device = "cuda"
|
44 |
-
# Check for MPS (Apple Silicon) support if not CUDA
|
45 |
-
elif torch.backends.mps.is_available():
|
46 |
-
device = "mps"
|
47 |
-
else:
|
48 |
-
device = "cpu"
|
49 |
-
|
50 |
-
logger.info(f"Using device: {device}")
|
51 |
-
tts_pipeline = pipeline("text-to-speech", model=MODEL_NAME, device=device)
|
52 |
-
logger.info(f"Model '{MODEL_NAME}' loaded successfully in {time.time() - start_load_time:.2f} seconds.")
|
53 |
-
except Exception as e:
|
54 |
-
logger.error(f"FATAL: Could not load TTS model '{MODEL_NAME}'. Error: {e}", exc_info=True)
|
55 |
-
# The application can still run, but the /api/tts endpoint will fail until the model is loaded/fixed.
|
56 |
|
57 |
# --- Initialize FastAPI App ---
|
58 |
app = FastAPI(
|
59 |
-
title="
|
60 |
-
description=
|
61 |
-
version="1.0.0"
|
62 |
)
|
63 |
|
64 |
-
# --- Background Task for Cleanup ---
|
65 |
-
def cleanup_temp_file(filepath: Path):
|
66 |
-
"""Removes a file in the background."""
|
67 |
-
try:
|
68 |
-
if filepath.exists():
|
69 |
-
os.remove(filepath)
|
70 |
-
logger.info(f"Cleaned up temp file: {filepath.name}")
|
71 |
-
except OSError as e:
|
72 |
-
logger.error(f"Error deleting temp file {filepath.name}: {e}")
|
73 |
-
|
74 |
# --- API Endpoint for Text-to-Speech ---
|
75 |
@app.post(
|
76 |
"/api/tts",
|
77 |
tags=["TTS"],
|
78 |
-
summary="Generate Speech
|
79 |
-
description="""Send a JSON object with
|
80 |
-
Returns the generated speech as
|
81 |
responses={
|
82 |
200: {
|
83 |
-
"content": {"audio/
|
84 |
-
"description": "Successful response returning the
|
85 |
},
|
86 |
-
400: {"description": "Bad Request (e.g., empty text)"},
|
87 |
-
500: {"description": "Internal Server Error (e.g.,
|
88 |
-
503: {"description": "Service Unavailable (e.g., model not loaded)"},
|
89 |
},
|
90 |
)
|
91 |
-
async def
|
92 |
-
background_tasks: BackgroundTasks,
|
93 |
tts_request: TTSRequest = Body(...)
|
94 |
):
|
95 |
"""
|
96 |
-
Receives text via POST request
|
|
|
97 |
"""
|
98 |
-
if tts_pipeline is None:
|
99 |
-
raise HTTPException(status_code=503, detail="TTS Model is not available or failed to load.")
|
100 |
-
|
101 |
text = tts_request.text
|
|
|
|
|
|
|
102 |
if not text or not text.strip():
|
|
|
|
|
103 |
raise HTTPException(status_code=400, detail="Input text cannot be empty.")
|
104 |
|
105 |
-
logger.info(f"Received
|
106 |
start_synth_time = time.time()
|
107 |
|
108 |
try:
|
109 |
-
# --- Generate Audio ---
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
audio_data = output.get("audio")
|
114 |
-
sampling_rate = output.get("sampling_rate")
|
115 |
-
|
116 |
-
if audio_data is None or sampling_rate is None:
|
117 |
-
logger.error("TTS pipeline output missing 'audio' or 'sampling_rate'.")
|
118 |
-
raise ValueError("Invalid output from TTS pipeline.")
|
119 |
-
|
120 |
-
# Ensure NumPy array
|
121 |
-
if isinstance(audio_data, torch.Tensor):
|
122 |
-
# Ensure it's on CPU before converting to numpy
|
123 |
-
audio_data = audio_data.cpu().numpy()
|
124 |
-
if not isinstance(audio_data, np.ndarray):
|
125 |
-
logger.error(f"Unexpected audio data type: {type(audio_data)}")
|
126 |
-
raise TypeError(f"Expected audio data as NumPy array, got {type(audio_data)}")
|
127 |
-
|
128 |
-
# Normalize if float and outside [-1, 1] range (important for WAV)
|
129 |
-
if np.issubdtype(audio_data.dtype, np.floating):
|
130 |
-
max_val = np.max(np.abs(audio_data))
|
131 |
-
if max_val > 1.0:
|
132 |
-
audio_data = audio_data / max_val
|
133 |
-
# Convert to 16-bit integer format for standard WAV
|
134 |
-
audio_data = (audio_data * 32767).astype(np.int16)
|
135 |
-
elif not np.issubdtype(audio_data.dtype, np.integer):
|
136 |
-
logger.warning(f"Audio data is not float or int: {audio_data.dtype}. Attempting conversion to int16.")
|
137 |
-
# Attempt conversion if possible, might need adjustment based on model output
|
138 |
-
audio_data = audio_data.astype(np.int16)
|
139 |
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
synthesis_time = time.time() - start_synth_time
|
142 |
-
logger.info(f"
|
143 |
|
144 |
-
# ---
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
logger.info(f"Temporary audio saved to: {filepath.name}")
|
150 |
-
|
151 |
-
# Schedule the cleanup task to run after the response is sent
|
152 |
-
background_tasks.add_task(cleanup_temp_file, filepath)
|
153 |
-
|
154 |
-
# Return the file directly as a streaming response
|
155 |
-
return FileResponse(
|
156 |
-
path=filepath,
|
157 |
-
media_type="audio/wav",
|
158 |
-
filename=filename # Suggests a filename to the client
|
159 |
)
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
#
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
except Exception as e:
|
169 |
-
logger.error(f"
|
170 |
-
|
171 |
-
if 'filepath' in locals() and filepath.exists():
|
172 |
-
logger.info(f"Cleaning up temp file due to error: {filepath.name}")
|
173 |
-
os.remove(filepath)
|
174 |
-
raise HTTPException(status_code=500, detail=f"Failed to process speech request. Error: {str(e)}")
|
175 |
|
176 |
|
177 |
# --- Health Check Endpoint (Good Practice) ---
|
178 |
@app.get("/health", tags=["System"], summary="Check API Health")
|
179 |
async def health_check():
|
180 |
"""
|
181 |
-
Simple health check endpoint.
|
182 |
"""
|
183 |
-
if
|
184 |
-
|
185 |
-
#
|
186 |
-
|
|
|
|
|
|
|
187 |
|
188 |
# --- Root Endpoint (Optional Information) ---
|
189 |
@app.get("/", tags=["System"], summary="API Information")
|
@@ -192,41 +114,16 @@ async def read_root():
|
|
192 |
Provides basic information about the API.
|
193 |
"""
|
194 |
return {
|
195 |
-
"message": "Welcome to the
|
196 |
-
"
|
197 |
"tts_endpoint": "/api/tts",
|
198 |
"health_endpoint": "/health",
|
|
|
|
|
199 |
"documentation": "/docs" # Link to FastAPI auto-generated docs
|
200 |
}
|
201 |
|
202 |
-
# --- Optional: Add cleanup for *old* files on startup (if using FileResponse) ---
|
203 |
-
def cleanup_old_audio_files(max_age_seconds: int = 3600): # Clean files older than 1 hour
|
204 |
-
now = time.time()
|
205 |
-
count = 0
|
206 |
-
try:
|
207 |
-
for filename in os.listdir(TEMP_AUDIO_DIR):
|
208 |
-
filepath = TEMP_AUDIO_DIR / filename
|
209 |
-
if filepath.is_file() and filename.startswith("speech_") and filename.endswith(".wav"):
|
210 |
-
try:
|
211 |
-
file_mod_time = os.path.getmtime(filepath)
|
212 |
-
if (now - file_mod_time) > max_age_seconds:
|
213 |
-
os.remove(filepath)
|
214 |
-
logger.info(f"Startup cleanup: Removed old temp file {filename}")
|
215 |
-
count += 1
|
216 |
-
except OSError as e:
|
217 |
-
logger.warning(f"Startup cleanup: Error removing file {filename}: {e}")
|
218 |
-
if count > 0:
|
219 |
-
logger.info(f"Startup cleanup: Removed {count} old audio files.")
|
220 |
-
except Exception as e:
|
221 |
-
logger.error(f"Startup cleanup: Error during old file cleanup: {e}")
|
222 |
-
|
223 |
-
# Run cleanup on startup
|
224 |
-
cleanup_old_audio_files()
|
225 |
-
|
226 |
# --- How to Run Locally (for testing) ---
|
227 |
# if __name__ == "__main__":
|
228 |
# import uvicorn
|
229 |
-
#
|
230 |
-
# TEMP_AUDIO_DIR.mkdir(parents=True, exist_ok=True)
|
231 |
-
# cleanup_old_audio_files() # Run cleanup before starting server
|
232 |
-
# uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True) # Use reload=False for production testing
|
|
|
|
|
|
|
|
|
|
|
1 |
import io
|
2 |
import logging
|
3 |
+
import time
|
4 |
|
5 |
+
from fastapi import FastAPI, HTTPException, Body, Response
|
6 |
+
from fastapi.responses import StreamingResponse
|
7 |
+
from pydantic import BaseModel, Field # Field for adding validation/defaults
|
8 |
+
from gtts import gTTS, gTTSError
|
|
|
|
|
|
|
9 |
|
10 |
# --- Configuration ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
14 |
# --- Pydantic Model for Request Body ---
|
15 |
class TTSRequest(BaseModel):
|
16 |
+
text: str = Field(..., min_length=1, description="The text to be converted to speech.")
|
17 |
+
lang: str = Field("en", description="Language code for the speech (e.g., 'en', 'es', 'fr'). See gTTS documentation for supported languages.")
|
18 |
+
# Optional: Add tld if you need specific accents tied to Google domains
|
19 |
+
# tld: str = Field("com", description="Top-level domain for Google TTS endpoint (e.g., 'com', 'co.uk', 'com.au')")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# --- Initialize FastAPI App ---
|
22 |
app = FastAPI(
|
23 |
+
title="gTTS API Service",
|
24 |
+
description="A simple API service that uses gTTS (Google Text-to-Speech) to convert text into speech (MP3 audio).",
|
25 |
+
version="1.0.0",
|
26 |
)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# --- API Endpoint for Text-to-Speech ---
|
29 |
@app.post(
|
30 |
"/api/tts",
|
31 |
tags=["TTS"],
|
32 |
+
summary="Generate Speech using gTTS",
|
33 |
+
description="""Send a JSON object with 'text' and optionally 'lang' fields.
|
34 |
+
Returns the generated speech as an MP3 audio stream.""",
|
35 |
responses={
|
36 |
200: {
|
37 |
+
"content": {"audio/mpeg": {}}, # MP3 content type
|
38 |
+
"description": "Successful response returning the MP3 audio stream.",
|
39 |
},
|
40 |
+
400: {"description": "Bad Request (e.g., empty text, invalid language)"},
|
41 |
+
500: {"description": "Internal Server Error (e.g., gTTS failed)"},
|
|
|
42 |
},
|
43 |
)
|
44 |
+
async def generate_speech_gtts_api(
|
|
|
45 |
tts_request: TTSRequest = Body(...)
|
46 |
):
|
47 |
"""
|
48 |
+
Receives text and language via POST request, uses gTTS to generate
|
49 |
+
speech, and returns the MP3 audio directly as a stream.
|
50 |
"""
|
|
|
|
|
|
|
51 |
text = tts_request.text
|
52 |
+
lang = tts_request.lang
|
53 |
+
# tld = tts_request.tld # Uncomment if using tld
|
54 |
+
|
55 |
if not text or not text.strip():
|
56 |
+
# The pydantic model validation (min_length=1) should catch this,
|
57 |
+
# but belt-and-suspenders approach is fine.
|
58 |
raise HTTPException(status_code=400, detail="Input text cannot be empty.")
|
59 |
|
60 |
+
logger.info(f"Received gTTS request: lang='{lang}', text='{text[:50]}...'")
|
61 |
start_synth_time = time.time()
|
62 |
|
63 |
try:
|
64 |
+
# --- Generate Audio using gTTS ---
|
65 |
+
# Create gTTS object
|
66 |
+
tts = gTTS(text=text, lang=lang, slow=False) # Add tld=tld if using
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# --- Prepare Audio for Streaming ---
|
69 |
+
# Use an in-memory buffer (avoids temporary files)
|
70 |
+
mp3_fp = io.BytesIO()
|
71 |
+
tts.write_to_fp(mp3_fp)
|
72 |
+
mp3_fp.seek(0) # Rewind the buffer to the beginning for reading
|
73 |
|
74 |
synthesis_time = time.time() - start_synth_time
|
75 |
+
logger.info(f"gTTS audio generated in {synthesis_time:.2f} seconds.")
|
76 |
|
77 |
+
# --- Return Streaming Response ---
|
78 |
+
return StreamingResponse(
|
79 |
+
mp3_fp,
|
80 |
+
media_type="audio/mpeg", # Standard MIME type for MP3
|
81 |
+
headers={'Content-Disposition': 'attachment; filename="speech.mp3"'} # Suggest filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
)
|
83 |
|
84 |
+
except gTTSError as e:
|
85 |
+
logger.error(f"gTTS Error: {e}", exc_info=True)
|
86 |
+
# Check for common errors like invalid language
|
87 |
+
if "Language not supported" in str(e):
|
88 |
+
raise HTTPException(status_code=400, detail=f"Language '{lang}' not supported by gTTS. Error: {e}")
|
89 |
+
else:
|
90 |
+
raise HTTPException(status_code=500, detail=f"gTTS failed to generate speech. Error: {e}")
|
91 |
except Exception as e:
|
92 |
+
logger.error(f"An unexpected error occurred during speech generation: {e}", exc_info=True)
|
93 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred. Error: {str(e)}")
|
|
|
|
|
|
|
|
|
94 |
|
95 |
|
96 |
# --- Health Check Endpoint (Good Practice) ---
|
97 |
@app.get("/health", tags=["System"], summary="Check API Health")
|
98 |
async def health_check():
|
99 |
"""
|
100 |
+
Simple health check endpoint. Returns status ok if the service is running.
|
101 |
"""
|
102 |
+
# Can add a quick gTTS test here if needed, but might slow down health check
|
103 |
+
# try:
|
104 |
+
# gTTS(text='test', lang='en').save('test.mp3') # Dummy generation
|
105 |
+
# os.remove('test.mp3')
|
106 |
+
# except Exception as e:
|
107 |
+
# return {"status": "unhealthy", "reason": f"gTTS basic test failed: {e}"}
|
108 |
+
return {"status": "ok"}
|
109 |
|
110 |
# --- Root Endpoint (Optional Information) ---
|
111 |
@app.get("/", tags=["System"], summary="API Information")
|
|
|
114 |
Provides basic information about the API.
|
115 |
"""
|
116 |
return {
|
117 |
+
"message": "Welcome to the gTTS API Service!",
|
118 |
+
"tts_engine": "gTTS (Google Text-to-Speech)",
|
119 |
"tts_endpoint": "/api/tts",
|
120 |
"health_endpoint": "/health",
|
121 |
+
"expected_request_body": {"text": "string", "lang": "string (optional, default 'en')"},
|
122 |
+
"response_content_type": "audio/mpeg",
|
123 |
"documentation": "/docs" # Link to FastAPI auto-generated docs
|
124 |
}
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# --- How to Run Locally (for testing) ---
|
127 |
# if __name__ == "__main__":
|
128 |
# import uvicorn
|
129 |
+
# uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)
|
|
|
|
|
|