Instructions to use nexh98/clamp-checkpoints with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use nexh98/clamp-checkpoints with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("nexh98/clamp-checkpoints", 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
Download diffusion/inference.py from nexh98/clamp-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/nexh98/clamp-checkpoints/resolve/main/diffusion/inference.py
- Command line
-
hf download hf://nexh98/clamp-checkpoints/diffusion/inference.py
-
curl -L -o inference.py https://huggingface.co/nexh98/clamp-checkpoints/resolve/main/diffusion/inference.py
1.74 kB
| """ | |
| Minimal inference example for the CLAMP diffusion checkpoint (clamp_ESD). | |
| Requires: diffusers, transformers, torch, accelerate, safetensors | |
| pip install diffusers transformers torch accelerate safetensors | |
| Note: pipe.load_lora_weights() and unet.load_lora_adapter() do NOT work with this | |
| checkpoint (it was saved via the older UNet-level attn-procs format, not the newer | |
| pipeline-level LoRA format, so the key prefixes don't match what those methods | |
| expect - they will silently attach zero LoRA parameters). Use | |
| unet.load_attn_procs() instead, as below - it is deprecated in newer diffusers | |
| versions but is the one that actually works for this file. | |
| """ | |
| import torch | |
| from diffusers import StableDiffusionPipeline | |
| from huggingface_hub import snapshot_download | |
| REPO_ID = "nexh98/clamp-checkpoints" | |
| # Base model: Stable Diffusion V1-4 with the paper's ESD erasure already applied | |
| # to the sub-concepts. Using this (rather than vanilla SD v1.4) is required to | |
| # reproduce the paper's reported harmful/benign adaptation numbers. | |
| base_model_dir = snapshot_download(REPO_ID, allow_patterns="diffusion/base_model/*") | |
| lora_dir = snapshot_download(REPO_ID, allow_patterns="diffusion/clamp_ESD/*") | |
| base_model_path = f"{base_model_dir}/diffusion/base_model" | |
| lora_path = f"{lora_dir}/diffusion/clamp_ESD" | |
| pipe = StableDiffusionPipeline.from_pretrained( | |
| base_model_path, torch_dtype=torch.float16, safety_checker=None | |
| ) | |
| pipe.unet.load_attn_procs(lora_path, weight_name="pytorch_lora_weights.safetensors") | |
| pipe = pipe.to("cuda") | |
| image = pipe( | |
| "A picture of a Kavri", # one of the paper's merged/harmful concepts (elephant+zebra) | |
| num_inference_steps=25, | |
| guidance_scale=7.5, | |
| ).images[0] | |
| image.save("clamp_diffusion_sample.png") | |