Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,306 Bytes
d2f27e3 00adabe 85b9ea4 51a7d9e f45ee4f 85b9ea4 efca2cc bb262d7 51a7d9e 5f81f1f 3539ef1 efca2cc 85b9ea4 36093ae 9221192 00adabe 9221192 00adabe bc59787 85b9ea4 f45ee4f 85b9ea4 f45ee4f 85b9ea4 f45ee4f 85b9ea4 f45ee4f bb262d7 d491dbe 16bf29e f45ee4f 85b9ea4 6ab5cc8 85b9ea4 6fdb3d2 85b9ea4 bc59787 85b9ea4 26c517b 37acca7 cd3265c 37acca7 cd3265c bc59787 37acca7 cd3265c 37acca7 cd3265c 37acca7 bc59787 37acca7 bc59787 37acca7 85b9ea4 26c517b 85b9ea4 1b84faf 85b9ea4 37acca7 92af117 37acca7 cd3265c 85b9ea4 cd3265c 37acca7 cd3265c 37acca7 92af117 51a7d9e 3b3b7a4 |
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 |
import os
import torch
import spaces
import gradio as gr
from diffusers import FluxFillPipeline
import random
import numpy as np
from huggingface_hub import hf_hub_download
from PIL import Image, ImageOps
CSS = """
h1 {
margin-top: 10px
}
"""
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
MAX_SEED = np.iinfo(np.int32).max
repo_id = "black-forest-labs/FLUX.1-Fill-dev"
if torch.cuda.is_available():
pipe = FluxFillPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16).to("cuda")
@spaces.GPU()
def gen(
prompt,
image,
mask_image,
width,
height,
num_inference_steps,
seed,
guidance_scale,
):
generator = torch.Generator("cpu").manual_seed(seed)
result = pipe(
prompt=prompt,
image=image,
mask_image=mask_image,
width=width,
height=height,
num_inference_steps=num_inference_steps,
generator=generator,
guidance_scale=guidance_scale,
max_sequence_length=512,
).images[0]
return result
def inpaintGen(
imgMask,
inpaint_prompt: str,
guidance: float,
num_steps: int,
seed: int,
randomize_seed: bool,
progress=gr.Progress(track_tqdm=True)):
source_path = imgMask["background"]
mask_path = imgMask["layers"][0]
if not source_path:
raise gr.Error("Please upload an image.")
if not mask_path:
raise gr.Error("Please draw a mask on the image.")
source_img = Image.open(source_path).convert("RGB")
mask_img = Image.open(mask_path)
alpha_channel=mask_img.split()[3]
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
width, height = source_img.size
new_width = (width // 16) * 16
new_height = (height // 16) * 16
# If the image size is not already divisible by 16, resize it
if width != new_width or height != new_height:
source_img = source_img.resize((new_width, new_height), Image.LANCZOS)
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator("cpu").manual_seed(seed)
result = gen(
inpaint_prompt,
source_img,
binary_mask,
new_width,
new_height,
num_steps,
seed,
guidance,
)
return result, seed
def add_border_and_mask(image, zoom_all=1.0, zoom_left=0, zoom_right=0, zoom_up=0, zoom_down=0, overlap=0.01):
"""Adds a black border around the image with individual side control and mask overlap"""
orig_width, orig_height = image.size
# Calculate padding for each side (in pixels)
left_pad = int(orig_width * zoom_left)
right_pad = int(orig_width * zoom_right)
top_pad = int(orig_height * zoom_up)
bottom_pad = int(orig_height * zoom_down)
# Calculate overlap in pixels
overlap_left = int(orig_width * overlap)
overlap_right = int(orig_width * overlap)
overlap_top = int(orig_height * overlap)
overlap_bottom = int(orig_height * overlap)
# If using the all-sides zoom, add it to each side
if zoom_all > 1.0:
extra_each_side = (zoom_all - 1.0) / 2
left_pad += int(orig_width * extra_each_side)
right_pad += int(orig_width * extra_each_side)
top_pad += int(orig_height * extra_each_side)
bottom_pad += int(orig_height * extra_each_side)
# Calculate new dimensions (ensure they're multiples of 32)
new_width = 32 * round((orig_width + left_pad + right_pad) / 32)
new_height = 32 * round((orig_height + top_pad + bottom_pad) / 32)
# Create new image with black border
bordered_image = Image.new("RGB", (new_width, new_height), (0, 0, 0))
# Paste original image in position
paste_x = left_pad
paste_y = top_pad
bordered_image.paste(image, (paste_x, paste_y))
# Create mask (white where the border is, black where the original image was)
mask = Image.new("L", (new_width, new_height), 255) # White background
# Paste black rectangle with overlap adjustment
mask.paste(
0,
(
paste_x + overlap_left, # Left edge moves right
paste_y + overlap_top, # Top edge moves down
paste_x + orig_width - overlap_right, # Right edge moves left
paste_y + orig_height - overlap_bottom, # Bottom edge moves up
),
)
return bordered_image, mask
def outpaintGen(
img,
outpaint_prompt: str,
overlap: float,
zoom_all: float,
zoom_left: float,
zoom_right: float,
zoom_up: float,
zoom_down: float,
guidance: float,
num_steps: int,
seed: int,
randomize_seed: bool
):
image = Image.open(img)
new_image, mask_image = add_border_and_mask(
image,
zoom_all=zoom_all,
zoom_left=zoom_left,
zoom_right=zoom_right,
zoom_up=zoom_up,
zoom_down=zoom_down,
overlap=overlap,
)
width, height = new_image.size
if randomize_seed:
seed = random.randint(0, MAX_SEED)
result = gen(
outpaint_prompt,
new_image,
mask_image,
width,
height,
num_steps,
seed,
guidance,
)
return result, seed
with gr.Blocks(theme="ocean", title="Flux.1 Fill dev", css=CSS) as demo:
gr.HTML("<h1><center>Flux.1 Fill dev</center></h1>")
gr.HTML("""
<p>
<center>
FLUX.1 Fill [dev] is a 12 billion parameter rectified flow transformer capable of filling areas in existing images based on a text description.
</center>
</p>
""")
with gr.Tab("Inpainting"):
with gr.Row():
with gr.Column():
imgMask = gr.ImageMask(type="filepath", label="Image", layers=False, height=800)
inpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="A hat...")
with gr.Row():
Inpaint_sendBtn = gr.Button(value="Submit", variant='primary')
Inpaint_clearBtn = gr.ClearButton([imgMask, inpaint_prompt], value="Clear")
image_out = gr.Image(type="pil", label="Output", height=960)
with gr.Accordion("Advanced ⚙️", open=False):
guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=50, value=30.0, step=0.1)
num_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
seed = gr.Number(label="Seed", value=42, precision=0)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
gr.on(
triggers = [
inpaint_prompt.submit,
Inpaint_sendBtn.click,
],
fn = inpaintGen,
inputs = [
imgMask,
inpaint_prompt,
guidance,
num_steps,
seed,
randomize_seed
],
outputs = [image_out, seed]
)
with gr.Tab("Outpainting"):
with gr.Row():
with gr.Column():
img = gr.Image(type="filepath", label="Image", height=800)
outpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="In city...")
with gr.Row():
outpaint_sendBtn = gr.Button(value="Submit", variant='primary')
outpaint_clearBtn = gr.ClearButton([img, outpaint_prompt], value="Clear")
image_exp = gr.Image(type="pil", label="Output", height=960)
with gr.Accordion("Advanced ⚙️", open=False):
overlap = gr.Slider(label="Overlap", minimum=0.01, maximum=0.25, value=0.01, step=0.01)
zoom_all = gr.Slider(label="Zoom Out Amount (All Sides)", minimum=1.0, maximum=3.0, value=1.0, step=0.1)
with gr.Row():
zoom_left = gr.Slider(label="Left", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
zoom_right = gr.Slider(label="Right", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
with gr.Row():
zoom_up = gr.Slider(label="Up", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
zoom_down = gr.Slider(label="Down", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
op_guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=50, value=30.0, step=0.1)
op_num_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
op_seed = gr.Number(label="Seed", value=42, precision=0)
op_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
gr.on(
triggers = [
outpaint_prompt.submit,
outpaint_sendBtn.click,
],
fn = outpaintGen,
inputs = [
img,
outpaint_prompt,
overlap,
zoom_all,
zoom_left,
zoom_right,
zoom_up,
zoom_down,
op_guidance,
op_num_steps,
op_seed,
op_randomize_seed
],
outputs = [image_exp, op_seed]
)
if __name__ == "__main__":
demo.launch(show_api=False, share=False) |