test_docker_deployment.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """测试 Docker 工具部署 — 在远程 Docker 上部署 rembg 并注册"""
  2. import httpx
  3. import time
  4. BASE_URL = "http://localhost:8001"
  5. def main():
  6. task_spec = """在远程 Docker 上部署 rembg 图像背景移除工具(使用 git clone 方式)。
  7. 背景信息:
  8. - GitHub: https://github.com/danielgatis/rembg
  9. - rembg 是一个 AI 图像背景移除工具,22k+ stars
  10. - 项目自带 HTTP 服务器,运行 'rembg s' 即可启动
  11. - 需要部署到远程 Docker 容器(不是本地 uv)
  12. - rembg 首次启动会下载 AI 模型到 /root/.u2net/ 目录
  13. 任务要求:
  14. 1. 使用 create_docker_env 创建 Docker 容器:
  15. - 镜像: python:3.12-slim
  16. - 端口: [7000]
  17. - volumes: {
  18. "/opt/tool_agent/docker/rembg/app": "/app", # 代码目录
  19. "/opt/tool_agent/docker/rembg/models": "/root/.u2net" # 模型目录
  20. }
  21. 2. 在容器内 git clone 项目:
  22. - cd /app
  23. - git clone https://github.com/danielgatis/rembg.git
  24. - cd rembg
  25. 3. 安装依赖:
  26. - 先查看 README.md 或 setup.py 了解安装方式
  27. - 安装项目:pip install -e .[cpu,cli]
  28. 4. 后台启动服务: rembg s --host 0.0.0.0 --port 7000
  29. 5. 等待服务启动完成(首次会下载 AI 模型,需要几分钟)
  30. 6. 使用 test_deployment 工具验证部署(容器状态、路由、连通性)
  31. 7. 验证通过后注册到工具库
  32. 8. 容器需要持久化运行,不要销毁
  33. 注册信息:
  34. - tool_id: rembg_bg_remover
  35. - name: Rembg Background Remover
  36. - category: cv
  37. - runtime_type: docker
  38. - endpoint_path: /api/remove
  39. - http_method: POST
  40. - input_schema: {"type": "object", "properties": {"file": {"type": "string", "format": "binary", "description": "image file"}}, "required": ["file"]}
  41. """
  42. print("Submitting rembg Docker deployment task...")
  43. resp = httpx.post(
  44. f"{BASE_URL}/create_tool",
  45. json={"task_spec": task_spec, "tool_id": "rembg_bg_remover"},
  46. timeout=30,
  47. )
  48. resp.raise_for_status()
  49. data = resp.json()
  50. task_id = data["task_id"]
  51. print(f"Task submitted: {task_id}")
  52. print("Waiting for task completion (polling every 30s)...")
  53. while True:
  54. time.sleep(30)
  55. try:
  56. resp = httpx.get(f"{BASE_URL}/task/{task_id}", timeout=30)
  57. resp.raise_for_status()
  58. task_data = resp.json()
  59. status = task_data["status"]
  60. print(f"[{time.strftime('%H:%M:%S')}] status: {status}")
  61. if status == "completed":
  62. print("\nDone!")
  63. print(task_data.get("result", ""))
  64. break
  65. elif status == "failed":
  66. print("\nFailed!")
  67. print(task_data.get("error", ""))
  68. break
  69. except Exception as e:
  70. print(f"[{time.strftime('%H:%M:%S')}] poll error: {e}")
  71. if __name__ == "__main__":
  72. main()