import os import torch import spaces import gradio as gr from diffusers import FluxFillPipeline, FluxTransformer2DModel, AutoencoderKL import random import numpy as np from huggingface_hub import hf_hub_download 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_img = imgMask["background"] mask_img = 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.") width, height = source_img.size if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator("cpu").manual_seed(seed) result = pipe( prompt=inpaint_prompt, image=source_img, seed=seed, mask_image=mask_img, width=width, height=height, num_inference_steps=num_steps, generator=generator, guidance_scale=guidance, max_sequence_length=512, ).images[0] return result with gr.Blocks(theme="ocean", title="Flux.1 dev inpaint", css=CSS) as demo: gr.HTML("