2024-12-31 01:52:16 -07:00
|
|
|
"""
|
|
|
|
FastAPI OpenAI Compatible API
|
|
|
|
"""
|
2024-12-31 01:57:00 -07:00
|
|
|
|
2024-12-30 13:21:17 -07:00
|
|
|
from contextlib import asynccontextmanager
|
2024-12-31 02:55:51 -07:00
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
from loguru import logger
|
2024-12-30 04:17:50 -07:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
from .core.config import settings
|
2025-01-03 00:53:41 -07:00
|
|
|
from .services.tts_model import TTSModel
|
|
|
|
from .services.tts_service import TTSService
|
2024-12-31 02:55:51 -07:00
|
|
|
from .routers.openai_compatible import router as openai_router
|
2025-01-03 17:54:17 -07:00
|
|
|
from .routers.text_processing import router as text_router
|
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...")
|
|
|
|
|
2025-01-01 17:38:22 -07:00
|
|
|
# Initialize the main model with warm-up
|
2025-01-03 03:16:42 -07:00
|
|
|
voicepack_count = TTSModel.setup()
|
2025-01-04 17:54:54 -07:00
|
|
|
logger.info("""
|
|
|
|
███████╗█████╗█████████████████╗ ██╗██████╗██╗ ██╗██████╗
|
|
|
|
██╔════██╔══████╔════╚══██╔══██║ ██╔██╔═══████║ ██╔██╔═══██╗
|
|
|
|
█████╗ ██████████████╗ ██║ █████╔╝██║ ███████╔╝██║ ██║
|
|
|
|
██╔══╝ ██╔══██╚════██║ ██║ ██╔═██╗██║ ████╔═██╗██║ ██║
|
|
|
|
██║ ██║ █████████║ ██║ ██║ ██╚██████╔██║ ██╚██████╔╝
|
|
|
|
╚═╝ ╚═╝ ╚═╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═════╝╚═╝ ╚═╝╚═════╝ """)
|
2025-01-03 03:16:42 -07:00
|
|
|
logger.info(f"Model loaded and warmed up on {TTSModel.get_device()}")
|
2025-01-01 17:38:22 -07:00
|
|
|
logger.info(f"{voicepack_count} voice packs loaded successfully")
|
2025-01-04 17:54:54 -07:00
|
|
|
logger.info("#" * 80)
|
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=["*"],
|
|
|
|
)
|
|
|
|
|
2025-01-03 17:54:17 -07:00
|
|
|
# Include routers
|
2024-12-31 01:52:16 -07:00
|
|
|
app.include_router(openai_router, prefix="/v1")
|
2025-01-03 17:54:17 -07:00
|
|
|
app.include_router(text_router)
|
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)
|