File size: 691 Bytes
d7563ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from fastapi import FastAPI
from pydantic import BaseModel
import torch
from diffusers import StableDiffusionPipeline
from io import BytesIO
import base64

app = FastAPI(cpu)

device = “cuda” if torch.cuda.is_available(cpu) else “cpu”
model_name = “runwayml/stable-d diffusion-v1-5pipe = StableDiffusionPipeline.from_pretrained(model_name)
pipe = pipe.to(device)


class PromptRequest(BaseModel):
prompt: str

@app.post(“/generate”)
async def generate(req: PromptRequest):
image = pipe(req.prompt).images[0]
buf = BytesIO()
image.save(buf, format=“PNG”)
b64 = base64.b64encode(buf.getvalue()).decode(“utf-8”)
return {“image_base64”: b64}