--- name: classification_server_python_run overview: 为纯 Python 分类服务补齐独立运行配套:新增 requirements.txt 依赖清单,并在 main.py 末尾添加 uvicorn 启动入口,使 `python main.py` 一条命令即可启动,完全不依赖 Go。 todos: - id: create-requirements content: 新建 thirdparty/classification_server/requirements.txt 依赖清单并添加 GPU 安装说明注释 status: completed - id: add-start-entry content: 在 main.py 末尾追加 uvicorn 启动入口并做语法验证 status: completed --- ## 用户需求 分类服务(Fashion-CLIP 衣物分类)要能完全不依赖 Go 项目、纯 Python 独立运行。用户已确认需要补齐以下运行配套文件: - 新增 `requirements.txt` 依赖清单 - 在 `main.py` 末尾添加启动入口,使 `python main.py` 一条命令即可启动服务 不要求本地测试脚本和 README。 ## 核心功能 - **依赖清单**:声明 torch、transformers、fastapi、uvicorn、pillow 五个依赖及版本下限,并注明 GPU 用户需单独安装 CUDA 版 torch - **一键启动**:`python main.py` 直接以 0.0.0.0:8000 启动 FastAPI 服务,无需 `uvicorn main:app` 命令 ## 技术方案 ### 实现方式 1. 新建 `thirdparty/classification_server/requirements.txt`,列出运行所需的 5 个第三方依赖,采用宽松下限版本避免锁死小版本: - torch>=2.0.0、transformers>=4.30.0、fastapi>=0.100.0、uvicorn>=0.23.0、pillow>=9.5.0 - 文件内添加注释:PyPI 默认安装 CPU 版 torch,GPU 用户需按 PyTorch 官网命令单独安装对应 CUDA 版本 2. 修改 `main.py` 末尾(第 594-596 行 `/healthz` 路由之后)追加启动入口: ```python if **name** == "**main**": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) ``` - **关键点**:Fashion-CLIP 模型在模块 import 时于模块级加载(`model`、`processor` 为模块级变量),因此必须直接传 `app` 对象调用 `uvicorn.run(app, ...)`,不能使用 `"main:app"` 字符串加 reload 的方式,否则会导致模型重复加载 - uvicorn 放在入口内局部 import,保持顶部 import 区原结构不变,改动最小化 ### 验证方式 - `python3 -m py_compile main.py` 确认语法无误 - 交付后运行验证:`pip install -r requirements.txt && python main.py`,访问 `http://localhost:8000/healthz` 返回 `{"status": "ok"}` ### 目录结构 ``` thirdparty/classification_server/ ├── main.py # [MODIFY] 末尾追加 __main__ 启动入口,python main.py 直接启动 └── requirements.txt # [NEW] 依赖清单(torch/transformers/fastapi/uvicorn/pillow + GPU 安装说明注释) ```