import{S as ot,i as it,s as rt,e as i,k as c,w as v,t as n,M as pt,c as r,d as t,m as f,a as p,x as M,h as o,b as u,N as ct,G as l,g as s,y as J,L as ft,q as k,o as T,B as $,v as ut}from"../../chunks/vendor-hf-doc-builder.js";import{I as mt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as R}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as dt}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function ht(ze){let h,K,b,w,P,j,_e,V,ve,O,W,ee,m,Me,Q,Je,ke,U,Te,$e,te,y,je,G,We,Ue,ae,Y,Be,le,B,se,N,xe,ne,x,oe,g,Ie,I,Xe,Ze,ie,X,re,F,Se,pe,Z,ce,_,Ee,H,Ce,Qe,fe,S,ue,d,Ge,q,Ye,Ne,A,Fe,De,me,E,de,C,L,Re,he;return j=new mt({}),W=new dt({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/textual_inversion_inference.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/textual_inversion_inference.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/textual_inversion_inference.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/textual_inversion_inference.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/textual_inversion_inference.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/textual_inversion_inference.ipynb"}]}}),B=new R({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`}}),x=new R({props:{code:"aW1wb3J0JTIwb3MlMEFpbXBvcnQlMjB0b3JjaCUwQSUwQWltcG9ydCUyMFBJTCUwQWZyb20lMjBQSUwlMjBpbXBvcnQlMjBJbWFnZSUwQSUwQWZyb20lMjBkaWZmdXNlcnMlMjBpbXBvcnQlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZSUwQWZyb20lMjBkaWZmdXNlcnMudXRpbHMlMjBpbXBvcnQlMjBtYWtlX2ltYWdlX2dyaWQlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQ0xJUEZlYXR1cmVFeHRyYWN0b3IlMkMlMjBDTElQVGV4dE1vZGVsJTJDJTIwQ0xJUFRva2VuaXplcg==",highlighted:`<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> torch

<span class="hljs-keyword">import</span> PIL
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image

<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> make_image_grid
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer`}}),X=new R({props:{code:"cHJldHJhaW5lZF9tb2RlbF9uYW1lX29yX3BhdGglMjAlM0QlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUwQXJlcG9faWRfZW1iZWRzJTIwJTNEJTIwJTIyc2QtY29uY2VwdHMtbGlicmFyeSUyRmNhdC10b3klMjI=",highlighted:`pretrained_model_name_or_path = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
repo_id_embeds = <span class="hljs-string">&quot;sd-concepts-library/cat-toy&quot;</span>`}}),Z=new R({props:{code:"cGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwcHJldHJhaW5lZF9tb2RlbF9uYW1lX29yX3BhdGglMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlJTBBKS50byglMjJjdWRhJTIyKSUwQSUwQXBpcGVsaW5lLmxvYWRfdGV4dHVhbF9pbnZlcnNpb24ocmVwb19pZF9lbWJlZHMp",highlighted:`pipeline = StableDiffusionPipeline.from_pretrained(
    pretrained_model_name_or_path, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>
).to(<span class="hljs-string">&quot;cuda&quot;</span>)

pipeline.load_textual_inversion(repo_id_embeds)`}}),S=new R({props:{code:"cHJvbXB0JTIwJTNEJTIwJTIyYSUyMGdyYWZpdHRpJTIwaW4lMjBhJTIwZmF2ZWxhJTIwd2FsbCUyMHdpdGglMjBhJTIwJTNDY2F0LXRveSUzRSUyMG9uJTIwaXQlMjIlMEElMEFudW1fc2FtcGxlcyUyMCUzRCUyMDIlMEFudW1fcm93cyUyMCUzRCUyMDI=",highlighted:`prompt = <span class="hljs-string">&quot;a grafitti in a favela wall with a &lt;cat-toy&gt; on it&quot;</span>

num_samples = <span class="hljs-number">2</span>
num_rows = <span class="hljs-number">2</span>`}}),E=new R({props:{code:"YWxsX2ltYWdlcyUyMCUzRCUyMCU1QiU1RCUwQWZvciUyMF8lMjBpbiUyMHJhbmdlKG51bV9yb3dzKSUzQSUwQSUyMCUyMCUyMCUyMGltYWdlcyUyMCUzRCUyMHBpcGUocHJvbXB0JTJDJTIwbnVtX2ltYWdlc19wZXJfcHJvbXB0JTNEbnVtX3NhbXBsZXMlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTAlMkMlMjBndWlkYW5jZV9zY2FsZSUzRDcuNSkuaW1hZ2VzJTBBJTIwJTIwJTIwJTIwYWxsX2ltYWdlcy5leHRlbmQoaW1hZ2VzKSUwQSUwQWdyaWQlMjAlM0QlMjBtYWtlX2ltYWdlX2dyaWQoYWxsX2ltYWdlcyUyQyUyMG51bV9zYW1wbGVzJTJDJTIwbnVtX3Jvd3MpJTBBZ3JpZA==",highlighted:`all_images = []
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_rows):
    images = pipe(prompt, num_images_per_prompt=num_samples, num_inference_steps=<span class="hljs-number">50</span>, guidance_scale=<span class="hljs-number">7.5</span>).images
    all_images.extend(images)

grid = make_image_grid(all_images, num_samples, num_rows)
grid`}}),{c(){h=i("meta"),K=c(),b=i("h1"),w=i("a"),P=i("span"),v(j.$$.fragment),_e=c(),V=i("span"),ve=n("Textual inversion"),O=c(),v(W.$$.fragment),ee=c(),m=i("p"),Me=n("The "),Q=i("a"),Je=n("StableDiffusionPipeline"),ke=n(" supports textual inversion, a technique that enables a model like Stable Diffusion to learn a new concept from just a few sample images. This gives you more control over the generated images and allows you to tailor the model towards specific concepts. You can get started quickly with a collection of community created concepts in the "),U=i("a"),Te=n("Stable Diffusion Conceptualizer"),$e=n("."),te=c(),y=i("p"),je=n("This guide will show you how to run inference with textual inversion using a pre-learned concept from the Stable Diffusion Conceptualizer. If you\u2019re interested in teaching a model new concepts with textual inversion, take a look at the "),G=i("a"),We=n("Textual Inversion"),Ue=n(" training guide."),ae=c(),Y=i("p"),Be=n("Login to your Hugging Face account:"),le=c(),v(B.$$.fragment),se=c(),N=i("p"),xe=n("Import the necessary libraries:"),ne=c(),v(x.$$.fragment),oe=c(),g=i("p"),Ie=n("Pick a Stable Diffusion checkpoint and a pre-learned concept from the "),I=i("a"),Xe=n("Stable Diffusion Conceptualizer"),Ze=n(":"),ie=c(),v(X.$$.fragment),re=c(),F=i("p"),Se=n("Now you can load a pipeline, and pass the pre-learned concept to it:"),pe=c(),v(Z.$$.fragment),ce=c(),_=i("p"),Ee=n("Create a prompt with the pre-learned concept by using the special placeholder token "),H=i("code"),Ce=n("<cat-toy>"),Qe=n(", and choose the number of samples and rows of images you\u2019d like to generate:"),fe=c(),v(S.$$.fragment),ue=c(),d=i("p"),Ge=n("Then run the pipeline (feel free to adjust the parameters like "),q=i("code"),Ye=n("num_inference_steps"),Ne=n(" and "),A=i("code"),Fe=n("guidance_scale"),De=n(" to see how they affect image quality), save the generated images and visualize them with the helper function you created at the beginning:"),me=c(),v(E.$$.fragment),de=c(),C=i("div"),L=i("img"),this.h()},l(e){const a=pt('[data-svelte="svelte-1phssyn"]',document.head);h=r(a,"META",{name:!0,content:!0}),a.forEach(t),K=f(e),b=r(e,"H1",{class:!0});var be=p(b);w=r(be,"A",{id:!0,class:!0,href:!0});var Pe=p(w);P=r(Pe,"SPAN",{});var Ve=p(P);M(j.$$.fragment,Ve),Ve.forEach(t),Pe.forEach(t),_e=f(be),V=r(be,"SPAN",{});var He=p(V);ve=o(He,"Textual inversion"),He.forEach(t),be.forEach(t),O=f(e),M(W.$$.fragment,e),ee=f(e),m=r(e,"P",{});var D=p(m);Me=o(D,"The "),Q=r(D,"A",{href:!0});var qe=p(Q);Je=o(qe,"StableDiffusionPipeline"),qe.forEach(t),ke=o(D," supports textual inversion, a technique that enables a model like Stable Diffusion to learn a new concept from just a few sample images. This gives you more control over the generated images and allows you to tailor the model towards specific concepts. You can get started quickly with a collection of community created concepts in the "),U=r(D,"A",{href:!0,rel:!0});var Ae=p(U);Te=o(Ae,"Stable Diffusion Conceptualizer"),Ae.forEach(t),$e=o(D,"."),D.forEach(t),te=f(e),y=r(e,"P",{});var we=p(y);je=o(we,"This guide will show you how to run inference with textual inversion using a pre-learned concept from the Stable Diffusion Conceptualizer. If you\u2019re interested in teaching a model new concepts with textual inversion, take a look at the "),G=r(we,"A",{href:!0});var Le=p(G);We=o(Le,"Textual Inversion"),Le.forEach(t),Ue=o(we," training guide."),we.forEach(t),ae=f(e),Y=r(e,"P",{});var Ke=p(Y);Be=o(Ke,"Login to your Hugging Face account:"),Ke.forEach(t),le=f(e),M(B.$$.fragment,e),se=f(e),N=r(e,"P",{});var Oe=p(N);xe=o(Oe,"Import the necessary libraries:"),Oe.forEach(t),ne=f(e),M(x.$$.fragment,e),oe=f(e),g=r(e,"P",{});var ye=p(g);Ie=o(ye,"Pick a Stable Diffusion checkpoint and a pre-learned concept from the "),I=r(ye,"A",{href:!0,rel:!0});var et=p(I);Xe=o(et,"Stable Diffusion Conceptualizer"),et.forEach(t),Ze=o(ye,":"),ye.forEach(t),ie=f(e),M(X.$$.fragment,e),re=f(e),F=r(e,"P",{});var tt=p(F);Se=o(tt,"Now you can load a pipeline, and pass the pre-learned concept to it:"),tt.forEach(t),pe=f(e),M(Z.$$.fragment,e),ce=f(e),_=r(e,"P",{});var ge=p(_);Ee=o(ge,"Create a prompt with the pre-learned concept by using the special placeholder token "),H=r(ge,"CODE",{});var at=p(H);Ce=o(at,"<cat-toy>"),at.forEach(t),Qe=o(ge,", and choose the number of samples and rows of images you\u2019d like to generate:"),ge.forEach(t),fe=f(e),M(S.$$.fragment,e),ue=f(e),d=r(e,"P",{});var z=p(d);Ge=o(z,"Then run the pipeline (feel free to adjust the parameters like "),q=r(z,"CODE",{});var lt=p(q);Ye=o(lt,"num_inference_steps"),lt.forEach(t),Ne=o(z," and "),A=r(z,"CODE",{});var st=p(A);Fe=o(st,"guidance_scale"),st.forEach(t),De=o(z," to see how they affect image quality), save the generated images and visualize them with the helper function you created at the beginning:"),z.forEach(t),me=f(e),M(E.$$.fragment,e),de=f(e),C=r(e,"DIV",{class:!0});var nt=p(C);L=r(nt,"IMG",{src:!0}),nt.forEach(t),this.h()},h(){u(h,"name","hf:doc:metadata"),u(h,"content",JSON.stringify(bt)),u(w,"id","textual-inversion"),u(w,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(w,"href","#textual-inversion"),u(b,"class","relative group"),u(Q,"href","/docs/diffusers/main/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline"),u(U,"href","https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer"),u(U,"rel","nofollow"),u(G,"href","./training/text_inversion"),u(I,"href","https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer"),u(I,"rel","nofollow"),ct(L.src,Re="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/textual_inversion_inference.png")||u(L,"src",Re),u(C,"class","flex justify-center")},m(e,a){l(document.head,h),s(e,K,a),s(e,b,a),l(b,w),l(w,P),J(j,P,null),l(b,_e),l(b,V),l(V,ve),s(e,O,a),J(W,e,a),s(e,ee,a),s(e,m,a),l(m,Me),l(m,Q),l(Q,Je),l(m,ke),l(m,U),l(U,Te),l(m,$e),s(e,te,a),s(e,y,a),l(y,je),l(y,G),l(G,We),l(y,Ue),s(e,ae,a),s(e,Y,a),l(Y,Be),s(e,le,a),J(B,e,a),s(e,se,a),s(e,N,a),l(N,xe),s(e,ne,a),J(x,e,a),s(e,oe,a),s(e,g,a),l(g,Ie),l(g,I),l(I,Xe),l(g,Ze),s(e,ie,a),J(X,e,a),s(e,re,a),s(e,F,a),l(F,Se),s(e,pe,a),J(Z,e,a),s(e,ce,a),s(e,_,a),l(_,Ee),l(_,H),l(H,Ce),l(_,Qe),s(e,fe,a),J(S,e,a),s(e,ue,a),s(e,d,a),l(d,Ge),l(d,q),l(q,Ye),l(d,Ne),l(d,A),l(A,Fe),l(d,De),s(e,me,a),J(E,e,a),s(e,de,a),s(e,C,a),l(C,L),he=!0},p:ft,i(e){he||(k(j.$$.fragment,e),k(W.$$.fragment,e),k(B.$$.fragment,e),k(x.$$.fragment,e),k(X.$$.fragment,e),k(Z.$$.fragment,e),k(S.$$.fragment,e),k(E.$$.fragment,e),he=!0)},o(e){T(j.$$.fragment,e),T(W.$$.fragment,e),T(B.$$.fragment,e),T(x.$$.fragment,e),T(X.$$.fragment,e),T(Z.$$.fragment,e),T(S.$$.fragment,e),T(E.$$.fragment,e),he=!1},d(e){t(h),e&&t(K),e&&t(b),$(j),e&&t(O),$(W,e),e&&t(ee),e&&t(m),e&&t(te),e&&t(y),e&&t(ae),e&&t(Y),e&&t(le),$(B,e),e&&t(se),e&&t(N),e&&t(ne),$(x,e),e&&t(oe),e&&t(g),e&&t(ie),$(X,e),e&&t(re),e&&t(F),e&&t(pe),$(Z,e),e&&t(ce),e&&t(_),e&&t(fe),$(S,e),e&&t(ue),e&&t(d),e&&t(me),$(E,e),e&&t(de),e&&t(C)}}}const bt={local:"textual-inversion",title:"Textual inversion"};function wt(ze){return ut(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Mt extends ot{constructor(h){super();it(this,h,wt,ht,rt,{})}}export{Mt as default,bt as metadata};
