yashvii commited on
Commit
9e82706
1 Parent(s): 3286552

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. gradio_demo/app.py +30 -244
gradio_demo/app.py CHANGED
@@ -30,7 +30,6 @@ from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstant
30
  from model_util import load_models_xl, get_torch_device, torch_gc
31
 
32
  import os
33
- import requests
34
 
35
  # try:
36
  # # Send a GET request to the URL
@@ -75,21 +74,11 @@ logo = logo.resize((100, 100))
75
  from cv2 import imencode
76
  import base64
77
 
78
- # def encode_pil_to_base64_new(pil_image):
79
- # print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
80
- # image_arr = np.asarray(pil_image)[:,:,::-1]
81
- # _, byte_data = imencode('.png', image_arr)
82
- # base64_data = base64.b64encode(byte_data)
83
- # base64_string_opencv = base64_data.decode("utf-8")
84
- # return "data:image/png;base64," + base64_string_opencv
85
-
86
  import gradio as gr
87
  from google.cloud import storage
88
  from io import BytesIO
89
 
90
 
91
- # gr.processing_utils.encode_pil_to_base64 = encode_pil_to_base64_new
92
-
93
  def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=False):
94
 
95
  if pretrained_model_name_or_path.endswith(
@@ -136,16 +125,8 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
136
  pipe.disable_lora()
137
 
138
  def remove_tips():
139
- print("GG")
140
  return gr.update(visible=False)
141
 
142
-
143
- # prompts = [
144
- # ["superman","Vibrant Color"], ["japanese anime character with white/neon hair","Watercolor"],
145
- # # ["Suited professional","(No style)"],
146
- # ["Scooba diver","Line art"], ["eskimo","Snow"]
147
- # ]
148
-
149
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
150
  return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
151
 
@@ -170,28 +151,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
170
  # if email != "":
171
  p,n = styles.get(style, styles.get(STYLE_NAMES[1]))
172
  return generate_image(face_file, p[3], n)
173
-
174
- # def validate_and_process(face_file, style, email):
175
-
176
- # # Your processing logic here
177
- # gallery1, gallery2, gallery3, gallery4 = run_for_prompts1(face_file, style), run_for_prompts2(face_file, style), run_for_prompts3(face_file, style), run_for_prompts4(face_file, style)
178
- # return gallery1, gallery2, gallery3, gallery4
179
-
180
- # def upload_image_to_gcs(source_file_name, destination_blob_name):
181
- # bucket_name="idfy-gff-public"
182
- # # Initialize a GCP client
183
- # storage_client = storage.Client()
184
-
185
- # # Get the bucket
186
- # bucket = storage_client.get_bucket(bucket_name)
187
-
188
- # # Create a blob object from the filename
189
- # blob = bucket.blob(destination_blob_name)
190
-
191
- # # Upload the file to GCS
192
- # blob.upload_from_filename(source_file_name)
193
-
194
- # print(f"File {source_file_name} uploaded to {destination_blob_name}.")
195
 
196
  def upload_pil_image_to_gcs(image, destination_blob_name):
197
  bucket_name="idfy-gff-public"
@@ -212,8 +171,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
212
  # Upload the image to GCS
213
  blob.upload_from_file(image_byte_array, content_type=f'image/png')
214
 
215
- print(f"Image uploaded to {destination_blob_name} in bucket {bucket_name}.")
216
-
217
  def draw_kps(image_pil, kps, color_list=[(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]):
218
  stickwidth = 4
219
  limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]])
@@ -246,7 +203,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
246
  pad_to_max_side=True, mode=PIL.Image.BILINEAR, base_pixel_number=64):
247
 
248
  w, h = input_image.size
249
- print(f"Original Size --> {input_image.size}")
250
  if size is not None:
251
  w_resize_new, h_resize_new = size
252
  else:
@@ -264,15 +220,13 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
264
  offset_y = (max_side - h_resize_new) // 2
265
  res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
266
  input_image = Image.fromarray(res)
267
-
268
- print(f"Final modified image size --> {input_image.size}")
269
  return input_image
270
 
271
  # def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
272
  # p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
273
  # return p.replace("{prompt}", positive), n + ' ' + negative
274
 
275
- def store_images(email, gallery1, gallery2, gallery3, gallery4,consent):
276
  if not email:
277
  raise gr.Error("Email Id not provided")
278
  if not consent:
@@ -280,8 +234,7 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
280
  for i, img in enumerate([gallery1, gallery2, gallery3, gallery4], start=1):
281
  if isinstance(img, np.ndarray):
282
  img = Image.fromarray(img)
283
- print(f"Gallery {i} type after conversion: {type(img)}")
284
- dest = f'{email}/img{i}.png'
285
  upload_pil_image_to_gcs(img,dest)
286
  gr.Info("Thankyou!! Your avatar is on the way to your inbox")
287
  return None,None,None,None,None
@@ -337,10 +290,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
337
  num_steps = 15
338
  guidance_scale = 5
339
  seed = random.randint(0, MAX_SEED)
340
- print(f"Seed --> {seed}")
341
-
342
- # negative_prompt = ""
343
- # negative_prompt += neg
344
  enhance_face_region = True
345
  if enable_LCM:
346
  pipe.enable_lora()
@@ -351,14 +300,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
351
 
352
  if face_image is None:
353
  raise gr.Error(f"Cannot find any input face image! Please upload the face image")
354
-
355
- # if prompt is None:
356
- # prompt = "a person"
357
-
358
- # apply the style template
359
- # prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
360
-
361
- # face_image = load_image(face_image_path)
362
  face_image = resize_img(face_image)
363
  face_image_cv2 = convert_from_image_to_cv2(face_image)
364
  height, width, _ = face_image_cv2.shape
@@ -399,9 +340,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
399
 
400
  generator = torch.Generator(device=device).manual_seed(seed)
401
 
402
- print("Start inference...")
403
- print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
404
-
405
  pipe.set_ip_adapter_scale(adapter_strength_ratio)
406
  images = pipe(
407
  prompt=prompt,
@@ -434,21 +372,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
434
  article = r""""""
435
 
436
  tips = r""""""
437
- # css = '''
438
- # .gradio-container {
439
- # width: 95% !important;
440
- # background-image: url('./InstantID/gradio_demo/logo.png');
441
- # background-size: cover;
442
- # background-position: center;
443
- # }
444
- # .image-gallery {
445
- # height: 100vh !important;
446
- # overflow: auto;
447
- # }
448
- # .gradio-row .gradio-element {
449
- # margin: 0 !important;
450
- # }
451
- # '''
452
  css = '''
453
  .gradio-container {width: 100% !important; color: white; background: linear-gradient(135deg, #1C43B9, #254977, #343434);}
454
  .gradio-row .gradio-element { margin: 0 !important; }
@@ -477,38 +400,14 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
477
  submit = gr.Button("Submit", variant="primary",elem_id="submit-btn")
478
  with gr.Column():
479
  with gr.Row():
480
- gallery1 = gr.Image(label="Generated Images", interactive=False)
481
- gallery2 = gr.Image(label="Generated Images", interactive=False)
482
  with gr.Row():
483
- gallery3 = gr.Image(label="Generated Images", interactive=False)
484
- gallery4 = gr.Image(label="Generated Images", interactive=False)
485
  email = gr.Textbox(label="Email", info="Enter your email address", value="")
486
  consent = gr.Checkbox(label="I am giving my consent to use my data to share my AI Avtar and IDfy relevant information from time to time", value=True)
487
  submit1 = gr.Button("SUBMIT", variant = "primary", elem_id="store-btn")
488
- # with gr.Blocks(css=css) as demo:
489
-
490
- # # description
491
- # gr.Markdown(title)
492
- # with gr.Column():
493
- # with gr.Row():
494
- # gr.Image("./gradio_demo/logo.png",scale=0,min_width=50,show_label=False,show_download_button=False)
495
- # gr.Markdown(description)
496
- # style = gr.Dropdown(label="Choose your STYLE", choices=STYLE_NAMES)
497
- # face_file = gr.Image(label="Upload a photo of your face", type="pil",sources="webcam", height=400, width=500)
498
- # submit = gr.Button("Submit", variant="primary")
499
- # with gr.Column():
500
- # with gr.Row():
501
- # gallery1 = gr.Image(label="Generated Images")
502
- # gallery2 = gr.Image(label="Generated Images")
503
- # with gr.Row():
504
- # gallery3 = gr.Image(label="Generated Images")
505
- # gallery4 = gr.Image(label="Generated Images")
506
- # email = gr.Textbox(label="Email",
507
- # info="Enter your email address",
508
- # value="")
509
- # consent = gr.Checkbox(label="I am giving my consent to use my data to share my AI Avtar and IDfy relevant information from time to time")
510
- # submit1 = gr.Button("STORE", variant="primary")
511
- # # submit1 = gr.Button("Store")
512
  usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)
513
 
514
  face_file.upload(
@@ -534,33 +433,33 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
534
  inputs=[face_file,style],
535
  outputs=[gallery4]
536
  )
537
- # submit.click(
538
- # fn=remove_tips,
539
- # outputs=usage_tips,
540
- # queue=True,
541
- # api_name=False,
542
- # show_progress = "full"
543
- # ).then(
544
- # fn=run_for_prompts1,
545
- # inputs=[face_file,style],
546
- # outputs=[gallery1]
547
- # ).then(
548
- # fn=run_for_prompts2,
549
- # inputs=[face_file,style],
550
- # outputs=[gallery2]
551
- # ).then(
552
- # fn=run_for_prompts3,
553
- # inputs=[face_file,style],
554
- # outputs=[gallery3]
555
- # ).then(
556
- # fn=run_for_prompts4,
557
- # inputs=[face_file,style],
558
- # outputs=[gallery4]
559
- # )
560
 
561
  submit1.click(
562
  fn=store_images,
563
- inputs=[email,gallery1,gallery2,gallery3,gallery4,consent],
564
  outputs=[face_file,gallery1,gallery2,gallery3,gallery4])
565
 
566
 
@@ -568,119 +467,6 @@ def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=F
568
  gr.Markdown(article)
569
 
570
  demo.launch(share=True)
571
-
572
- # with gr.Blocks(css=css, js=js) as demo:
573
-
574
- # # description
575
- # gr.Markdown(title)
576
- # with gr.Row():
577
- # gr.Image("./gradio_demo/logo.png",scale=0,min_width=50,show_label=False,show_download_button=False)
578
- # gr.Markdown(description)
579
- # with gr.Row():
580
- # with gr.Column():
581
- # style = gr.Dropdown(label="Choose your STYLE", choices=STYLE_NAMES)
582
- # face_file = gr.Image(label="Upload a photo of your face", type="pil",sources="webcam")
583
- # submit = gr.Button("Submit", variant="primary")
584
- # with gr.Column():
585
- # with gr.Row():
586
- # gallery1 = gr.Image(label="Generated Images")
587
- # gallery2 = gr.Image(label="Generated Images")
588
- # with gr.Row():
589
- # gallery3 = gr.Image(label="Generated Images")
590
- # gallery4 = gr.Image(label="Generated Images")
591
- # email = gr.Textbox(label="Email",
592
- # info="Enter your email address",
593
- # value="")
594
-
595
- # usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)
596
- # # identitynet_strength_ratio = gr.Slider(
597
- # # label="IdentityNet strength (for fidelity)",
598
- # # minimum=0,
599
- # # maximum=1.5,
600
- # # step=0.05,
601
- # # value=0.95,
602
- # # )
603
- # # adapter_strength_ratio = gr.Slider(
604
- # # label="Image adapter strength (for detail)",
605
- # # minimum=0,
606
- # # maximum=1.5,
607
- # # step=0.05,
608
- # # value=0.60,
609
- # # )
610
- # # negative_prompt = gr.Textbox(
611
- # # label="Negative Prompt",
612
- # # placeholder="low quality",
613
- # # value="(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
614
- # # )
615
- # # num_steps = gr.Slider(
616
- # # label="Number of sample steps",
617
- # # minimum=15,
618
- # # maximum=100,
619
- # # step=1,
620
- # # value=5 if enable_lcm_arg else 15,
621
- # # )
622
- # # guidance_scale = gr.Slider(
623
- # # label="Guidance scale",
624
- # # minimum=0.1,
625
- # # maximum=10.0,
626
- # # step=0.1,
627
- # # value=0 if enable_lcm_arg else 8.5,
628
- # # )
629
- # # if email is None:
630
- # # print("STOPPPP")
631
- # # raise gr.Error("Email ID is compulsory")
632
- # face_file.upload(
633
- # fn=remove_tips,
634
- # outputs=usage_tips,
635
- # queue=True,
636
- # api_name=False,
637
- # show_progress = "full"
638
- # ).then(
639
- # fn=run_for_prompts1,
640
- # inputs=[face_file,style],
641
- # outputs=[gallery1]
642
- # ).then(
643
- # fn=run_for_prompts2,
644
- # inputs=[face_file,style],
645
- # outputs=[gallery2]
646
- # ).then(
647
- # fn=run_for_prompts3,
648
- # inputs=[face_file,style],
649
- # outputs=[gallery3]
650
- # ).then(
651
- # fn=run_for_prompts4,
652
- # inputs=[face_file,style],
653
- # outputs=[gallery4]
654
- # )
655
- # submit.click(
656
- # fn=remove_tips,
657
- # outputs=usage_tips,
658
- # queue=True,
659
- # api_name=False,
660
- # show_progress = "full"
661
- # ).then(
662
- # fn=run_for_prompts1,
663
- # inputs=[face_file,style],
664
- # outputs=[gallery1]
665
- # ).then(
666
- # fn=run_for_prompts2,
667
- # inputs=[face_file,style],
668
- # outputs=[gallery2]
669
- # ).then(
670
- # fn=run_for_prompts3,
671
- # inputs=[face_file,style],
672
- # outputs=[gallery3]
673
- # ).then(
674
- # fn=run_for_prompts4,
675
- # inputs=[face_file,style],
676
- # outputs=[gallery4]
677
- # )
678
-
679
-
680
- # gr.Markdown(article)
681
-
682
- # demo.launch(share=True)
683
-
684
  if __name__ == "__main__":
685
  parser = argparse.ArgumentParser()
686
  parser.add_argument("--pretrained_model_name_or_path", type=str, default="wangqixun/YamerMIX_v8")
 
30
  from model_util import load_models_xl, get_torch_device, torch_gc
31
 
32
  import os
 
33
 
34
  # try:
35
  # # Send a GET request to the URL
 
74
  from cv2 import imencode
75
  import base64
76
 
 
 
 
 
 
 
 
 
77
  import gradio as gr
78
  from google.cloud import storage
79
  from io import BytesIO
80
 
81
 
 
 
82
  def main(pretrained_model_name_or_path="wangqixun/YamerMIX_v8", enable_lcm_arg=False):
83
 
84
  if pretrained_model_name_or_path.endswith(
 
125
  pipe.disable_lora()
126
 
127
  def remove_tips():
 
128
  return gr.update(visible=False)
129
 
 
 
 
 
 
 
 
130
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
131
  return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
132
 
 
151
  # if email != "":
152
  p,n = styles.get(style, styles.get(STYLE_NAMES[1]))
153
  return generate_image(face_file, p[3], n)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  def upload_pil_image_to_gcs(image, destination_blob_name):
156
  bucket_name="idfy-gff-public"
 
171
  # Upload the image to GCS
172
  blob.upload_from_file(image_byte_array, content_type=f'image/png')
173
 
 
 
174
  def draw_kps(image_pil, kps, color_list=[(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]):
175
  stickwidth = 4
176
  limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]])
 
203
  pad_to_max_side=True, mode=PIL.Image.BILINEAR, base_pixel_number=64):
204
 
205
  w, h = input_image.size
 
206
  if size is not None:
207
  w_resize_new, h_resize_new = size
208
  else:
 
220
  offset_y = (max_side - h_resize_new) // 2
221
  res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
222
  input_image = Image.fromarray(res)
 
 
223
  return input_image
224
 
225
  # def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
226
  # p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
227
  # return p.replace("{prompt}", positive), n + ' ' + negative
228
 
229
+ def store_images(email, gallery1, gallery2, gallery3, gallery4,consent,style):
230
  if not email:
231
  raise gr.Error("Email Id not provided")
232
  if not consent:
 
234
  for i, img in enumerate([gallery1, gallery2, gallery3, gallery4], start=1):
235
  if isinstance(img, np.ndarray):
236
  img = Image.fromarray(img)
237
+ dest = f'{email}/img{i}@{style}.png'
 
238
  upload_pil_image_to_gcs(img,dest)
239
  gr.Info("Thankyou!! Your avatar is on the way to your inbox")
240
  return None,None,None,None,None
 
290
  num_steps = 15
291
  guidance_scale = 5
292
  seed = random.randint(0, MAX_SEED)
 
 
 
 
293
  enhance_face_region = True
294
  if enable_LCM:
295
  pipe.enable_lora()
 
300
 
301
  if face_image is None:
302
  raise gr.Error(f"Cannot find any input face image! Please upload the face image")
 
 
 
 
 
 
 
 
303
  face_image = resize_img(face_image)
304
  face_image_cv2 = convert_from_image_to_cv2(face_image)
305
  height, width, _ = face_image_cv2.shape
 
340
 
341
  generator = torch.Generator(device=device).manual_seed(seed)
342
 
 
 
 
343
  pipe.set_ip_adapter_scale(adapter_strength_ratio)
344
  images = pipe(
345
  prompt=prompt,
 
372
  article = r""""""
373
 
374
  tips = r""""""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
  css = '''
376
  .gradio-container {width: 100% !important; color: white; background: linear-gradient(135deg, #1C43B9, #254977, #343434);}
377
  .gradio-row .gradio-element { margin: 0 !important; }
 
400
  submit = gr.Button("Submit", variant="primary",elem_id="submit-btn")
401
  with gr.Column():
402
  with gr.Row():
403
+ gallery1 = gr.Image(label="Generated Images", interactive=False, height=640, width=640)
404
+ gallery2 = gr.Image(label="Generated Images", interactive=False, height=640, width=640)
405
  with gr.Row():
406
+ gallery3 = gr.Image(label="Generated Images", interactive=False, height=640, width=640)
407
+ gallery4 = gr.Image(label="Generated Images", interactive=False, height=640, width=640)
408
  email = gr.Textbox(label="Email", info="Enter your email address", value="")
409
  consent = gr.Checkbox(label="I am giving my consent to use my data to share my AI Avtar and IDfy relevant information from time to time", value=True)
410
  submit1 = gr.Button("SUBMIT", variant = "primary", elem_id="store-btn")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411
  usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)
412
 
413
  face_file.upload(
 
433
  inputs=[face_file,style],
434
  outputs=[gallery4]
435
  )
436
+ submit.click(
437
+ fn=remove_tips,
438
+ outputs=usage_tips,
439
+ queue=True,
440
+ api_name=False,
441
+ show_progress = "full"
442
+ ).then(
443
+ fn=run_for_prompts1,
444
+ inputs=[face_file,style],
445
+ outputs=[gallery1]
446
+ ).then(
447
+ fn=run_for_prompts2,
448
+ inputs=[face_file,style],
449
+ outputs=[gallery2]
450
+ ).then(
451
+ fn=run_for_prompts3,
452
+ inputs=[face_file,style],
453
+ outputs=[gallery3]
454
+ ).then(
455
+ fn=run_for_prompts4,
456
+ inputs=[face_file,style],
457
+ outputs=[gallery4]
458
+ )
459
 
460
  submit1.click(
461
  fn=store_images,
462
+ inputs=[email,gallery1,gallery2,gallery3,gallery4,consent,style],
463
  outputs=[face_file,gallery1,gallery2,gallery3,gallery4])
464
 
465
 
 
467
  gr.Markdown(article)
468
 
469
  demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
  if __name__ == "__main__":
471
  parser = argparse.ArgumentParser()
472
  parser.add_argument("--pretrained_model_name_or_path", type=str, default="wangqixun/YamerMIX_v8")