2024-12-31 01:52:16 -07:00
|
|
|
"""
|
|
|
|
FastAPI OpenAI Compatible API
|
|
|
|
"""
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-30 04:17:50 -07:00
|
|
|
import uvicorn
|
2024-12-30 13:21:17 -07:00
|
|
|
from contextlib import asynccontextmanager
|
2024-12-30 04:17:50 -07:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-12-31 01:57:00 -07:00
|
|
|
from loguru import logger
|
2024-12-30 04:17:50 -07:00
|
|
|
|
|
|
|
from .core.config import settings
|
2024-12-31 01:52:16 -07:00
|
|
|
from .routers.openai_compatible import router as openai_router
|
2024-12-31 01:57:00 -07:00
|
|
|
from .services.tts import TTSModel, TTSService
|
2024-12-31 01:52:16 -07:00
|
|
|
|
2024-12-30 13:21:17 -07:00
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def lifespan(app: FastAPI):
|
2024-12-31 01:57:00 -07:00
|
|
|
"""Lifespan context manager for model initialization"""
|
|
|
|
logger.info("Loading TTS model and voice packs...")
|
|
|
|
|
|
|
|
# Initialize the main model
|
|
|
|
model, device = TTSModel.get_instance()
|
|
|
|
logger.info(f"Model loaded on {device}")
|
2024-12-31 01:52:16 -07:00
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
# Initialize all voice packs
|
|
|
|
tts_service = TTSService()
|
|
|
|
voices = tts_service.list_voices()
|
|
|
|
for voice in voices:
|
|
|
|
logger.info(f"Loading voice pack: {voice}")
|
|
|
|
TTSModel.get_voicepack(voice)
|
2024-12-31 01:52:16 -07:00
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
logger.info("All models and voice packs loaded successfully")
|
2024-12-30 13:21:17 -07:00
|
|
|
yield
|
2024-12-30 04:17:50 -07:00
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-30 04:17:50 -07:00
|
|
|
# Initialize FastAPI app
|
|
|
|
app = FastAPI(
|
|
|
|
title=settings.api_title,
|
|
|
|
description=settings.api_description,
|
|
|
|
version=settings.api_version,
|
2024-12-31 01:52:16 -07:00
|
|
|
lifespan=lifespan,
|
|
|
|
openapi_url="/openapi.json", # Explicitly enable OpenAPI schema
|
2024-12-30 04:17:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add CORS middleware
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
2024-12-31 01:52:16 -07:00
|
|
|
# Include OpenAI compatible router
|
|
|
|
app.include_router(openai_router, prefix="/v1")
|
2024-12-30 04:17:50 -07:00
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-30 04:17:50 -07:00
|
|
|
# Health check endpoint
|
|
|
|
@app.get("/health")
|
|
|
|
async def health_check():
|
|
|
|
"""Health check endpoint"""
|
|
|
|
return {"status": "healthy"}
|
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-31 01:52:16 -07:00
|
|
|
@app.get("/v1/test")
|
|
|
|
async def test_endpoint():
|
|
|
|
"""Test endpoint to verify routing"""
|
|
|
|
return {"status": "ok"}
|
2024-12-30 04:17:50 -07:00
|
|
|
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-30 04:17:50 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
uvicorn.run("api.src.main:app", host=settings.host, port=settings.port, reload=True)
|