import{s as Te,n as je,o as Ue}from"../chunks/scheduler.53228c21.js";import{S as Je,i as be,e as i,s as a,c,h as ge,a as p,d as l,b as n,f as we,g as h,j as o,k as fe,l as Ce,m as s,n as m,t as M,o as u,p as y}from"../chunks/index.100fac89.js";import{C as Ie}from"../chunks/CopyLLMTxtMenu.0ee1b69b.js";import{C as x}from"../chunks/CodeBlock.d30a6509.js";import{H as ve,E as $e}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.136ef3fd.js";function Ge(ie){let r,V,X,H,d,R,w,Y,f,pe="Diffusers’ pipelines can be used as an inference engine for a server. It supports concurrent and multithreaded requests to generate images that may be requested by multiple users at the same time.",E,T,oe='This guide will show you how to use the <a href="/docs/diffusers/main/en/api/pipelines/stable_diffusion/stable_diffusion_3#diffusers.StableDiffusion3Pipeline">StableDiffusion3Pipeline</a> in a server, but feel free to use any pipeline you want.',N,j,re="Start by navigating to the <code>examples/server</code> folder and installing all of the dependencies.",S,U,F,J,ce="Launch the server with the following command.",L,b,Q,g,he="The server is accessed at http://localhost:8000. You can curl this model with the following command.",q,C,z,I,me='If you need to upgrade some dependencies, you can use either <a href="https://github.com/jazzband/pip-tools" rel="nofollow">pip-tools</a> or <a href="https://github.com/astral-sh/uv" rel="nofollow">uv</a>. For example, upgrade the dependencies with <code>uv</code> using the following command.',P,v,D,$,Me='The server is built with <a href="https://fastapi.tiangolo.com/async/" rel="nofollow">FastAPI</a>. The endpoint for <code>v1/images/generations</code> is shown below.',K,G,O,Z,ue='The <code>generate_image</code> function is defined as asynchronous with the <a href="https://fastapi.tiangolo.com/async/" rel="nofollow">async</a> keyword so that FastAPI knows that whatever is happening in this function won’t necessarily return a result right away. Once it hits some point in the function that it needs to await some other <a href="https://docs.python.org/3/library/asyncio-task.html#asyncio.Task" rel="nofollow">Task</a>, the main thread goes back to answering other HTTP requests. This is shown in the code below with the <a href="https://fastapi.tiangolo.com/async/#async-and-await" rel="nofollow">await</a> keyword.',ee,k,te,_,ye='At this point, the execution of the pipeline function is placed onto a <a href="https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor" rel="nofollow">new thread</a>, and the main thread performs other things until a result is returned from the <code>pipeline</code>.',le,A,de="Another important aspect of this implementation is creating a <code>pipeline</code> from <code>shared_pipeline</code>. The goal behind this is to avoid loading the underlying model more than once onto the GPU while still allowing for each new request that is running on a separate thread to have its own generator and scheduler. The scheduler, in particular, is not thread-safe, and it will cause errors like: <code>IndexError: index 21 is out of bounds for dimension 0 with size 21</code> if you try to use the same scheduler across multiple threads.",se,B,ae,W,ne;return d=new Ie({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),w=new ve({props:{title:"Create a server",local:"create-a-server",headingTag:"h1"}}),U=new x({props:{code:"cGlwJTIwaW5zdGFsbCUyMC4lMEFwaXAlMjBpbnN0YWxsJTIwLWYlMjByZXF1aXJlbWVudHMudHh0",highlighted:`pip install .
pip install -f requirements.txt`,wrap:!1}}),b=new x({props:{code:"cHl0aG9uJTIwc2VydmVyLnB5",highlighted:"python server.py",wrap:!1}}),C=new x({props:{code:"Y3VybCUyMC1YJTIwUE9TVCUyMC1IJTIwJTIyQ29udGVudC1UeXBlJTNBJTIwYXBwbGljYXRpb24lMkZqc29uJTIyJTIwLS1kYXRhJTIwJyU3QiUyMm1vZGVsJTIyJTNBJTIwJTIyc29tZXRoaW5nJTIyJTJDJTIwJTIycHJvbXB0JTIyJTNBJTIwJTIyYSUyMGtpdHRlbiUyMGluJTIwZnJvbnQlMjBvZiUyMGElMjBmaXJlcGxhY2UlMjIlN0QnJTIwaHR0cCUzQSUyRiUyRmxvY2FsaG9zdCUzQTgwMDAlMkZ2MSUyRmltYWdlcyUyRmdlbmVyYXRpb25z",highlighted:'curl -X POST -H <span class="hljs-string">&quot;Content-Type: application/json&quot;</span> <span class="hljs-params">--data</span> &#x27;{<span class="hljs-string">&quot;model&quot;</span>: <span class="hljs-string">&quot;something&quot;</span>, <span class="hljs-string">&quot;prompt&quot;</span>: <span class="hljs-string">&quot;a kitten in front of a fireplace&quot;</span>}&#x27; http:<span class="hljs-string">//localhost</span><span class="hljs-function">:8000</span>/v1/images/generations',wrap:!1}}),v=new x({props:{code:"dXYlMjBwaXAlMjBjb21waWxlJTIwcmVxdWlyZW1lbnRzLmluJTIwLW8lMjByZXF1aXJlbWVudHMudHh0",highlighted:'uv pip compile <span class="hljs-keyword">requirements</span>.<span class="hljs-keyword">in</span> -o <span class="hljs-keyword">requirements</span>.txt',wrap:!1}}),G=new x({props:{code:"JTQwYXBwLnBvc3QoJTIyJTJGdjElMkZpbWFnZXMlMkZnZW5lcmF0aW9ucyUyMiklMEFhc3luYyUyMGRlZiUyMGdlbmVyYXRlX2ltYWdlKGltYWdlX2lucHV0JTNBJTIwVGV4dFRvSW1hZ2VJbnB1dCklM0ElMEElMjAlMjAlMjAlMjB0cnklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb29wJTIwJTNEJTIwYXN5bmNpby5nZXRfZXZlbnRfbG9vcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NoZWR1bGVyJTIwJTNEJTIwc2hhcmVkX3BpcGVsaW5lLnBpcGVsaW5lLnNjaGVkdWxlci5mcm9tX2NvbmZpZyhzaGFyZWRfcGlwZWxpbmUucGlwZWxpbmUuc2NoZWR1bGVyLmNvbmZpZyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwaXBlbGluZSUyMCUzRCUyMFN0YWJsZURpZmZ1c2lvbjNQaXBlbGluZS5mcm9tX3BpcGUoc2hhcmVkX3BpcGVsaW5lLnBpcGVsaW5lJTJDJTIwc2NoZWR1bGVyJTNEc2NoZWR1bGVyKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjdWRhJTIyKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdlb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Zm9ybWF0X2V4YygpKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJhaXNlJTIwSFRUUEV4Y2VwdGlvbihzdGF0dXNfY29kZSUzRDUwMCUyQyUyMGRldGFpbCUzRHN0cihlKSUyMCUyQiUyMHRyYWNlYmFjay5mb3JtYXRfZXhjKCkp",highlighted:`<span class="hljs-meta">@app.post(<span class="hljs-params"><span class="hljs-string">&quot;/v1/images/generations&quot;</span></span>)</span>
<span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_image</span>(<span class="hljs-params">image_input: TextToImageInput</span>):
    <span class="hljs-keyword">try</span>:
        loop = asyncio.get_event_loop()
        scheduler = shared_pipeline.pipeline.scheduler.from_config(shared_pipeline.pipeline.scheduler.config)
        pipeline = StableDiffusion3Pipeline.from_pipe(shared_pipeline.pipeline, scheduler=scheduler)
        generator = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>)
        generator.manual_seed(random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">10000000</span>))
        output = <span class="hljs-keyword">await</span> loop.run_in_executor(<span class="hljs-literal">None</span>, <span class="hljs-keyword">lambda</span>: pipeline(image_input.prompt, generator = generator))
        logger.info(<span class="hljs-string">f&quot;output: <span class="hljs-subst">{output}</span>&quot;</span>)
        image_url = save_image(output.images[<span class="hljs-number">0</span>])
        <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;data&quot;</span>: [{<span class="hljs-string">&quot;url&quot;</span>: image_url}]}
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(e, HTTPException):
            <span class="hljs-keyword">raise</span> e
        <span class="hljs-keyword">elif</span> <span class="hljs-built_in">hasattr</span>(e, <span class="hljs-string">&#x27;message&#x27;</span>):
            <span class="hljs-keyword">raise</span> HTTPException(status_code=<span class="hljs-number">500</span>, detail=e.message + traceback.format_exc())
        <span class="hljs-keyword">raise</span> HTTPException(status_code=<span class="hljs-number">500</span>, detail=<span class="hljs-built_in">str</span>(e) + traceback.format_exc())`,wrap:!1}}),k=new x({props:{code:"b3V0cHV0JTIwJTNEJTIwYXdhaXQlMjBsb29wLnJ1bl9pbl9leGVjdXRvcihOb25lJTJDJTIwbGFtYmRhJTNBJTIwcGlwZWxpbmUoaW1hZ2VfaW5wdXQucHJvbXB0JTJDJTIwZ2VuZXJhdG9yJTIwJTNEJTIwZ2VuZXJhdG9yKSk=",highlighted:'output = <span class="hljs-keyword">await</span> loop.run_in_executor(<span class="hljs-literal">None</span>, <span class="hljs-keyword">lambda</span>: pipeline(image_input.prompt, generator = generator))',wrap:!1}}),B=new $e({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/create_a_server.md"}}),{c(){r=i("meta"),V=a(),X=i("p"),H=a(),c(d.$$.fragment),R=a(),c(w.$$.fragment),Y=a(),f=i("p"),f.textContent=pe,E=a(),T=i("p"),T.innerHTML=oe,N=a(),j=i("p"),j.innerHTML=re,S=a(),c(U.$$.fragment),F=a(),J=i("p"),J.textContent=ce,L=a(),c(b.$$.fragment),Q=a(),g=i("p"),g.textContent=he,q=a(),c(C.$$.fragment),z=a(),I=i("p"),I.innerHTML=me,P=a(),c(v.$$.fragment),D=a(),$=i("p"),$.innerHTML=Me,K=a(),c(G.$$.fragment),O=a(),Z=i("p"),Z.innerHTML=ue,ee=a(),c(k.$$.fragment),te=a(),_=i("p"),_.innerHTML=ye,le=a(),A=i("p"),A.innerHTML=de,se=a(),c(B.$$.fragment),ae=a(),W=i("p"),this.h()},l(e){const t=ge("svelte-u9bgzb",document.head);r=p(t,"META",{name:!0,content:!0}),t.forEach(l),V=n(e),X=p(e,"P",{}),we(X).forEach(l),H=n(e),h(d.$$.fragment,e),R=n(e),h(w.$$.fragment,e),Y=n(e),f=p(e,"P",{"data-svelte-h":!0}),o(f)!=="svelte-1tax1pm"&&(f.textContent=pe),E=n(e),T=p(e,"P",{"data-svelte-h":!0}),o(T)!=="svelte-1tktrqi"&&(T.innerHTML=oe),N=n(e),j=p(e,"P",{"data-svelte-h":!0}),o(j)!=="svelte-1aiwesf"&&(j.innerHTML=re),S=n(e),h(U.$$.fragment,e),F=n(e),J=p(e,"P",{"data-svelte-h":!0}),o(J)!=="svelte-c719ce"&&(J.textContent=ce),L=n(e),h(b.$$.fragment,e),Q=n(e),g=p(e,"P",{"data-svelte-h":!0}),o(g)!=="svelte-1qh6v4"&&(g.textContent=he),q=n(e),h(C.$$.fragment,e),z=n(e),I=p(e,"P",{"data-svelte-h":!0}),o(I)!=="svelte-1j1bynp"&&(I.innerHTML=me),P=n(e),h(v.$$.fragment,e),D=n(e),$=p(e,"P",{"data-svelte-h":!0}),o($)!=="svelte-1cbt1a"&&($.innerHTML=Me),K=n(e),h(G.$$.fragment,e),O=n(e),Z=p(e,"P",{"data-svelte-h":!0}),o(Z)!=="svelte-zd8in7"&&(Z.innerHTML=ue),ee=n(e),h(k.$$.fragment,e),te=n(e),_=p(e,"P",{"data-svelte-h":!0}),o(_)!=="svelte-88w77b"&&(_.innerHTML=ye),le=n(e),A=p(e,"P",{"data-svelte-h":!0}),o(A)!=="svelte-hjn2ql"&&(A.innerHTML=de),se=n(e),h(B.$$.fragment,e),ae=n(e),W=p(e,"P",{}),we(W).forEach(l),this.h()},h(){fe(r,"name","hf:doc:metadata"),fe(r,"content",Ze)},m(e,t){Ce(document.head,r),s(e,V,t),s(e,X,t),s(e,H,t),m(d,e,t),s(e,R,t),m(w,e,t),s(e,Y,t),s(e,f,t),s(e,E,t),s(e,T,t),s(e,N,t),s(e,j,t),s(e,S,t),m(U,e,t),s(e,F,t),s(e,J,t),s(e,L,t),m(b,e,t),s(e,Q,t),s(e,g,t),s(e,q,t),m(C,e,t),s(e,z,t),s(e,I,t),s(e,P,t),m(v,e,t),s(e,D,t),s(e,$,t),s(e,K,t),m(G,e,t),s(e,O,t),s(e,Z,t),s(e,ee,t),m(k,e,t),s(e,te,t),s(e,_,t),s(e,le,t),s(e,A,t),s(e,se,t),m(B,e,t),s(e,ae,t),s(e,W,t),ne=!0},p:je,i(e){ne||(M(d.$$.fragment,e),M(w.$$.fragment,e),M(U.$$.fragment,e),M(b.$$.fragment,e),M(C.$$.fragment,e),M(v.$$.fragment,e),M(G.$$.fragment,e),M(k.$$.fragment,e),M(B.$$.fragment,e),ne=!0)},o(e){u(d.$$.fragment,e),u(w.$$.fragment,e),u(U.$$.fragment,e),u(b.$$.fragment,e),u(C.$$.fragment,e),u(v.$$.fragment,e),u(G.$$.fragment,e),u(k.$$.fragment,e),u(B.$$.fragment,e),ne=!1},d(e){e&&(l(V),l(X),l(H),l(R),l(Y),l(f),l(E),l(T),l(N),l(j),l(S),l(F),l(J),l(L),l(Q),l(g),l(q),l(z),l(I),l(P),l(D),l($),l(K),l(O),l(Z),l(ee),l(te),l(_),l(le),l(A),l(se),l(ae),l(W)),l(r),y(d,e),y(w,e),y(U,e),y(b,e),y(C,e),y(v,e),y(G,e),y(k,e),y(B,e)}}}const Ze='{"title":"Create a server","local":"create-a-server","sections":[],"depth":1}';function ke(ie){return Ue(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class We extends Je{constructor(r){super(),be(this,r,ke,Ge,Te,{})}}export{We as component};
