feat(接口2/3): 加 use_mask 开关用于遮罩对比;网关回退纯透传

- 接口3 generate_grow_b 增加 use_mask(默认 true):false 跳过检测、
  直接送划线图(空遮罩)
- 接口2 generate_grow_results 增加 use_mask:false 用干净原图+空遮罩,
  只跑一次 ComfyUI、N 项复用
- app.py 接口2/3 增加 use_mask form 参数并透传
- gateway/app.py 回退为纯透传(移除 OpenAPI schema 注入,请求体原样转发)
- docs 补 use_mask 说明

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
xsl
2026-06-17 23:01:12 +08:00
co-authored by Claude Opus 4.8
parent 52f1bb4c0b
commit 5311c8d7c7
4 changed files with 35 additions and 80 deletions
+3 -68
View File
@@ -214,8 +214,7 @@ def _proxy(request: Request, path: str):
return proxy_request(request, path)
# 声明各接口的 form 参数 → 注入 OpenAPI schema(实际转发直接读 Request body,不在此解析)。
# 每项字段:type(file/string/boolean)、description,可选 required(默认否)/default。
# 声明各接口的 form 参数用于 OpenAPI schema(实际转发直接读 Request
_MEASURE_FORMS = {
"image_file": {"type": "file", "description": "上传图片文件(JPG/PNG,≤ 1 MB"},
"image_url": {"type": "string", "description": "图片 URL"},
@@ -224,31 +223,13 @@ _MEASURE_FORMS = {
_GROW_FORMS = {
**_MEASURE_FORMS,
"gender": {"type": "string", "description": "性别 male/female(必填)", "required": True},
"beauty_enabled": {"type": "boolean", "description": "是否开启美颜(本期不生效)", "default": False},
"beauty_enabled": {"type": "boolean", "description": "是否开启美颜效果"},
}
_GROW_B_FORMS = {
"marked_image_file": {"type": "file", "description": "划线图片文件JPG/PNG,≤ 1 MB"},
"marked_image_file": {"type": "file", "description": "划线图片文件"},
"marked_image_url": {"type": "string", "description": "划线图片 URL"},
"marked_image_base64": {"type": "string", "description": "划线图片 base64"},
"use_mask": {"type": "boolean",
"description": "是否启用自动检测遮罩,默认 true;false 跳过检测直接送图(空遮罩),测试对比用",
"default": True},
}
_HAIRLINE_FORMS = {
**_MEASURE_FORMS,
"gender": {"type": "string", "description": "性别 male/female(必填)", "required": True},
}
# 路由(path, method) → form 参数表,供 custom_openapi 注入 requestBody。
# 接口4(/api/v1/face/features) 在网关本地用 Form() 声明,FastAPI 自动生成 schema,不在此列。
_ROUTE_FORMS = {
("/api/v1/face/measure", "post"): _MEASURE_FORMS,
("/api/v1/hair/grow", "post"): _GROW_FORMS,
("/api/v1/hair/grow-b", "post"): _GROW_B_FORMS,
("/api/v1/hairline/generate", "post"): _HAIRLINE_FORMS,
}
@@ -339,49 +320,3 @@ async def face_features(
async def hairline_generate(request: Request):
"""接口5:发际线PNG生成"""
return await _proxy(request, "/api/v1/hairline/generate")
# ---------------------------------------------------------------------------
# 自定义 OpenAPI:代理路由用 Request 透传、不声明 FormFastAPI 默认生成不出入参。
# 这里把 _ROUTE_FORMS 注入各路由的 multipart/form-data requestBody,使 /docs 入参完整。
# ---------------------------------------------------------------------------
def _form_schema(forms: dict) -> dict:
"""form 参数表 → multipart/form-data 的 JSON Schema。"""
props, required = {}, []
for name, spec in forms.items():
prop = ({"type": "string", "format": "binary"} if spec["type"] == "file"
else {"type": spec["type"]})
if spec.get("description"):
prop["description"] = spec["description"]
if "default" in spec:
prop["default"] = spec["default"]
props[name] = prop
if spec.get("required"):
required.append(name)
schema = {"type": "object", "properties": props}
if required:
schema["required"] = required
return schema
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
from fastapi.openapi.utils import get_openapi
schema = get_openapi(title=app.title, version=app.version,
description=app.description, routes=app.routes)
for (path, method), forms in _ROUTE_FORMS.items():
op = schema.get("paths", {}).get(path, {}).get(method)
if op is not None:
op["requestBody"] = {
"required": True,
"content": {"multipart/form-data": {"schema": _form_schema(forms)}},
}
app.openapi_schema = schema
return schema
app.openapi = custom_openapi