forked from bananaml/demo-sd-hf-safetensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (49 loc) · 1.71 KB
/
app.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from io import BytesIO
from potassium import Potassium, Request, Response
from diffusers import DiffusionPipeline, DDPMScheduler
import torch
import base64
# create a new Potassium app
app = Potassium("my_app")
# @app.init runs at startup, and loads models into the app's context
@app.init
def init():
repo_id="Meina/MeinaUnreal_V3"
ddpm = DDPMScheduler.from_pretrained(repo_id, subfolder="scheduler")
model = DiffusionPipeline.from_pretrained(
repo_id,
use_safetensors=True,
torch_dtype=torch.float16,
scheduler=ddpm
).to("cuda")
context = {
"model": model,
}
return context
# @app.handler runs for every call
@app.handler()
def handler(context: dict, request: Request) -> Response:
model = context.get("model")
prompt = request.json.get("prompt")
negative_prompt = "(worst quality, low quality:1.4), monochrome, zombie, (interlocked fingers), cleavage, nudity, naked, nude"
image = model(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=7,
num_inference_steps=request.json.get("steps", 50),
generator=torch.Generator(device="cuda").manual_seed(request.json.get("seed")) if request.json.get("seed") else None,
width=512,
height=768,
).images[0]
buffered = BytesIO()
image.save(buffered, format="JPEG", quality=80)
img_str = base64.b64encode(buffered.getvalue())
# You could also consider writing this image to S3
# and returning the S3 URL instead of the image data
# for a slightly faster response time
return Response(
json = {"output": str(img_str, "utf-8")},
status=200
)
if __name__ == "__main__":
app.serve()