Spaces:
Running
on
Zero
Running
on
Zero
Update app.py (#7)
Browse files- Update app.py (6520f9014e53dfb80f0a622ec8cbe3667c631981)
app.py
CHANGED
@@ -17,6 +17,7 @@ MODELS = {
|
|
17 |
"Juggernaut-XL-V9-GE-RDPhoto2": "AiWise/Juggernaut-XL-V9-GE-RDPhoto2-Lightning_4S",
|
18 |
"SatPony-Lightning": "John6666/satpony-lightning-v2-sdxl"
|
19 |
}
|
|
|
20 |
def init_pipeline(model_name):
|
21 |
config_file = hf_hub_download(
|
22 |
"xinsir/controlnet-union-sdxl-1.0",
|
@@ -50,22 +51,24 @@ def init_pipeline(model_name):
|
|
50 |
# Initialize with the default model
|
51 |
default_model_name = "RealVisXL V5.0 Lightning"
|
52 |
pipe = init_pipeline(default_model_name)
|
|
|
53 |
|
54 |
def update_pipeline(model_selection):
|
55 |
-
global pipe
|
56 |
-
if
|
|
|
57 |
pipe = init_pipeline(model_selection)
|
|
|
58 |
return pipe
|
59 |
-
|
60 |
@spaces.GPU(duration=12)
|
61 |
def fill_image(prompt, image, model_selection, paste_back):
|
62 |
-
print(f"Received image: {image}")
|
63 |
global pipe
|
64 |
update_pipeline(model_selection)
|
|
|
65 |
if image is None:
|
66 |
yield None, None
|
67 |
return
|
68 |
-
|
69 |
(
|
70 |
prompt_embeds,
|
71 |
negative_prompt_embeds,
|
@@ -78,7 +81,6 @@ def fill_image(prompt, image, model_selection, paste_back):
|
|
78 |
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
|
79 |
cnet_image = source.copy()
|
80 |
cnet_image.paste(0, (0, 0), binary_mask)
|
81 |
-
|
82 |
for image in pipe(
|
83 |
prompt_embeds=prompt_embeds,
|
84 |
negative_prompt_embeds=negative_prompt_embeds,
|
@@ -87,7 +89,6 @@ def fill_image(prompt, image, model_selection, paste_back):
|
|
87 |
image=cnet_image,
|
88 |
):
|
89 |
yield image, cnet_image
|
90 |
-
|
91 |
print(f"{model_selection=}")
|
92 |
print(f"{paste_back=}")
|
93 |
if paste_back:
|
@@ -196,9 +197,9 @@ def preview_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
196 |
return preview
|
197 |
|
198 |
@spaces.GPU(duration=12)
|
199 |
-
def inpaint(prompt, image,
|
200 |
global pipe
|
201 |
-
update_pipeline(
|
202 |
mask = Image.fromarray(image["mask"]).convert("L")
|
203 |
image = Image.fromarray(image["image"])
|
204 |
inpaint_final_prompt = f"score_9, score_8_up, score_7_up, {prompt}"
|
@@ -206,7 +207,7 @@ def inpaint(prompt, image, inpaint_model, paste_back):
|
|
206 |
if paste_back:
|
207 |
result.paste(image, (0, 0), Image.fromarray(255 - np.array(mask)))
|
208 |
return result
|
209 |
-
|
210 |
@spaces.GPU(duration=12)
|
211 |
def outpaint(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
212 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
@@ -237,7 +238,7 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
|
|
237 |
@spaces.GPU(duration=12)
|
238 |
def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
239 |
global pipe
|
240 |
-
update_pipeline(model_selection) #
|
241 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
242 |
if not can_expand(background.width, background.height, width, height, alignment):
|
243 |
alignment = "Middle"
|
@@ -348,9 +349,11 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
348 |
with gr.Column():
|
349 |
model_selection = gr.Dropdown(
|
350 |
choices=list(MODELS.keys()),
|
351 |
-
value=
|
352 |
label="Model",
|
353 |
)
|
|
|
|
|
354 |
with gr.Row():
|
355 |
run_button = gr.Button("Generate")
|
356 |
paste_back = gr.Checkbox(True, label="Paste back original")
|
@@ -362,8 +365,8 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
362 |
interactive=False,
|
363 |
label="Generated Image",
|
364 |
)
|
|
|
365 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
366 |
-
loading_message = gr.Label(label="Status", value="", visible=False) # Added loading message label
|
367 |
|
368 |
use_as_input_button.click(
|
369 |
fn=use_output_as_input, inputs=[result], outputs=[input_image]
|
@@ -477,7 +480,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
477 |
overlap_percentage = gr.Slider(
|
478 |
label="Mask overlap (%)",
|
479 |
minimum=1,
|
480 |
-
maximum=
|
481 |
value=10,
|
482 |
step=1
|
483 |
)
|
@@ -517,10 +520,12 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
517 |
interactive=False,
|
518 |
label="Generated Image",
|
519 |
)
|
520 |
-
use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
|
521 |
-
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
522 |
-
preview_image = gr.Image(label="Preview")
|
523 |
loading_message_outpaint = gr.Label(label="Status", value="", visible=False) # Added loading message label
|
|
|
|
|
|
|
|
|
|
|
524 |
|
525 |
|
526 |
target_ratio.change(
|
@@ -556,6 +561,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
556 |
inputs=[result_outpaint],
|
557 |
outputs=[input_image_outpaint]
|
558 |
)
|
|
|
559 |
runout_button.click(
|
560 |
fn=clear_result,
|
561 |
inputs=None,
|
@@ -593,6 +599,10 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
593 |
fn=clear_result,
|
594 |
inputs=None,
|
595 |
outputs=result_outpaint,
|
|
|
|
|
|
|
|
|
596 |
).then(
|
597 |
fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
|
598 |
inputs=None,
|
|
|
17 |
"Juggernaut-XL-V9-GE-RDPhoto2": "AiWise/Juggernaut-XL-V9-GE-RDPhoto2-Lightning_4S",
|
18 |
"SatPony-Lightning": "John6666/satpony-lightning-v2-sdxl"
|
19 |
}
|
20 |
+
|
21 |
def init_pipeline(model_name):
|
22 |
config_file = hf_hub_download(
|
23 |
"xinsir/controlnet-union-sdxl-1.0",
|
|
|
51 |
# Initialize with the default model
|
52 |
default_model_name = "RealVisXL V5.0 Lightning"
|
53 |
pipe = init_pipeline(default_model_name)
|
54 |
+
loaded_model_name = default_model_name # Track the loaded model
|
55 |
|
56 |
def update_pipeline(model_selection):
|
57 |
+
global pipe, loaded_model_name
|
58 |
+
if model_selection != loaded_model_name:
|
59 |
+
print(f"Loading new model: {model_selection}")
|
60 |
pipe = init_pipeline(model_selection)
|
61 |
+
loaded_model_name = model_selection
|
62 |
return pipe
|
63 |
+
|
64 |
@spaces.GPU(duration=12)
|
65 |
def fill_image(prompt, image, model_selection, paste_back):
|
|
|
66 |
global pipe
|
67 |
update_pipeline(model_selection)
|
68 |
+
print(f"Received image: {image}")
|
69 |
if image is None:
|
70 |
yield None, None
|
71 |
return
|
|
|
72 |
(
|
73 |
prompt_embeds,
|
74 |
negative_prompt_embeds,
|
|
|
81 |
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
|
82 |
cnet_image = source.copy()
|
83 |
cnet_image.paste(0, (0, 0), binary_mask)
|
|
|
84 |
for image in pipe(
|
85 |
prompt_embeds=prompt_embeds,
|
86 |
negative_prompt_embeds=negative_prompt_embeds,
|
|
|
89 |
image=cnet_image,
|
90 |
):
|
91 |
yield image, cnet_image
|
|
|
92 |
print(f"{model_selection=}")
|
93 |
print(f"{paste_back=}")
|
94 |
if paste_back:
|
|
|
197 |
return preview
|
198 |
|
199 |
@spaces.GPU(duration=12)
|
200 |
+
def inpaint(prompt, image, model_selection, paste_back):
|
201 |
global pipe
|
202 |
+
update_pipeline(model_selection)
|
203 |
mask = Image.fromarray(image["mask"]).convert("L")
|
204 |
image = Image.fromarray(image["image"])
|
205 |
inpaint_final_prompt = f"score_9, score_8_up, score_7_up, {prompt}"
|
|
|
207 |
if paste_back:
|
208 |
result.paste(image, (0, 0), Image.fromarray(255 - np.array(mask)))
|
209 |
return result
|
210 |
+
|
211 |
@spaces.GPU(duration=12)
|
212 |
def outpaint(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
213 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
|
|
238 |
@spaces.GPU(duration=12)
|
239 |
def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
240 |
global pipe
|
241 |
+
update_pipeline(model_selection) # Ensure model_selection is defined
|
242 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
243 |
if not can_expand(background.width, background.height, width, height, alignment):
|
244 |
alignment = "Middle"
|
|
|
349 |
with gr.Column():
|
350 |
model_selection = gr.Dropdown(
|
351 |
choices=list(MODELS.keys()),
|
352 |
+
value=default_model_name,
|
353 |
label="Model",
|
354 |
)
|
355 |
+
loading_message = gr.Label(label="Status", value="", visible=False) # Added loading message label
|
356 |
+
|
357 |
with gr.Row():
|
358 |
run_button = gr.Button("Generate")
|
359 |
paste_back = gr.Checkbox(True, label="Paste back original")
|
|
|
365 |
interactive=False,
|
366 |
label="Generated Image",
|
367 |
)
|
368 |
+
|
369 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
|
|
370 |
|
371 |
use_as_input_button.click(
|
372 |
fn=use_output_as_input, inputs=[result], outputs=[input_image]
|
|
|
480 |
overlap_percentage = gr.Slider(
|
481 |
label="Mask overlap (%)",
|
482 |
minimum=1,
|
483 |
+
maximum=100,
|
484 |
value=10,
|
485 |
step=1
|
486 |
)
|
|
|
520 |
interactive=False,
|
521 |
label="Generated Image",
|
522 |
)
|
|
|
|
|
|
|
523 |
loading_message_outpaint = gr.Label(label="Status", value="", visible=False) # Added loading message label
|
524 |
+
use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
|
525 |
+
with gr.Accordion():
|
526 |
+
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
527 |
+
preview_image = gr.Image(label="Preview")
|
528 |
+
|
529 |
|
530 |
|
531 |
target_ratio.change(
|
|
|
561 |
inputs=[result_outpaint],
|
562 |
outputs=[input_image_outpaint]
|
563 |
)
|
564 |
+
|
565 |
runout_button.click(
|
566 |
fn=clear_result,
|
567 |
inputs=None,
|
|
|
599 |
fn=clear_result,
|
600 |
inputs=None,
|
601 |
outputs=result_outpaint,
|
602 |
+
).then(
|
603 |
+
fn=lambda: gr.update(visible=False),
|
604 |
+
inputs=None,
|
605 |
+
outputs=use_as_input_button_outpaint,
|
606 |
).then(
|
607 |
fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
|
608 |
inputs=None,
|