Skip to content

Commit 64270ef

Browse files
Improve README to show how to use SD without an access token (#315)
* Readme sd * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Anton Lozhkov <anton@huggingface.co> Co-authored-by: Anton Lozhkov <anton@huggingface.co>
1 parent 3c138a4 commit 64270ef

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

README.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,64 @@ See the [model card](https://huggingface.co/CompVis/stable-diffusion) for more i
4141

4242
You need to accept the model license before downloading or using the Stable Diffusion weights. Please, visit the [model card](https://huggingface.co/CompVis/stable-diffusion-v1-3), read the license and tick the checkbox if you agree. You have to be a registered user in 🤗 Hugging Face Hub, and you'll also need to use an access token for the code to work. For more information on access tokens, please refer to [this section](https://huggingface.co/docs/hub/security-tokens) of the documentation.
4343

44+
4445
### Text-to-Image generation with Stable Diffusion
4546

4647
```python
4748
# make sure you're logged in with `huggingface-cli login`
4849
from torch import autocast
49-
import torch
50-
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
50+
from diffusers import StableDiffusionPipeline
51+
52+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)
53+
pipe = pipe.to("cuda")
54+
55+
prompt = "a photo of an astronaut riding a horse on mars"
56+
with autocast("cuda"):
57+
image = pipe(prompt)["sample"][0]
58+
```
59+
60+
**Note**: If you don't want to use the token, you can also simply download the model weights
61+
(after having [accepted the license](https://huggingface.co/CompVis/stable-diffusion-v1-4)) and pass
62+
the path to the local folder to the `StableDiffusionPipeline`.
63+
64+
```
65+
git lfs install
66+
git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
67+
```
68+
69+
Assuming the folder is stored locally under `./stable-diffusion-v1-4`, you can also run stable diffusion
70+
without requiring an authentication token:
71+
72+
```python
73+
pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-4")
74+
pipe = pipe.to("cuda")
75+
76+
prompt = "a photo of an astronaut riding a horse on mars"
77+
with autocast("cuda"):
78+
image = pipe(prompt)["sample"][0]
79+
```
80+
81+
If you are limited by GPU memory, you might want to consider using the model in `fp16`.
82+
83+
```python
84+
pipe = StableDiffusionPipeline.from_pretrained(
85+
"CompVis/stable-diffusion-v1-4",
86+
revision="fp16",
87+
torch_dtype=torch.float16,
88+
use_auth_token=True
89+
)
90+
pipe = pipe.to("cuda")
91+
92+
prompt = "a photo of an astronaut riding a horse on mars"
93+
with autocast("cuda"):
94+
image = pipe(prompt)["sample"][0]
95+
```
96+
97+
Finally, if you wish to use a different scheduler, you can simply instantiate
98+
it before the pipeline and pass it to `from_pretrained`.
99+
100+
```python
101+
from diffusers import LMSDiscreteScheduler
51102

52103
lms = LMSDiscreteScheduler(
53104
beta_start=0.00085,
@@ -86,12 +137,15 @@ from diffusers import StableDiffusionImg2ImgPipeline
86137

87138
# load the pipeline
88139
device = "cuda"
140+
model_id_or_path = "CompVis/stable-diffusion-v1-4"
89141
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
90-
"CompVis/stable-diffusion-v1-4",
142+
model_id_or_path,
91143
revision="fp16",
92144
torch_dtype=torch.float16,
93145
use_auth_token=True
94146
)
147+
# or download via git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
148+
# and pass `model_id_or_path="./stable-diffusion-v1-4"` without having to use `use_auth_token=True`.
95149
pipe = pipe.to(device)
96150

97151
# let's download an initial image
@@ -135,12 +189,15 @@ init_image = download_image(img_url).resize((512, 512))
135189
mask_image = download_image(mask_url).resize((512, 512))
136190

137191
device = "cuda"
192+
model_id_or_path = "CompVis/stable-diffusion-v1-4"
138193
pipe = StableDiffusionInpaintPipeline.from_pretrained(
139-
"CompVis/stable-diffusion-v1-4",
194+
model_id_or_path,
140195
revision="fp16",
141196
torch_dtype=torch.float16,
142197
use_auth_token=True
143198
)
199+
# or download via git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
200+
# and pass `model_id_or_path="./stable-diffusion-v1-4"` without having to use `use_auth_token=True`.
144201
pipe = pipe.to(device)
145202

146203
prompt = "a cat sitting on a bench"

src/diffusers/pipelines/stable_diffusion/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ The summary of the model is the following:
1212

1313
- Stable Diffusion has the same architecture as [Latent Diffusion](https://arxiv.org/abs/2112.10752) but uses a frozen CLIP Text Encoder instead of training the text encoder jointly with the diffusion model.
1414
- An in-detail explanation of the Stable Diffusion model can be found under [Stable Diffusion with 🧨 Diffusers](https://huggingface.co/blog/stable_diffusion).
15+
- If you don't want to rely on the Hugging Face Hub and having to pass a authentification token, you can
16+
download the weights with `git lfs install; git clone https://huggingface.co/CompVis/stable-diffusion-v1-4` and instead pass the local path to the cloned folder to `from_pretrained` as shown below.
1517
- Stable Diffusion can work with a variety of different samplers as is shown below.
1618

1719
## Available Pipelines:
@@ -24,6 +26,35 @@ The summary of the model is the following:
2426

2527
## Examples:
2628

29+
### Using Stable Diffusion without being logged into the Hub.
30+
31+
If you want to download the model weights using a single Python line, you need to pass the token
32+
to `use_auth_token` or be logged in via `huggingface-cli login`.
33+
For more information on access tokens, please refer to [this section](https://huggingface.co/docs/hub/security-tokens) of the documentation.
34+
35+
Assuming your token is stored under YOUR_TOKEN, you can download the stable diffusion pipeline as follows:
36+
37+
```python
38+
from diffusers import DiffusionPipeline
39+
40+
pipeline = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
41+
```
42+
43+
This however can make it difficult to build applications on top of `diffusers` as you will always have to pass the token around. A potential way to solve this issue is by downloading the weights to a local path `"./stable-diffusion-v1-4"`:
44+
45+
```
46+
git lfs install
47+
git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
48+
```
49+
50+
and simply passing the local path to `from_pretrained`:
51+
52+
```python
53+
from diffusers import StableDiffusionPipeline
54+
55+
pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-4")
56+
```
57+
2758
### Text-to-Image with default PLMS scheduler
2859

2960
```python

0 commit comments

Comments
 (0)