gbarbadillo commited on
Commit
39e14ce
1 Parent(s): e3f1149

refactor and bugfix

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -8,38 +8,7 @@ from insightface.utils import face_align
8
  import gradio as gr
9
  from huggingface_hub import hf_hub_download
10
 
11
- base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
12
- vae_model_path = "stabilityai/sd-vae-ft-mse"
13
- image_encoder_path = "IP-Adapter/models/image_encoder"
14
- ip_ckpt = "IP-Adapter-FaceID/ip-adapter-faceid-plus_sd15.bin"
15
-
16
-
17
- if torch.cuda.is_available():
18
- device = 'cuda'
19
- torch_dtype = torch.float16
20
- else:
21
- device = 'cpu'
22
- torch_dtype = torch.float32
23
- print(f'Using device: {device}')
24
-
25
- noise_scheduler = DDIMScheduler(
26
- num_train_timesteps=1000,
27
- beta_start=0.00085,
28
- beta_end=0.012,
29
- beta_schedule="scaled_linear",
30
- clip_sample=False,
31
- set_alpha_to_one=False,
32
- steps_offset=1,
33
- )
34
- vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch_dtype)
35
- pipe = StableDiffusionPipeline.from_pretrained(
36
- base_model_path,
37
- torch_dtype=torch_dtype,
38
- scheduler=noise_scheduler,
39
- vae=vae,
40
- feature_extractor=None,
41
- safety_checker=None
42
- )
43
 
44
  def download_models():
45
  hf_hub_download(
@@ -56,10 +25,45 @@ def download_models():
56
  local_dir='IP-Adapter')
57
 
58
 
59
- ip_model = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_ckpt, device, num_tokens=4, torch_dtype=torch_dtype)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
 
 
61
 
62
 
 
63
  app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
64
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.2)
65
 
 
8
  import gradio as gr
9
  from huggingface_hub import hf_hub_download
10
 
11
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def download_models():
14
  hf_hub_download(
 
25
  local_dir='IP-Adapter')
26
 
27
 
28
+ def get_ip_model():
29
+ download_models()
30
+ base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
31
+ vae_model_path = "stabilityai/sd-vae-ft-mse"
32
+ image_encoder_path = "IP-Adapter/models/image_encoder"
33
+ ip_ckpt = "IP-Adapter-FaceID/ip-adapter-faceid-plus_sd15.bin"
34
+
35
+ if torch.cuda.is_available():
36
+ device = 'cuda'
37
+ torch_dtype = torch.float16
38
+ else:
39
+ device = 'cpu'
40
+ torch_dtype = torch.float32
41
+ print(f'Using device: {device}')
42
+
43
+ noise_scheduler = DDIMScheduler(
44
+ num_train_timesteps=1000,
45
+ beta_start=0.00085,
46
+ beta_end=0.012,
47
+ beta_schedule="scaled_linear",
48
+ clip_sample=False,
49
+ set_alpha_to_one=False,
50
+ steps_offset=1,
51
+ )
52
+ vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch_dtype)
53
+ pipe = StableDiffusionPipeline.from_pretrained(
54
+ base_model_path,
55
+ torch_dtype=torch_dtype,
56
+ scheduler=noise_scheduler,
57
+ vae=vae,
58
+ feature_extractor=None,
59
+ safety_checker=None
60
+ )
61
 
62
+ ip_model = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_ckpt, device, num_tokens=4, torch_dtype=torch_dtype)
63
+ return ip_model
64
 
65
 
66
+ ip_model = get_ip_model()
67
  app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
68
  app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.2)
69