AbstractPhil commited on
Commit
377ff40
Β·
verified Β·
1 Parent(s): b6b9cb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -133,8 +133,9 @@ def encode_sdxl_prompt(pipe, prompt, negative_prompt, device):
133
 
134
  # ─── Main Inference Function ──────────────────────────────────
135
  @spaces.GPU
136
- def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, noise, gate_prob,
137
- use_anchor, steps, cfg_scale, scheduler_name, width, height, seed):
 
138
 
139
  global t5_tok, t5_mod, pipe
140
  device = torch.device("cuda")
@@ -193,10 +194,17 @@ def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, noi
193
  # Handle different return formats
194
  anchor_l = adapter_output[0]
195
  delta_l = adapter_output[1]
 
196
  gate_l = adapter_output[-1] if len(adapter_output) > 2 else torch.ones_like(delta_l)
197
  tau_l = adapter_output[-2] if len(adapter_output) > 6 else torch.tensor(1.0)
198
  g_pred_l = adapter_output[-3] if len(adapter_output) > 6 else torch.tensor(1.0)
199
 
 
 
 
 
 
 
200
  # Apply gate scaling
201
  gate_l_scaled = torch.sigmoid(gate_l) * gate_prob
202
 
@@ -206,11 +214,16 @@ def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, noi
206
  # Apply delta to embeddings
207
  clip_l_mod = clip_embeds["clip_l"] + delta_l_final.to(dtype)
208
 
 
 
 
 
 
209
  # Apply anchor mixing if enabled
210
  if use_anchor:
211
  clip_l_mod = clip_l_mod * (1 - gate_l_scaled.to(dtype)) + anchor_l.to(dtype) * gate_l_scaled.to(dtype)
212
 
213
- # Add noise if specified
214
  if noise > 0:
215
  clip_l_mod += torch.randn_like(clip_l_mod) * noise
216
  else:
@@ -233,10 +246,17 @@ def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, noi
233
  # Handle different return formats
234
  anchor_g = adapter_output[0]
235
  delta_g = adapter_output[1]
 
236
  gate_g = adapter_output[-1] if len(adapter_output) > 2 else torch.ones_like(delta_g)
237
  tau_g = adapter_output[-2] if len(adapter_output) > 6 else torch.tensor(1.0)
238
  g_pred_g = adapter_output[-3] if len(adapter_output) > 6 else torch.tensor(1.0)
239
 
 
 
 
 
 
 
240
  # Apply gate scaling
241
  gate_g_scaled = torch.sigmoid(gate_g) * gate_prob
242
 
@@ -246,11 +266,16 @@ def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, noi
246
  # Apply delta to embeddings
247
  clip_g_mod = clip_embeds["clip_g"] + delta_g_final.to(dtype)
248
 
 
 
 
 
 
249
  # Apply anchor mixing if enabled
250
  if use_anchor:
251
  clip_g_mod = clip_g_mod * (1 - gate_g_scaled.to(dtype)) + anchor_g.to(dtype) * gate_g_scaled.to(dtype)
252
 
253
- # Add noise if specified
254
  if noise > 0:
255
  clip_g_mod += torch.randn_like(clip_g_mod) * noise
256
  else:
@@ -330,8 +355,11 @@ def create_interface():
330
 
331
  # Controls
332
  gr.Markdown("### πŸŽ›οΈ Adapter Controls")
333
- strength = gr.Slider(0.0, 5.0, value=1.0, step=0.1, label="Adapter Strength")
334
- noise = gr.Slider(0.0, 1.0, value=0.0, step=0.05, label="Noise Injection")
 
 
 
335
  gate_prob = gr.Slider(0.0, 1.0, value=1.0, step=0.05, label="Gate Probability")
336
  use_anchor = gr.Checkbox(label="Use Anchor Points", value=True)
337
 
@@ -385,8 +413,9 @@ def create_interface():
385
  generate_btn.click(
386
  fn=run_generation,
387
  inputs=[
388
- prompt, negative_prompt, adapter_l, adapter_g, strength, noise, gate_prob,
389
- use_anchor, steps, cfg_scale, scheduler_name, width, height, seed
 
390
  ],
391
  outputs=[output_image, delta_l_img, gate_l_img, delta_g_img, gate_g_img, stats_l_text, stats_g_text]
392
  )
 
133
 
134
  # ─── Main Inference Function ──────────────────────────────────
135
  @spaces.GPU
136
+ def infer(prompt, negative_prompt, adapter_l_file, adapter_g_file, strength, delta_scale,
137
+ sigma_scale, gpred_scale, noise, gate_prob, use_anchor, steps, cfg_scale,
138
+ scheduler_name, width, height, seed):
139
 
140
  global t5_tok, t5_mod, pipe
141
  device = torch.device("cuda")
 
194
  # Handle different return formats
195
  anchor_l = adapter_output[0]
196
  delta_l = adapter_output[1]
197
+ log_sigma_l = adapter_output[2] if len(adapter_output) > 2 else torch.zeros_like(delta_l)
198
  gate_l = adapter_output[-1] if len(adapter_output) > 2 else torch.ones_like(delta_l)
199
  tau_l = adapter_output[-2] if len(adapter_output) > 6 else torch.tensor(1.0)
200
  g_pred_l = adapter_output[-3] if len(adapter_output) > 6 else torch.tensor(1.0)
201
 
202
+ # Scale delta values
203
+ delta_l = delta_l * delta_scale
204
+
205
+ # Apply g_pred scaling to gate
206
+ gate_l = gate_l * g_pred_l * gpred_scale
207
+
208
  # Apply gate scaling
209
  gate_l_scaled = torch.sigmoid(gate_l) * gate_prob
210
 
 
214
  # Apply delta to embeddings
215
  clip_l_mod = clip_embeds["clip_l"] + delta_l_final.to(dtype)
216
 
217
+ # Apply sigma-based noise if specified
218
+ if sigma_scale > 0:
219
+ sigma_l = torch.exp(log_sigma_l * sigma_scale)
220
+ clip_l_mod += torch.randn_like(clip_l_mod) * sigma_l.to(dtype)
221
+
222
  # Apply anchor mixing if enabled
223
  if use_anchor:
224
  clip_l_mod = clip_l_mod * (1 - gate_l_scaled.to(dtype)) + anchor_l.to(dtype) * gate_l_scaled.to(dtype)
225
 
226
+ # Add additional noise if specified
227
  if noise > 0:
228
  clip_l_mod += torch.randn_like(clip_l_mod) * noise
229
  else:
 
246
  # Handle different return formats
247
  anchor_g = adapter_output[0]
248
  delta_g = adapter_output[1]
249
+ log_sigma_g = adapter_output[2] if len(adapter_output) > 2 else torch.zeros_like(delta_g)
250
  gate_g = adapter_output[-1] if len(adapter_output) > 2 else torch.ones_like(delta_g)
251
  tau_g = adapter_output[-2] if len(adapter_output) > 6 else torch.tensor(1.0)
252
  g_pred_g = adapter_output[-3] if len(adapter_output) > 6 else torch.tensor(1.0)
253
 
254
+ # Scale delta values
255
+ delta_g = delta_g * delta_scale
256
+
257
+ # Apply g_pred scaling to gate
258
+ gate_g = gate_g * g_pred_g * gpred_scale
259
+
260
  # Apply gate scaling
261
  gate_g_scaled = torch.sigmoid(gate_g) * gate_prob
262
 
 
266
  # Apply delta to embeddings
267
  clip_g_mod = clip_embeds["clip_g"] + delta_g_final.to(dtype)
268
 
269
+ # Apply sigma-based noise if specified
270
+ if sigma_scale > 0:
271
+ sigma_g = torch.exp(log_sigma_g * sigma_scale)
272
+ clip_g_mod += torch.randn_like(clip_g_mod) * sigma_g.to(dtype)
273
+
274
  # Apply anchor mixing if enabled
275
  if use_anchor:
276
  clip_g_mod = clip_g_mod * (1 - gate_g_scaled.to(dtype)) + anchor_g.to(dtype) * gate_g_scaled.to(dtype)
277
 
278
+ # Add additional noise if specified
279
  if noise > 0:
280
  clip_g_mod += torch.randn_like(clip_g_mod) * noise
281
  else:
 
355
 
356
  # Controls
357
  gr.Markdown("### πŸŽ›οΈ Adapter Controls")
358
+ strength = gr.Slider(0.0, 5.0, value=1.0, step=0.01, label="Adapter Strength")
359
+ delta_scale = gr.Slider(-15.0, 15.0, value=1.0, step=0.1, label="Delta Scale", info="Scales the delta values, recommended 1")
360
+ sigma_scale = gr.Slider(-15.0, 15.0, value=1.0, step=0.1, label="Sigma Scale", info="Scales the noise variance, recommended 1")
361
+ gpred_scale = gr.Slider(0.0, 10.0, value=2.0, step=0.1, label="G-Pred Scale", info="Scales the gate prediction, recommended 2")
362
+ noise = gr.Slider(0.0, 1.0, value=0.0, step=0.01, label="Noise Injection")
363
  gate_prob = gr.Slider(0.0, 1.0, value=1.0, step=0.05, label="Gate Probability")
364
  use_anchor = gr.Checkbox(label="Use Anchor Points", value=True)
365
 
 
413
  generate_btn.click(
414
  fn=run_generation,
415
  inputs=[
416
+ prompt, negative_prompt, adapter_l, adapter_g, strength, delta_scale,
417
+ sigma_scale, gpred_scale, noise, gate_prob, use_anchor, steps, cfg_scale,
418
+ scheduler_name, width, height, seed
419
  ],
420
  outputs=[output_image, delta_l_img, gate_l_img, delta_g_img, gate_g_img, stats_l_text, stats_g_text]
421
  )