Spaces:
Running
Running
axeronis commited on
Commit Β·
d3ef2bc
1
Parent(s): ef10fdb
fix(chatterbox): use lifespan event for model loading at startup
Browse files- chatterbox-service/server.py +30 -12
chatterbox-service/server.py
CHANGED
|
@@ -9,6 +9,7 @@ import io
|
|
| 9 |
import os
|
| 10 |
import logging
|
| 11 |
from typing import Optional
|
|
|
|
| 12 |
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 13 |
from fastapi.responses import StreamingResponse
|
| 14 |
from pydantic import BaseModel
|
|
@@ -16,10 +17,36 @@ from pydantic import BaseModel
|
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
app = FastAPI(
|
| 20 |
title="Chatterbox TTS API",
|
| 21 |
description="Free emotional TTS with voice cloning",
|
| 22 |
-
version="1.1.0"
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
# Built-in voices (multilingual) with voice-specific settings
|
|
@@ -51,20 +78,11 @@ BUILTIN_VOICES = {
|
|
| 51 |
CUSTOM_VOICES_DIR = "custom_voices"
|
| 52 |
os.makedirs(CUSTOM_VOICES_DIR, exist_ok=True)
|
| 53 |
|
| 54 |
-
# Lazy load model
|
| 55 |
-
_model = None
|
| 56 |
-
|
| 57 |
def get_model():
|
|
|
|
| 58 |
global _model
|
| 59 |
if _model is None:
|
| 60 |
-
|
| 61 |
-
try:
|
| 62 |
-
from chatterbox.tts import ChatterboxTTS
|
| 63 |
-
_model = ChatterboxTTS.from_pretrained(device="cpu")
|
| 64 |
-
logger.info("β
Chatterbox model loaded successfully!")
|
| 65 |
-
except Exception as e:
|
| 66 |
-
logger.error(f"β Failed to load Chatterbox: {e}")
|
| 67 |
-
raise
|
| 68 |
return _model
|
| 69 |
|
| 70 |
|
|
|
|
| 9 |
import os
|
| 10 |
import logging
|
| 11 |
from typing import Optional
|
| 12 |
+
from contextlib import asynccontextmanager
|
| 13 |
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 14 |
from fastapi.responses import StreamingResponse
|
| 15 |
from pydantic import BaseModel
|
|
|
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
+
# Global model reference
|
| 21 |
+
_model = None
|
| 22 |
+
|
| 23 |
+
def load_chatterbox_model():
|
| 24 |
+
"""Load Chatterbox model (called once at startup)."""
|
| 25 |
+
global _model
|
| 26 |
+
logger.info("π Loading Chatterbox model (may take 1-2 minutes)...")
|
| 27 |
+
try:
|
| 28 |
+
from chatterbox.tts import ChatterboxTTS
|
| 29 |
+
_model = ChatterboxTTS.from_pretrained(device="cpu")
|
| 30 |
+
logger.info("β
Chatterbox model loaded successfully!")
|
| 31 |
+
return True
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logger.error(f"β Failed to load Chatterbox: {e}")
|
| 34 |
+
return False
|
| 35 |
+
|
| 36 |
+
@asynccontextmanager
|
| 37 |
+
async def lifespan(app: FastAPI):
|
| 38 |
+
"""Startup/shutdown lifecycle."""
|
| 39 |
+
# Startup: load model
|
| 40 |
+
load_chatterbox_model()
|
| 41 |
+
yield
|
| 42 |
+
# Shutdown: cleanup
|
| 43 |
+
logger.info("π Shutting down Chatterbox TTS API")
|
| 44 |
+
|
| 45 |
app = FastAPI(
|
| 46 |
title="Chatterbox TTS API",
|
| 47 |
description="Free emotional TTS with voice cloning",
|
| 48 |
+
version="1.1.0",
|
| 49 |
+
lifespan=lifespan
|
| 50 |
)
|
| 51 |
|
| 52 |
# Built-in voices (multilingual) with voice-specific settings
|
|
|
|
| 78 |
CUSTOM_VOICES_DIR = "custom_voices"
|
| 79 |
os.makedirs(CUSTOM_VOICES_DIR, exist_ok=True)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
def get_model():
|
| 82 |
+
"""Get loaded model."""
|
| 83 |
global _model
|
| 84 |
if _model is None:
|
| 85 |
+
raise HTTPException(status_code=503, detail="Model not loaded yet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
return _model
|
| 87 |
|
| 88 |
|