2024-10-22 22:51:03 +02:00
|
|
|
# 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
|
2024-10-23 01:49:35 +02:00
|
|
|
import uvicorn
|
2024-10-22 22:51:03 +02:00
|
|
|
|
|
|
|
# Настройка логирования
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
|
|
title="Toxicity Assessment API",
|
|
|
|
description="API для оценки токсичности текста",
|
|
|
|
version="1.0"
|
|
|
|
)
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
2024-10-23 00:10:51 +02:00
|
|
|
allow_origins=["*"],
|
2024-10-22 22:51:03 +02:00
|
|
|
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": "Внутренняя ошибка сервера."},
|
|
|
|
)
|
2024-10-23 01:49:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
uvicorn.run(
|
|
|
|
"app.main:app",
|
|
|
|
loop="uvloop"
|
|
|
|
)
|