修改接口3 加入是否用遮罩接口
This commit is contained in:
+68
-3
@@ -214,7 +214,8 @@ def _proxy(request: Request, path: str):
|
||||
return proxy_request(request, path)
|
||||
|
||||
|
||||
# 声明各接口的 form 参数用于 OpenAPI schema(实际转发直接读 Request)
|
||||
# 声明各接口的 form 参数 → 注入 OpenAPI schema(实际转发直接读 Request body,不在此解析)。
|
||||
# 每项字段:type(file/string/boolean)、description,可选 required(默认否)/default。
|
||||
_MEASURE_FORMS = {
|
||||
"image_file": {"type": "file", "description": "上传图片文件(JPG/PNG,≤ 1 MB)"},
|
||||
"image_url": {"type": "string", "description": "图片 URL"},
|
||||
@@ -223,13 +224,31 @@ _MEASURE_FORMS = {
|
||||
|
||||
_GROW_FORMS = {
|
||||
**_MEASURE_FORMS,
|
||||
"beauty_enabled": {"type": "boolean", "description": "是否开启美颜效果"},
|
||||
"gender": {"type": "string", "description": "性别 male/female(必填)", "required": True},
|
||||
"beauty_enabled": {"type": "boolean", "description": "是否开启美颜(本期不生效)", "default": False},
|
||||
}
|
||||
|
||||
_GROW_B_FORMS = {
|
||||
"marked_image_file": {"type": "file", "description": "划线图片文件"},
|
||||
"marked_image_file": {"type": "file", "description": "划线图片文件(JPG/PNG,≤ 1 MB)"},
|
||||
"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,
|
||||
}
|
||||
|
||||
|
||||
@@ -320,3 +339,49 @@ async def face_features(
|
||||
async def hairline_generate(request: Request):
|
||||
"""接口5:发际线PNG生成"""
|
||||
return await _proxy(request, "/api/v1/hairline/generate")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 自定义 OpenAPI:代理路由用 Request 透传、不声明 Form,FastAPI 默认生成不出入参。
|
||||
# 这里把 _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
|
||||
|
||||
Reference in New Issue
Block a user