rickveloper commited on
Commit
0c9c5cf
·
verified ·
1 Parent(s): 7cd5b28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -46
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 call_public_space_dynamic(prompt: str, width: int, height: int) -> Image.Image:
124
- # Discover an api_name and feed only fields we can infer.
125
  from gradio_client import Client
126
- client = Client(PUBLIC_SPACE_ID)
127
- info = client.view_api(all_endpoints=True)
128
- last_err = None
129
- for ep in info:
130
- api = ep.get("api_name")
131
- params = ep.get("parameters", [])
132
- # Build arg list by index
133
- args = [None] * len(params)
134
- bad = False
135
- for idx, p in enumerate(params):
136
- label = (p.get("label") or "").lower()
137
- # Skip endpoints that clearly require an image upload
138
- if "image" in label and "prompt" not in label:
139
- bad = True
140
- break
141
- if "prompt" in label or label in ("prompt", "text", "caption"):
142
- args[idx] = prompt
143
- elif "width" in label:
144
- args[idx] = int(width)
145
- elif "height" in label:
146
- args[idx] = int(height)
147
- elif "negative" in label:
148
- args[idx] = ""
149
- elif "steps" in label or "num_inference_steps" in label:
150
- args[idx] = 4
151
- elif "guidance" in label or "cfg" in label:
152
- args[idx] = 3.5
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 = call_public_space_dynamic(prompt, width, height)
183
- return img, f"✅ Public Space: **{PUBLIC_SPACE_ID}** (discovered endpoint)"
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