from PIL import Image  # 正确导入
import numpy as np

import uvicorn
from PIL import Image
from fastapi import FastAPI
from core.all import load_model,detect
from ds.config import SERVER_CONFIG
from middle.error_handler import global_exception_handler
from service.user_service import router as user_router
from service.sensitive_service import router as sensitive_router
from service.face_service import router as face_router
from service.device_service import router as device_router
from ws.ws import ws_router, lifespan


# ------------------------------
# 初始化 FastAPI 应用、指定生命周期管理
# ------------------------------
app = FastAPI(
    title="内容安全审核后台",
    description="内容安全审核后台",
    version="1.0.0",
    lifespan=lifespan
)

# ------------------------------
# 注册路由
# ------------------------------
app.include_router(user_router)
app.include_router(device_router)
app.include_router(face_router)
app.include_router(sensitive_router)
app.include_router(ws_router)

# ------------------------------
# 注册全局异常处理器
# ------------------------------
app.add_exception_handler(Exception, global_exception_handler)

# ------------------------------
# 启动服务
# ------------------------------
if __name__ == "__main__":
    # -------------------------- 配置调整 --------------------------
    # 模型配置路径（建议改为环境变量）
    YOLO_MODEL_PATH = r"/core/models\best.pt"
    OCR_CONFIG_PATH = r"/core/config\config.yaml"

    # 初始化项目（默认端口设为8000，避免初始化失败时port未定义）
    port = int(SERVER_CONFIG.get("port", 8000))

    # 启动 UVicorn 服务
    uvicorn.run(
        app="main:app",
        host="0.0.0.0",
        port=port,
        workers=8,
        ws="websockets"
    )
