Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,51 +120,36 @@ def call_inference_api(model_id: str, prompt: str, width: int, height: int) -> I
|
|
| 120 |
raise RuntimeError(f"{model_id}:{r.status_code}")
|
| 121 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 122 |
|
| 123 |
-
def
|
| 124 |
-
|
| 125 |
from gradio_client import Client
|
| 126 |
-
client = Client(
|
| 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 |
-
elif "seed" in label:
|
| 154 |
-
args[idx] = 42
|
| 155 |
-
else:
|
| 156 |
-
# leave None; gradio_client will fill defaults
|
| 157 |
-
pass
|
| 158 |
-
if bad: # requires an image input etc.
|
| 159 |
-
continue
|
| 160 |
-
try:
|
| 161 |
-
res = client.predict(*args, api_name=api)
|
| 162 |
-
if isinstance(res, list): res = res[0]
|
| 163 |
-
return Image.open(res).convert("RGB")
|
| 164 |
-
except Exception as e:
|
| 165 |
-
last_err = e
|
| 166 |
-
continue
|
| 167 |
-
raise RuntimeError(f"space-no-endpoint:{last_err}")
|
| 168 |
|
| 169 |
def generate_image_auto(prompt: str, width: int, height: int):
|
| 170 |
tried = []
|
|
@@ -179,8 +164,8 @@ def generate_image_auto(prompt: str, width: int, height: int):
|
|
| 179 |
continue
|
| 180 |
# 2) Public Space dynamic
|
| 181 |
try:
|
| 182 |
-
img =
|
| 183 |
-
|
| 184 |
except Exception as e:
|
| 185 |
tried.append(f"{PUBLIC_SPACE_ID}→{str(e)}")
|
| 186 |
# 3) Gradient
|
|
|
|
| 120 |
raise RuntimeError(f"{model_id}:{r.status_code}")
|
| 121 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 122 |
|
| 123 |
+
def call_public_space(prompt: str, width: int, height: int) -> Image.Image:
|
| 124 |
+
"""Use the FLUX public Space directly via its /infer endpoint."""
|
| 125 |
from gradio_client import Client
|
| 126 |
+
client = Client("black-forest-labs/FLUX.1-schnell")
|
| 127 |
+
# order: prompt, seed, randomize_seed, width, height, num_inference_steps
|
| 128 |
+
result, _seed = client.predict(
|
| 129 |
+
prompt,
|
| 130 |
+
0, # seed (0 = let Space choose unless randomize_seed=False)
|
| 131 |
+
True, # randomize_seed
|
| 132 |
+
int(width),
|
| 133 |
+
int(height),
|
| 134 |
+
4, # num_inference_steps (keep tiny for speed on mobile)
|
| 135 |
+
api_name="/infer"
|
| 136 |
+
)
|
| 137 |
+
# result is a dict with path/url
|
| 138 |
+
path = None
|
| 139 |
+
if isinstance(result, dict):
|
| 140 |
+
path = result.get("path") or result.get("url")
|
| 141 |
+
elif isinstance(result, list) and result:
|
| 142 |
+
item = result[0]
|
| 143 |
+
if isinstance(item, dict):
|
| 144 |
+
path = item.get("path") or item.get("url")
|
| 145 |
+
else:
|
| 146 |
+
path = item
|
| 147 |
+
else:
|
| 148 |
+
path = result
|
| 149 |
+
if not path:
|
| 150 |
+
raise RuntimeError("public-space returned empty result")
|
| 151 |
+
from PIL import Image
|
| 152 |
+
return Image.open(path).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def generate_image_auto(prompt: str, width: int, height: int):
|
| 155 |
tried = []
|
|
|
|
| 164 |
continue
|
| 165 |
# 2) Public Space dynamic
|
| 166 |
try:
|
| 167 |
+
img = call_public_space(prompt, width, height)
|
| 168 |
+
return img, "✅ Public Space: FLUX /infer"
|
| 169 |
except Exception as e:
|
| 170 |
tried.append(f"{PUBLIC_SPACE_ID}→{str(e)}")
|
| 171 |
# 3) Gradient
|