import spaces import gradio as gr import torch from diffusers import ( AutoencoderKL, EulerAncestralDiscreteScheduler, ) from diffusers.utils import load_image from replace_bg.model.pipeline_controlnet_sd_xl import StableDiffusionXLControlNetPipeline from replace_bg.model.controlnet import ControlNetModel from replace_bg.utilities import resize_image, remove_bg_from_image, paste_fg_over_image, get_control_image_tensor controlnet = ControlNetModel.from_pretrained("briaai/BRIA-2.3-ControlNet-BG-Gen", torch_dtype=torch.float16) vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) pipe = StableDiffusionXLControlNetPipeline.from_pretrained("briaai/BRIA-2.3", controlnet=controlnet, torch_dtype=torch.float16, vae=vae).to('cuda:0') pipe.load_lora_weights(".", weight_name="77d3c43e-96be-4ecf-b102-4acf0d1abe09_4092_678_webui.safetensors") pipe.scheduler = EulerAncestralDiscreteScheduler( beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000, steps_offset=1 ) @spaces.GPU def generate_(prompt, negative_prompt, control_tensor, num_steps, controlnet_conditioning_scale, seed): generator = torch.Generator("cuda").manual_seed(seed) gen_img = pipe( negative_prompt=negative_prompt, prompt=prompt, controlnet_conditioning_scale=float(controlnet_conditioning_scale), num_inference_steps=num_steps, image = control_tensor, cross_attention_kwargs={"scale": 0.9}, generator=generator ).images[0] return gen_img @spaces.GPU def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed): image = resize_image(input_image) mask = remove_bg_from_image(image) control_tensor = get_control_image_tensor(pipe.vae, image, mask) gen_image = generate_(prompt, negative_prompt, control_tensor, num_steps, controlnet_conditioning_scale, seed) result_image = paste_fg_over_image(gen_image, image, mask) return result_image block = gr.Blocks().queue() with block: gr.Markdown("## HBS_V2") gr.HTML('''
Human Body Segmentation model v2 developed by WildanJR, Designed to effectively separate foreground from background in a range of categories and image types. And then generate image background from user input.
This model has been trained on a carefully selected dataset, which includes: general stock images, e-commerce, gaming, and advertising content, making it suitable for commercial use cases powering enterprise content creation at scale. The accuracy, efficiency, and versatility currently rival leading source-available models. It is ideal where content safety, legally licensed datasets, and bias mitigation are paramount. For test upload your image and type query then wait.