Refactor server.py: add base64 support, separate from training

- POST /solve accepts JSON with base64 encoded image
- POST /solve/upload keeps multipart file upload compatibility
- Server only depends on inference code (onnxruntime), no torch
- Catch invalid image errors with proper 400 response
- Update CLAUDE.md with new API documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hua
2026-03-11 19:28:09 +08:00
parent 788ddcae1a
commit 68ab86e6b9
2 changed files with 80 additions and 40 deletions

View File

@@ -440,11 +440,19 @@ uv run python cli.py serve --port 8080
## HTTP 服务 (server.py可选)
纯推理服务,不依赖 torch / 训练代码,仅需 onnxruntime + FastAPI。
```python
# FastAPI 服务,提供 REST API
# POST /solve - 上传图片,返回识别结果
# 请求: multipart/form-data字段名 image
# 响应: {"type": "normal", "result": "A3B8", "confidence": 0.95, "time_ms": 45}
# POST /solve - JSON base64 图片识别
# 请求: {"image": "<base64>", "type": "normal"} (type 可选)
# 响应: {"type": "normal", "result": "A3B8", "raw": "A3B8", "time_ms": 12.3}
#
# POST /solve/upload - multipart 文件上传识别
# 请求: multipart/form-data, 字段名 image, 可选 query param type
# 响应: 同上
#
# GET /health - 健康检查
# 响应: {"status": "ok", "models_loaded": true}
```
## 关键约束和注意事项