Files
video/main.py
2025-09-16 20:17:48 +08:00

94 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import uvicorn
import time
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# 原有业务导入
from core.all import load_model
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 service.model_service import router as model_router
from service.file_service import router as file_router
from service.device_danger_service import router as device_danger_router
from ws.ws import ws_router, lifespan
from core.establish import create_directory_structure
# 初始化 FastAPI 应用
app = FastAPI(
title="内容安全审核后台",
description="含图片访问服务和动态模型管理",
version="1.0.0",
lifespan=lifespan
)
ALLOWED_ORIGINS = [
"*"
]
# 配置 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS, # 允许的前端域名
allow_credentials=True, # 允许携带 Cookie
allow_methods=["*"], # 允许所有 HTTP 方法
allow_headers=["*"], # 允许所有请求头
)
# 注册路由
app.include_router(user_router)
app.include_router(device_router)
app.include_router(face_router)
app.include_router(sensitive_router)
app.include_router(model_router)
app.include_router(file_router)
app.include_router(device_danger_router)
app.include_router(ws_router)
# 注册全局异常处理器
app.add_exception_handler(Exception, global_exception_handler)
# 主服务启动入口
if __name__ == "__main__":
create_directory_structure()
print(f"[初始化] 目录结构创建完成")
# 创建模型保存目录
MODEL_SAVE_DIR = os.path.join("core", "models")
os.makedirs(MODEL_SAVE_DIR, exist_ok=True)
print(f"[初始化] 模型保存目录:{MODEL_SAVE_DIR}")
# 确保图片目录存在原Flask服务负责的目录
BASE_IMAGE_DIR = os.path.abspath(os.path.join("resource", "dect"))
if not os.path.exists(BASE_IMAGE_DIR):
print(f"[初始化] 图片根目录不存在,创建:{BASE_IMAGE_DIR}")
os.makedirs(BASE_IMAGE_DIR, exist_ok=True)
# 加载检测模型
try:
load_success = load_model()
if load_success:
print(f"[初始化] 检测模型加载完成")
else:
print(f"[初始化] 未找到默认模型可通过API上传并设置默认模型")
except Exception as e:
print(f"[初始化] 模型加载警告:{str(e)}(服务仍启动)")
# 启动 FastAPI 主服务仅使用8000端口
port = int(SERVER_CONFIG.get("port", 8000))
print(f"\n[FastAPI 服务] 准备启动,端口:{port}")
print(f"[FastAPI 服务] 接口文档http://服务器IP:{port}/docs\n")
uvicorn.run(
app="main:app",
host="0.0.0.0",
port=port,
workers=1,
ws="websockets",
reload=False
)