GSDEE / app.py
Raj Singh
Update app.py
e536f7a verified
Raw History Blame
4.3 kB
import gradio as gr
import spaces
import torch
from diffusers import AutoencoderKL, TCDScheduler
from diffusers.models.model_loading_utils import load_state_dict
from gradio_imageslider import ImageSlider
from huggingface_hub import hf_hub_download
import transformers
from controlnet_union import ControlNetModel_Union
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
# Migrate Transformers cache to prevent runtime errors
transformers.utils.move_cache()
MODELS = {
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
}
# Download and load the controlnet configuration and model
config_file = hf_hub_download(
"xinsir/controlnet-union-sdxl-1.0",
filename="config_promax.json",
)
config = ControlNetModel_Union.load_config(config_file)
controlnet_model = ControlNetModel_Union.from_config(config)
model_file = hf_hub_download(
"xinsir/controlnet-union-sdxl-1.0",
filename="diffusion_pytorch_model_promax.safetensors",
)
state_dict = load_state_dict(model_file)
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
)
model.to(device="cuda", dtype=torch.float16)
# Load the VAE model
vae = AutoencoderKL.from_pretrained(
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
).to("cuda")
# Initialize the Stable Diffusion pipeline
pipe = StableDiffusionXLFillPipeline.from_pretrained(
"SG161222/RealVisXL_V5.0_Lightning",
torch_dtype=torch.float16,
vae=vae,
controlnet=model,
variant="fp16",
).to("cuda")
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
prompt = "high quality"
(
prompt_embeds,
negative_prompt_embeds,
pooled_prompt_embeds,
negative_pooled_prompt_embeds,
) = pipe.encode_prompt(prompt, "cuda", True)
@spaces.GPU
def fill_image(image):
source = image["background"]
mask = image["layers"][0]
alpha_channel = mask.split()[3]
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
cnet_image = source.copy()
cnet_image.paste(0, (0, 0), binary_mask)
for img in pipe(
prompt_embeds=prompt_embeds,
negative_prompt_embeds=negative_prompt_embeds,
pooled_prompt_embeds=pooled_prompt_embeds,
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
image=cnet_image,
):
yield img, cnet_image
img = img.convert("RGBA")
cnet_image.paste(img, (0, 0), binary_mask)
yield source, cnet_image
def clear_result():
return gr.update(value=None)
# Updated CSS with media queries for better responsiveness
css = """
.gradio-container {
margin: 0 auto; /* Center the container */
padding: 1rem;
max-width: 100%;
}
/* Target the ImageMask component using the custom class */
.input-image {
height: auto !important;
max-height: 700px !important;
}
/* Media queries for responsive heights */
@media (min-width: 1200px) {
.input-image {
height: 600px !important; /* Increased height for larger screens */
}
}
@media (min-width: 768px) and (max-width: 1199px) {
.input-image {
height: 400px !important; /* Moderate height for tablets */
}
}
@media (max-width: 767px) {
.input-image {
height: 300px !important; /* Smaller height for mobile devices */
}
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row(equal_height=False):
with gr.Column(scale=1, min_width=300):
input_image = gr.ImageMask(
type="pil",
label="Input Image",
crop_size=(512, 512), # Reduced for better fit
canvas_size=(512, 512), # Reduced for better fit
layers=False,
sources=["upload"],
elem_classes=["input-image"], # Assign custom CSS class
)
run_button = gr.Button("Generate", variant="primary")
with gr.Column(scale=1, min_width=300):
result = ImageSlider(
interactive=False,
label="Generated Image",
)
run_button.click(
fn=clear_result,
inputs=None,
outputs=result,
).then(
fn=fill_image,
inputs=[input_image],
outputs=result,
)
demo.launch(share=False)