Buckets:

download
raw
9.79 kB
import"../chunks/DsnmJJEf.js";import{i as j,h as T,C as U,H as J,a as e,E as g,s as f}from"../chunks/CmJXCtRL.js";import{p as b,o as I,s,f as C,a as y,b as G,d as M,n as Z}from"../chunks/DK803DsY.js";const v='{"title":"Create a server","local":"create-a-server","sections":[],"depth":1}';var B=M('<meta name="hf:doc:metadata"/>'),A=M('<p></p> <!> <!> <p>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.</p> <p>This guide will show you how to use the <a href="/docs/diffusers/pr_14407/en/api/pipelines/stable_diffusion/stable_diffusion_3#diffusers.StableDiffusion3Pipeline">StableDiffusion3Pipeline</a> in a server, but feel free to use any pipeline you want.</p> <p>Start by navigating to the <code>examples/server</code> folder and installing all of the dependencies.</p> <!> <p>Launch the server with the following command.</p> <!> <p>The server is accessed at http://localhost:8000. You can curl this model with the following command.</p> <!> <p>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> <!> <p>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.</p> <!> <p>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.</p> <!> <p>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>.</p> <p>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.</p> <!> <p></p>',1);function V(u,w){b(w,!1),I(()=>{new URLSearchParams(window.location.search).get("fw")}),j();var a=A();T("ncq2c6",h=>{var d=B();f(d,"content",v),y(h,d)});var l=s(C(a),2);U(l,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var n=s(l,2);J(n,{title:"Create a server",local:"create-a-server",headingTag:"h1"});var t=s(n,8);e(t,{code:"cGlwJTIwaW5zdGFsbCUyMC4lMEFwaXAlMjBpbnN0YWxsJTIwLWYlMjByZXF1aXJlbWVudHMudHh0",highlighted:`pip install .
pip install -f requirements.txt`,lang:"py",wrap:!1});var o=s(t,4);e(o,{code:"cHl0aG9uJTIwc2VydmVyLnB5",highlighted:"python server.py",lang:"py",wrap:!1});var i=s(o,4);e(i,{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',lang:"",wrap:!1});var p=s(i,4);e(p,{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',lang:"",wrap:!1});var r=s(p,4);e(r,{code:"JTQwYXBwLnBvc3QoJTIyJTJGdjElMkZpbWFnZXMlMkZnZW5lcmF0aW9ucyUyMiklMEFhc3luYyUyMGRlZiUyMGdlbmVyYXRlX2ltYWdlKGltYWdlX2lucHV0JTNBJTIwVGV4dFRvSW1hZ2VJbnB1dCklM0ElMEElMjAlMjAlMjAlMjB0cnklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb29wJTIwJTNEJTIwYXN5bmNpby5nZXRfZXZlbnRfbG9vcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NoZWR1bGVyJTIwJTNEJTIwc2hhcmVkX3BpcGVsaW5lLnBpcGVsaW5lLnNjaGVkdWxlci5mcm9tX2NvbmZpZyhzaGFyZWRfcGlwZWxpbmUucGlwZWxpbmUuc2NoZWR1bGVyLmNvbmZpZyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwaXBlbGluZSUyMCUzRCUyMFN0YWJsZURpZmZ1c2lvbjNQaXBlbGluZS5mcm9tX3BpcGUoc2hhcmVkX3BpcGVsaW5lLnBpcGVsaW5lJTJDJTIwc2NoZWR1bGVyJTNEc2NoZWR1bGVyKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjdWRhJ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aWwlM0RlLm1lc3NhZ2UlMjAlMkIlMjB0cmFjZWJhY2suZm9ybWF0X2V4YygpKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJhaXNlJTIwSFRUUEV4Y2VwdGlvbihzdGF0dXNfY29kZSUzRDUwMCUyQyUyMGRldGFpbCUzRHN0cihlKSUyMCUyQiUyMHRyYWNlYmFjay5mb3JtYXRfZXhjKCkp",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())`,lang:"py",wrap:!1});var c=s(r,4);e(c,{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))',lang:"py",wrap:!1});var m=s(c,6);g(m,{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/create_a_server.md"}),Z(2),y(u,a),G()}export{V as component};

Xet Storage Details

Size:
9.79 kB
·
Xet hash:
95f5e18e2f86a8e48d24ff0fbbad6378c4aaa4ffb2e82e8449d28142f5060b2f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.