k-l-lambda commited on
Commit
cc3a0a5
1 Parent(s): 9e8fb21

app.py: added api payload control.

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py CHANGED
@@ -355,6 +355,70 @@ def generate_image (
355
  return image, gr.update(visible=True)
356
 
357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
  # Description
359
  title = r'''
360
  <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds (via Novita)</h1>
@@ -543,6 +607,8 @@ with gr.Blocks(css=css) as demo:
543
  label='InstantID Usage Tips', value=tips, visible=False
544
  )
545
 
 
 
546
  submit.click(
547
  fn=remove_tips,
548
  outputs=usage_tips,
@@ -552,6 +618,31 @@ with gr.Blocks(css=css) as demo:
552
  outputs=seed,
553
  queue=False,
554
  api_name=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
555
  ).then(
556
  fn=generate_image,
557
  inputs=[
 
355
  return image, gr.update(visible=True)
356
 
357
 
358
+ def get_payload (
359
+ model_name,
360
+ lora_selection,
361
+ face_image_path,
362
+ pose_image_path,
363
+ prompt,
364
+ negative_prompt,
365
+ style_name,
366
+ num_steps,
367
+ identitynet_strength_ratio,
368
+ adapter_strength_ratio,
369
+ controlnet_strength_1, controlnet_strength_2, controlnet_strength_3, controlnet_strength_4,
370
+ controlnet_selection,
371
+ guidance_scale,
372
+ seed,
373
+ scheduler,
374
+ ):
375
+ prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
376
+
377
+ ref_image_path = pose_image_path if pose_image_path else face_image_path
378
+ ref_image = PIL.Image.open(ref_image_path)
379
+
380
+ width, height = ref_image.size
381
+ large_edge = max(width, height)
382
+ if large_edge < 1024:
383
+ scaling = 1024 / large_edge
384
+ width = int(width * scaling)
385
+ height = int(height * scaling)
386
+
387
+ (
388
+ CONTROLNET_DICT['pose'].strength,
389
+ CONTROLNET_DICT['canny'].strength,
390
+ CONTROLNET_DICT['depth'].strength,
391
+ CONTROLNET_DICT['lineart'].strength,
392
+ ) = [controlnet_strength_1, controlnet_strength_2, controlnet_strength_3, controlnet_strength_4]
393
+
394
+ return {
395
+ 'extra': {
396
+ 'response_image_type': 'jpeg',
397
+ },
398
+ 'model_name': f'{model_name}.safetensors',
399
+ 'face_image_assets_ids': "[assets_ids of id image, please manually upload to novita.ai]",
400
+ 'ref_image_assets_ids': "[assets_ids of reference image, please manually upload to novita.ai]",
401
+ 'prompt': prompt,
402
+ 'negative_prompt': negative_prompt,
403
+ 'controlnet': {
404
+ 'units': [CONTROLNET_DICT[name] for name in controlnet_selection if name in CONTROLNET_DICT],
405
+ },
406
+ 'loras': [dict(
407
+ model_name=f'{name}.safetensors',
408
+ strength=1,
409
+ ) for name in lora_selection],
410
+ 'image_num': 1,
411
+ 'steps': num_steps,
412
+ 'seed': seed,
413
+ 'guidance_scale': guidance_scale,
414
+ 'sampler_name': scheduler,
415
+ 'id_strength': identitynet_strength_ratio,
416
+ 'adapter_strength': adapter_strength_ratio,
417
+ 'width': width,
418
+ 'height': height,
419
+ }
420
+
421
+
422
  # Description
423
  title = r'''
424
  <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds (via Novita)</h1>
 
607
  label='InstantID Usage Tips', value=tips, visible=False
608
  )
609
 
610
+ api_payload = gr.JSON(label="Novita API Payload, POST /v3/async/instant-id")
611
+
612
  submit.click(
613
  fn=remove_tips,
614
  outputs=usage_tips,
 
618
  outputs=seed,
619
  queue=False,
620
  api_name=False,
621
+ ).then(
622
+ fn=get_payload,
623
+ inputs=[
624
+ model_name,
625
+ lora_selection,
626
+ face_file,
627
+ pose_file,
628
+ prompt,
629
+ negative_prompt,
630
+ style,
631
+ num_steps,
632
+ identitynet_strength_ratio,
633
+ adapter_strength_ratio,
634
+ #[
635
+ pose_strength,
636
+ canny_strength,
637
+ depth_strength,
638
+ lineart_strength,
639
+ #],
640
+ controlnet_selection,
641
+ guidance_scale,
642
+ seed,
643
+ scheduler,
644
+ ],
645
+ outputs=api_payload,
646
  ).then(
647
  fn=generate_image,
648
  inputs=[