forked from ant-research/MagicQuill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_run.py
335 lines (302 loc) · 12.3 KB
/
gradio_run.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import subprocess
import os
import gradio as gr
import os
from gradio_magicquill import MagicQuill
import random
import torch
import numpy as np
from PIL import Image, ImageOps
import base64
import io
from fastapi import FastAPI, Request
import uvicorn
import requests
from MagicQuill import folder_paths
from MagicQuill.llava_new import LLaVAModel
from MagicQuill.scribble_color_edit import ScribbleColorEditModel
import time
import io
llavaModel = LLaVAModel()
scribbleColorEditModel = ScribbleColorEditModel()
def tensor_to_base64(tensor):
tensor = tensor.squeeze(0) * 255.
pil_image = Image.fromarray(tensor.cpu().byte().numpy())
buffered = io.BytesIO()
pil_image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
def read_base64_image(base64_image):
if base64_image.startswith("data:image/png;base64,"):
base64_image = base64_image.split(",")[1]
elif base64_image.startswith("data:image/jpeg;base64,"):
base64_image = base64_image.split(",")[1]
elif base64_image.startswith("data:image/webp;base64,"):
base64_image = base64_image.split(",")[1]
else:
raise ValueError("Unsupported image format.")
image_data = base64.b64decode(base64_image)
image = Image.open(io.BytesIO(image_data))
image = ImageOps.exif_transpose(image)
return image
def create_alpha_mask(base64_image):
"""Create an alpha mask from the alpha channel of an image."""
image = read_base64_image(base64_image)
mask = torch.zeros((1, image.height, image.width), dtype=torch.float32, device="cpu")
if 'A' in image.getbands():
alpha_channel = np.array(image.getchannel('A')).astype(np.float32) / 255.0
mask[0] = 1.0 - torch.from_numpy(alpha_channel)
return mask
def load_and_preprocess_image(base64_image, convert_to='RGB', has_alpha=False):
"""Load and preprocess a base64 image."""
image = read_base64_image(base64_image)
image = image.convert(convert_to)
image_array = np.array(image).astype(np.float32) / 255.0
image_tensor = torch.from_numpy(image_array)[None,]
return image_tensor
def load_and_resize_image(base64_image, convert_to='RGB', max_size=512):
"""Load and preprocess a base64 image, resize if necessary."""
image = read_base64_image(base64_image)
image = image.convert(convert_to)
width, height = image.size
scaling_factor = max_size / min(width, height)
new_size = (int(width * scaling_factor), int(height * scaling_factor))
image = image.resize(new_size, Image.LANCZOS)
image_array = np.array(image).astype(np.float32) / 255.0
image_tensor = torch.from_numpy(image_array)[None,]
return image_tensor
def prepare_images_and_masks(total_mask, original_image, add_color_image, add_edge_image, remove_edge_image):
total_mask = create_alpha_mask(total_mask)
original_image_tensor = load_and_preprocess_image(original_image)
if add_color_image:
add_color_image_tensor = load_and_preprocess_image(add_color_image)
else:
add_color_image_tensor = original_image_tensor
add_edge_mask = create_alpha_mask(add_edge_image) if add_edge_image else torch.zeros_like(total_mask)
remove_edge_mask = create_alpha_mask(remove_edge_image) if remove_edge_image else torch.zeros_like(total_mask)
return add_color_image_tensor, original_image_tensor, total_mask, add_edge_mask, remove_edge_mask
def guess(original_image_tensor, add_color_image_tensor, add_edge_mask):
description, ans1, ans2 = llavaModel.process(original_image_tensor, add_color_image_tensor, add_edge_mask)
ans_list = []
if ans1 and ans1 != "":
ans_list.append(ans1)
if ans2 and ans2 != "":
ans_list.append(ans2)
return ", ".join(ans_list)
def guess_prompt_handler(original_image, add_color_image, add_edge_image):
original_image_tensor = load_and_preprocess_image(original_image)
if add_color_image:
add_color_image_tensor = load_and_preprocess_image(add_color_image)
else:
add_color_image_tensor = original_image_tensor
width, height = original_image_tensor.shape[1], original_image_tensor.shape[2]
add_edge_mask = create_alpha_mask(add_edge_image) if add_edge_image else torch.zeros((1, height, width), dtype=torch.float32, device="cpu")
res = guess(original_image_tensor, add_color_image_tensor, add_edge_mask)
return res
def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_image, remove_edge_image, positive_prompt, negative_prompt, grow_size, stroke_as_edge, fine_edge, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
add_color_image, original_image, total_mask, add_edge_mask, remove_edge_mask = prepare_images_and_masks(total_mask, original_image, add_color_image, add_edge_image, remove_edge_image)
progress = None
if torch.sum(remove_edge_mask).item() > 0 and torch.sum(add_edge_mask).item() == 0:
if positive_prompt == "":
positive_prompt = "empty scene"
edge_strength /= 3.
latent_samples, final_image, lineart_output, color_output = scribbleColorEditModel.process(
ckpt_name,
original_image,
add_color_image,
positive_prompt,
negative_prompt,
total_mask,
add_edge_mask,
remove_edge_mask,
grow_size,
stroke_as_edge,
fine_edge,
edge_strength,
color_strength,
inpaint_strength,
seed,
steps,
cfg,
sampler_name,
scheduler,
progress
)
final_image_base64 = tensor_to_base64(final_image)
return final_image_base64
def generate_image_handler(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
if seed == -1:
seed = random.randint(0, 2**32 - 1)
ms_data = x['from_frontend']
positive_prompt = x['from_backend']['prompt']
stroke_as_edge = "enable"
res = generate(
ckpt_name,
ms_data['total_mask'],
ms_data['original_image'],
ms_data['add_color_image'],
ms_data['add_edge_image'],
ms_data['remove_edge_image'],
positive_prompt,
negative_prompt,
grow_size,
stroke_as_edge,
fine_edge,
edge_strength,
color_strength,
inpaint_strength,
seed,
steps,
cfg,
sampler_name,
scheduler
)
x["from_backend"]["generated_image"] = res
return x
def save_generated_image(image_data):
if image_data is None:
return "No image to save"
try:
if isinstance(image_data, dict) and 'original_image' in image_data.get('from_frontend', {}):
img_str = image_data['from_frontend']['original_image']
if img_str.startswith("data:image/png;base64,"):
img_str = img_str.split(",")[1]
img_data = base64.b64decode(img_str)
img = Image.open(io.BytesIO(img_data))
os.makedirs("output", exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
save_path = os.path.join("output", f"magicquill_{timestamp}.png")
img.save(save_path)
return f"Image saved to: {save_path}"
except Exception as e:
return f"Error saving image: {str(e)}"
return "Failed to save image"
css = '''
.row {
width: 90%;
margin: auto;
}
footer {
visibility:
hidden
}
'''
with gr.Blocks(css=css) as demo:
with gr.Row(elem_classes="row"):
ms = MagicQuill()
with gr.Row(elem_classes="row"):
with gr.Column():
btn = gr.Button("Run", variant="primary")
with gr.Column():
with gr.Accordion("parameters", open=False):
ckpt_name = gr.Dropdown(
label="Base Model Name",
choices=folder_paths.get_filename_list("checkpoints"),
value=os.path.join('SD1.5', 'realisticVisionV60B1_v51VAE.safetensors'),
interactive=True
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
value="",
interactive=True
)
# stroke_as_edge = gr.Radio(
# label="Stroke as Edge",
# choices=['enable', 'disable'],
# value='enable',
# interactive=True
# )
fine_edge = gr.Radio(
label="Fine Edge",
choices=['enable', 'disable'],
value='disable',
interactive=True
)
grow_size = gr.Slider(
label="Grow Size",
minimum=0,
maximum=100,
value=15,
step=1,
interactive=True
)
edge_strength = gr.Slider(
label="Edge Strength",
minimum=0.0,
maximum=5.0,
value=0.55,
step=0.01,
interactive=True
)
color_strength = gr.Slider(
label="Color Strength",
minimum=0.0,
maximum=5.0,
value=0.55,
step=0.01,
interactive=True
)
inpaint_strength = gr.Slider(
label="Inpaint Strength",
minimum=0.0,
maximum=5.0,
value=1.0,
step=0.01,
interactive=True
)
seed = gr.Number(
label="Seed",
value=-1,
precision=0,
interactive=True
)
steps = gr.Slider(
label="Steps",
minimum=1,
maximum=50,
value=20,
interactive=True
)
cfg = gr.Slider(
label="CFG",
minimum=0.0,
maximum=100.0,
value=5.0,
step=0.1,
interactive=True
)
sampler_name = gr.Dropdown(
label="Sampler Name",
choices=["euler", "euler_ancestral", "heun", "heunpp2","dpm_2", "dpm_2_ancestral", "lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu", "dpmpp_2m", "dpmpp_2m_sde", "dpmpp_2m_sde_gpu", "dpmpp_3m_sde", "dpmpp_3m_sde_gpu", "ddpm", "lcm", "ddim", "uni_pc", "uni_pc_bh2"],
value='euler_ancestral',
interactive=True
)
scheduler = gr.Dropdown(
label="Scheduler",
choices=["normal", "karras", "exponential", "sgm_uniform", "simple", "ddim_uniform"],
value='karras',
interactive=True
)
btn.click(generate_image_handler, inputs=[ms, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler], outputs=ms)
# with gr.Row(elem_classes="row"):
# with gr.Column():
# save_btn = gr.Button("Save Image", variant="secondary")
# # save_status = gr.Textbox(label="Save Status", interactive=False)
# save_btn.click(fn=save_generated_image, inputs=[ms])
app = FastAPI()
@app.post("/magic_quill/guess_prompt")
async def guess_prompt(request: Request):
data = await request.json()
res = guess_prompt_handler(data['original_image'], data['add_color_image'], data['add_edge_image'])
return res
@app.post("/magic_quill/process_background_img")
async def process_background_img(request: Request):
img = await request.json()
resized_img_tensor = load_and_resize_image(img)
resized_img_base64 = "data:image/png;base64," + tensor_to_base64(resized_img_tensor)
# add more processing here
return resized_img_base64
app = gr.mount_gradio_app(app, demo, "/")
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=7860)
# demo.launch()