BiliSakura commited on
Commit
270f50f
·
verified ·
1 Parent(s): 6e28688

Fix inference type coercion for Gradio float inputs (ADM)

Browse files
Files changed (2) hide show
  1. app.py +59 -2
  2. model_loader.py +46 -8
app.py CHANGED
@@ -17,6 +17,7 @@ except ImportError: # Local development without the Spaces runtime.
17
 
18
  spaces = _SpacesStub() # type: ignore[assignment]
19
 
 
20
  from typing import Any
21
 
22
  import gradio as gr
@@ -28,7 +29,7 @@ from model_catalog import (
28
  get_profile_by_label,
29
  parse_model_label,
30
  )
31
- from model_loader import PIPELINE_MANAGER, run_inference
32
 
33
 
34
  DEFAULT_MODEL = MODEL_LABELS[0]
@@ -112,6 +113,34 @@ def on_model_change(model_label: str):
112
  return _config_from_profile(get_profile_by_label(model_label))
113
 
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  def _gpu_duration(
116
  model_label: str,
117
  num_steps: int,
@@ -127,6 +156,7 @@ def _gpu_duration(
127
  noise_scale: float,
128
  ) -> int:
129
  profile = get_profile_by_label(model_label)
 
130
  step_budget = num_steps if not profile.steps_are_list else max(num_steps, 40)
131
  base = 45 if profile.gpu_size == "large" else 90
132
  return int(min(300, max(base, step_budget * 0.6 + 30)))
@@ -167,6 +197,32 @@ def _generate_on_gpu(
167
  guidance_interval_max: float,
168
  noise_scale: float,
169
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  profile = get_profile_by_label(model_label)
171
  collection, variant = parse_model_label(model_label)
172
  if PIPELINE_MANAGER.loaded_label != model_label or PIPELINE_MANAGER.pipe is None:
@@ -228,7 +284,8 @@ def generate(
228
  noise_scale,
229
  )
230
  except Exception as exc:
231
- raise gr.Error(f"Generation failed for `{model_label}`: {exc}") from exc
 
232
  label = PIPELINE_MANAGER.loaded_label or model_label
233
  return f"Generated with `{label}`.", image
234
 
 
17
 
18
  spaces = _SpacesStub() # type: ignore[assignment]
19
 
20
+ import traceback
21
  from typing import Any
22
 
23
  import gradio as gr
 
29
  get_profile_by_label,
30
  parse_model_label,
31
  )
32
+ from model_loader import PIPELINE_MANAGER, _to_float, _to_int, run_inference
33
 
34
 
35
  DEFAULT_MODEL = MODEL_LABELS[0]
 
113
  return _config_from_profile(get_profile_by_label(model_label))
114
 
115
 
116
+ def _coerce_inference_inputs(
117
+ class_label: Any,
118
+ seed: Any,
119
+ num_steps: Any,
120
+ guidance_scale: Any,
121
+ height: Any,
122
+ width: Any,
123
+ guidance_interval_start: Any,
124
+ guidance_interval_end: Any,
125
+ guidance_interval_min: Any,
126
+ guidance_interval_max: Any,
127
+ noise_scale: Any,
128
+ ) -> tuple[str, int, int, float, int, int, float, float, float, float, float]:
129
+ return (
130
+ str(class_label or "").strip(),
131
+ _to_int(seed, default=42),
132
+ _to_int(num_steps, default=50),
133
+ _to_float(guidance_scale, default=4.0),
134
+ _to_int(height, default=256),
135
+ _to_int(width, default=256),
136
+ _to_float(guidance_interval_start, default=0.0),
137
+ _to_float(guidance_interval_end, default=0.7),
138
+ _to_float(guidance_interval_min, default=0.2),
139
+ _to_float(guidance_interval_max, default=0.6),
140
+ _to_float(noise_scale, default=4.0),
141
+ )
142
+
143
+
144
  def _gpu_duration(
145
  model_label: str,
146
  num_steps: int,
 
156
  noise_scale: float,
157
  ) -> int:
158
  profile = get_profile_by_label(model_label)
159
+ num_steps = _to_int(num_steps, default=profile.default_steps)
160
  step_budget = num_steps if not profile.steps_are_list else max(num_steps, 40)
161
  base = 45 if profile.gpu_size == "large" else 90
162
  return int(min(300, max(base, step_budget * 0.6 + 30)))
 
197
  guidance_interval_max: float,
198
  noise_scale: float,
199
  ):
200
+ (
201
+ class_label,
202
+ seed,
203
+ num_steps,
204
+ guidance_scale,
205
+ height,
206
+ width,
207
+ guidance_interval_start,
208
+ guidance_interval_end,
209
+ guidance_interval_min,
210
+ guidance_interval_max,
211
+ noise_scale,
212
+ ) = _coerce_inference_inputs(
213
+ class_label,
214
+ seed,
215
+ num_steps,
216
+ guidance_scale,
217
+ height,
218
+ width,
219
+ guidance_interval_start,
220
+ guidance_interval_end,
221
+ guidance_interval_min,
222
+ guidance_interval_max,
223
+ noise_scale,
224
+ )
225
+
226
  profile = get_profile_by_label(model_label)
227
  collection, variant = parse_model_label(model_label)
228
  if PIPELINE_MANAGER.loaded_label != model_label or PIPELINE_MANAGER.pipe is None:
 
284
  noise_scale,
285
  )
286
  except Exception as exc:
287
+ detail = traceback.format_exc(limit=6).strip()
288
+ raise gr.Error(f"Generation failed for `{model_label}`: {exc}\n\n{detail}") from exc
289
  label = PIPELINE_MANAGER.loaded_label or model_label
290
  return f"Generated with `{label}`.", image
291
 
model_loader.py CHANGED
@@ -170,11 +170,36 @@ class PipelineManager:
170
  PIPELINE_MANAGER = PipelineManager()
171
 
172
 
173
- def build_inference_steps(profile: ModelProfile, steps: int) -> int | list[int]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  if profile.steps_are_list:
175
  per_stage = max(1, steps // 4)
176
  return [per_stage, per_stage, per_stage, per_stage]
177
- return int(steps)
178
 
179
 
180
  def run_inference(
@@ -189,19 +214,32 @@ def run_inference(
189
  width: int,
190
  extra_kwargs: dict[str, Any] | None = None,
191
  ) -> Any:
192
- generator = torch.Generator(device="cuda").manual_seed(int(seed))
 
 
 
 
 
 
193
  call_kwargs: dict[str, Any] = {
194
  "num_inference_steps": build_inference_steps(profile, num_steps),
195
- "guidance_scale": float(guidance_scale),
196
  "generator": generator,
197
  }
198
  call_kwargs.update(extra_kwargs if extra_kwargs is not None else profile.extra_call_kwargs)
199
 
200
- label = class_label.strip() or profile.default_class_label
201
- call_kwargs["class_labels"] = int(label) if label.isdigit() else label
 
 
 
 
 
202
 
 
203
  if height > 0 and width > 0:
204
- call_kwargs["height"] = int(height)
205
- call_kwargs["width"] = int(width)
 
206
 
207
  return pipe(**call_kwargs).images[0]
 
170
  PIPELINE_MANAGER = PipelineManager()
171
 
172
 
173
+ def _to_int(value: Any, *, default: int = 0) -> int:
174
+ if value is None:
175
+ return default
176
+ if isinstance(value, bool):
177
+ return int(value)
178
+ if isinstance(value, (int, float)):
179
+ return int(value)
180
+ text = str(value).strip()
181
+ if not text:
182
+ return default
183
+ return int(float(text))
184
+
185
+
186
+ def _to_float(value: Any, *, default: float = 0.0) -> float:
187
+ if value is None:
188
+ return default
189
+ if isinstance(value, (int, float)):
190
+ return float(value)
191
+ text = str(value).strip()
192
+ if not text:
193
+ return default
194
+ return float(text)
195
+
196
+
197
+ def build_inference_steps(profile: ModelProfile, steps: Any) -> int | list[int]:
198
+ steps = _to_int(steps, default=profile.default_steps)
199
  if profile.steps_are_list:
200
  per_stage = max(1, steps // 4)
201
  return [per_stage, per_stage, per_stage, per_stage]
202
+ return steps
203
 
204
 
205
  def run_inference(
 
214
  width: int,
215
  extra_kwargs: dict[str, Any] | None = None,
216
  ) -> Any:
217
+ seed = _to_int(seed, default=profile.default_seed)
218
+ num_steps = _to_int(num_steps, default=profile.default_steps)
219
+ guidance_scale = _to_float(guidance_scale, default=profile.default_guidance)
220
+ height = _to_int(height, default=0)
221
+ width = _to_int(width, default=0)
222
+
223
+ generator = torch.Generator(device="cuda").manual_seed(seed)
224
  call_kwargs: dict[str, Any] = {
225
  "num_inference_steps": build_inference_steps(profile, num_steps),
226
+ "guidance_scale": guidance_scale,
227
  "generator": generator,
228
  }
229
  call_kwargs.update(extra_kwargs if extra_kwargs is not None else profile.extra_call_kwargs)
230
 
231
+ label = str(class_label or "").strip() or profile.default_class_label
232
+ if label.isdigit():
233
+ call_kwargs["class_labels"] = int(label)
234
+ elif label.replace(".", "", 1).isdigit():
235
+ call_kwargs["class_labels"] = int(float(label))
236
+ else:
237
+ call_kwargs["class_labels"] = label
238
 
239
+ native = profile.infer_resolution()
240
  if height > 0 and width > 0:
241
+ if profile.collection != "ADM-diffusers" or height != native or width != native:
242
+ call_kwargs["height"] = height
243
+ call_kwargs["width"] = width
244
 
245
  return pipe(**call_kwargs).images[0]