Delete app.py
Browse files
app.py
DELETED
@@ -1,188 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import spaces
|
3 |
-
import torch
|
4 |
-
from diffusers import AutoencoderKL, TCDScheduler
|
5 |
-
from diffusers.models.model_loading_utils import load_state_dict
|
6 |
-
from gradio_imageslider import ImageSlider
|
7 |
-
from huggingface_hub import hf_hub_download
|
8 |
-
from PIL import ImageDraw, ImageFont, Image
|
9 |
-
from controlnet_union import ControlNetModel_Union
|
10 |
-
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
11 |
-
|
12 |
-
MODELS = {
|
13 |
-
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
|
14 |
-
}
|
15 |
-
|
16 |
-
config_file = hf_hub_download(
|
17 |
-
"xinsir/controlnet-union-sdxl-1.0",
|
18 |
-
filename="config_promax.json",
|
19 |
-
)
|
20 |
-
|
21 |
-
config = ControlNetModel_Union.load_config(config_file)
|
22 |
-
controlnet_model = ControlNetModel_Union.from_config(config)
|
23 |
-
model_file = hf_hub_download(
|
24 |
-
"xinsir/controlnet-union-sdxl-1.0",
|
25 |
-
filename="diffusion_pytorch_model_promax.safetensors",
|
26 |
-
)
|
27 |
-
state_dict = load_state_dict(model_file)
|
28 |
-
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
|
29 |
-
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
|
30 |
-
)
|
31 |
-
model.to(device="cuda", dtype=torch.float16)
|
32 |
-
|
33 |
-
vae = AutoencoderKL.from_pretrained(
|
34 |
-
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
35 |
-
).to("cuda")
|
36 |
-
|
37 |
-
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
38 |
-
"SG161222/RealVisXL_V5.0_Lightning",
|
39 |
-
torch_dtype=torch.float16,
|
40 |
-
vae=vae,
|
41 |
-
controlnet=model,
|
42 |
-
variant="fp16",
|
43 |
-
).to("cuda")
|
44 |
-
|
45 |
-
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
46 |
-
|
47 |
-
|
48 |
-
def add_watermark(image, text="ProFaker", font_path="BRLNSDB.TTF", font_size=25):
|
49 |
-
# Load the Berlin Sans Demi font with the specified size
|
50 |
-
font = ImageFont.truetype(font_path, font_size)
|
51 |
-
|
52 |
-
# Position the watermark in the bottom right corner, adjusting for text size
|
53 |
-
text_bbox = font.getbbox(text)
|
54 |
-
text_width, text_height = text_bbox[2], text_bbox[3]
|
55 |
-
watermark_position = (image.width - text_width - 100, image.height - text_height - 150)
|
56 |
-
|
57 |
-
# Draw the watermark text with a translucent white color
|
58 |
-
draw = ImageDraw.Draw(image)
|
59 |
-
draw.text(watermark_position, text, font=font, fill=(255, 255, 255, 150)) # RGBA for transparency
|
60 |
-
|
61 |
-
return image
|
62 |
-
|
63 |
-
|
64 |
-
@spaces.GPU
|
65 |
-
def fill_image(prompt, image, model_selection, paste_back):
|
66 |
-
(
|
67 |
-
prompt_embeds,
|
68 |
-
negative_prompt_embeds,
|
69 |
-
pooled_prompt_embeds,
|
70 |
-
negative_pooled_prompt_embeds,
|
71 |
-
) = pipe.encode_prompt(prompt, "cuda", True)
|
72 |
-
|
73 |
-
source = image["background"]
|
74 |
-
mask = image["layers"][0]
|
75 |
-
|
76 |
-
alpha_channel = mask.split()[3]
|
77 |
-
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
|
78 |
-
cnet_image = source.copy()
|
79 |
-
cnet_image.paste(0, (0, 0), binary_mask)
|
80 |
-
|
81 |
-
for image in pipe(
|
82 |
-
prompt_embeds=prompt_embeds,
|
83 |
-
negative_prompt_embeds=negative_prompt_embeds,
|
84 |
-
pooled_prompt_embeds=pooled_prompt_embeds,
|
85 |
-
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
86 |
-
image=cnet_image,
|
87 |
-
):
|
88 |
-
yield image, cnet_image
|
89 |
-
|
90 |
-
print(f"{model_selection=}")
|
91 |
-
print(f"{paste_back=}")
|
92 |
-
|
93 |
-
if paste_back:
|
94 |
-
image = image.convert("RGBA")
|
95 |
-
cnet_image.paste(image, (0, 0), binary_mask)
|
96 |
-
else:
|
97 |
-
cnet_image = image
|
98 |
-
|
99 |
-
cnet_image = add_watermark(cnet_image)
|
100 |
-
yield source, cnet_image
|
101 |
-
|
102 |
-
|
103 |
-
def clear_result():
|
104 |
-
return gr.update(value=None)
|
105 |
-
|
106 |
-
|
107 |
-
title = """<h1 align="center">ProFaker's Editing</h1>"""
|
108 |
-
|
109 |
-
with gr.Blocks() as demo:
|
110 |
-
gr.HTML(title)
|
111 |
-
with gr.Row():
|
112 |
-
with gr.Column():
|
113 |
-
prompt = gr.Textbox(
|
114 |
-
label="Prompt",
|
115 |
-
info="Describe what to inpaint the mask with",
|
116 |
-
lines=3,
|
117 |
-
)
|
118 |
-
with gr.Column():
|
119 |
-
model_selection = gr.Dropdown(
|
120 |
-
choices=list(MODELS.keys()),
|
121 |
-
value="RealVisXL V5.0 Lightning",
|
122 |
-
label="Model",
|
123 |
-
)
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
with gr.Column():
|
127 |
-
run_button = gr.Button("Generate")
|
128 |
-
|
129 |
-
with gr.Column():
|
130 |
-
paste_back = gr.Checkbox(True, label="Paste back original")
|
131 |
-
|
132 |
-
with gr.Row():
|
133 |
-
input_image = gr.ImageMask(
|
134 |
-
type="pil", label="Input Image", crop_size=(1024, 1024), layers=False
|
135 |
-
)
|
136 |
-
|
137 |
-
result = ImageSlider(
|
138 |
-
interactive=False,
|
139 |
-
label="Generated Image",
|
140 |
-
)
|
141 |
-
|
142 |
-
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
143 |
-
|
144 |
-
def use_output_as_input(output_image):
|
145 |
-
return gr.update(value=output_image[1])
|
146 |
-
|
147 |
-
use_as_input_button.click(
|
148 |
-
fn=use_output_as_input, inputs=[result], outputs=[input_image]
|
149 |
-
)
|
150 |
-
|
151 |
-
run_button.click(
|
152 |
-
fn=clear_result,
|
153 |
-
inputs=None,
|
154 |
-
outputs=result,
|
155 |
-
).then(
|
156 |
-
fn=lambda: gr.update(visible=False),
|
157 |
-
inputs=None,
|
158 |
-
outputs=use_as_input_button,
|
159 |
-
).then(
|
160 |
-
fn=fill_image,
|
161 |
-
inputs=[prompt, input_image, model_selection, paste_back],
|
162 |
-
outputs=result,
|
163 |
-
).then(
|
164 |
-
fn=lambda: gr.update(visible=True),
|
165 |
-
inputs=None,
|
166 |
-
outputs=use_as_input_button,
|
167 |
-
)
|
168 |
-
|
169 |
-
prompt.submit(
|
170 |
-
fn=clear_result,
|
171 |
-
inputs=None,
|
172 |
-
outputs=result,
|
173 |
-
).then(
|
174 |
-
fn=lambda: gr.update(visible=False),
|
175 |
-
inputs=None,
|
176 |
-
outputs=use_as_input_button,
|
177 |
-
).then(
|
178 |
-
fn=fill_image,
|
179 |
-
inputs=[prompt, input_image, model_selection, paste_back],
|
180 |
-
outputs=result,
|
181 |
-
).then(
|
182 |
-
fn=lambda: gr.update(visible=True),
|
183 |
-
inputs=None,
|
184 |
-
outputs=use_as_input_button,
|
185 |
-
)
|
186 |
-
|
187 |
-
|
188 |
-
demo.queue(max_size=12).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|