Instructions to use KarthikAI/InstantID-i2i with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use KarthikAI/InstantID-i2i with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("KarthikAI/InstantID-i2i", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
| import base64 | |
| import io | |
| from PIL import Image | |
| import torch | |
| from diffusers import StableDiffusionImg2ImgPipeline | |
| # Global pipeline instance | |
| torch_device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = None | |
| class EndpointHandler: | |
| def __init__(self, model_dir: str): | |
| # model_dir is ignored; HF clones your repo here | |
| pass | |
| def init(self): | |
| global pipe | |
| if pipe is None: | |
| # Load your InstantID img2img model | |
| pipe = StableDiffusionImg2ImgPipeline.from_pretrained( | |
| "karthikAI/InstantID-i2i", | |
| revision="main", | |
| torch_dtype=torch.float16 | |
| ).to(torch_device) | |
| def inference(self, model_inputs: dict) -> dict: | |
| # 1) decode base64 image | |
| b64 = model_inputs.get("inputs") | |
| if b64 is None: | |
| return {"error": "No 'inputs' key with base64 image provided."} | |
| img = Image.open(io.BytesIO(base64.b64decode(b64))).convert("RGB") | |
| # 2) extract prompt | |
| prompt = model_inputs.get("parameters", {}).get("prompt", "") | |
| # 3) minimal call: prompt + image only | |
| out = pipe(prompt=prompt, image=img) | |
| result_img = out.images[0] | |
| # 4) encode output | |
| buf = io.BytesIO() | |
| result_img.save(buf, format="PNG") | |
| b64_out = base64.b64encode(buf.getvalue()).decode() | |
| return {"generated_image_base64": b64_out} |