-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
171 lines (131 loc) · 6.37 KB
/
views.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from flask import Blueprint, request, render_template, Response, jsonify
from stable_diffusion import sd_generate, sd_generate_out, sd_get_model_id, sd_change_model, sd_change_scheduler, sd_get_cached_models_list, scheduler_names, scheduler_sel
import time, base64, os
from PIL import Image, ImageFilter
from io import BytesIO
import numpy as np
import datetime
views = Blueprint(__name__, "views")
start_from_no_image = True
@views.route("/")
def index():
cache_busting_param = str(time.time())
model_sel=sd_get_model_id()
models=sd_get_cached_models_list()
return render_template('index.html', model_sel=model_sel, models=models, scheduler_sel=scheduler_sel, schedulers=scheduler_names, cache_busting_param=cache_busting_param)
@views.route('/test', methods=['POST'])
def testfn():
w = int(request.form.get('width'))
h = int(request.form.get('height'))
mask_data = request.form.get('mask').split(',')
a = np.asarray(mask_data).astype('byte').reshape(-1)
image = Image.frombuffer('RGBA', (w, h), a)
image.save('out_mask.png')
return 'Success!'
def save_renders(images, orig=None, mask=None):
print(f'Num images generated: {len(images)}', flush=True)
if not os.path.exists('static/images'):
os.makedirs('static/images')
current_time = datetime.datetime.now()
files = []
for i, image in enumerate(images):
name = 'static/images/tmp'+current_time.strftime("%Y%m%d_%H%M%S")+str(i)+'.png'
files.append(name)
if orig and mask: image.paste(orig, (0, 0), mask)
image.save(name)
return files
@views.route('/txt2img', methods=['POST'])
def txt2img():
prompt = request.form.get('prompt')
negative = request.form.get('negative')
width = int(request.form.get('width'))
height = int(request.form.get('height'))
num_steps = int(request.form.get('numSteps'))
guidance = float(request.form.get('guidance'))
batch_size = int(request.form.get('batch_size'))
images = sd_generate(prompt=prompt, negative=negative, width=width, height=height, steps=num_steps, guidance=guidance, batch_size=batch_size);
files = save_renders(images)
return jsonify({'urls':files, 'count': len(files)})
@views.route('/img2img', methods=['POST'])
def img2img():
prompt = request.form.get('prompt')
negative = request.form.get('negative')
width = int(request.form.get('width'))
height = int(request.form.get('height'))
num_steps = int(request.form.get('numSteps'))
guidance = float(request.form.get('guidance'))
batch_size = int(request.form.get('batch_size'))
noise = float(request.form.get('noise'))/100
_, img_data = request.form.get('image').split(',')
img = Image.open(BytesIO(base64.b64decode(img_data))).convert("RGB")
images = sd_generate(image=img, prompt=prompt, negative=negative, width=width, height=height, steps=num_steps, guidance=guidance, noise=noise, batch_size=batch_size)
files = save_renders(images)
return jsonify({'urls':files, 'count': len(files)})
@views.route('/inpainting', methods=['POST'])
def inpainting():
prompt = request.form.get('prompt')
negative = request.form.get('negative')
width = int(request.form.get('width'))
height = int(request.form.get('height'))
num_steps = int(request.form.get('numSteps'))
guidance = float(request.form.get('guidance'))
batch_size = int(request.form.get('batch_size'))
noise = float(request.form.get('noise'))/100 # converting user scale of 0-100 to model scale of 0-1.
_, img_data = request.form.get('image').split(',')
_, mask_data = request.form.get('mask').split(',')
orig = Image.open(BytesIO(base64.b64decode(img_data))).convert("RGB")
mask = Image.open(BytesIO(base64.b64decode(mask_data)))
mask = mask.filter(ImageFilter.MaxFilter(9))
npimg = np.array(mask)
trans_mask = (npimg[:,:,3] == 0)
npimg[trans_mask] = [0, 0, 0, 255]
npimg[np.logical_not(trans_mask)] = [0,0,0,0]
pastemask = Image.fromarray(npimg)
pastemask = pastemask.filter(ImageFilter.BoxBlur(2))
mask = mask.convert("RGB")
images = sd_generate(image=orig, mask=mask, prompt=prompt, negative=negative, width=width, height=height, steps=num_steps, guidance=guidance, noise=noise, batch_size=batch_size)
files = save_renders(images, orig, pastemask)
return jsonify({'urls':files, 'count': len(files)})
@views.route('/outpainting', methods=['POST'])
def outpainting():
prompt = request.form.get('prompt')
negative = request.form.get('negative')
width = int(request.form.get('width'))
height = int(request.form.get('height'))
num_steps = int(request.form.get('numSteps'))
guidance = float(request.form.get('guidance'))
batch_size = int(request.form.get('batch_size'))
_, img_data = request.form.get('image').split(',')
_, mask_data = request.form.get('mask').split(',')
orig = Image.open(BytesIO(base64.b64decode(img_data)))
mask = Image.open(BytesIO(base64.b64decode(mask_data)))
mask = mask.filter(ImageFilter.MaxFilter(9))
npimg = np.array(mask)
trans_mask = (npimg[:,:,3] == 0)
npimg[trans_mask] = [0, 0, 0, 255]
npimg[np.logical_not(trans_mask)] = [0,0,0,0]
pastemask = Image.fromarray(npimg)
pastemask = pastemask.filter(ImageFilter.BoxBlur(2))
mask = mask.convert("RGB")
images = sd_generate_out(image=orig.convert("RGB"), mask=mask, prompt=prompt, negative=negative, width=width, height=height, steps=num_steps, guidance=guidance, batch_size=batch_size)
files = save_renders(images, orig, pastemask)
return jsonify({'urls':files, 'count': len(files)})
@views.route('/change_model', methods=['POST'])
def change_model():
mid = request.form.get('model_id')
mid = sd_change_model(mid)
return jsonify({'model_id':mid})
@views.route('/change_scheduler', methods=['POST'])
def change_scheduler():
scheduler = request.form.get('scheduler')
scheduler = sd_change_scheduler(scheduler)
return jsonify({'scheduler': scheduler})
@views.route('/save_image', methods=['POST'])
def save_image():
_, img_data = request.form.get('image').split(',')
image = Image.open(BytesIO(base64.b64decode(img_data)))
if not os.path.exists('static/saves'):
os.makedirs('static/saves')
current_time = datetime.datetime.now()
image.save('static/saves/image'+current_time.strftime("%Y%m%d_%H%M%S")+'.png')
return 'Success'