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