Skip to content

Commit db0e248

Browse files
patrickvonplatensayakpaulpcuenca
authored
Finish docs textual inversion (huggingface#3068)
* Finish docs textual inversion * Apply suggestions from code review Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]> --------- Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: Pedro Cuenca <[email protected]>
1 parent ecea056 commit db0e248

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

loaders.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def load_textual_inversion(
368368
):
369369
r"""
370370
Load textual inversion embeddings into the text encoder of stable diffusion pipelines. Both `diffusers` and
371-
`Automatic1111` formats are supported.
371+
`Automatic1111` formats are supported (see example below).
372372
373373
<Tip warning={true}>
374374
@@ -427,6 +427,42 @@ def load_textual_inversion(
427427
models](https://huggingface.co/docs/hub/models-gated#gated-models).
428428
429429
</Tip>
430+
431+
Example:
432+
433+
To load a textual inversion embedding vector in `diffusers` format:
434+
```py
435+
from diffusers import StableDiffusionPipeline
436+
import torch
437+
438+
model_id = "runwayml/stable-diffusion-v1-5"
439+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
440+
441+
pipe.load_textual_inversion("sd-concepts-library/cat-toy")
442+
443+
prompt = "A <cat-toy> backpack"
444+
445+
image = pipe(prompt, num_inference_steps=50).images[0]
446+
image.save("cat-backpack.png")
447+
```
448+
449+
To load a textual inversion embedding vector in Automatic1111 format, make sure to first download the vector,
450+
e.g. from [civitAI](https://civitai.com/models/3036?modelVersionId=9857) and then load the vector locally:
451+
452+
```py
453+
from diffusers import StableDiffusionPipeline
454+
import torch
455+
456+
model_id = "runwayml/stable-diffusion-v1-5"
457+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
458+
459+
pipe.load_textual_inversion("./charturnerv2.pt")
460+
461+
prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a woman wearing a black jacket and red shirt, best quality, intricate details."
462+
463+
image = pipe(prompt, num_inference_steps=50).images[0]
464+
image.save("character.png")
465+
```
430466
"""
431467
if not hasattr(self, "tokenizer") or not isinstance(self.tokenizer, PreTrainedTokenizer):
432468
raise ValueError(

0 commit comments

Comments
 (0)