|
| 1 | +--- |
| 2 | +title: The Hugging Face Concepts Library and Importing Textual Inversion files |
| 3 | +--- |
| 4 | + |
| 5 | +# :material-file-document: Concepts Library |
| 6 | + |
| 7 | +## Using Textual Inversion Files |
| 8 | + |
| 9 | +Textual inversion (TI) files are small models that customize the output of |
| 10 | +Stable Diffusion image generation. They can augment SD with |
| 11 | +specialized subjects and artistic styles. They are also known as |
| 12 | +"embeds" in the machine learning world. |
| 13 | + |
| 14 | +Each TI file introduces one or more vocabulary terms to the SD |
| 15 | +model. These are known in InvokeAI as "triggers." Triggers are often, |
| 16 | +but not always, denoted using angle brackets as in |
| 17 | +"<trigger-phrase>". The two most common type of TI files that you'll |
| 18 | +encounter are `.pt` and `.bin` files, which are produced by different |
| 19 | +TI training packages. InvokeAI supports both formats, but its [built-in |
| 20 | +TI training system](TEXTUAL_INVERSION.md) produces `.pt`. |
| 21 | + |
| 22 | +The [Hugging Face company](https://huggingface.co/sd-concepts-library) |
| 23 | +has amassed a large ligrary of >800 community-contributed TI files |
| 24 | +covering a broad range of subjects and styles. InvokeAI has built-in |
| 25 | +support for this library which downloads and merges TI files |
| 26 | +automatically upon request. You can also install your own or others' |
| 27 | +TI files by placing them in a designated directory. |
| 28 | + |
| 29 | +### An Example |
| 30 | + |
| 31 | +Here are a few examples to illustrate how it works. All these images |
| 32 | +were generated using the command-line client and the Stable Diffusion |
| 33 | +1.5 model: |
| 34 | + |
| 35 | +Japanese gardener |
| 36 | +<br> |
| 37 | +<img src="../assets/concepts/image1.png"> |
| 38 | + |
| 39 | +Japanese gardener <ghibli-face> |
| 40 | +<br> |
| 41 | +<img src="../assets/concepts/image2.png"> |
| 42 | + |
| 43 | +Japanese gardener <hoi4-leaders> |
| 44 | +<br> |
| 45 | +<img src="../assets/concepts/image3.png"> |
| 46 | + |
| 47 | +Japanese gardener <cartoona-animals> |
| 48 | +<br> |
| 49 | +<img src="../assets/concepts/image4.png"> |
| 50 | + |
| 51 | +You can also combine styles and concepts: |
| 52 | + |
| 53 | +A portrait of <alf> in <cartoona-animal> style |
| 54 | +<br> |
| 55 | +<img src="../assets/concepts/image5.png"> |
| 56 | + |
| 57 | +## Using a Hugging Face Concept |
| 58 | + |
| 59 | +Hugging Face TI concepts are downloaded and installed automatically as |
| 60 | +you require them. This requires your machine to be connected to the |
| 61 | +Internet. To find out what each concept is for, you can browse the |
| 62 | +[Hugging Face concepts |
| 63 | +library](https://huggingface.co/sd-concepts-library) and look at |
| 64 | +examples of what each concept produces. |
| 65 | + |
| 66 | +When you have an idea of a concept you wish to try, go to the |
| 67 | +command-line client (CLI) and type a "<" character and the beginning |
| 68 | +of the Hugging Face concept name you wish to load. Press the Tab key, |
| 69 | +and the CLI will show you all matching concepts. You can also type "<" |
| 70 | +and Tab to get a listing of all ~800 concepts, but be prepared to |
| 71 | +scroll up to see them all! If there is more than one match you can |
| 72 | +continue to type and Tab until the concept is completed. |
| 73 | + |
| 74 | +For example if you type "<x" and Tab, you'll be prompted with the completions: |
| 75 | + |
| 76 | +``` |
| 77 | +<xatu2> <xatu> <xbh> <xi> <xidiversity> <xioboma> <xuna> <xyz> |
| 78 | +``` |
| 79 | + |
| 80 | +Now type "id" and press Tab. It will be autocompleted to |
| 81 | +"<xidiversity>" because this is a unique match. |
| 82 | + |
| 83 | +Finish your prompt and generate as usual. You may include multiple |
| 84 | +concept terms in the prompt. |
| 85 | + |
| 86 | +If you have never used this concept before, you will see a message |
| 87 | +that the TI model is being downloaded and installed. After this, the |
| 88 | +concept will be saved locally (in the `models/sd-concepts-library` |
| 89 | +directory) for future use. |
| 90 | + |
| 91 | +Several steps happen during downloading and |
| 92 | +installation, including a scan of the file for malicious code. Should |
| 93 | +any errors occur, you will be warned and the concept will fail to |
| 94 | +load. Generation will then continue treating the trigger term as a |
| 95 | +normal string of characters (e.g. as literal "<ghibli-face>"). |
| 96 | + |
| 97 | +Currently auto-installation of concepts is a feature only available on |
| 98 | +the command-line client. Support for the WebUI is a work in progress. |
| 99 | + |
| 100 | +## Installing your Own TI Files |
| 101 | + |
| 102 | +You may install any number of `.pt` and `.bin` files simply by copying |
| 103 | +them into the `embeddings` directory of the InvokeAI runtime directory |
| 104 | +(usually `invokeai` in your home directory). You may create |
| 105 | +subdirectories in order to organize the files in any way you wish. Be |
| 106 | +careful not to overwrite one file with another. For example, TI files |
| 107 | +generated by the Hugging Face toolkit share the named |
| 108 | +`learned_embedding.bin`. You can use subdirectories to keep them |
| 109 | +distinct. |
| 110 | + |
| 111 | +At startup time, InvokeAI will scan the `embeddings` directory and |
| 112 | +load any TI files it finds there. At startup you will see a message |
| 113 | +similar to this one: |
| 114 | + |
| 115 | +``` |
| 116 | +>> Current embedding manager terms: *, <HOI4-Leader>, <princess-knight> |
| 117 | +``` |
| 118 | + |
| 119 | +Note the "*" trigger term. This is a placeholder term that many early |
| 120 | +TI tutorials taught people to use rather than a more descriptive |
| 121 | +term. Unfortunately, if you have multiple TI files that all use this |
| 122 | +term, only the first one loaded will be triggered by use of the term. |
| 123 | + |
| 124 | +To avoid this problem, you can use the `merge_embeddings.py` script to |
| 125 | +merge two or more TI files together. If it encounters a collision of |
| 126 | +terms, the script will prompt you to select new terms that do not |
| 127 | +collide. See [Textual Inversion](TEXTUAL_INVERSION.md) for details. |
| 128 | + |
| 129 | +## Further Reading |
| 130 | + |
| 131 | +Please see [the repository](https://github.com/rinongal/textual_inversion) and |
| 132 | +associated paper for details and limitations. |
0 commit comments