Spaces:
Sleeping
Sleeping
Download modelscope_ui_app.py from wkea/blockdiffusion-api: direct link, hf CLI and curl.
- Browser
- Download file 8.04 kB
-
https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/modelscope_ui_app.py
- Command line
-
hf download hf://spaces/wkea/blockdiffusion-api/modelscope_ui_app.py
-
curl -L -o modelscope_ui_app.py https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/modelscope_ui_app.py
8.04 kB
| from __future__ import annotations | |
| """ | |
| ModelScope SwingDeploy(FC)UI 函数 app.py(仅适配 text-to-image-synthesis)。 | |
| 用法: | |
| - 将本文件内容复制到 FC 的 model_ui_func 里作为 app.py | |
| - 环境变量由部署自动注入: | |
| - MODEL_ID / MODEL_VERSION / TASK / API_URL / MODEL_BACKEND | |
| 说明: | |
| - UI 会把请求转发到 API_URL + "/invoke"(pipeline 模式默认); | |
| - 兼容服务端返回 image 的两种形式: | |
| - base64 字符串(可带 data:image/... 前缀) | |
| - HWC 的嵌套数组(uint8) | |
| """ | |
| import base64 | |
| import io | |
| import os | |
| import json | |
| from typing import Any, Dict, List, Optional, Tuple, Union | |
| import gradio as gr | |
| import numpy as np | |
| import requests | |
| from PIL import Image | |
| MODEL_ID = os.getenv("MODEL_ID", "") or "" | |
| MODEL_VERSION = os.getenv("MODEL_VERSION", "") or "" | |
| TASK = os.getenv("TASK", "") or "" | |
| API_URL = (os.getenv("API_URL", "") or "").rstrip("/") | |
| MODEL_BACKEND = os.getenv("MODEL_BACKEND", "pipeline") or "pipeline" | |
| # pipeline 默认推理路径为 /invoke;ollama 会是 /v1(这里只做兼容,不建议用 UI 跑 ollama) | |
| INFER_PATH = "v1" if MODEL_BACKEND == "ollama" else "invoke" | |
| INFER_URL = (API_URL + "/" + INFER_PATH) if API_URL else "" | |
| def _unwrap_response(resp_json: Any) -> Dict[str, Any]: | |
| """ | |
| SwingDeploy 常见返回: | |
| - {"Code":200,"Data":{...},...} | |
| 也兼容直接返回 dict 的情况。 | |
| """ | |
| if isinstance(resp_json, dict): | |
| if "Code" in resp_json and resp_json.get("Code") != 200: | |
| raise gr.Error(f"推理失败:Code={resp_json.get('Code')} Message={resp_json.get('Message')}") | |
| data = resp_json.get("Data", None) | |
| if isinstance(data, dict): | |
| return data | |
| # 有些情况下 Data 不是 dict,就当作整体返回 | |
| return resp_json | |
| raise gr.Error("推理返回不是JSON对象") | |
| def _decode_base64_image(s: str) -> Optional[Image.Image]: | |
| s = (s or "").strip() | |
| if not s: | |
| return None | |
| if s.startswith("data:image"): | |
| parts = s.split(",", 1) | |
| if len(parts) == 2: | |
| s = parts[1].strip() | |
| try: | |
| raw = base64.b64decode(s, validate=False) | |
| return Image.open(io.BytesIO(raw)).convert("RGBA") | |
| except Exception: | |
| return None | |
| def _decode_array_image(x: Any) -> Optional[Image.Image]: | |
| try: | |
| a = np.asarray(x) | |
| except Exception: | |
| return None | |
| if a.ndim != 3 or a.shape[-1] not in (3, 4): | |
| return None | |
| if a.dtype != np.uint8: | |
| try: | |
| a = a.astype(np.uint8) | |
| except Exception: | |
| return None | |
| mode = "RGBA" if a.shape[-1] == 4 else "RGB" | |
| try: | |
| return Image.fromarray(a, mode=mode) | |
| except Exception: | |
| return None | |
| def _extract_images(data: Dict[str, Any]) -> List[Image.Image]: | |
| """ | |
| 兼容 keys: | |
| - output_img / output_imgs | |
| - 其他上游包装层(递归查找) | |
| """ | |
| def pick(obj: Any) -> List[Any]: | |
| found: List[Any] = [] | |
| if isinstance(obj, dict): | |
| for k in ("output_imgs", "output_img", "images", "image", "imgs", "img"): | |
| if k in obj and obj[k] is not None: | |
| found.append(obj[k]) | |
| for v in obj.values(): | |
| if isinstance(v, (dict, list)): | |
| found.extend(pick(v)) | |
| elif isinstance(obj, list): | |
| for v in obj: | |
| if isinstance(v, (dict, list)): | |
| found.extend(pick(v)) | |
| return found | |
| candidates = pick(data) | |
| images: List[Image.Image] = [] | |
| def push_one(v: Any) -> None: | |
| if v is None: | |
| return | |
| if isinstance(v, Image.Image): | |
| images.append(v) | |
| return | |
| if isinstance(v, str): | |
| im = _decode_base64_image(v) | |
| if im is not None: | |
| images.append(im) | |
| return | |
| # 数组(HWC) | |
| im = _decode_array_image(v) | |
| if im is not None: | |
| images.append(im) | |
| for c in candidates: | |
| # output_imgs 可能是 list[img] | |
| if isinstance(c, list): | |
| # 也可能是单张 HWC 嵌套数组 | |
| im_single = _decode_array_image(c) | |
| if im_single is not None: | |
| images.append(im_single) | |
| continue | |
| for one in c: | |
| push_one(one) | |
| else: | |
| push_one(c) | |
| if not images: | |
| raise gr.Error("推理成功但未在返回中找到图片(output_img/output_imgs)") | |
| return images | |
| def _post_infer(payload: Dict[str, Any]) -> Dict[str, Any]: | |
| if not INFER_URL: | |
| raise gr.Error("缺少 API_URL 环境变量,无法调用推理接口") | |
| try: | |
| r = requests.post(INFER_URL, json=payload, timeout=600) | |
| except Exception as e: | |
| raise gr.Error(f"请求失败:{e!r}") | |
| try: | |
| j = r.json() | |
| except Exception: | |
| raise gr.Error(f"返回非JSON,HTTP={r.status_code}") | |
| return _unwrap_response(j) | |
| def build_demo() -> gr.Blocks: | |
| title = "ModelScope × FC:text-to-image-synthesis" | |
| article = ( | |
| f"- 模型ID:{MODEL_ID}\n" | |
| f"- 模型版本:{MODEL_VERSION}\n" | |
| f"- TASK:{TASK}\n" | |
| f"- 推理URL:{INFER_URL}\n" | |
| ) | |
| with gr.Blocks(title=title) as demo: | |
| gr.Markdown(f"## {title}") | |
| gr.Markdown(article) | |
| if TASK != "text-to-image-synthesis": | |
| gr.Markdown("### 当前 UI 仅支持 TASK=text-to-image-synthesis") | |
| return demo | |
| with gr.Row(): | |
| prompt = gr.Textbox(label="Prompt", value="stone block texture", lines=2) | |
| def build_seed_component(): | |
| # 兼容旧版 gradio:可能没有 gr.Number | |
| if hasattr(gr, "Number"): | |
| return gr.Number(label="seed(-1表示不传)", value=-1, precision=0) | |
| return gr.Textbox(label="seed(-1表示不传)", value="-1") | |
| def build_json_component(): | |
| # 兼容旧版 gradio:可能没有 gr.JSON | |
| if hasattr(gr, "JSON"): | |
| return gr.JSON(label="原始输出(Data)") | |
| return gr.Textbox(label="原始输出(Data)", lines=12) | |
| with gr.Row(): | |
| n = gr.Slider(label="n(生成张数)", minimum=1, maximum=8, step=1, value=1) | |
| ddim_steps = gr.Slider(label="ddim_steps", minimum=5, maximum=100, step=1, value=50) | |
| cfg_scale = gr.Slider(label="cfg_scale(仅 text_cond 生效)", minimum=0.0, maximum=20.0, step=0.5, value=3.0) | |
| seed = build_seed_component() | |
| run_btn = gr.Button("生成") | |
| # 兼容旧版 gradio:Gallery 可能没有 .style() | |
| gallery = gr.Gallery(label="结果") | |
| raw_json = build_json_component() | |
| def handler(p: str, n_v: int, steps_v: int, cfg_v: float, seed_v: Any): | |
| payload: Dict[str, Any] = { | |
| "input": {"text": p}, | |
| "parameters": {"n": int(n_v), "ddim_steps": int(steps_v), "cfg_scale": float(cfg_v)}, | |
| } | |
| # seed 兼容:Number 会传 float/int,Textbox 会传 str | |
| try: | |
| seed_int = int(float(seed_v)) | |
| except Exception: | |
| seed_int = -1 | |
| if seed_int >= 0: | |
| payload["parameters"]["seed"] = seed_int | |
| data = _post_infer(payload) | |
| imgs = _extract_images(data) | |
| # Gallery 支持 list[PIL.Image] | |
| if hasattr(gr, "JSON"): | |
| return imgs, data | |
| return imgs, json.dumps(data, ensure_ascii=False, indent=2) | |
| run_btn.click(handler, inputs=[prompt, n, ddim_steps, cfg_scale, seed], outputs=[gallery, raw_json]) | |
| return demo | |
| if __name__ == "__main__": | |
| demo = build_demo() | |
| demo.launch(server_name="0.0.0.0") | |