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 I,v as ut}from"../../chunks/vendor-hf-doc-builder.js";import{I as mt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as V}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as dt}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function ht(Re){let h,K,w,y,Y,U,Me,H,Je,O,k,ee,m,Te,Q,_e,ve,B,je,Ie,te,b,Ue,X,ke,Be,se,N,Ze,ae,Z,le,z,$e,ne,$,ie,g,We,W,Se,Ce,oe,S,re,F,xe,pe,C,ce,M,Ge,P,Ee,Qe,fe,x,ue,d,Xe,q,Ne,ze,A,Fe,De,me,G,de,E,L,Ve,he;return U=new mt({}),k=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 V({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

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