Upload 4 files
Browse files- app.py +93 -0
- background.jpg +0 -0
- jisoo.jpg +0 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import (
|
| 5 |
+
AutoencoderKL,
|
| 6 |
+
EulerAncestralDiscreteScheduler,
|
| 7 |
+
)
|
| 8 |
+
from diffusers.utils import load_image
|
| 9 |
+
from replace_bg.model.pipeline_controlnet_sd_xl import StableDiffusionXLControlNetPipeline
|
| 10 |
+
from replace_bg.model.controlnet import ControlNetModel
|
| 11 |
+
from replace_bg.utilities import resize_image, remove_bg_from_image, paste_fg_over_image, get_control_image_tensor
|
| 12 |
+
|
| 13 |
+
controlnet = ControlNetModel.from_pretrained("briaai/BRIA-2.3-ControlNet-BG-Gen", torch_dtype=torch.float16)
|
| 14 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 15 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained("briaai/BRIA-2.3", controlnet=controlnet, torch_dtype=torch.float16, vae=vae).to('cuda:0')
|
| 16 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler(
|
| 17 |
+
beta_start=0.00085,
|
| 18 |
+
beta_end=0.012,
|
| 19 |
+
beta_schedule="scaled_linear",
|
| 20 |
+
num_train_timesteps=1000,
|
| 21 |
+
steps_offset=1
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@spaces.GPU
|
| 26 |
+
def generate_(prompt, negative_prompt, control_tensor, num_steps, controlnet_conditioning_scale, seed):
|
| 27 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 28 |
+
gen_img = pipe(
|
| 29 |
+
negative_prompt=negative_prompt,
|
| 30 |
+
prompt=prompt,
|
| 31 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
| 32 |
+
num_inference_steps=num_steps,
|
| 33 |
+
image = control_tensor,
|
| 34 |
+
generator=generator
|
| 35 |
+
).images[0]
|
| 36 |
+
|
| 37 |
+
return gen_img
|
| 38 |
+
|
| 39 |
+
@spaces.GPU
|
| 40 |
+
def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
| 41 |
+
|
| 42 |
+
image = resize_image(input_image)
|
| 43 |
+
mask = remove_bg_from_image(image)
|
| 44 |
+
control_tensor = get_control_image_tensor(pipe.vae, image, mask)
|
| 45 |
+
|
| 46 |
+
gen_image = generate_(prompt, negative_prompt, control_tensor, num_steps, controlnet_conditioning_scale, seed)
|
| 47 |
+
result_image = paste_fg_over_image(gen_image, image, mask)
|
| 48 |
+
|
| 49 |
+
return result_image
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
block = gr.Blocks().queue()
|
| 54 |
+
|
| 55 |
+
with block:
|
| 56 |
+
gr.Markdown("## HBS_V2")
|
| 57 |
+
gr.HTML('''
|
| 58 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 59 |
+
Human Body Segmentation model v2 developed by <a href='https://github.com/WildanJR09' target='_blank'><b>WildanJR</b></a>, Designed to effectively separate foreground from background in a range of categories and image types. And then generate image background from user input.<br>
|
| 60 |
+
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.
|
| 61 |
+
</p>
|
| 62 |
+
''')
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column():
|
| 65 |
+
input_image = gr.Image(sources='upload', type="pil", label="Upload", elem_id="image_upload", height=600)
|
| 66 |
+
prompt = gr.Textbox(label="Prompt for Background")
|
| 67 |
+
run_button = gr.Button(value="Generate Background")
|
| 68 |
+
|
| 69 |
+
with gr.Column():
|
| 70 |
+
result_gallery = gr.Image(label='Output', type="pil", show_label=True, elem_id="output-img")
|
| 71 |
+
|
| 72 |
+
# Define default values for hidden parameters
|
| 73 |
+
negative_prompt = "Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"
|
| 74 |
+
num_steps = 30
|
| 75 |
+
controlnet_conditioning_scale = 1.0
|
| 76 |
+
seed = 309005275 # None for random seed, or specify a fixed integer
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height=600)
|
| 80 |
+
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
| 81 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
| 82 |
+
|
| 83 |
+
gr.Examples(
|
| 84 |
+
examples=[
|
| 85 |
+
["./jisoo.png"],
|
| 86 |
+
],
|
| 87 |
+
fn=process,
|
| 88 |
+
inputs=[input_image],
|
| 89 |
+
cache_examples=False,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
block.launch(debug = True)
|
background.jpg
ADDED
|
jisoo.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
| 5 |
+
typing
|
| 6 |
+
scikit-image
|
| 7 |
+
Diffusers==0.26.2
|
| 8 |
+
transformers>=4.39.1
|
| 9 |
+
accelerate
|