File size: 10,354 Bytes
3249e1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Remote tool calls to public Hugging Face Spaces via gradio_client.

ZeroGPU billing: when this Space forwards the visitor's `x-ip-token` header, the remote
ZeroGPU Space bills the *visitor's* free daily GPU minutes, not the owner's
(see https://huggingface.co/docs/hub/spaces-zerogpu). No HF token of ours is used.

Endpoint signatures were read with Client.view_api() on Oct 8, 2026 and saved in
../sources/hf-api/*.json. Community Spaces can change; update the wrappers if they do.
MiniMax-H3 is intentionally NOT included (its license bans use in the US, incl. outputs).
"""
from __future__ import annotations
import json, os, tempfile, time, urllib.request
from typing import Any

try:
    from gradio_client import Client, handle_file
except Exception:  # pragma: no cover
    Client = None
    handle_file = None

# tool key -> ordered list of Spaces (primary first, then fallbacks)
TOOLS: dict[str, list[str]] = {
    "edit": ["black-forest-labs/FLUX.1-Kontext-Dev", "Qwen/Qwen-Image-Edit-2509"],
    "same_face": ["yanze/PuLID-FLUX", "InstantX/InstantID"],
    "outpaint": ["fffiloni/diffusers-image-outpaint"],
    "i2v": ["zerogpu-aoti/wan2-2-fp8da-aoti-faster", "Lightricks/ltx-video-distilled"],
    "flf_loop": ["linoyts/LTX-2-3-First-Last-Frame"],
    "upscale": ["finegrain/finegrain-image-enhancer"],
}
LICENSE_NOTES = {
    "black-forest-labs/FLUX.1-Kontext-Dev": "FLUX.1 [dev] Non-Commercial License for the model; its terms allow commercial use of outputs (verify before selling)",
    "Qwen/Qwen-Image-Edit-2509": "Apache-2.0",
    "yanze/PuLID-FLUX": "PuLID Apache-2.0 on a FLUX.1 [dev] base (same output terms as above)",
    "InstantX/InstantID": "Apache-2.0 (SDXL base, OpenRAIL++)",
    "fffiloni/diffusers-image-outpaint": "community Space (SDXL ControlNet Union; OpenRAIL++)",
    "zerogpu-aoti/wan2-2-fp8da-aoti-faster": "Wan2.2: Apache-2.0",
    "Lightricks/ltx-video-distilled": "LTXV Open Weights License (free under $10M revenue)",
    "linoyts/LTX-2-3-First-Last-Frame": "LTX-2 Community License (free under $10M revenue)",
    "finegrain/finegrain-image-enhancer": "community Space (SD1.5 + ESRGAN; check model licenses)",
}
DEFAULT_TIMEOUT = 600
_status_cache: dict[str, tuple[float, str]] = {}


class ToolError(RuntimeError):
    pass


def _friendly(e: Exception) -> str:
    msg = str(e)
    low = msg.lower()
    if any(k in low for k in ("quota", "zerogpu", "gpu task aborted", "exceeded", "you have exceeded")):
        return "Daily free GPU minutes used up for this tool. Log in to Hugging Face for more minutes, or try again tomorrow."
    if "timeout" in low or "timed out" in low:
        return "The remote tool took too long. It may be busy; try again in a minute."
    if "runtime_error" in low or "could not fetch config" in low or "space is not running" in low:
        return "That remote tool is down right now."
    return msg[:400]


def space_status(space: str) -> str:
    now = time.time()
    if space in _status_cache and now - _status_cache[space][0] < 60:
        return _status_cache[space][1]
    try:
        with urllib.request.urlopen(f"https://huggingface.co/api/spaces/{space}/runtime", timeout=6) as r:
            stage = json.load(r).get("stage", "UNKNOWN")
    except Exception:
        stage = "UNKNOWN"
    _status_cache[space] = (now, stage)
    return stage


def status_table() -> list[list[str]]:
    rows = []
    for tool, spaces in TOOLS.items():
        for i, sp in enumerate(spaces):
            st = space_status(sp)
            light = "🟢" if st == "RUNNING" else ("🟡" if st in ("SLEEPING", "BUILDING", "APP_STARTING", "PAUSED") else "🔴")
            rows.append([tool, "primary" if i == 0 else "fallback", sp, f"{light} {st}", LICENSE_NOTES.get(sp, "")])
    return rows


def _client(space: str, request=None):
    if Client is None:
        raise ToolError("gradio_client is not installed.")
    headers = {}
    if request is not None:
        try:
            tok = request.headers.get("x-ip-token")
            if tok:
                headers["x-ip-token"] = tok
        except Exception:
            pass
    return Client(space, headers=headers or None, verbose=False, download_files=tempfile.mkdtemp(prefix="aiking_dl_"))


def _first_path(x: Any) -> str | None:
    """Pull a local file path out of whatever a Space returned."""
    if x is None:
        return None
    if isinstance(x, str):
        return x
    if isinstance(x, dict):
        for k in ("path", "video", "image", "name", "url"):
            if k in x and x[k]:
                return _first_path(x[k])
    if isinstance(x, (list, tuple)):
        for item in x:
            p = _first_path(item)
            if p:
                return p
    return None


def _call(space, api_name, request=None, timeout=DEFAULT_TIMEOUT, **kwargs):
    job = _client(space, request).submit(api_name=api_name, **kwargs)
    return job.result(timeout=timeout)


def _with_fallback(tool: str, runners: dict, request=None):
    errors = []
    for sp in TOOLS[tool]:
        if space_status(sp) in ("RUNTIME_ERROR", "BUILD_ERROR", "CONFIG_ERROR", "NO_APP_FILE", "STOPPED"):
            errors.append(f"{sp}: down")
            continue
        try:
            return runners[sp](sp), sp
        except Exception as e:
            errors.append(f"{sp}: {_friendly(e)}")
    raise ToolError(" | ".join(errors))


# ---------- tool wrappers ----------

def edit_image(image_path, prompt, request=None):
    runners = {
        "black-forest-labs/FLUX.1-Kontext-Dev": lambda sp: _first_path(_call(
            sp, "/infer", request, input_image=handle_file(image_path), prompt=prompt,
            seed=0, randomize_seed=True, guidance_scale=2.5, steps=28)),
        "Qwen/Qwen-Image-Edit-2509": lambda sp: _first_path(_call(
            sp, "/infer", request, images=[{"image": handle_file(image_path), "caption": None}], prompt=prompt,
            seed=0, randomize_seed=True, true_guidance_scale=4.0, num_inference_steps=40,
            height=1024, width=1024, rewrite_prompt=False)),
    }
    return _with_fallback("edit", runners, request)


def same_face(face_path, prompt, negative, width=896, height=1152, request=None):
    runners = {
        "yanze/PuLID-FLUX": lambda sp: _first_path(_call(
            sp, "/generate_image", request, prompt=prompt, id_image=handle_file(face_path),
            start_step=0, guidance=4, seed="-1", true_cfg=1, width=width, height=height,
            num_steps=20, id_weight=1, neg_prompt=negative, timestep_to_start_cfg=1, max_sequence_length=128)),
        "InstantX/InstantID": lambda sp: _first_path(_call(
            sp, "/generate_image", request, face_image_path=handle_file(face_path), pose_image_path=None,
            prompt=prompt, negative_prompt=negative, style_name="(No style)", num_steps=30,
            identitynet_strength_ratio=0.8, adapter_strength_ratio=0.8, canny_strength=0.4, depth_strength=0.4,
            controlnet_selection=["depth"], guidance_scale=5.0, seed=42, scheduler="EulerDiscreteScheduler",
            enable_LCM=False, enhance_face_region=True)),
    }
    return _with_fallback("same_face", runners, request)


def outpaint(image_path, width, height, prompt="", alignment="Middle", request=None):
    runners = {
        "fffiloni/diffusers-image-outpaint": lambda sp: _first_path(list(reversed(list(_call(
            sp, "/infer", request, image=handle_file(image_path), width=int(width), height=int(height),
            overlap_percentage=10, num_inference_steps=8, resize_option="Full", custom_resize_percentage=50,
            prompt_input=prompt or "", alignment=alignment, overlap_left=True, overlap_right=True,
            overlap_top=True, overlap_bottom=True) or [])))),
    }
    return _with_fallback("outpaint", runners, request)


def image_to_video(image_path, prompt, negative="", seconds=5.0, request=None):
    runners = {
        "zerogpu-aoti/wan2-2-fp8da-aoti-faster": lambda sp: _first_path(_call(
            sp, "/generate_video", request, input_image=handle_file(image_path), prompt=prompt, steps=6,
            negative_prompt=negative or "worst quality, blurry, distorted face, extra limbs, text, watermark",
            duration_seconds=float(seconds), guidance_scale=1, guidance_scale_2=1, seed=42, randomize_seed=True)),
        "Lightricks/ltx-video-distilled": lambda sp: _first_path(_call(
            sp, "/image_to_video", request, prompt=prompt,
            negative_prompt=negative or "worst quality, inconsistent motion, blurry, jittery, distorted",
            input_image_filepath=handle_file(image_path), input_video_filepath=None, height_ui=512, width_ui=704,
            mode="image-to-video", duration_ui=min(float(seconds), 8.5), ui_frames_to_use=9, seed_ui=42,
            randomize_seed=True, ui_guidance_scale=1, improve_texture_flag=True)),
    }
    return _with_fallback("i2v", runners, request)


def first_last_loop(first_path, last_path, prompt, seconds=4.0, request=None):
    def run(sp):
        w, h = 1536, 1024
        try:
            w, h = _call(sp, "/on_image_upload", request, timeout=60, first_image=handle_file(first_path),
                         last_image=handle_file(last_path or first_path), high_res=False)
        except Exception:
            pass
        return _first_path(_call(
            sp, "/generate_video", request, first_image=handle_file(first_path),
            last_image=handle_file(last_path or first_path), input_audio=None, prompt=prompt,
            duration=float(seconds), enhance_prompt=False, seed=10, randomize_seed=True, height=int(h), width=int(w)))
    return _with_fallback("flf_loop", {"linoyts/LTX-2-3-First-Last-Frame": run}, request)


def upscale(image_path, factor=2, prompt="masterpiece, best quality, highres", request=None):
    runners = {
        "finegrain/finegrain-image-enhancer": lambda sp: _first_path(list(reversed(list(_call(
            sp, "/process", request, input_image=handle_file(image_path), prompt=prompt,
            negative_prompt="worst quality, low quality, blurry, watermark", seed=42, upscale_factor=float(factor),
            controlnet_scale=0.6, controlnet_decay=1.0, condition_scale=6, tile_width=112, tile_height=144,
            denoise_strength=0.35, num_inference_steps=18, solver="DDIM") or [])))),
    }
    return _with_fallback("upscale", runners, request)