Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,553 Bytes
d2f27e3 00adabe 85b9ea4 51a7d9e f45ee4f 85b9ea4 efca2cc bb262d7 51a7d9e 5f81f1f 3539ef1 efca2cc 85b9ea4 36093ae 9221192 00adabe 9221192 00adabe 85b9ea4 f45ee4f 85b9ea4 f45ee4f 85b9ea4 f45ee4f 85b9ea4 f45ee4f bb262d7 d491dbe 16bf29e f45ee4f 85b9ea4 6ab5cc8 85b9ea4 6fdb3d2 85b9ea4 16bf29e 6ab5cc8 85b9ea4 6fdb3d2 85b9ea4 26c517b 37acca7 e938f7b 37acca7 206dcd3 37acca7 3b3b7a4 37acca7 92af117 37acca7 c9f5fea 37acca7 888c470 37acca7 888c470 37acca7 888c470 37acca7 888c470 37acca7 85b9ea4 26c517b 85b9ea4 1b84faf 85b9ea4 37acca7 92af117 37acca7 85b9ea4 92af117 37acca7 206dcd3 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 |
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 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 = pipe(
prompt=inpaint_prompt,
image=source_img,
mask_image=binary_mask,
width=new_width,
height=new_height,
num_inference_steps=num_steps,
generator=generator,
guidance_scale=guidance,
max_sequence_length=512,
).images[0]
return result, seed
@spaces.GPU()
def outpaintGen(
img,
outpaint_prompt: str,
overlap_top: int,
overlap_right: int,
overlap_bottom: int,
overlap_left: int,
op_guidance: float,
op_num_steps: int,
op_seed: int,
op_randomize_seed: bool
):
image = Image.open(img)
# Convert input to PIL Image if it's a numpy array
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Get original dimensions
original_width, original_height = image.size
# Calculate new dimensions
new_width = original_width + overlap_left + overlap_right
new_height = original_height + overlap_top + overlap_bottom
# Create new blank mask image (black background)
mask_image = Image.new('RGB', (new_width, new_height), color='black')
# Create white rectangle for original image area
white_area = Image.new('RGB', (original_width, original_height), color='white')
# Paste white rectangle at the appropriate position
mask_image.paste(white_area, (overlap_left, overlap_top))
# Convert to grayscale
mask_image = mask_image.convert('L')
mask_image = Image.eval(mask_image, lambda x: 255 - x)
fix_width = (new_width // 16) * 16
fix_height = (new_height // 16) * 16
# If the image size is not already divisible by 16, resize it
# if new_width != fix_width or new_height != fix_height:
# mask_image = mask_image.resize((fix_width, fix_height), Image.LANCZOS)
if op_randomize_seed:
op_seed = random.randint(0, MAX_SEED)
generator = torch.Generator("cpu").manual_seed(op_seed)
result = pipe(
prompt=outpaint_prompt,
image=image,
mask_image=mask_image,
width=fix_width,
height=fix_height,
num_inference_steps=op_num_steps,
generator=generator,
guidance_scale=op_guidance,
max_sequence_length=512,
).images[0]
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):
with gr.Row():
overlap_top = gr.Number(label="Top", value=64, precision=0)
overlap_right = gr.Number(label="Right", value=64, precision=0)
overlap_bottom = gr.Number(label="Bottom", value=64, precision=0)
overlap_left = gr.Number(label="Left", value=64, precision=0)
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_top,
overlap_right,
overlap_bottom,
overlap_left,
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) |