import{S as it,i as ot,s as rt,e as o,k as c,w as J,t as n,M as pt,c as r,d as t,m as f,a as p,x as T,h as i,b as u,N as ct,G as a,g as l,y as _,L as ft,q as v,o as j,B as k,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(Ye){let h,K,y,w,V,U,Me,P,Je,O,I,ee,m,Te,E,_e,ve,B,je,ke,te,b,Ue,Q,Ie,Be,se,z,Ze,ae,Z,le,N,$e,ne,$,ie,g,We,W,Ce,Se,oe,C,re,F,xe,pe,S,ce,M,Xe,H,Ge,Ee,fe,x,ue,d,Qe,q,ze,Ne,A,Fe,De,me,X,de,G,L,Re,he;return U=new mt({}),I=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"}]}}),Z=new R({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`}}),$=new R({props:{code:"aW1wb3J0JTIwb3MlMEFpbXBvcnQlMjB0b3JjaCUwQSUwQWltcG9ydCUyMFBJTCUwQWZyb20lMjBQSUwlMjBpbXBvcnQlMjBJbWFnZSUwQSUwQWZyb20lMjBkaWZmdXNlcnMlMjBpbXBvcnQlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZSUwQWZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBDTElQRmVhdHVyZUV4dHJhY3RvciUyQyUyMENMSVBUZXh0TW9kZWwlMkMlMjBDTElQVG9rZW5pemVyJTBBJTBBJTBBZGVmJTIwaW1hZ2VfZ3JpZChpbWdzJTJDJTIwcm93cyUyQyUyMGNvbHMpJTNBJTBBJTIwJTIwJTIwJTIwYXNzZXJ0JTIwbGVuKGltZ3MpJTIwJTNEJTNEJTIwcm93cyUyMColMjBjb2xzJTBBJTBBJTIwJTIwJTIwJTIwdyUyQyUyMGglMjAlM0QlMjBpbWdzJTVCMCU1RC5zaXplJTBBJTIwJTIwJTIwJTIwZ3JpZCUyMCUzRCUyMEltYWdlLm5ldyglMjJSR0IlMjIlMkMlMjBzaXplJTNEKGNvbHMlMjAqJTIwdyUyQyUyMHJvd3MlMjAqJTIwaCkpJTBBJTIwJTIwJTIwJTIwZ3JpZF93JTJDJTIwZ3JpZF9oJTIwJTNEJTIwZ3JpZC5zaXplJTBBJTBBJTIwJTIwJTIwJTIwZm9yJTIwaSUyQyUyMGltZyUyMGluJTIwZW51bWVyYXRlKGltZ3MpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZ3JpZC5wYXN0ZShpbWclMkMlMjBib3glM0QoaSUyMCUyNSUyMGNvbHMlMjAqJTIwdyUyQyUyMGklMjAlMkYlMkYlMjBjb2xzJTIwKiUyMGgpKSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMGdyaWQ=",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> transformers <span class="hljs-keyword">import</span> CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer


<span class="hljs-keyword">def</span> <span class="hljs-title function_">image_grid</span>(<span class="hljs-params">imgs, rows, cols</span>):
    <span class="hljs-keyword">assert</span> <span class="hljs-built_in">len</span>(imgs) == rows * cols

    w, h = imgs[<span class="hljs-number">0</span>].size
    grid = Image.new(<span class="hljs-string">&quot;RGB&quot;</span>, size=(cols * w, rows * h))
    grid_w, grid_h = grid.size

    <span class="hljs-keyword">for</span> i, img <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    <span class="hljs-keyword">return</span> grid`}}),C=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>`}}),S=new R({props:{code:"cGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQocHJldHJhaW5lZF9tb2RlbF9uYW1lX29yX3BhdGglMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYpLnRvKCUyMmN1ZGElMjIpJTBBJTBBcGlwZWxpbmUubG9hZF90ZXh0dWFsX2ludmVyc2lvbihyZXBvX2lkX2VtYmVkcyk=",highlighted:`pipeline = StableDiffusionPipeline.from_pretrained(pretrained_model_name_or_path, torch_dtype=torch.float16).to(<span class="hljs-string">&quot;cuda&quot;</span>)

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