Instructions to use Charles-Elena/InstantID with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use Charles-Elena/InstantID with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Charles-Elena/InstantID", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Update handler.py
#1
by Jacobmadwed - opened
- handler.py +10 -38
handler.py
CHANGED
|
@@ -2,28 +2,19 @@ import cv2
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
import PIL
|
| 7 |
from PIL import Image
|
| 8 |
from typing import Tuple
|
| 9 |
-
|
| 10 |
import diffusers
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from diffusers.models import ControlNetModel
|
| 13 |
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
|
| 14 |
-
|
| 15 |
-
# from huggingface_hub import hf_hub_download
|
| 16 |
-
|
| 17 |
from insightface.app import FaceAnalysis
|
| 18 |
-
|
| 19 |
from style_template import styles
|
| 20 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
| 21 |
-
|
| 22 |
from controlnet_aux import OpenposeDetector
|
| 23 |
-
|
| 24 |
import torch.nn.functional as F
|
| 25 |
from torchvision.transforms import Compose
|
| 26 |
-
|
| 27 |
import os
|
| 28 |
|
| 29 |
# global variable
|
|
@@ -32,39 +23,22 @@ dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
|
|
| 32 |
STYLE_NAMES = list(styles.keys())
|
| 33 |
DEFAULT_STYLE_NAME = "Spring Festival"
|
| 34 |
|
| 35 |
-
class EndpointHandler
|
| 36 |
def __init__(self, model_dir):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/config.json", local_dir="./checkpoints")
|
| 39 |
-
# hf_hub_download(
|
| 40 |
-
# repo_id="InstantX/InstantID",
|
| 41 |
-
# filename="ControlNetModel/diffusion_pytorch_model.safetensors",
|
| 42 |
-
# local_dir="./checkpoints",
|
| 43 |
-
# )
|
| 44 |
-
# hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")
|
| 45 |
-
|
| 46 |
-
# Load face encoder
|
| 47 |
-
# self.app = FaceAnalysis(
|
| 48 |
-
# name="antelopev2",
|
| 49 |
-
# root="./",
|
| 50 |
-
# providers=["CPUExecutionProvider"],
|
| 51 |
-
# )
|
| 52 |
-
|
| 53 |
-
dir_path = os.path.join("", "models", "antelopev2")
|
| 54 |
-
print(dir_path)
|
| 55 |
-
print(model_dir)
|
| 56 |
-
|
| 57 |
-
self.app = FaceAnalysis(name='antelopev2', root='./', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 58 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 59 |
-
|
| 60 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 61 |
|
| 62 |
# Path to InstantID models
|
| 63 |
-
face_adapter =
|
| 64 |
-
controlnet_path =
|
| 65 |
-
|
| 66 |
-
# face_adapter = f"./checkpoints/ip-adapter.bin"
|
| 67 |
-
# controlnet_path = f"./checkpoints/ControlNetModel"
|
| 68 |
|
| 69 |
# Load pipeline face ControlNetModel
|
| 70 |
self.controlnet_identitynet = ControlNetModel.from_pretrained(
|
|
@@ -165,8 +139,6 @@ class EndpointHandler():
|
|
| 165 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 166 |
return p.replace("{prompt}", positive), n + " " + negative
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
face_image_path = data.pop("face_image_path", "https://i.ibb.co/GQzm527/examples-musk-resize.jpg")
|
| 171 |
pose_image_path = data.pop("pose_image_path", "https://i.ibb.co/TRCK4MS/examples-poses-pose2.jpg")
|
| 172 |
style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
|
|
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import PIL
|
| 6 |
from PIL import Image
|
| 7 |
from typing import Tuple
|
|
|
|
| 8 |
import diffusers
|
| 9 |
from diffusers.utils import load_image
|
| 10 |
from diffusers.models import ControlNetModel
|
| 11 |
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
|
|
|
|
|
|
|
|
|
|
| 12 |
from insightface.app import FaceAnalysis
|
|
|
|
| 13 |
from style_template import styles
|
| 14 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
|
|
|
| 15 |
from controlnet_aux import OpenposeDetector
|
|
|
|
| 16 |
import torch.nn.functional as F
|
| 17 |
from torchvision.transforms import Compose
|
|
|
|
| 18 |
import os
|
| 19 |
|
| 20 |
# global variable
|
|
|
|
| 23 |
STYLE_NAMES = list(styles.keys())
|
| 24 |
DEFAULT_STYLE_NAME = "Spring Festival"
|
| 25 |
|
| 26 |
+
class EndpointHandler:
|
| 27 |
def __init__(self, model_dir):
|
| 28 |
+
dir_path = os.path.join(model_dir, "models", "antelopev2")
|
| 29 |
+
if not os.path.exists(dir_path):
|
| 30 |
+
print(f"Model path {dir_path} does not exist. Attempting to download.")
|
| 31 |
+
self.app = FaceAnalysis(name='antelopev2', root=model_dir, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 32 |
+
else:
|
| 33 |
+
print(f"Model path {dir_path} exists. Skipping download.")
|
| 34 |
+
self.app = FaceAnalysis(name='antelopev2', root=model_dir, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
| 37 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 38 |
|
| 39 |
# Path to InstantID models
|
| 40 |
+
face_adapter = os.path.join(model_dir, "checkpoints", "ip-adapter.bin")
|
| 41 |
+
controlnet_path = os.path.join(model_dir, "checkpoints", "ControlNetModel")
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Load pipeline face ControlNetModel
|
| 44 |
self.controlnet_identitynet = ControlNetModel.from_pretrained(
|
|
|
|
| 139 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 140 |
return p.replace("{prompt}", positive), n + " " + negative
|
| 141 |
|
|
|
|
|
|
|
| 142 |
face_image_path = data.pop("face_image_path", "https://i.ibb.co/GQzm527/examples-musk-resize.jpg")
|
| 143 |
pose_image_path = data.pop("pose_image_path", "https://i.ibb.co/TRCK4MS/examples-poses-pose2.jpg")
|
| 144 |
style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
|