cocktailpeanut commited on
Commit
a5e6b9f
1 Parent(s): 3760ea6
Files changed (2) hide show
  1. app.py +46 -10
  2. requirements.txt +5 -4
app.py CHANGED
@@ -5,7 +5,7 @@ import cv2
5
  import gradio as gr
6
  import numpy as np
7
  import PIL
8
- import spaces
9
  import torch
10
  from diffusers.models import ControlNetModel
11
  from diffusers.utils import load_image
@@ -17,7 +17,16 @@ from style_template import styles
17
 
18
  # global variable
19
  MAX_SEED = np.iinfo(np.int32).max
20
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
21
  STYLE_NAMES = list(styles.keys())
22
  DEFAULT_STYLE_NAME = "Watercolor"
23
 
@@ -31,6 +40,7 @@ hf_hub_download(
31
  local_dir="./checkpoints",
32
  )
33
  hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")
 
34
 
35
  # Load face encoder
36
  app = FaceAnalysis(name="antelopev2", root="./", providers=["CPUExecutionProvider"])
@@ -39,23 +49,49 @@ app.prepare(ctx_id=0, det_size=(640, 640))
39
  # Path to InstantID models
40
  face_adapter = "./checkpoints/ip-adapter.bin"
41
  controlnet_path = "./checkpoints/ControlNetModel"
 
42
 
43
  # Load pipeline
44
- controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
 
45
 
46
  base_model_path = "wangqixun/YamerMIX_v8"
47
 
48
  pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
49
  base_model_path,
50
  controlnet=controlnet,
51
- torch_dtype=torch.float16,
 
52
  safety_checker=None,
53
  feature_extractor=None,
54
  )
55
- pipe.cuda()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  pipe.load_ip_adapter_instantid(face_adapter)
57
- pipe.image_proj_model.to("cuda")
58
- pipe.unet.to("cuda")
 
 
 
 
59
 
60
 
61
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
@@ -187,7 +223,7 @@ def check_input_image(face_image):
187
  raise gr.Error("Cannot find any input face image! Please upload the face image")
188
 
189
 
190
- @spaces.GPU
191
  def generate_image(
192
  face_image_path,
193
  pose_image_path,
@@ -369,14 +405,14 @@ with gr.Blocks(css=css) as demo:
369
  minimum=20,
370
  maximum=100,
371
  step=1,
372
- value=30,
373
  )
374
  guidance_scale = gr.Slider(
375
  label="Guidance scale",
376
  minimum=0.1,
377
  maximum=10.0,
378
  step=0.1,
379
- value=5,
380
  )
381
  seed = gr.Slider(
382
  label="Seed",
 
5
  import gradio as gr
6
  import numpy as np
7
  import PIL
8
+ #import spaces
9
  import torch
10
  from diffusers.models import ControlNetModel
11
  from diffusers.utils import load_image
 
17
 
18
  # global variable
19
  MAX_SEED = np.iinfo(np.int32).max
20
+ #device = "cuda" if torch.cuda.is_available() else "cpu"
21
+ torch_dtype = torch.float16
22
+ if torch.backends.mps.is_available():
23
+ device = "mps"
24
+ torch_dtype = torch.float32
25
+ elif torch.cuda.is_available():
26
+ device = "cuda"
27
+ else:
28
+ device = "cpu"
29
+
30
  STYLE_NAMES = list(styles.keys())
31
  DEFAULT_STYLE_NAME = "Watercolor"
32
 
 
40
  local_dir="./checkpoints",
41
  )
42
  hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")
43
+ hf_hub_download(repo_id="latent-consistency/lcm-lora-sdxl", filename="pytorch_lora_weights.safetensors", local_dir="./checkpoints")
44
 
45
  # Load face encoder
46
  app = FaceAnalysis(name="antelopev2", root="./", providers=["CPUExecutionProvider"])
 
49
  # Path to InstantID models
50
  face_adapter = "./checkpoints/ip-adapter.bin"
51
  controlnet_path = "./checkpoints/ControlNetModel"
52
+ lcm_lora_path = "./checkpoints/pytorch_lora_weights.safetensors"
53
 
54
  # Load pipeline
55
+ #controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
56
+ controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch_dtype)
57
 
58
  base_model_path = "wangqixun/YamerMIX_v8"
59
 
60
  pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
61
  base_model_path,
62
  controlnet=controlnet,
63
+ #torch_dtype=torch.float16,
64
+ torch_dtype=torch_dtype,
65
  safety_checker=None,
66
  feature_extractor=None,
67
  )
68
+ #pipe.cuda()
69
+
70
+ num_inference_steps = 30
71
+ guidance_scale = 5
72
+
73
+ +# LCM
74
+ if os.environ.get("LCM"):
75
+ num_inference_steps = 10
76
+ guidance_scale = 0
77
+
78
+ pipe.load_lora_weights(lcm_lora_path)
79
+ pipe.fuse_lora()
80
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
81
+
82
+ if device == 'mps':
83
+ pipe.to("mps", torch_dtype)
84
+ pipe.enable_attention_slicing()
85
+ elif device == 'cuda':
86
+ pipe.cuda()
87
+
88
  pipe.load_ip_adapter_instantid(face_adapter)
89
+ #pipe.image_proj_model.to("cuda")
90
+ #pipe.unet.to("cuda")
91
+ if device == 'mps' or device == 'cuda':
92
+ pipe.image_proj_model.to(device)
93
+ pipe.unet.to(device)
94
+
95
 
96
 
97
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
 
223
  raise gr.Error("Cannot find any input face image! Please upload the face image")
224
 
225
 
226
+ #@spaces.GPU
227
  def generate_image(
228
  face_image_path,
229
  pose_image_path,
 
405
  minimum=20,
406
  maximum=100,
407
  step=1,
408
+ value=num_inference_steps,
409
  )
410
  guidance_scale = gr.Slider(
411
  label="Guidance scale",
412
  minimum=0.1,
413
  maximum=10.0,
414
  step=0.1,
415
+ value=guidance_scale,
416
  )
417
  seed = gr.Slider(
418
  label="Seed",
requirements.txt CHANGED
@@ -1,14 +1,15 @@
1
  diffusers==0.25.0
2
- torch==2.0.0
3
- torchvision==0.15.1
4
  transformers==4.36.2
5
  accelerate
6
  safetensors
7
  einops
8
- onnxruntime-gpu
 
9
  spaces==0.19.4
10
  omegaconf
11
  peft
12
  huggingface-hub==0.20.2
13
  opencv-python
14
- insightface
 
1
  diffusers==0.25.0
2
+ #torch==2.0.0
3
+ #torchvision==0.15.1
4
  transformers==4.36.2
5
  accelerate
6
  safetensors
7
  einops
8
+ #onnxruntime-gpu
9
+ onnxruntime
10
  spaces==0.19.4
11
  omegaconf
12
  peft
13
  huggingface-hub==0.20.2
14
  opencv-python
15
+ insightface