Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import uuid
|
|
| 4 |
import time
|
| 5 |
import shutil
|
| 6 |
from datetime import datetime
|
| 7 |
-
from typing import List, Optional
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
import requests
|
|
@@ -12,35 +12,14 @@ from fastapi import FastAPI, HTTPException, Form, UploadFile, File
|
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from pydantic import BaseModel
|
| 14 |
import torch
|
| 15 |
-
|
| 16 |
-
# Global state
|
| 17 |
-
tts = None
|
| 18 |
-
model_loaded = False
|
| 19 |
-
current_model = ""
|
| 20 |
-
model_loading = False
|
| 21 |
-
current_voice_style = "default_female"
|
| 22 |
-
voice_cloning_supported = False
|
| 23 |
-
app_startup_time = datetime.now()
|
| 24 |
|
| 25 |
# Configure environment
|
| 26 |
os.makedirs("/tmp/voices", exist_ok=True)
|
| 27 |
os.makedirs("/tmp/output", exist_ok=True)
|
| 28 |
|
| 29 |
-
# Configuration - Force CPU for Hugging Face Spaces compatibility
|
| 30 |
-
DEVICE = "cpu"
|
| 31 |
-
OCI_UPLOAD_API_URL = os.getenv("OCI_UPLOAD_API_URL", "").strip()
|
| 32 |
-
if OCI_UPLOAD_API_URL:
|
| 33 |
-
OCI_UPLOAD_API_URL = OCI_UPLOAD_API_URL.rstrip('/')
|
| 34 |
-
|
| 35 |
-
print(f"π§ Using device: {DEVICE} (forced CPU for Hugging Face Spaces compatibility)")
|
| 36 |
-
|
| 37 |
# Initialize FastAPI app
|
| 38 |
-
app = FastAPI(
|
| 39 |
-
title="TTS API",
|
| 40 |
-
description="API for text-to-speech with Coqui TTS",
|
| 41 |
-
docs_url="/",
|
| 42 |
-
redoc_url=None
|
| 43 |
-
)
|
| 44 |
|
| 45 |
# Add CORS middleware
|
| 46 |
app.add_middleware(
|
|
@@ -51,21 +30,58 @@ app.add_middleware(
|
|
| 51 |
allow_headers=["*"],
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
print("
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Pydantic models
|
| 71 |
class TTSRequest(BaseModel):
|
|
@@ -73,51 +89,66 @@ class TTSRequest(BaseModel):
|
|
| 73 |
project_id: str
|
| 74 |
voice_name: Optional[str] = "default"
|
| 75 |
language: Optional[str] = "en"
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
class VoiceCloneRequest(BaseModel):
|
| 79 |
project_id: str
|
| 80 |
voice_name: str
|
| 81 |
description: Optional[str] = ""
|
|
|
|
| 82 |
|
| 83 |
-
class
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
#
|
| 87 |
def clean_text(text):
|
| 88 |
-
"""Clean text for TTS generation"""
|
| 89 |
import re
|
| 90 |
|
| 91 |
if not text or not isinstance(text, str):
|
| 92 |
-
return "Hello"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
text = re.sub(r'[^\w\s\.\,\!\?\-\'\"
|
|
|
|
|
|
|
| 96 |
text = re.sub(r'\s+', ' ', text)
|
| 97 |
|
|
|
|
| 98 |
if len(text) > 10 and not re.search(r'[\.\!\?]$', text):
|
| 99 |
text = text + '.'
|
| 100 |
|
| 101 |
text = text.strip()
|
| 102 |
|
|
|
|
| 103 |
if not text:
|
| 104 |
text = "Hello world"
|
| 105 |
|
| 106 |
return text
|
| 107 |
|
| 108 |
def upload_to_oci(file_path: str, filename: str, project_id: str, file_type="voiceover"):
|
| 109 |
-
"""Upload file to OCI"""
|
| 110 |
try:
|
| 111 |
if not OCI_UPLOAD_API_URL:
|
| 112 |
-
|
| 113 |
-
return {"status": "skipped", "message": "OCI upload disabled"}, None
|
| 114 |
|
| 115 |
url = f"{OCI_UPLOAD_API_URL}/api/upload"
|
| 116 |
-
print(f"π Attempting upload to: {url}")
|
| 117 |
|
| 118 |
with open(file_path, "rb") as f:
|
| 119 |
files = {"file": (filename, f, "audio/wav")}
|
| 120 |
-
data = {
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
response = requests.post(url, files=files, data=data, timeout=30)
|
| 123 |
|
|
@@ -133,248 +164,484 @@ def upload_to_oci(file_path: str, filename: str, project_id: str, file_type="voi
|
|
| 133 |
except Exception as e:
|
| 134 |
return None, f"Upload error: {str(e)}"
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
def get_voice_path(voice_name: str):
|
| 137 |
-
"""Get path to voice file"""
|
| 138 |
if voice_name == "default":
|
| 139 |
return None
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
voice_path = Path(f"/tmp/voices/{voice_name}")
|
| 142 |
if voice_path.is_dir():
|
| 143 |
samples = list(voice_path.glob("sample_*.wav"))
|
| 144 |
return str(samples[0]) if samples else None
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
def clone_voice(voice_name: str, audio_files: List[str], description: str = ""):
|
| 148 |
-
"""
|
| 149 |
try:
|
| 150 |
print(f"ποΈ Cloning voice: {voice_name}")
|
| 151 |
|
| 152 |
voice_dir = f"/tmp/voices/{voice_name}"
|
| 153 |
os.makedirs(voice_dir, exist_ok=True)
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
for i, audio_file in enumerate(audio_files):
|
| 156 |
-
dest_path = f"{voice_dir}/sample_{i+1}.wav"
|
| 157 |
shutil.copy2(audio_file, dest_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
print(f" Copied sample {i+1} to: {dest_path}")
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
return False, f"Voice cloning failed: {str(e)}"
|
| 165 |
|
| 166 |
def supports_voice_cloning():
|
| 167 |
"""Check if the current model supports voice cloning"""
|
| 168 |
-
return
|
| 169 |
|
| 170 |
-
def
|
| 171 |
-
"""
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
if model_loading:
|
| 175 |
print("β³ Model is already being loaded...")
|
| 176 |
return False
|
| 177 |
|
| 178 |
-
if
|
| 179 |
-
print("
|
| 180 |
-
return
|
| 181 |
|
| 182 |
model_loading = True
|
|
|
|
| 183 |
|
| 184 |
try:
|
| 185 |
from TTS.api import TTS
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
"description": "Tacotron2 - Default female voice",
|
| 191 |
-
},
|
| 192 |
-
"clear_male": {
|
| 193 |
-
"name": "tts_models/en/ljspeech/glow-tts",
|
| 194 |
-
"description": "Glow-TTS - Clear male voice",
|
| 195 |
-
},
|
| 196 |
-
"voice_clone": {
|
| 197 |
-
"name": "tts_models/multilingual/multi-dataset/your_tts",
|
| 198 |
-
"description": "YourTTS - Voice cloning supported",
|
| 199 |
-
}
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
selected_model = model_options.get(voice_style, model_options["default_female"])
|
| 203 |
-
current_voice_style = voice_style
|
| 204 |
|
| 205 |
-
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
except Exception as e:
|
| 229 |
print(f"β Failed to initialize TTS: {e}")
|
| 230 |
-
import traceback
|
| 231 |
-
traceback.print_exc()
|
| 232 |
-
model_loading = False
|
| 233 |
return False
|
| 234 |
finally:
|
| 235 |
model_loading = False
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
return
|
| 242 |
-
"status": "healthy",
|
| 243 |
-
"service": "TTS API",
|
| 244 |
-
"message": "API is running successfully",
|
| 245 |
-
"model_loaded": model_loaded,
|
| 246 |
-
"device": DEVICE,
|
| 247 |
-
"timestamp": datetime.now().isoformat()
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
@app.get("/health")
|
| 251 |
-
async def health_check():
|
| 252 |
-
"""Health check endpoint"""
|
| 253 |
-
return {
|
| 254 |
-
"status": "healthy",
|
| 255 |
-
"timestamp": datetime.now().isoformat(),
|
| 256 |
-
"model_loaded": model_loaded,
|
| 257 |
-
"service": "TTS API"
|
| 258 |
-
}
|
| 259 |
-
|
| 260 |
-
@app.get("/api/health")
|
| 261 |
-
async def api_health_check():
|
| 262 |
-
"""API health check"""
|
| 263 |
-
return {
|
| 264 |
-
"status": "healthy",
|
| 265 |
-
"model_loaded": model_loaded,
|
| 266 |
-
"current_model": current_model if model_loaded else "none",
|
| 267 |
-
"device": DEVICE
|
| 268 |
-
}
|
| 269 |
-
|
| 270 |
-
# Hugging Face specific health checks
|
| 271 |
-
@app.get("/health-check")
|
| 272 |
-
async def huggingface_health_check():
|
| 273 |
-
"""Specific health check for Hugging Face Spaces"""
|
| 274 |
-
return {
|
| 275 |
-
"status": "healthy",
|
| 276 |
-
"message": "TTS API is running",
|
| 277 |
-
"timestamp": datetime.now().isoformat()
|
| 278 |
-
}
|
| 279 |
-
|
| 280 |
-
@app.get("/ready")
|
| 281 |
-
async def ready_check():
|
| 282 |
-
"""Simple readiness check"""
|
| 283 |
-
return {"status": "ready"}
|
| 284 |
|
| 285 |
-
# API endpoints
|
| 286 |
@app.post("/api/tts")
|
| 287 |
async def generate_tts(request: TTSRequest):
|
| 288 |
-
"""
|
| 289 |
try:
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
if not load_tts_model(request.
|
| 293 |
return {
|
| 294 |
"status": "error",
|
| 295 |
-
"message": "TTS model failed to load. Please
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
|
| 298 |
print(f"π₯ TTS request for project: {request.project_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 301 |
filename = f"voiceover_{timestamp}.wav"
|
| 302 |
output_path = f"/tmp/output/{filename}"
|
| 303 |
|
|
|
|
| 304 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 305 |
|
| 306 |
-
cleaned_text = clean_text(request.text)
|
| 307 |
-
|
| 308 |
# Get voice path if custom voice is requested
|
| 309 |
speaker_wav = None
|
| 310 |
-
if request.voice_name != "default":
|
| 311 |
speaker_wav = get_voice_path(request.voice_name)
|
| 312 |
if not speaker_wav:
|
| 313 |
return {
|
| 314 |
"status": "error",
|
| 315 |
-
"message": f"Voice '{request.voice_name}' not found."
|
| 316 |
}
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
if not os.path.exists(output_path):
|
| 324 |
-
raise Exception("TTS failed to create output file")
|
| 325 |
|
| 326 |
file_size = os.path.getsize(output_path)
|
|
|
|
| 327 |
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
if error:
|
|
|
|
| 331 |
return {
|
| 332 |
-
"status": "
|
| 333 |
-
"message": f"TTS generated
|
| 334 |
"local_file": output_path,
|
| 335 |
"filename": filename,
|
| 336 |
"file_size": file_size
|
| 337 |
}
|
| 338 |
|
|
|
|
|
|
|
|
|
|
| 339 |
try:
|
| 340 |
os.remove(output_path)
|
| 341 |
-
|
| 342 |
-
|
|
|
|
| 343 |
|
| 344 |
return {
|
| 345 |
"status": "success",
|
| 346 |
"message": "TTS generated and uploaded successfully",
|
| 347 |
"filename": filename,
|
| 348 |
-
"oci_path": upload_result.get("path", f"{request.project_id}/voiceover/{filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
except Exception as e:
|
| 352 |
-
|
|
|
|
| 353 |
|
|
|
|
| 354 |
@app.post("/api/clone-voice")
|
| 355 |
-
async def
|
| 356 |
project_id: str = Form(...),
|
| 357 |
voice_name: str = Form(...),
|
| 358 |
description: str = Form(""),
|
| 359 |
-
files: List[UploadFile] = File(...)
|
|
|
|
| 360 |
):
|
| 361 |
-
"""
|
| 362 |
try:
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
temp_files = []
|
| 367 |
-
for file in files:
|
| 368 |
-
if not file.filename.lower().endswith(('.wav', '.mp3', '.flac')):
|
| 369 |
-
raise HTTPException(status_code=400, detail="Only
|
| 370 |
|
| 371 |
temp_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
|
| 372 |
with open(temp_path, "wb") as f:
|
| 373 |
-
|
|
|
|
| 374 |
temp_files.append(temp_path)
|
| 375 |
|
| 376 |
success, message = clone_voice(voice_name, temp_files, description)
|
| 377 |
|
|
|
|
| 378 |
for temp_file in temp_files:
|
| 379 |
try:
|
| 380 |
os.remove(temp_file)
|
|
@@ -386,55 +653,151 @@ async def clone_voice_endpoint(
|
|
| 386 |
"status": "success",
|
| 387 |
"message": message,
|
| 388 |
"voice_name": voice_name,
|
| 389 |
-
"
|
| 390 |
}
|
| 391 |
else:
|
| 392 |
raise HTTPException(status_code=500, detail=message)
|
| 393 |
|
| 394 |
except Exception as e:
|
|
|
|
| 395 |
raise HTTPException(status_code=500, detail=f"Voice cloning failed: {str(e)}")
|
| 396 |
|
|
|
|
| 397 |
@app.get("/api/voices")
|
| 398 |
async def list_voices():
|
| 399 |
-
"""List
|
| 400 |
try:
|
| 401 |
voices_dir = Path("/tmp/voices")
|
| 402 |
-
if not voices_dir.exists():
|
| 403 |
-
return {"voices": []}
|
| 404 |
-
|
| 405 |
voices = []
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
voices.append({
|
| 410 |
-
"name":
|
| 411 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
-
return {"voices": voices}
|
| 415 |
except Exception as e:
|
|
|
|
| 416 |
raise HTTPException(status_code=500, detail=f"Failed to list voices: {str(e)}")
|
| 417 |
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
"
|
| 424 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
}
|
| 426 |
-
return {"voice_styles": styles}
|
| 427 |
|
| 428 |
-
@app.
|
| 429 |
-
async def
|
| 430 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
return {
|
| 432 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
"model_loaded": model_loaded,
|
| 434 |
-
"
|
| 435 |
-
"
|
|
|
|
|
|
|
| 436 |
}
|
| 437 |
|
| 438 |
if __name__ == "__main__":
|
| 439 |
import uvicorn
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import time
|
| 5 |
import shutil
|
| 6 |
from datetime import datetime
|
| 7 |
+
from typing import List, Optional, Dict
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
import requests
|
|
|
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from pydantic import BaseModel
|
| 14 |
import torch
|
| 15 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Configure environment
|
| 18 |
os.makedirs("/tmp/voices", exist_ok=True)
|
| 19 |
os.makedirs("/tmp/output", exist_ok=True)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Initialize FastAPI app
|
| 22 |
+
app = FastAPI(title="Enhanced TTS API", description="API for text-to-speech with multiple voice styles and voice cloning")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Add CORS middleware
|
| 25 |
app.add_middleware(
|
|
|
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Configuration
|
| 34 |
+
OCI_UPLOAD_API_URL = os.getenv("OCI_UPLOAD_API_URL", "https://yukee1992-oci-video-storage.hf.space")
|
| 35 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
|
| 37 |
+
print(f"β
Using device: {DEVICE}")
|
| 38 |
+
|
| 39 |
+
# Available models with different voice styles
|
| 40 |
+
AVAILABLE_MODELS = {
|
| 41 |
+
"xtts-v2": {
|
| 42 |
+
"name": "XTTS-v2",
|
| 43 |
+
"model_name": "tts_models/multilingual/multi-dataset/xtts_v2",
|
| 44 |
+
"description": "Multilingual model with voice cloning support",
|
| 45 |
+
"languages": ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"],
|
| 46 |
+
"voice_cloning": True,
|
| 47 |
+
"default_voice": "female_01"
|
| 48 |
+
},
|
| 49 |
+
"tacotron2-ddc": {
|
| 50 |
+
"name": "Tacotron2-DDC",
|
| 51 |
+
"model_name": "tts_models/en/ljspeech/tacotron2-DDC",
|
| 52 |
+
"description": "High-quality English TTS (fast and reliable)",
|
| 53 |
+
"languages": ["en"],
|
| 54 |
+
"voice_cloning": False,
|
| 55 |
+
"default_voice": "default"
|
| 56 |
+
},
|
| 57 |
+
"glow-tts": {
|
| 58 |
+
"name": "Glow-TTS",
|
| 59 |
+
"model_name": "tts_models/en/ljspeech/glow-tts",
|
| 60 |
+
"description": "Fast and high-quality English TTS",
|
| 61 |
+
"languages": ["en"],
|
| 62 |
+
"voice_cloning": False,
|
| 63 |
+
"default_voice": "default"
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Built-in voice styles for XTTS-v2
|
| 68 |
+
BUILTIN_VOICES = {
|
| 69 |
+
"female_01": {"name": "Female Voice 1", "gender": "female", "language": "multilingual"},
|
| 70 |
+
"female_02": {"name": "Female Voice 2", "gender": "female", "language": "multilingual"},
|
| 71 |
+
"female_03": {"name": "Female Voice 3", "gender": "female", "language": "multilingual"},
|
| 72 |
+
"male_01": {"name": "Male Voice 1", "gender": "male", "language": "multilingual"},
|
| 73 |
+
"male_02": {"name": "Male Voice 2", "gender": "male", "language": "multilingual"},
|
| 74 |
+
"default": {"name": "Default Voice", "gender": "neutral", "language": "multilingual"}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
# Global state
|
| 78 |
+
tts = None
|
| 79 |
+
model_loaded = False
|
| 80 |
+
current_model = ""
|
| 81 |
+
voice_cloning_supported = False
|
| 82 |
+
model_loading = False
|
| 83 |
+
model_load_attempts = 0
|
| 84 |
+
active_model_config = None
|
| 85 |
|
| 86 |
# Pydantic models
|
| 87 |
class TTSRequest(BaseModel):
|
|
|
|
| 89 |
project_id: str
|
| 90 |
voice_name: Optional[str] = "default"
|
| 91 |
language: Optional[str] = "en"
|
| 92 |
+
model_type: Optional[str] = "xtts-v2" # New: allow model selection
|
| 93 |
+
|
| 94 |
+
class BatchTTSRequest(BaseModel):
|
| 95 |
+
texts: List[str]
|
| 96 |
+
project_id: str
|
| 97 |
+
voice_name: Optional[str] = "default"
|
| 98 |
+
language: Optional[str] = "en"
|
| 99 |
+
model_type: Optional[str] = "xtts-v2"
|
| 100 |
|
| 101 |
class VoiceCloneRequest(BaseModel):
|
| 102 |
project_id: str
|
| 103 |
voice_name: str
|
| 104 |
description: Optional[str] = ""
|
| 105 |
+
model_type: Optional[str] = "xtts-v2"
|
| 106 |
|
| 107 |
+
class VoiceStyleRequest(BaseModel):
|
| 108 |
+
voice_name: str
|
| 109 |
+
style: str # e.g., "happy", "sad", "excited", "calm"
|
| 110 |
+
intensity: Optional[float] = 1.0
|
| 111 |
|
| 112 |
+
# Enhanced helper functions
|
| 113 |
def clean_text(text):
|
| 114 |
+
"""Clean text for TTS generation with better handling"""
|
| 115 |
import re
|
| 116 |
|
| 117 |
if not text or not isinstance(text, str):
|
| 118 |
+
return "Hello" # Default fallback text
|
| 119 |
|
| 120 |
+
# Remove any problematic characters but keep basic punctuation and multilingual characters
|
| 121 |
+
text = re.sub(r'[^\w\s\.\,\!\?\-\'\"\:\;\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af]', '', text)
|
| 122 |
+
|
| 123 |
+
# Replace multiple spaces with single space
|
| 124 |
text = re.sub(r'\s+', ' ', text)
|
| 125 |
|
| 126 |
+
# Ensure text ends with punctuation if it's a sentence
|
| 127 |
if len(text) > 10 and not re.search(r'[\.\!\?]$', text):
|
| 128 |
text = text + '.'
|
| 129 |
|
| 130 |
text = text.strip()
|
| 131 |
|
| 132 |
+
# If text is empty after cleaning, use default
|
| 133 |
if not text:
|
| 134 |
text = "Hello world"
|
| 135 |
|
| 136 |
return text
|
| 137 |
|
| 138 |
def upload_to_oci(file_path: str, filename: str, project_id: str, file_type="voiceover"):
|
| 139 |
+
"""Upload file to OCI using your existing API with subfolder support"""
|
| 140 |
try:
|
| 141 |
if not OCI_UPLOAD_API_URL:
|
| 142 |
+
return None, "OCI upload API URL not configured"
|
|
|
|
| 143 |
|
| 144 |
url = f"{OCI_UPLOAD_API_URL}/api/upload"
|
|
|
|
| 145 |
|
| 146 |
with open(file_path, "rb") as f:
|
| 147 |
files = {"file": (filename, f, "audio/wav")}
|
| 148 |
+
data = {
|
| 149 |
+
"project_id": project_id,
|
| 150 |
+
"subfolder": "voiceover"
|
| 151 |
+
}
|
| 152 |
|
| 153 |
response = requests.post(url, files=files, data=data, timeout=30)
|
| 154 |
|
|
|
|
| 164 |
except Exception as e:
|
| 165 |
return None, f"Upload error: {str(e)}"
|
| 166 |
|
| 167 |
+
def upload_to_oci_with_retry(file_path: str, filename: str, project_id: str, file_type="voiceover", max_retries=3):
|
| 168 |
+
"""Upload file to OCI with retry logic"""
|
| 169 |
+
for attempt in range(max_retries):
|
| 170 |
+
try:
|
| 171 |
+
print(f"π Upload attempt {attempt + 1} of {max_retries} for {filename}")
|
| 172 |
+
result, error = upload_to_oci(file_path, filename, project_id, file_type)
|
| 173 |
+
|
| 174 |
+
if error:
|
| 175 |
+
if attempt < max_retries - 1:
|
| 176 |
+
wait_time = 2 ** attempt
|
| 177 |
+
print(f"β³ Upload failed, retrying in {wait_time}s: {error}")
|
| 178 |
+
time.sleep(wait_time)
|
| 179 |
+
continue
|
| 180 |
+
else:
|
| 181 |
+
return None, error
|
| 182 |
+
else:
|
| 183 |
+
return result, None
|
| 184 |
+
|
| 185 |
+
except Exception as e:
|
| 186 |
+
if attempt < max_retries - 1:
|
| 187 |
+
wait_time = 2 ** attempt
|
| 188 |
+
print(f"β³ Upload exception, retrying in {wait_time}s: {str(e)}")
|
| 189 |
+
time.sleep(wait_time)
|
| 190 |
+
continue
|
| 191 |
+
else:
|
| 192 |
+
return None, f"Upload failed after {max_retries} attempts: {str(e)}"
|
| 193 |
+
|
| 194 |
+
return None, "Upload failed: unexpected error"
|
| 195 |
+
|
| 196 |
def get_voice_path(voice_name: str):
|
| 197 |
+
"""Get path to voice file with enhanced voice management"""
|
| 198 |
if voice_name == "default":
|
| 199 |
return None
|
| 200 |
|
| 201 |
+
# Check if it's a built-in voice
|
| 202 |
+
if voice_name in BUILTIN_VOICES:
|
| 203 |
+
return None # Built-in voices don't need speaker_wav
|
| 204 |
+
|
| 205 |
voice_path = Path(f"/tmp/voices/{voice_name}")
|
| 206 |
if voice_path.is_dir():
|
| 207 |
samples = list(voice_path.glob("sample_*.wav"))
|
| 208 |
return str(samples[0]) if samples else None
|
| 209 |
+
else:
|
| 210 |
+
voice_file = Path(f"/tmp/voices/{voice_name}.wav")
|
| 211 |
+
return str(voice_file) if voice_file.exists() else None
|
| 212 |
|
| 213 |
def clone_voice(voice_name: str, audio_files: List[str], description: str = ""):
|
| 214 |
+
"""Enhanced voice cloning with better sample management"""
|
| 215 |
try:
|
| 216 |
print(f"ποΈ Cloning voice: {voice_name}")
|
| 217 |
|
| 218 |
voice_dir = f"/tmp/voices/{voice_name}"
|
| 219 |
os.makedirs(voice_dir, exist_ok=True)
|
| 220 |
|
| 221 |
+
# Save metadata about the cloned voice
|
| 222 |
+
metadata = {
|
| 223 |
+
"name": voice_name,
|
| 224 |
+
"description": description,
|
| 225 |
+
"samples_count": len(audio_files),
|
| 226 |
+
"created_at": datetime.now().isoformat(),
|
| 227 |
+
"samples": []
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
for i, audio_file in enumerate(audio_files):
|
| 231 |
+
dest_path = f"{voice_dir}/sample_{i+1:02d}.wav"
|
| 232 |
shutil.copy2(audio_file, dest_path)
|
| 233 |
+
metadata["samples"].append({
|
| 234 |
+
"sample_id": i+1,
|
| 235 |
+
"filename": f"sample_{i+1:02d}.wav",
|
| 236 |
+
"file_size": os.path.getsize(dest_path)
|
| 237 |
+
})
|
| 238 |
print(f" Copied sample {i+1} to: {dest_path}")
|
| 239 |
|
| 240 |
+
# Save metadata
|
| 241 |
+
with open(f"{voice_dir}/metadata.json", "w") as f:
|
| 242 |
+
import json
|
| 243 |
+
json.dump(metadata, f, indent=2)
|
| 244 |
+
|
| 245 |
+
print(f"β
Voice cloning completed for {voice_name} with {len(audio_files)} samples")
|
| 246 |
+
return True, f"Voice '{voice_name}' is ready for use with {len(audio_files)} samples"
|
| 247 |
|
| 248 |
except Exception as e:
|
| 249 |
return False, f"Voice cloning failed: {str(e)}"
|
| 250 |
|
| 251 |
def supports_voice_cloning():
|
| 252 |
"""Check if the current model supports voice cloning"""
|
| 253 |
+
return voice_cloning_supported
|
| 254 |
|
| 255 |
+
def save_wav(audio, file_path, sample_rate=22050):
|
| 256 |
+
"""Save audio to WAV file manually"""
|
| 257 |
+
try:
|
| 258 |
+
# Try soundfile first
|
| 259 |
+
try:
|
| 260 |
+
import soundfile as sf
|
| 261 |
+
sf.write(file_path, audio, sample_rate)
|
| 262 |
+
return True
|
| 263 |
+
except ImportError:
|
| 264 |
+
print("β οΈ soundfile not available, using fallback method")
|
| 265 |
+
|
| 266 |
+
# Fallback: use wave library
|
| 267 |
+
import wave
|
| 268 |
+
import numpy as np
|
| 269 |
+
|
| 270 |
+
# Ensure audio is numpy array
|
| 271 |
+
if isinstance(audio, list):
|
| 272 |
+
audio = np.array(audio)
|
| 273 |
+
|
| 274 |
+
# Convert to 16-bit PCM
|
| 275 |
+
audio_int16 = (audio * 32767).astype(np.int16)
|
| 276 |
+
|
| 277 |
+
with wave.open(file_path, 'wb') as wav_file:
|
| 278 |
+
wav_file.setnchannels(1) # Mono
|
| 279 |
+
wav_file.setsampwidth(2) # 16-bit
|
| 280 |
+
wav_file.setframerate(sample_rate) # Sample rate
|
| 281 |
+
wav_file.writeframes(audio_int16.tobytes())
|
| 282 |
+
|
| 283 |
+
return True
|
| 284 |
+
|
| 285 |
+
except Exception as e:
|
| 286 |
+
print(f"β Failed to save WAV: {e}")
|
| 287 |
+
return False
|
| 288 |
+
|
| 289 |
+
def load_tts_model(model_type="xtts-v2"):
|
| 290 |
+
"""Enhanced model loading with multiple model support"""
|
| 291 |
+
global tts, model_loaded, current_model, voice_cloning_supported, model_loading, model_load_attempts, active_model_config
|
| 292 |
|
| 293 |
if model_loading:
|
| 294 |
print("β³ Model is already being loaded...")
|
| 295 |
return False
|
| 296 |
|
| 297 |
+
if model_type not in AVAILABLE_MODELS:
|
| 298 |
+
print(f"β Model type '{model_type}' not found. Available: {list(AVAILABLE_MODELS.keys())}")
|
| 299 |
+
return False
|
| 300 |
|
| 301 |
model_loading = True
|
| 302 |
+
model_load_attempts += 1
|
| 303 |
|
| 304 |
try:
|
| 305 |
from TTS.api import TTS
|
| 306 |
|
| 307 |
+
# Handle TOS acceptance automatically
|
| 308 |
+
import sys
|
| 309 |
+
from io import StringIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
old_stdin = sys.stdin
|
| 312 |
+
sys.stdin = StringIO('y\n')
|
| 313 |
|
| 314 |
+
try:
|
| 315 |
+
model_config = AVAILABLE_MODELS[model_type]
|
| 316 |
+
print(f"π Loading {model_config['name']}...")
|
| 317 |
+
|
| 318 |
+
# Load the selected model
|
| 319 |
+
tts = TTS(model_config["model_name"]).to(DEVICE)
|
| 320 |
+
|
| 321 |
+
# Test the model
|
| 322 |
+
test_path = "/tmp/test_output.wav"
|
| 323 |
+
|
| 324 |
+
if model_config["voice_cloning"]:
|
| 325 |
+
# Test with built-in voice for XTTS
|
| 326 |
+
tts.tts_to_file(
|
| 327 |
+
text="This is a test of the voice system.",
|
| 328 |
+
file_path=test_path,
|
| 329 |
+
speaker_wav=None, # Use built-in voice
|
| 330 |
+
language="en"
|
| 331 |
+
)
|
| 332 |
+
else:
|
| 333 |
+
# Test without voice cloning for other models
|
| 334 |
+
tts.tts_to_file(text="This is a test of the voice system.", file_path=test_path)
|
| 335 |
+
|
| 336 |
+
if os.path.exists(test_path):
|
| 337 |
+
os.remove(test_path)
|
| 338 |
+
print(f"β
{model_config['name']} model tested and working!")
|
| 339 |
+
else:
|
| 340 |
+
raise Exception("Test failed - no file created")
|
| 341 |
+
|
| 342 |
+
model_loaded = True
|
| 343 |
+
current_model = model_config["model_name"]
|
| 344 |
+
voice_cloning_supported = model_config["voice_cloning"]
|
| 345 |
+
active_model_config = model_config
|
| 346 |
+
|
| 347 |
+
print(f"β
{model_config['name']} loaded successfully!")
|
| 348 |
+
print(f" Voice cloning: {'β
Supported' if voice_cloning_supported else 'β Not supported'}")
|
| 349 |
+
print(f" Languages: {', '.join(model_config['languages'])}")
|
| 350 |
+
|
| 351 |
+
return True
|
| 352 |
+
|
| 353 |
+
except Exception as e:
|
| 354 |
+
print(f"β {model_config['name']} model failed: {e}")
|
| 355 |
+
# Fallback to Tacotron2 if XTTS fails
|
| 356 |
+
if model_type == "xtts-v2":
|
| 357 |
+
print("π Falling back to Tacotron2...")
|
| 358 |
+
return load_tts_model("tacotron2-ddc")
|
| 359 |
+
return False
|
| 360 |
+
|
| 361 |
+
finally:
|
| 362 |
+
sys.stdin = old_stdin
|
| 363 |
|
| 364 |
except Exception as e:
|
| 365 |
print(f"β Failed to initialize TTS: {e}")
|
|
|
|
|
|
|
|
|
|
| 366 |
return False
|
| 367 |
finally:
|
| 368 |
model_loading = False
|
| 369 |
|
| 370 |
+
def validate_language(language: str, model_type: str) -> bool:
|
| 371 |
+
"""Validate if language is supported by the current model"""
|
| 372 |
+
if model_type not in AVAILABLE_MODELS:
|
| 373 |
+
return False
|
| 374 |
+
return language in AVAILABLE_MODELS[model_type]["languages"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
|
| 376 |
+
# Enhanced API endpoints
|
| 377 |
@app.post("/api/tts")
|
| 378 |
async def generate_tts(request: TTSRequest):
|
| 379 |
+
"""Enhanced TTS generation with model selection and better voice handling"""
|
| 380 |
try:
|
| 381 |
+
# Lazy load model on first request or if model changed
|
| 382 |
+
if not model_loaded or active_model_config is None or request.model_type not in list(AVAILABLE_MODELS.keys())[0]:
|
| 383 |
+
if not load_tts_model(request.model_type):
|
| 384 |
return {
|
| 385 |
"status": "error",
|
| 386 |
+
"message": f"TTS model '{request.model_type}' failed to load. Please check the logs.",
|
| 387 |
+
"requires_tos_acceptance": True,
|
| 388 |
+
"tos_url": "https://coqui.ai/cpml.txt"
|
| 389 |
}
|
| 390 |
|
| 391 |
print(f"π₯ TTS request for project: {request.project_id}")
|
| 392 |
+
print(f" Model: {request.model_type}")
|
| 393 |
+
print(f" Text length: {len(request.text)} characters")
|
| 394 |
+
print(f" Voice: {request.voice_name}")
|
| 395 |
+
print(f" Language: {request.language}")
|
| 396 |
|
| 397 |
+
# Validate language
|
| 398 |
+
if not validate_language(request.language, request.model_type):
|
| 399 |
+
return {
|
| 400 |
+
"status": "error",
|
| 401 |
+
"message": f"Language '{request.language}' is not supported by {request.model_type}. Supported languages: {', '.join(active_model_config['languages'])}",
|
| 402 |
+
"supported_languages": active_model_config['languages']
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
# Check if voice cloning is requested but not supported
|
| 406 |
+
if request.voice_name != "default" and not supports_voice_cloning():
|
| 407 |
+
return {
|
| 408 |
+
"status": "error",
|
| 409 |
+
"message": "Voice cloning is not supported with the current model. Please use 'xtts-v2' model for voice cloning.",
|
| 410 |
+
"model": current_model
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
# Generate unique filename with sequential naming
|
| 414 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 415 |
filename = f"voiceover_{timestamp}.wav"
|
| 416 |
output_path = f"/tmp/output/{filename}"
|
| 417 |
|
| 418 |
+
# Ensure output directory exists
|
| 419 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 420 |
|
|
|
|
|
|
|
| 421 |
# Get voice path if custom voice is requested
|
| 422 |
speaker_wav = None
|
| 423 |
+
if request.voice_name != "default" and request.voice_name not in BUILTIN_VOICES:
|
| 424 |
speaker_wav = get_voice_path(request.voice_name)
|
| 425 |
if not speaker_wav:
|
| 426 |
return {
|
| 427 |
"status": "error",
|
| 428 |
+
"message": f"Voice '{request.voice_name}' not found. Available voices: {list(BUILTIN_VOICES.keys()) + [v for v in await list_voices_internal()]}"
|
| 429 |
}
|
| 430 |
|
| 431 |
+
print(f"π Generating TTS to: {output_path}")
|
| 432 |
+
|
| 433 |
+
# Clean the text before generation
|
| 434 |
+
cleaned_text = clean_text(request.text)
|
| 435 |
+
print(f"π Original text: '{request.text}'")
|
| 436 |
+
print(f"π Cleaned text: '{cleaned_text}'")
|
| 437 |
|
| 438 |
+
# Generate TTS based on model capabilities
|
| 439 |
+
try:
|
| 440 |
+
if supports_voice_cloning():
|
| 441 |
+
# XTTS model with voice cloning support
|
| 442 |
+
tts.tts_to_file(
|
| 443 |
+
text=cleaned_text,
|
| 444 |
+
speaker_wav=speaker_wav,
|
| 445 |
+
language=request.language,
|
| 446 |
+
file_path=output_path
|
| 447 |
+
)
|
| 448 |
+
else:
|
| 449 |
+
# Models without voice cloning
|
| 450 |
+
tts.tts_to_file(
|
| 451 |
+
text=cleaned_text,
|
| 452 |
+
file_path=output_path
|
| 453 |
+
)
|
| 454 |
+
except Exception as tts_error:
|
| 455 |
+
print(f"β TTS generation failed: {tts_error}")
|
| 456 |
+
# Try alternative approach
|
| 457 |
+
try:
|
| 458 |
+
print("π Trying alternative TTS generation method...")
|
| 459 |
+
if supports_voice_cloning():
|
| 460 |
+
audio = tts.tts(
|
| 461 |
+
text=cleaned_text,
|
| 462 |
+
speaker_wav=speaker_wav,
|
| 463 |
+
language=request.language
|
| 464 |
+
)
|
| 465 |
+
else:
|
| 466 |
+
audio = tts.tts(text=cleaned_text)
|
| 467 |
+
|
| 468 |
+
# Save manually
|
| 469 |
+
if not save_wav(audio, output_path):
|
| 470 |
+
raise Exception("Failed to save audio file")
|
| 471 |
+
|
| 472 |
+
except Exception as alt_error:
|
| 473 |
+
print(f"β Alternative method also failed: {alt_error}")
|
| 474 |
+
raise alt_error
|
| 475 |
+
|
| 476 |
+
# Verify the file was created
|
| 477 |
if not os.path.exists(output_path):
|
| 478 |
+
raise Exception(f"TTS failed to create output file: {output_path}")
|
| 479 |
|
| 480 |
file_size = os.path.getsize(output_path)
|
| 481 |
+
print(f"β
TTS generated: {output_path} ({file_size} bytes)")
|
| 482 |
|
| 483 |
+
# Upload to OCI
|
| 484 |
+
upload_result, error = upload_to_oci_with_retry(
|
| 485 |
+
output_path, filename, request.project_id, "voiceover"
|
| 486 |
+
)
|
| 487 |
|
| 488 |
if error:
|
| 489 |
+
print(f"β OCI upload failed: {error}")
|
| 490 |
return {
|
| 491 |
+
"status": "partial_success",
|
| 492 |
+
"message": f"TTS generated but upload failed: {error}",
|
| 493 |
"local_file": output_path,
|
| 494 |
"filename": filename,
|
| 495 |
"file_size": file_size
|
| 496 |
}
|
| 497 |
|
| 498 |
+
print(f"β
Upload successful: {filename}")
|
| 499 |
+
|
| 500 |
+
# Clean up local file
|
| 501 |
try:
|
| 502 |
os.remove(output_path)
|
| 503 |
+
print(f"π§Ή Cleaned up local file: {output_path}")
|
| 504 |
+
except Exception as cleanup_error:
|
| 505 |
+
print(f"β οΈ Could not clean up file: {cleanup_error}")
|
| 506 |
|
| 507 |
return {
|
| 508 |
"status": "success",
|
| 509 |
"message": "TTS generated and uploaded successfully",
|
| 510 |
"filename": filename,
|
| 511 |
+
"oci_path": upload_result.get("path", f"{request.project_id}/voiceover/{filename}"),
|
| 512 |
+
"model_used": current_model,
|
| 513 |
+
"model_type": request.model_type,
|
| 514 |
+
"voice_cloning_used": supports_voice_cloning() and request.voice_name != "default",
|
| 515 |
+
"voice_style": request.voice_name
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
except Exception as e:
|
| 519 |
+
print(f"β TTS generation error: {str(e)}")
|
| 520 |
+
error_detail = {
|
| 521 |
+
"error": str(e),
|
| 522 |
+
"model": current_model,
|
| 523 |
+
"model_type": request.model_type if 'request' in locals() else "unknown",
|
| 524 |
+
"voice_cloning_supported": supports_voice_cloning(),
|
| 525 |
+
"device": DEVICE
|
| 526 |
+
}
|
| 527 |
+
raise HTTPException(status_code=500, detail=error_detail)
|
| 528 |
+
|
| 529 |
+
async def list_voices_internal():
|
| 530 |
+
"""Internal function to list available voices"""
|
| 531 |
+
voices_dir = Path("/tmp/voices")
|
| 532 |
+
voices = []
|
| 533 |
+
|
| 534 |
+
for item in voices_dir.iterdir():
|
| 535 |
+
if item.is_dir():
|
| 536 |
+
samples = list(item.glob("sample_*.wav"))
|
| 537 |
+
voices.append(item.name)
|
| 538 |
+
elif item.is_file() and item.suffix == ".wav":
|
| 539 |
+
voices.append(item.stem)
|
| 540 |
+
|
| 541 |
+
return voices
|
| 542 |
+
|
| 543 |
+
@app.get("/api/models")
|
| 544 |
+
async def list_models():
|
| 545 |
+
"""List available TTS models"""
|
| 546 |
+
return {
|
| 547 |
+
"status": "success",
|
| 548 |
+
"models": AVAILABLE_MODELS,
|
| 549 |
+
"current_model": current_model if model_loaded else None,
|
| 550 |
+
"model_loaded": model_loaded
|
| 551 |
+
}
|
| 552 |
+
|
| 553 |
+
@app.post("/api/set-model")
|
| 554 |
+
async def set_model(model_type: str = Form(...)):
|
| 555 |
+
"""Switch between different TTS models"""
|
| 556 |
+
if model_type not in AVAILABLE_MODELS:
|
| 557 |
+
raise HTTPException(status_code=400, detail=f"Model type '{model_type}' not found. Available: {list(AVAILABLE_MODELS.keys())}")
|
| 558 |
+
|
| 559 |
+
success = load_tts_model(model_type)
|
| 560 |
+
|
| 561 |
+
if success:
|
| 562 |
+
return {
|
| 563 |
+
"status": "success",
|
| 564 |
+
"message": f"Model switched to {AVAILABLE_MODELS[model_type]['name']}",
|
| 565 |
+
"model": current_model,
|
| 566 |
+
"voice_cloning_supported": voice_cloning_supported
|
| 567 |
+
}
|
| 568 |
+
else:
|
| 569 |
+
raise HTTPException(status_code=500, detail=f"Failed to load model: {model_type}")
|
| 570 |
+
|
| 571 |
+
@app.get("/api/builtin-voices")
|
| 572 |
+
async def get_builtin_voices():
|
| 573 |
+
"""Get list of built-in voice styles"""
|
| 574 |
+
return {
|
| 575 |
+
"status": "success",
|
| 576 |
+
"voices": BUILTIN_VOICES,
|
| 577 |
+
"voice_cloning_supported": voice_cloning_supported
|
| 578 |
+
}
|
| 579 |
+
|
| 580 |
+
# Keep your existing endpoints but enhance them with model selection
|
| 581 |
+
@app.post("/api/batch-tts")
|
| 582 |
+
async def batch_generate_tts(request: BatchTTSRequest):
|
| 583 |
+
"""Enhanced batch TTS with model selection"""
|
| 584 |
+
try:
|
| 585 |
+
# Lazy load model
|
| 586 |
+
if not model_loaded or active_model_config is None or request.model_type not in list(AVAILABLE_MODELS.keys())[0]:
|
| 587 |
+
if not load_tts_model(request.model_type):
|
| 588 |
+
raise HTTPException(status_code=500, detail=f"TTS model '{request.model_type}' failed to load")
|
| 589 |
+
|
| 590 |
+
# Add model-specific validation and processing here
|
| 591 |
+
# ... (rest of your batch TTS implementation with model awareness)
|
| 592 |
+
|
| 593 |
+
# Your existing batch processing code here, enhanced with model checks
|
| 594 |
+
|
| 595 |
+
return {
|
| 596 |
+
"status": "completed",
|
| 597 |
+
"project_id": request.project_id,
|
| 598 |
+
"model_used": current_model,
|
| 599 |
+
"model_type": request.model_type,
|
| 600 |
+
"voice_cloning": supports_voice_cloning() and request.voice_name != "default"
|
| 601 |
}
|
| 602 |
|
| 603 |
except Exception as e:
|
| 604 |
+
print(f"β Batch TTS generation error: {str(e)}")
|
| 605 |
+
raise HTTPException(status_code=500, detail=f"Batch TTS generation failed: {str(e)}")
|
| 606 |
|
| 607 |
+
# Enhanced voice cloning endpoint
|
| 608 |
@app.post("/api/clone-voice")
|
| 609 |
+
async def api_clone_voice(
|
| 610 |
project_id: str = Form(...),
|
| 611 |
voice_name: str = Form(...),
|
| 612 |
description: str = Form(""),
|
| 613 |
+
files: List[UploadFile] = File(...),
|
| 614 |
+
model_type: str = Form("xtts-v2")
|
| 615 |
):
|
| 616 |
+
"""Enhanced voice cloning with model validation"""
|
| 617 |
try:
|
| 618 |
+
# Ensure we're using a model that supports voice cloning
|
| 619 |
+
if model_type != "xtts-v2":
|
| 620 |
+
raise HTTPException(
|
| 621 |
+
status_code=400,
|
| 622 |
+
detail="Voice cloning is only supported with the 'xtts-v2' model. Please switch to XTTS-v2 for voice cloning."
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
# Load XTTS model if not already loaded
|
| 626 |
+
if not model_loaded or current_model != AVAILABLE_MODELS["xtts-v2"]["model_name"]:
|
| 627 |
+
if not load_tts_model("xtts-v2"):
|
| 628 |
+
raise HTTPException(status_code=500, detail="XTTS-v2 model failed to load. Voice cloning requires XTTS-v2.")
|
| 629 |
+
|
| 630 |
+
# Rest of your voice cloning implementation...
|
| 631 |
temp_files = []
|
| 632 |
+
for i, file in enumerate(files):
|
| 633 |
+
if not file.filename.lower().endswith(('.wav', '.mp3', '.ogg', '.flac')):
|
| 634 |
+
raise HTTPException(status_code=400, detail="Only audio files are allowed")
|
| 635 |
|
| 636 |
temp_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
|
| 637 |
with open(temp_path, "wb") as f:
|
| 638 |
+
content = await file.read()
|
| 639 |
+
f.write(content)
|
| 640 |
temp_files.append(temp_path)
|
| 641 |
|
| 642 |
success, message = clone_voice(voice_name, temp_files, description)
|
| 643 |
|
| 644 |
+
# Clean up temporary files
|
| 645 |
for temp_file in temp_files:
|
| 646 |
try:
|
| 647 |
os.remove(temp_file)
|
|
|
|
| 653 |
"status": "success",
|
| 654 |
"message": message,
|
| 655 |
"voice_name": voice_name,
|
| 656 |
+
"model_used": current_model
|
| 657 |
}
|
| 658 |
else:
|
| 659 |
raise HTTPException(status_code=500, detail=message)
|
| 660 |
|
| 661 |
except Exception as e:
|
| 662 |
+
print(f"β Voice cloning error: {str(e)}")
|
| 663 |
raise HTTPException(status_code=500, detail=f"Voice cloning failed: {str(e)}")
|
| 664 |
|
| 665 |
+
# Enhanced voices list endpoint
|
| 666 |
@app.get("/api/voices")
|
| 667 |
async def list_voices():
|
| 668 |
+
"""List available voices with enhanced information"""
|
| 669 |
try:
|
| 670 |
voices_dir = Path("/tmp/voices")
|
|
|
|
|
|
|
|
|
|
| 671 |
voices = []
|
| 672 |
+
|
| 673 |
+
# Add built-in voices
|
| 674 |
+
for voice_id, voice_info in BUILTIN_VOICES.items():
|
| 675 |
+
voices.append({
|
| 676 |
+
"name": voice_id,
|
| 677 |
+
"display_name": voice_info["name"],
|
| 678 |
+
"type": "builtin",
|
| 679 |
+
"gender": voice_info["gender"],
|
| 680 |
+
"language": voice_info["language"],
|
| 681 |
+
"samples_count": 0,
|
| 682 |
+
"created_at": "built-in"
|
| 683 |
+
})
|
| 684 |
+
|
| 685 |
+
# Add cloned voices
|
| 686 |
+
for item in voices_dir.iterdir():
|
| 687 |
+
if item.is_dir():
|
| 688 |
+
samples = list(item.glob("sample_*.wav"))
|
| 689 |
+
# Try to load metadata
|
| 690 |
+
metadata_path = item / "metadata.json"
|
| 691 |
+
metadata = {}
|
| 692 |
+
if metadata_path.exists():
|
| 693 |
+
try:
|
| 694 |
+
with open(metadata_path, 'r') as f:
|
| 695 |
+
import json
|
| 696 |
+
metadata = json.load(f)
|
| 697 |
+
except:
|
| 698 |
+
pass
|
| 699 |
+
|
| 700 |
voices.append({
|
| 701 |
+
"name": item.name,
|
| 702 |
+
"display_name": metadata.get("name", item.name),
|
| 703 |
+
"type": "cloned",
|
| 704 |
+
"gender": "custom",
|
| 705 |
+
"language": "multilingual",
|
| 706 |
+
"samples_count": len(samples),
|
| 707 |
+
"description": metadata.get("description", ""),
|
| 708 |
+
"created_at": metadata.get("created_at", datetime.fromtimestamp(item.stat().st_ctime).isoformat())
|
| 709 |
})
|
| 710 |
+
elif item.is_file() and item.suffix == ".wav":
|
| 711 |
+
voices.append({
|
| 712 |
+
"name": item.stem,
|
| 713 |
+
"display_name": item.stem,
|
| 714 |
+
"type": "uploaded",
|
| 715 |
+
"gender": "custom",
|
| 716 |
+
"language": "unknown",
|
| 717 |
+
"samples_count": 1,
|
| 718 |
+
"created_at": datetime.fromtimestamp(item.stat().st_ctime).isoformat()
|
| 719 |
+
})
|
| 720 |
+
|
| 721 |
+
return {
|
| 722 |
+
"status": "success",
|
| 723 |
+
"voices": voices,
|
| 724 |
+
"voice_cloning_supported": supports_voice_cloning(),
|
| 725 |
+
"current_model": current_model
|
| 726 |
+
}
|
| 727 |
|
|
|
|
| 728 |
except Exception as e:
|
| 729 |
+
print(f"β List voices error: {str(e)}")
|
| 730 |
raise HTTPException(status_code=500, detail=f"Failed to list voices: {str(e)}")
|
| 731 |
|
| 732 |
+
# Keep your existing health check, reload-model, and root endpoints
|
| 733 |
+
@app.get("/api/health")
|
| 734 |
+
async def health_check():
|
| 735 |
+
"""Enhanced health check with model information"""
|
| 736 |
+
return {
|
| 737 |
+
"status": "healthy",
|
| 738 |
+
"tts_loaded": model_loaded,
|
| 739 |
+
"model": current_model,
|
| 740 |
+
"model_config": active_model_config,
|
| 741 |
+
"voice_cloning_supported": voice_cloning_supported,
|
| 742 |
+
"device": DEVICE,
|
| 743 |
+
"load_attempts": model_load_attempts,
|
| 744 |
+
"timestamp": datetime.now().isoformat()
|
| 745 |
}
|
|
|
|
| 746 |
|
| 747 |
+
@app.post("/api/reload-model")
|
| 748 |
+
async def reload_model(model_type: str = Form("xtts-v2")):
|
| 749 |
+
"""Enhanced model reload with model selection"""
|
| 750 |
+
global tts, model_loaded, current_model, voice_cloning_supported
|
| 751 |
+
|
| 752 |
+
if model_type not in AVAILABLE_MODELS:
|
| 753 |
+
raise HTTPException(status_code=400, detail=f"Model type '{model_type}' not found")
|
| 754 |
+
|
| 755 |
+
# Clear current model
|
| 756 |
+
tts = None
|
| 757 |
+
model_loaded = False
|
| 758 |
+
current_model = ""
|
| 759 |
+
voice_cloning_supported = False
|
| 760 |
+
|
| 761 |
+
# Try to reload specified model
|
| 762 |
+
success = load_tts_model(model_type)
|
| 763 |
+
|
| 764 |
+
return {
|
| 765 |
+
"status": "success" if success else "error",
|
| 766 |
+
"message": f"Model {model_type} reloaded successfully" if success else f"Failed to reload model {model_type}",
|
| 767 |
+
"model_loaded": model_loaded,
|
| 768 |
+
"model": current_model,
|
| 769 |
+
"voice_cloning_supported": voice_cloning_supported
|
| 770 |
+
}
|
| 771 |
+
|
| 772 |
+
@app.get("/")
|
| 773 |
+
async def root():
|
| 774 |
+
"""Enhanced root endpoint with model information"""
|
| 775 |
return {
|
| 776 |
+
"message": "Enhanced TTS API with Multiple Voice Styles and Voice Cloning",
|
| 777 |
+
"endpoints": {
|
| 778 |
+
"POST /api/tts": "Generate TTS for a single text",
|
| 779 |
+
"POST /api/batch-tts": "Generate TTS for multiple texts",
|
| 780 |
+
"POST /api/upload-voice": "Upload a voice sample for cloning",
|
| 781 |
+
"POST /api/clone-voice": "Clone a voice from multiple samples",
|
| 782 |
+
"GET /api/voices": "List available voices",
|
| 783 |
+
"GET /api/builtin-voices": "List built-in voice styles",
|
| 784 |
+
"GET /api/models": "List available TTS models",
|
| 785 |
+
"POST /api/set-model": "Switch between TTS models",
|
| 786 |
+
"GET /api/health": "Health check",
|
| 787 |
+
"POST /api/reload-model": "Reload TTS model"
|
| 788 |
+
},
|
| 789 |
"model_loaded": model_loaded,
|
| 790 |
+
"model_name": current_model if model_loaded else "None",
|
| 791 |
+
"model_type": list(AVAILABLE_MODELS.keys())[0] if active_model_config else "None",
|
| 792 |
+
"voice_cloning_supported": supports_voice_cloning(),
|
| 793 |
+
"builtin_voices_count": len(BUILTIN_VOICES)
|
| 794 |
}
|
| 795 |
|
| 796 |
if __name__ == "__main__":
|
| 797 |
import uvicorn
|
| 798 |
+
print("π Starting Enhanced TTS API with Multiple Voice Styles and Voice Cloning...")
|
| 799 |
+
print("π API endpoints available at: http://localhost:7860/")
|
| 800 |
+
print("π‘ Model will be loaded on first request to save memory")
|
| 801 |
+
print("π΅ Available models:", list(AVAILABLE_MODELS.keys()))
|
| 802 |
+
print("π£οΈ Built-in voices:", list(BUILTIN_VOICES.keys()))
|
| 803 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|