51 lines
1.6 KiB
Python
51 lines
1.6 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
|
||
|
|
||
|
# Настройка логирования
|
||
|
logging.basicConfig(level=logging.INFO)
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
app = FastAPI(
|
||
|
title="Toxicity Assessment API",
|
||
|
description="API для оценки токсичности текста",
|
||
|
version="1.0"
|
||
|
)
|
||
|
|
||
|
# Разрешение CORS (опционально, настройте по необходимости)
|
||
|
app.add_middleware(
|
||
|
CORSMiddleware,
|
||
|
allow_origins=["*"], # Замените на конкретные домены при необходимости
|
||
|
allow_credentials=True,
|
||
|
allow_methods=["*"],
|
||
|
allow_headers=["*"],
|
||
|
)
|
||
|
|
||
|
# Включение маршрутов API
|
||
|
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": "Внутренняя ошибка сервера."},
|
||
|
)
|