q6 commited on
Commit
293d3de
·
1 Parent(s): 658ab0f

Add-lightweight-UI-GPU-ping

Browse files
Files changed (1) hide show
  1. app.py +55 -2
app.py CHANGED
@@ -107,8 +107,12 @@ PREVIEW_SIZE = 320
107
  IMAGE_KEY_CACHE_SIZE = 512
108
  DEFAULT_RETURN_SCALE = 1
109
  DEFAULT_BATCH_SIZE = 1
110
- DEFAULT_UI_BATCH_SIZE = 2
111
  MAX_BATCH_SIZE = 8
 
 
 
 
112
  STARTUP_ASSET_IDS = {
113
  "checkpoints": (16, 50, 35),
114
  "loras": (1, 3, 4, 30, 47, 12, 7, 49),
@@ -1226,6 +1230,32 @@ def infer_image(
1226
  return image
1227
 
1228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1229
  def generate_gpu(request, regions, style_image, second_style_image):
1230
  latent = state["latent"].generate(
1231
  width=request.width,
@@ -2341,6 +2371,21 @@ def generate(
2341
  ]
2342
 
2343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2344
  def api_generate(body, p=""):
2345
  if not valid_pass(p):
2346
  raise gr.Error("Invalid password")
@@ -3474,7 +3519,9 @@ with gr.Blocks(title="Image generation") as demo:
3474
  step=.01,
3475
  label="Return scale",
3476
  )
3477
- button = gr.Button("Generate", variant="primary")
 
 
3478
  with gr.Column(scale=6, min_width=420):
3479
  output = gr.Gallery(
3480
  label="Preview",
@@ -3539,6 +3586,12 @@ with gr.Blocks(title="Image generation") as demo:
3539
  api_name="ui_generate",
3540
  api_visibility="private",
3541
  )
 
 
 
 
 
 
3542
  upscale_input.change(
3543
  lambda enabled: gr.Column(visible=enabled),
3544
  upscale_input,
 
107
  IMAGE_KEY_CACHE_SIZE = 512
108
  DEFAULT_RETURN_SCALE = 1
109
  DEFAULT_BATCH_SIZE = 1
110
+ DEFAULT_UI_BATCH_SIZE = 1
111
  MAX_BATCH_SIZE = 8
112
+ PING_MODEL_ID = 50
113
+ PING_SIZE = 64
114
+ PING_SAMPLER = "euler"
115
+ PING_SCHEDULER = "simple"
116
  STARTUP_ASSET_IDS = {
117
  "checkpoints": (16, 50, 35),
118
  "loras": (1, 3, 4, 30, 47, 12, 7, 49),
 
1230
  return image
1231
 
1232
 
1233
+ @spaces.GPU(duration=1)
1234
+ def infer_ping(model, clip, vae):
1235
+ with torch.inference_mode():
1236
+ latent = state["latent"].generate(
1237
+ width=PING_SIZE,
1238
+ height=PING_SIZE,
1239
+ batch_size=1,
1240
+ )[0]
1241
+ positive = state["encode"].encode(clip=clip, text="")[0]
1242
+ negative = state["encode"].encode(clip=clip, text="")[0]
1243
+ samples = run_sampler(
1244
+ model,
1245
+ secrets.randbits(64),
1246
+ 1,
1247
+ 1,
1248
+ PING_SAMPLER,
1249
+ PING_SCHEDULER,
1250
+ positive,
1251
+ negative,
1252
+ latent,
1253
+ 1,
1254
+ )
1255
+ image = state["decode"].decode(vae=vae, samples=samples)[0]
1256
+ return tensor_image(image).resize((1, 1), Image.Resampling.LANCZOS)
1257
+
1258
+
1259
  def generate_gpu(request, regions, style_image, second_style_image):
1260
  latent = state["latent"].generate(
1261
  width=request.width,
 
2371
  ]
2372
 
2373
 
2374
+ def ping_image(p=""):
2375
+ if not valid_pass(p):
2376
+ raise gr.Error("Invalid password")
2377
+
2378
+ with lock:
2379
+ prefix = f"{PING_MODEL_ID}_"
2380
+ models = [name for name in generation_models() if name.startswith(prefix)]
2381
+ if len(models) != 1:
2382
+ raise gr.Error(f"Expected one checkpoint with prefix {prefix}")
2383
+ model_name = models[0]
2384
+ model, clip = load_chain(model_name, [])
2385
+ vae = load_vae(vae_name(model_name))
2386
+ return [infer_ping(model, clip, vae)]
2387
+
2388
+
2389
  def api_generate(body, p=""):
2390
  if not valid_pass(p):
2391
  raise gr.Error("Invalid password")
 
3519
  step=.01,
3520
  label="Return scale",
3521
  )
3522
+ with gr.Row():
3523
+ button = gr.Button("Generate", variant="primary")
3524
+ ping_button = gr.Button("Ping")
3525
  with gr.Column(scale=6, min_width=420):
3526
  output = gr.Gallery(
3527
  label="Preview",
 
3586
  api_name="ui_generate",
3587
  api_visibility="private",
3588
  )
3589
+ ping_button.click(
3590
+ ping_image,
3591
+ pass_input,
3592
+ output,
3593
+ api_visibility="private",
3594
+ )
3595
  upscale_input.change(
3596
  lambda enabled: gr.Column(visible=enabled),
3597
  upscale_input,