Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import shutil
|
|
| 6 |
from datetime import datetime
|
| 7 |
from typing import List, Optional
|
| 8 |
from pathlib import Path
|
| 9 |
-
from contextlib import asynccontextmanager
|
| 10 |
|
| 11 |
import requests
|
| 12 |
from fastapi import FastAPI, HTTPException, Form, UploadFile, File
|
|
@@ -21,7 +20,6 @@ current_model = ""
|
|
| 21 |
model_loading = False
|
| 22 |
current_voice_style = "default_female"
|
| 23 |
voice_cloning_supported = False
|
| 24 |
-
app_startup_time = datetime.now()
|
| 25 |
|
| 26 |
# Configure environment
|
| 27 |
os.makedirs("/tmp/voices", exist_ok=True)
|
|
@@ -34,27 +32,12 @@ if OCI_UPLOAD_API_URL:
|
|
| 34 |
|
| 35 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
async def lifespan(app: FastAPI):
|
| 39 |
-
# Startup
|
| 40 |
-
print("=" * 50)
|
| 41 |
-
print("π TTS API Starting Up...")
|
| 42 |
-
print(f"β
Device: {DEVICE}")
|
| 43 |
-
print(f"π§ OCI Upload: {OCI_UPLOAD_API_URL or 'Local only'}")
|
| 44 |
-
print("π Models will load on first request (lazy loading)")
|
| 45 |
-
print("β° Startup time:", app_startup_time.isoformat())
|
| 46 |
-
print("=" * 50)
|
| 47 |
-
yield
|
| 48 |
-
# Shutdown
|
| 49 |
-
print("π TTS API Shutting Down...")
|
| 50 |
-
|
| 51 |
-
# Initialize FastAPI app with lifespan
|
| 52 |
app = FastAPI(
|
| 53 |
title="TTS API",
|
| 54 |
description="API for text-to-speech with Coqui TTS",
|
| 55 |
docs_url="/",
|
| 56 |
-
redoc_url=None
|
| 57 |
-
lifespan=lifespan
|
| 58 |
)
|
| 59 |
|
| 60 |
# Add CORS middleware
|
|
@@ -66,10 +49,15 @@ app.add_middleware(
|
|
| 66 |
allow_headers=["*"],
|
| 67 |
)
|
| 68 |
|
| 69 |
-
print(
|
| 70 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
# Pydantic models
|
| 73 |
class TTSRequest(BaseModel):
|
| 74 |
text: str
|
| 75 |
project_id: str
|
|
@@ -85,7 +73,9 @@ class VoiceCloneRequest(BaseModel):
|
|
| 85 |
class ChangeVoiceRequest(BaseModel):
|
| 86 |
voice_style: str
|
| 87 |
|
| 88 |
-
#
|
|
|
|
|
|
|
| 89 |
def clean_text(text):
|
| 90 |
"""Clean text for TTS generation"""
|
| 91 |
import re
|
|
@@ -223,7 +213,7 @@ def load_tts_model(voice_style="default_female"):
|
|
| 223 |
finally:
|
| 224 |
model_loading = False
|
| 225 |
|
| 226 |
-
#
|
| 227 |
@app.get("/")
|
| 228 |
async def root():
|
| 229 |
"""Root endpoint"""
|
|
@@ -254,7 +244,6 @@ async def api_health_check():
|
|
| 254 |
"device": DEVICE
|
| 255 |
}
|
| 256 |
|
| 257 |
-
# API endpoints
|
| 258 |
@app.post("/api/tts")
|
| 259 |
async def generate_tts(request: TTSRequest):
|
| 260 |
"""Generate TTS for a single text"""
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
from typing import List, Optional
|
| 8 |
from pathlib import Path
|
|
|
|
| 9 |
|
| 10 |
import requests
|
| 11 |
from fastapi import FastAPI, HTTPException, Form, UploadFile, File
|
|
|
|
| 20 |
model_loading = False
|
| 21 |
current_voice_style = "default_female"
|
| 22 |
voice_cloning_supported = False
|
|
|
|
| 23 |
|
| 24 |
# Configure environment
|
| 25 |
os.makedirs("/tmp/voices", exist_ok=True)
|
|
|
|
| 32 |
|
| 33 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
|
| 35 |
+
# Initialize FastAPI app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
app = FastAPI(
|
| 37 |
title="TTS API",
|
| 38 |
description="API for text-to-speech with Coqui TTS",
|
| 39 |
docs_url="/",
|
| 40 |
+
redoc_url=None
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# Add CORS middleware
|
|
|
|
| 49 |
allow_headers=["*"],
|
| 50 |
)
|
| 51 |
|
| 52 |
+
print("=" * 50)
|
| 53 |
+
print("π TTS API Starting Up...")
|
| 54 |
+
print(f"β
Device: {DEVICE}")
|
| 55 |
+
print(f"π§ OCI Upload: {OCI_UPLOAD_API_URL or 'Local only'}")
|
| 56 |
+
print("π Models will load on first request (lazy loading)")
|
| 57 |
+
print("β° Startup time:", datetime.now().isoformat())
|
| 58 |
+
print("=" * 50)
|
| 59 |
|
| 60 |
+
# Pydantic models (keep all your existing models and classes the same)
|
| 61 |
class TTSRequest(BaseModel):
|
| 62 |
text: str
|
| 63 |
project_id: str
|
|
|
|
| 73 |
class ChangeVoiceRequest(BaseModel):
|
| 74 |
voice_style: str
|
| 75 |
|
| 76 |
+
# Keep all your existing helper functions exactly the same:
|
| 77 |
+
# clean_text, upload_to_oci, get_voice_path, clone_voice, supports_voice_cloning, load_tts_model
|
| 78 |
+
|
| 79 |
def clean_text(text):
|
| 80 |
"""Clean text for TTS generation"""
|
| 81 |
import re
|
|
|
|
| 213 |
finally:
|
| 214 |
model_loading = False
|
| 215 |
|
| 216 |
+
# Keep all your existing endpoints exactly the same:
|
| 217 |
@app.get("/")
|
| 218 |
async def root():
|
| 219 |
"""Root endpoint"""
|
|
|
|
| 244 |
"device": DEVICE
|
| 245 |
}
|
| 246 |
|
|
|
|
| 247 |
@app.post("/api/tts")
|
| 248 |
async def generate_tts(request: TTSRequest):
|
| 249 |
"""Generate TTS for a single text"""
|