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("