toxic-detector/app/main.py

56 lines
1.4 KiB
Python

# app/main.py
import logging
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from app.api import routes
from app.core.config import settings
from app.core.cache import cache
import uvicorn
# Настройка логирования
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="Toxicity Assessment API",
description="API для оценки токсичности текста",
version="1.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(routes.router)
@app.on_event("startup")
async def startup_event():
logger.info("Toxicity Assessment API запущен и готов к работе.")
logger.info(f"MODEL_CHECKPOINT: {settings.MODEL_CHECKPOINT}")
logger.info(f"USE_CUDA: {settings.USE_CUDA}")
await cache.connect()
@app.on_event("shutdown")
async def shutdown_event():
await cache.disconnect()
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Ошибка: {exc}")
return JSONResponse(
status_code=500,
content={"detail": "Внутренняя ошибка сервера."},
)
if __name__ == "__main__":
uvicorn.run(
"app.main:app",
loop="uvloop"
)