-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreecond_app.py
364 lines (340 loc) · 18 KB
/
freecond_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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
print("⏬ freecond_app.py activated, retrieving packages ...")
import gradio as gr
from PIL import Image
import numpy as np
import torch
from freecond_src.freecond_utils import get_pipeline_forward
from freecond_src.freecond import fc_config
# Mock functions for your model (replace these with actual implementations)
def load_pretrained_weights(weight_path):
print("!!!!")
if weight_path == "SDXLInpainting":
pipeline, forward = get_pipeline_forward(method="sd",variant="sdxl")
if weight_path == "StableDiffusionInpainting":
pipeline, forward = get_pipeline_forward(method="sd",variant="sd15")
if weight_path == "ControlNetInpainting":
pipeline, forward = get_pipeline_forward(method="cn")
if weight_path == "HD-Painter":
pipeline, forward = get_pipeline_forward(method="hdp",variant="sd15",device="cuda")
if weight_path == "PowerPaint":
pipeline, forward = get_pipeline_forward(method="pp",device="cuda")
if weight_path == "BrushNet":
pipeline, forward = get_pipeline_forward(method="bn",device="cuda")
return forward, f"{weight_path} loaded"
# Gradio app components
def process_inpainting(mask_bool, forward, image, mask, prompt, nprompt,
seed, hsize, wsize, gs, step,
tfc, a1, a2, b1, b2, g1, g2):
if mask_bool==True:
input_mask=mask
r_info="Use the specified mask instead of draw mask"
else:
input_mask=image["mask"]
torch.manual_seed(seed)
fc_control=fc_config(change_step=tfc, fg_1=a1, fg_2=a2, bg_1=b1, bg_2=b2, hq_1=0, hq_2=1,lq_1=1,lq_2=1,fq_th=int(g1*32))
output = forward(fc_control, init_image=image["image"].resize((hsize,wsize)), mask_image=input_mask.convert("L").resize((hsize,wsize))
, prompt=prompt, negative_prompt=nprompt, num_inference_steps=step, guidance_scale=gs)
r_info="Use brushed mask"
#return r_info, image["image"]
return r_info, output[0]
predefined_settings = {
"Draw a FREECOND in the galaxy (1)":{"prompt": "A huge word made by glowing neon light, fantasy style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 1234,
"gs": 15,
"image": Image.open("./demo_data/galaxy.jpg"),
"mask": Image.open("./demo_data/freecond_mask.png"),
"tfc": 35,
"a1": 2,
"a2": 2,
"b1": 0.6,
"b2": 0.6,
"g1":0.625,
},
"Draw a FREECOND in the galaxy (2)":{"prompt": "A huge word made by shining stars and magic runes, fantasy style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 1234,
"gs": 15,
"image": Image.open("./demo_data/galaxy.jpg"),
"mask": Image.open("./demo_data/freecond_mask.png"),
"tfc": 35,
"a1": 3,
"a2": 3,
"b1": 0.6,
"b2": 0.6,
"g1":0.75,
},
"Draw a FREECOND in the desert":{"prompt": "A huge word made by glowing light bulbs, christmas style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 47562,
"gs": 15,
"image": Image.open("./demo_data/desert.jpg"),
"mask": Image.open("./demo_data/freecond_mask.png"),
"tfc": 35,
"a1": 3,
"a2": 3,
"b1": 0,
"b2": 0.5,
"g1":0.625,
},
"Draw a christmas quokka":{"prompt": "A white quokka wearing round glasses, christmas style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 47562,
"gs": 15,
"image": Image.open("./demo_data/desert.jpg"),
"mask": Image.open("./demo_data/quokka_mask.png"),
"tfc": 25,
"a1": 2,
"a2": 1,
"b1": 0.2,
"b2": 0.2,
"g1":0.75,
},
"Draw a halloween quokka":{"prompt": "A white quokka wearing round glasses, vampire cloak, halloween style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 7414,
"gs": 15,
"image": Image.open("./demo_data/dark_woods.jpg"),
"mask": Image.open("./demo_data/quokka_mask.png"),
"tfc": 10,
"a1": 1,
"a2": 2,
"b1": 0.2,
"b2": 0.2,
"g1":0.75,
},
"Draw a gangster otter":{"prompt": "A gangster otter wearing sunglasses, anime style, jojo style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 1234,
"gs": 15,
"image": Image.open("./demo_data/alley.jpg"),
"mask": Image.open("./demo_data/otter_mask.png"),
"tfc": 15,
"a1": 1,
"a2": 2,
"b1": 0.2,
"b2": 0.2,
"g1":0.75,
},
"Draw a spy otter":{"prompt": "A spy otter wearing sunglasses wearing black cloak, anime style, Detective Conan style",
"nprompt":"word, bad quality, bad anatomy, ugly, mutation, blurry, error",
"seed": 9453,
"gs": 15,
"image": Image.open("./demo_data/alley.jpg"),
"mask": Image.open("./demo_data/otter_mask.png"),
"tfc": 10,
"a1": 3,
"a2": 1,
"b1": 0.2,
"b2": 0.2,
"g1":0.75,
},
}
def apply_preset(selected_preset):
settings = predefined_settings[selected_preset]
return True, settings["prompt"], settings["nprompt"], settings["seed"], settings["gs"], settings["image"], settings["mask"], settings["tfc"], settings["a1"], settings["a2"], settings["b1"], settings["b2"], settings["g1"]
with gr.Blocks() as demo:
forward_state = gr.State(value=None)
with gr.Row(equal_height=True):
with gr.Column(scale=2):
output_status = gr.Textbox(label="Infomations",value="No weights loaded", interactive=False)
with gr.Column(scale=1):
pretrained_weight_dropdown = gr.Dropdown(
label="Select Pretrained Weight",
choices=["SDXLInpainting",
"StableDiffusionInpainting",
"ControlNetInpainting",
"HD-Painter",
"PowerPaint",
"BrushNet"], # Replace with actual weight file paths
value=None
)
with gr.Column(scale=1):
load_button = gr.Button("Load Weights")
with gr.Row():
with gr.Column(scale=2):
image_input = gr.Image(type="pil", label="Upload Image and Draw Mask", tool="sketch")
prompt = gr.Textbox(
label="Prompt", value="A quokka wearing round glasses, cartoon style, chrismas vibe",placeholder="Enter your prompt here..."
)
run_button = gr.Button("Run Inpainting")
with gr.Column(scale=2):
output_image = gr.Image(label="Output Image")
with gr.Row():
with gr.Column(scale=2):
with gr.Tab("Inpainting Settings"):
seed= gr.Slider(
minimum=0, maximum=1000000, step=1, value=1234, label="Random Seed"
)
guidance = gr.Slider(
minimum=1.0, maximum=100, step=0.5, value=15, label="Guidance Scale"
)
step = gr.Slider(
minimum=1, maximum=100, step=1, value=50, label="Inference Step"
)
nprompt = gr.Textbox(
label="nprompt", placeholder="Enter your negative prompt", value="word, bad quality, bad anatomy, ugly, mutation, blurry, error"
)
with gr.Row():
with gr.Column(scale=1):
hsize= gr.Slider(
minimum=256, maximum=1024, step=1, value=512, label="Height"
)
with gr.Column(scale=1):
wsize= gr.Slider(
minimum=256, maximum=1024, step=1, value=512, label="Width"
)
with gr.Accordion("Specific png mask input (Optional)", open=False):
mask_checkbox = gr.Checkbox(value=False, label="Enable png mask assignment")
mask_input = gr.Image(type="pil", label="png Mask (Optional)")
with gr.Column(scale=2):
with gr.Tab("FreeCond Settings"):
with gr.Row():
fc_step = gr.Slider(
minimum=0, maximum=100, step=1, value=0, label="tfc (FreeCond Step)"
)
with gr.Row():
with gr.Column(scale=1):
alpha_1= gr.Slider(
minimum=-2, maximum=5, step=0.1, value=3, label="alpha_1 (inner mask scale before tfc)")
with gr.Column(scale=1):
alpha_2= gr.Slider(
minimum=-2, maximum=5, step=0.1, value=1, label="alpha_2 (inner mask scale after tfc)")
with gr.Row():
with gr.Column(scale=1):
beta_1= gr.Slider(
minimum=-2, maximum=5, step=0.1, value=0, label="beta_1 (outter mask scale before tfc)")
with gr.Column(scale=1):
beta_2= gr.Slider(
minimum=-2, maximum=5, step=0.1, value=0, label="beta_2 (outter mask scale after tfc)")
with gr.Row():
with gr.Column(scale=1):
gamma_1= gr.Slider(
minimum=0, maximum=1, step=0.05, value=0.5, label="gamma_1 (LPF threshold before tfc)")
with gr.Column(scale=1):
gamma_2= gr.Slider(
minimum=0, maximum=1, step=0.05, value=1, label="gamma_2 (LPF threshold after tfc)")
with gr.Row():
# deafult setting
preset_selector = gr.Radio(
choices=list(predefined_settings.keys()),
value="Default",
label="Select Parameter Preset",
)
# def process_inpainting(forward, image, mask, prompt, nprompt, seed, hsize, wsize, gs, step, tfc, a1, a2, b1, b2, g1, g2):
run_button.click(
fn=process_inpainting,
inputs = [mask_checkbox, forward_state, image_input, mask_input, prompt, nprompt, seed, hsize, wsize, guidance, step, fc_step,
alpha_1, alpha_2, beta_1, beta_2, gamma_1, gamma_2 ],
outputs = [output_status, output_image]
)
load_button.click(
fn=load_pretrained_weights,
inputs = [pretrained_weight_dropdown],
outputs = [forward_state, output_status]
)
# return settings["prompt"], settings["nprompt"], settings["seed"], settings["gs"], settings["image"],
# settings["mask"], settings["tfc"], settings["a1"], settings["a2"],
# settings["b1"], settings["b2"], settings["g1"]
preset_selector.change(
fn=apply_preset,
inputs=[preset_selector],
outputs=[mask_checkbox, prompt, nprompt, seed, guidance, image_input, mask_input, fc_step, alpha_1, alpha_2, beta_1, beta_2, gamma_1,],
)
demo.launch()
# with gr.Blocks() as demo:
# forward_state = gr.State(value=None)
# with gr.Row(equal_height=True):
# with gr.Column(scale=2):
# output_status = gr.Textbox(label="Infomations",value="No weights loaded", interactive=False)
# with gr.Column(scale=1):
# pretrained_weight_dropdown = gr.Dropdown(
# label="Select Pretrained Weight",
# choices=["SDXLInpainting",
# "StableDiffusionInpainting",
# "ControlNetInpainting",
# "HD-Painter",
# "PowerPaint",
# "BrushNet"], # Replace with actual weight file paths
# value=None
# )
# with gr.Column(scale=1):
# load_button = gr.Button("Load Weights")
# with gr.Row():
# with gr.Column(scale=2):
# image_input = gr.Image(type="pil", label="Upload Image and Draw Mask", tool="sketch")
# with gr.Column(scale=2):
# output_image = gr.Image(label="Output Image")
# with gr.Row():
# with gr.Column(scale=2):
# prompt = gr.Textbox(
# label="Prompt", value="A quokka wearing round glasses, cartoon style, chrismas vibe",placeholder="Enter your prompt here..."
# )
# with gr.Column(scale=2):
# run_button = gr.Button("Run Inpainting")
# with gr.Row():
# with gr.Column(scale=2):
# with gr.Tab("Inpainting Settings"):
# seed= gr.Slider(
# minimum=0, maximum=1000000, step=1, value=1234, label="Random Seed"
# )
# guidance = gr.Slider(
# minimum=1.0, maximum=100, step=0.5, value=15, label="Guidance Scale"
# )
# step = gr.Slider(
# minimum=1, maximum=100, step=1, value=50, label="Inference Step"
# )
# nprompt = gr.Textbox(
# label="nprompt", placeholder="Enter your negative prompt", value="word, bad quality, bad anatomy, ugly, mutation, blurry, error"
# )
# with gr.Row():
# with gr.Column(scale=1):
# hsize= gr.Slider(
# minimum=256, maximum=1024, step=1, value=512, label="Height"
# )
# with gr.Column(scale=1):
# wsize= gr.Slider(
# minimum=256, maximum=1024, step=1, value=512, label="Width"
# )
# with gr.Accordion("Specific png mask input (Optional)", open=False):
# mask_input = gr.Image(type="pil", label="png Mask (Optional)")
# with gr.Column(scale=2):
# with gr.Tab("FreeCond Settings"):
# with gr.Row():
# fc_step = gr.Slider(
# minimum=1.0, maximum=100, step=1, value=25, label="tfc (FreeCond Step)"
# )
# with gr.Row():
# with gr.Column(scale=1):
# alpha_1= gr.Slider(
# minimum=-2, maximum=5, step=0.1, value=1, label="alpha_1 (inner mask scale before tfc)")
# with gr.Column(scale=1):
# alpha_2= gr.Slider(
# minimum=-2, maximum=5, step=0.1, value=1, label="alpha_2 (inner mask scale after tfc)")
# with gr.Row():
# with gr.Column(scale=1):
# beta_1= gr.Slider(
# minimum=-2, maximum=5, step=0.1, value=0, label="beta_1 (outter mask scale before tfc)")
# with gr.Column(scale=1):
# beta_2= gr.Slider(
# minimum=-2, maximum=5, step=0.1, value=0, label="beta_2 (outter mask scale after tfc)")
# with gr.Row():
# with gr.Column(scale=1):
# gamma_1= gr.Slider(
# minimum=0, maximum=1, step=0.05, value=1, label="gamma_1 (LPF threshold before tfc)")
# with gr.Column(scale=1):
# gamma_2= gr.Slider(
# minimum=0, maximum=1, step=0.05, value=1, label="gamma_2 (LPF threshold after tfc)")
# # def process_inpainting(forward, image, mask, prompt, nprompt, seed, hsize, wsize, gs, step, tfc, a1, a2, b1, b2, g1, g2):
# run_button.click(
# fn=process_inpainting,
# inputs = [forward_state, image_input, mask_input, prompt, nprompt, seed, hsize, wsize, guidance, step, fc_step,
# alpha_1, alpha_2, beta_1, beta_2, gamma_1, gamma_2 ],
# outputs = [output_status, output_image]
# )
# load_button.click(
# fn=load_pretrained_weights,
# inputs = [pretrained_weight_dropdown],
# outputs = [forward_state, output_status]
# )
# demo.launch()