Spaces:
Running
on
Zero
Running
on
Zero
Commit
•
3a1e48f
1
Parent(s):
758de8d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from insightface.app import FaceAnalysis
|
3 |
+
import torch
|
4 |
+
from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
|
5 |
+
from PIL import Image
|
6 |
+
from ip_adapter.ip_adapter_faceid import IPAdapterFaceID
|
7 |
+
|
8 |
+
app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
9 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
10 |
+
|
11 |
+
base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
|
12 |
+
vae_model_path = "stabilityai/sd-vae-ft-mse"
|
13 |
+
ip_ckpt = hf_hub_download(repo_id='h94/IP-Adapter-FaceID', filename="ip-adapter-faceid_sd15.bin", repo_type="model")
|
14 |
+
|
15 |
+
device = "cuda"
|
16 |
+
|
17 |
+
noise_scheduler = DDIMScheduler(
|
18 |
+
num_train_timesteps=1000,
|
19 |
+
beta_start=0.00085,
|
20 |
+
beta_end=0.012,
|
21 |
+
beta_schedule="scaled_linear",
|
22 |
+
clip_sample=False,
|
23 |
+
set_alpha_to_one=False,
|
24 |
+
steps_offset=1,
|
25 |
+
)
|
26 |
+
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
|
27 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
28 |
+
base_model_path,
|
29 |
+
torch_dtype=torch.float16,
|
30 |
+
scheduler=noise_scheduler,
|
31 |
+
vae=vae,
|
32 |
+
#feature_extractor=None,
|
33 |
+
#safety_checker=None
|
34 |
+
)
|
35 |
+
|
36 |
+
ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
|
37 |
+
|
38 |
+
def generate_faceid_embeddings(image):
|
39 |
+
#image = cv2.imread("person.jpg")
|
40 |
+
faces = app.get(image)
|
41 |
+
faceid_embeds = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
|
42 |
+
return faceid_embeds
|
43 |
+
|
44 |
+
def generate_image(image, prompt, negative_prompt):
|
45 |
+
faceid_embeds = generate_faceid_embeddings(image)
|
46 |
+
images = ip_model.generate(
|
47 |
+
prompt=prompt, negative_prompt=negative_prompt, faceid_embeds=faceid_embeds, width=512, height=512, num_inference_steps=30
|
48 |
+
)
|
49 |
+
return images.image[0]
|
50 |
+
|
51 |
+
demo = gr.Interface(fn=generate_image, inputs=[gr.Image(label="Your face"), gr.Textbox(label="Prompt"), gr.Textbox(label="Negative Prompt")], outputs=[gr.Image(label="Generated Image")])
|
52 |
+
demo.launch()
|