import spaces import gradio as gr import torch from omegaconf import OmegaConf from PIL import Image from diffusers import StableDiffusionInpaintPipeline from model.clip_away import CLIPAway import cv2 import numpy as np import argparse # Load configuration and models config = OmegaConf.load("config/inference_config.yaml") sd_pipeline = StableDiffusionInpaintPipeline.from_pretrained( "botp/stable-diffusion-v1-5-inpainting", torch_dtype=torch.float32 ) clipaway = CLIPAway( sd_pipe=sd_pipeline, image_encoder_path=config.image_encoder_path, ip_ckpt=config.ip_adapter_ckpt_path, alpha_clip_path=config.alpha_clip_ckpt_pth, config=config, alpha_clip_id=config.alpha_clip_id, device="cpu", num_tokens=4 ) def dilate_mask(mask, kernel_size=5, iterations=5): mask = mask.convert("L").resize((512, 512), Image.NEAREST) kernel = np.ones((kernel_size, kernel_size), np.uint8) mask = cv2.dilate(np.array(mask), kernel, iterations=iterations) return Image.fromarray(mask) def remove_obj(image, seed): alpha_channel = image["layers"][0][:, :, 3] mask = np.where(alpha_channel == 0, 0, 255).astype(np.uint8) uploaded_mask = Image.fromarray(mask) background = Image.fromarray(img["background"]) mask = dilate_mask(uploaded_mask) seed = int(seed) latents = torch.randn((1, 4, 64, 64), generator=torch.Generator().manual_seed(seed)).to("cpu") final_image = clipaway.generate( prompt=[""], scale=1, seed=seed, pil_image=[background], alpha=[mask], strength=1, latents=latents )[0] return final_image # Define example data examples = [ ["gradio_examples/images/1.jpg", "gradio_examples/masks/1.png", 42], ["gradio_examples/images/2.jpg", "gradio_examples/masks/2.png", 42], ["gradio_examples/images/3.jpg", "gradio_examples/masks/3.png", 464], ] with gr.Blocks() as demo: gr.Markdown("