gbarbadillo commited on
Commit
e23754f
1 Parent(s): 42f9313

added app and requirements

Browse files
Files changed (2) hide show
  1. app.py +100 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
3
+ from PIL import Image
4
+ from ip_adapter.ip_adapter_faceid import IPAdapterFaceIDPlus
5
+ import cv2
6
+ from insightface.app import FaceAnalysis
7
+ from insightface.utils import face_align
8
+
9
+
10
+ base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
11
+ vae_model_path = "stabilityai/sd-vae-ft-mse"
12
+ #image_encoder_path = "h94/IP-Adapter/models/image_encoder"
13
+ image_encoder_path = "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
+
45
+ ip_model = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_ckpt, device, num_tokens=4, torch_dtype=torch_dtype)
46
+
47
+
48
+
49
+ app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
50
+ app.prepare(ctx_id=0, det_size=(640, 640), det_thresh=0.2)
51
+
52
+ def generate_images(img_filepath, prompt, n_images=3,
53
+ negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality, blurry",
54
+ img_prompt_scale=0.5,
55
+ num_inference_steps=30,
56
+ seed=None):
57
+ print(prompt)
58
+ image = cv2.imread(img_filepath)
59
+ faces = app.get(image)
60
+
61
+ faceid_embeds = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
62
+ face_image = face_align.norm_crop(image, landmark=faces[0].kps, image_size=224) # you can also segment the face
63
+ images = ip_model.generate(
64
+ prompt=prompt, negative_prompt=negative_prompt, face_image=face_image, faceid_embeds=faceid_embeds,
65
+ num_samples=n_images, width=512, height=512, num_inference_steps=num_inference_steps, seed=seed,
66
+ scale=img_prompt_scale, # with scale=1 I get weird images
67
+ )
68
+ return [images, Image.fromarray(face_image[..., [2, 1, 0]])]
69
+
70
+ import gradio as gr
71
+ with gr.Blocks() as demo:
72
+ gr.Markdown(
73
+ """
74
+ # IP-Adapter-FaceID-plus
75
+ Generate images conditioned on a image prompt and a text prompt. Learn more here: https://huggingface.co/h94/IP-Adapter-FaceID
76
+ """)
77
+ with gr.Row():
78
+ with gr.Column():
79
+ demo_inputs = []
80
+ demo_inputs.append(gr.Image(type='filepath', label='image prompt'))
81
+ demo_inputs.append(gr.Textbox(label='text prompt', value='headshot of a man, green moss wall in the background'))
82
+ demo_inputs.append(gr.Slider(maximum=3, minimum=1, value=3, step=1, label='number of images'))
83
+ with gr.Accordion(label='Advanced options', open=False):
84
+ demo_inputs.append(gr.Textbox(label='negative text prompt', value="monochrome, lowres, bad anatomy, worst quality, low quality, blurry"))
85
+ demo_inputs.append(gr.Slider(maximum=1, minimum=0, value=0.5, step=0.05, label='image prompt scale'))
86
+ btn = gr.Button("Generate")
87
+
88
+ with gr.Column():
89
+ demo_outputs = []
90
+ demo_outputs.append(gr.Gallery(label='generated images'))
91
+ demo_outputs.append(gr.Image(label='detected face', height=224, width=224))
92
+ btn.click(generate_images, inputs=demo_inputs, outputs=demo_outputs)
93
+ sample_prompts = [
94
+ 'headshot of a man, green moss wall in the background',
95
+ 'linkedin profile picture of a macdonalds worker',
96
+ 'LinkedIn profile picture of a beautiful man dressed in a suit, huge explosion in the background',
97
+ ]
98
+ gr.Examples(sample_prompts, inputs=demo_inputs[1], label='Sample prompts')
99
+
100
+ demo.launch(share=True, debug=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ insightface
2
+ onnxruntime
3
+ diffusers==0.22.1
4
+ einops
5
+ accelerate
6
+ git+https://github.com/ironbar/IP-Adapter.git@add-cpu-support