File size: 8,042 Bytes
c388140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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")