本文档帮助你快速上手 FastAPI Vision Service 项目。
# 检查 Python 版本(需要 3.10+)
python --version
# 检查 UV 是否安装
uv --version
# 如果没有 UV,安装它
pip install uvcd D:\aaaaa\fastapi-vision-serviceuv sync --extra dev这会安装所有需要的包,包括开发工具。
# Windows PowerShell
$env:SERVICE_MODE="t2i"
uv run uvicorn app.main:app --reload
# Linux/macOS
SERVICE_MODE=t2i uv run uvicorn app.main:app --reload# Windows PowerShell
$env:SERVICE_MODE="vl"
uv run uvicorn app.main:app --reload
# Linux/macOS
SERVICE_MODE=vl uv run uvicorn app.main:app --reloaddocker build -t fastapi-vision-service .# Windows PowerShell
docker run -p 8000:8000 `
-e SERVICE_MODE=t2i `
-e DEVICE=cpu `
-v ${PWD}/hf_cache:/models/hf `
fastapi-vision-service
# Linux/macOS/WSL2
docker run -p 8000:8000 \
-e SERVICE_MODE=t2i \
-e DEVICE=cpu \
-v $(pwd)/hf_cache:/models/hf \
fastapi-vision-service# Windows PowerShell
docker run -p 8000:8000 `
-e SERVICE_MODE=vl `
-e DEVICE=cpu `
-v ${PWD}/hf_cache:/models/hf `
fastapi-vision-service
# Linux/macOS/WSL2
docker run -p 8000:8000 \
-e SERVICE_MODE=vl \
-e DEVICE=cpu \
-v $(pwd)/hf_cache:/models/hf \
fastapi-vision-service# 启动文生图服务
docker-compose --profile t2i up
# 启动图片理解服务
docker-compose --profile vl up
# 后台运行
docker-compose --profile t2i up -d服务启动后,访问:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
curl http://localhost:8000/health# 生成图片(返回 Base64)
curl -X POST "http://localhost:8000/t2i/generate" \
-H "Content-Type: application/json" \
-d '{"prompt": "a beautiful sunset"}'
# 生成图片(返回 PNG 文件)
curl -X POST "http://localhost:8000/t2i/generate/image" \
-H "Content-Type: application/json" \
-d '{"prompt": "a cute cat"}' \
--output cat.png需要先准备一张测试图片 test.jpg:
# 上传图片并提问
curl -X POST "http://localhost:8000/vl/understand/upload" \
-F "image=@test.jpg" \
-F "question=What is in this image?"项目包含了一个测试脚本 examples_client.py:
uv run python examples_client.py# 运行所有测试
uv run pytest -v
# 运行特定测试
uv run pytest tests/test_health.py -v
# 查看测试覆盖率
uv run pytest --cov=app --cov-report=htmluv run ruff formatuv run ruff check
# 自动修复问题
uv run ruff check --fix# 手动运行所有 hooks
uv run pre-commit run --all-filesA: 第一次运行会下载模型(数 GB),请耐心等待。模型会缓存到 hf_cache/ 目录。
A: 这些模型比较大。文生图模型约需 6GB RAM,图片理解模型约需 8GB RAM。建议:
- 使用 CPU 模式(更省内存)
- 一次只运行一个服务
- 考虑使用更小的模型
A: 设置环境变量:
# 本地运行
export DEVICE=cuda # Linux/macOS
$env:DEVICE="cuda" # Windows
# Docker 运行
docker run --gpus all -e DEVICE=cuda ...A: 修改环境变量:
export T2I_MODEL_ID="your/model-id"
export VL_MODEL_ID="your/model-id"A: 可以使用浏览器 JavaScript:
// 文生图
const ws = new WebSocket('ws://localhost:8000/t2i/ws');
ws.onopen = () => ws.send(JSON.stringify({prompt: "a dog"}));
ws.onmessage = (e) => console.log(JSON.parse(e.data));
// 图片理解
const ws = new WebSocket('ws://localhost:8000/vl/ws');
ws.onopen = () => ws.send(JSON.stringify({
image_base64: "...",
question: "what is this?"
}));
ws.onmessage = (e) => console.log(JSON.parse(e.data));- 阅读完整的 README.md
- 查看 API 文档
- 研究源代码:
app/main.py- 入口app/models/- 模型实现app/routers/- API 路由
- 尝试修改配置参数
- 自己写新的测试用例
- 使用
--reload参数时,代码修改会自动重载 - 查看日志了解模型加载和推理过程
- 第一次生成可能较慢,之后会快很多
- 使用
hf_cache目录挂载避免重复下载模型
祝你使用愉快! 🚀