-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
151 lines (135 loc) · 4.43 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from potassium import Potassium, Request, Response
import io
import gzip
import torch
import qrcode
import base64
from PIL import Image
from io import BytesIO
from typing import List
from PIL.Image import LANCZOS
from diffusers import (
AutoencoderKL,
StableDiffusionControlNetPipeline,
ControlNetModel,
DPMSolverMultistepScheduler,
EulerDiscreteScheduler
)
BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
BASE_CACHE = "model-cache"
CONTROL_CACHE = "control-cache"
VAE_CACHE = "vae-cache"
IMG_CACHE = "img-cache"
SAMPLER_MAP = {
"DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
}
app = Potassium("my_app")
# @app.init runs at startup, and loads models into the app's context
@app.init
def init():
device = 0 if torch.cuda.is_available() else -1
vae = AutoencoderKL.from_pretrained(
"stabilityai/sd-vae-ft-mse",
torch_dtype=torch.float16,
cache_dir=VAE_CACHE,
)
controlnet = ControlNetModel.from_pretrained(
"monster-labs/control_v1p_sd15_qrcode_monster",
torch_dtype=torch.float16,
cache_dir=CONTROL_CACHE,
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
BASE_MODEL,
controlnet=controlnet,
vae=vae,
safety_checker=None,
torch_dtype=torch.float16,
cache_dir=BASE_CACHE,
).to("cuda")
context = {
"pipe": pipe
}
return context
def resize_for_condition_image(input_image, width, height):
input_image = input_image.convert("RGB")
W, H = input_image.size
k = float(min(width, height)) / min(H, W)
H *= k
W *= k
H = int(round(H / 64.0)) * 64
W = int(round(W / 64.0)) * 64
img = input_image.resize((W, H), resample=LANCZOS)
return img
def generate_qrcode(qr_code_content, background, border, width, height):
print("Generating QR Code from content")
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=border,
)
qr.add_data(qr_code_content)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color=background)
qrcode_image = resize_for_condition_image(qrcode_image, width, height)
return qrcode_image
# @app.handler runs for every call
@app.handler("/")
def handler(context: dict, request: Request) -> Response:
# Parameters
prompt = request.json.get("prompt")
qr_code_content = request.json.get("qr_code_content")
negative_prompt = request.json.get("negative_prompt")
controlnet_conditioning_scale = 1.0
image = request.json.get("image")
seed = int(request.json.get("seed"))
guidance_scale = 7.5
num_inference_steps = 40
width = 768
height = 768
num_outputs = 1
# Model
pipe = context.get("pipe")
# Seed
if seed == None:
seed = torch.randint(0, 2**32, (1,)).item()
print(f"Seed: {seed}")
image = base64.b64decode(image.encode("utf-8"))
image_io = io.BytesIO(image)
# Controlism img
if image is None:
if qrcode_background == "gray":
qrcode_background = "#808080"
image = generate_qrcode(
qr_code_content, background=qrcode_background, border=1, width=width, height=height,
)
else:
image = Image.open(image_io)
# Run pipeline
output = pipe(
prompt=[prompt] * num_outputs,
negative_prompt=[negative_prompt] * num_outputs,
image=[image] * num_outputs,
width=width,
height=height,
guidance_scale=guidance_scale,
controlnet_conditioning_scale=controlnet_conditioning_scale,
generator=torch.Generator().manual_seed(seed),
num_inference_steps=num_inference_steps,
)
img_out = output.images[0]
# Make return image smaller to fit under 1MB
img_out = img_out.resize((576, 576), Image.ANTIALIAS)
fname = f"out.png"
buffered = BytesIO()
img_out.save(buffered, format="png", optimize=True, quality=80)
img_data = buffered.getvalue()
compressed_img_data = gzip.compress(img_data)
compressed_img_str = base64.b64encode(compressed_img_data).decode('utf-8')
return Response(
json = {"outputs": compressed_img_str},
status=200
)
if __name__ == "__main__":
app.serve()