import os from collections import OrderedDict import gradio as gr import shutil import uuid import torch from pathlib import Path from lib.utils.iimage import IImage from PIL import Image from lib import models from lib.methods import rasg, sd, sr from lib.utils import poisson_blend, image_from_url_text TMP_DIR = 'gradio_tmp' if Path(TMP_DIR).exists(): shutil.rmtree(TMP_DIR) Path(TMP_DIR).mkdir(exist_ok=True, parents=True) os.environ['GRADIO_TEMP_DIR'] = TMP_DIR on_huggingspace = os.environ.get("SPACE_AUTHOR_NAME") == "PAIR" negative_prompt_str = "text, bad anatomy, bad proportions, blurry, cropped, deformed, disfigured, duplicate, error, extra limbs, gross proportions, jpeg artifacts, long neck, low quality, lowres, malformed, morbid, mutated, mutilated, out of frame, ugly, worst quality" positive_prompt_str = "Full HD, 4K, high quality, high resolution" example_inputs = [ ['assets/examples/images/a40.jpg', 'medieval castle'], ['assets/examples/images/a4.jpg', 'parrot'], ['assets/examples/images/a65.jpg', 'hoodie'], ['assets/examples/images/a54.jpg', 'salad'], ['assets/examples/images/a51.jpg', 'space helmet'], ['assets/examples/images/a46.jpg', 'stack of books'], ['assets/examples/images/a19.jpg', 'antique greek vase'], ['assets/examples/images/a2.jpg', 'sunglasses'], ] thumbnails = [ 'https://lh3.googleusercontent.com/pw/ABLVV87iCMYxrA59EAiPc3eRTA59jGF2_sJSnwBSilXwx2wtVlEhCsla2CNhCte4oHuDc4Gix4r7QAlbyRrYkHHgT8v6SOhpElvzlS-Ve91lYUEsWeiOf5yix3b47zuW8gEq_n5qBAGocyMpnc0In52mvl3CYIYcXonJx3dXmFvhhSOegluIcnq33t70mkA75YNMaFz3ovDyq9AfCIFEXGqI6cPUSy9n-fvoAUwG5VZZONdWQPpAClk0Im981hvHuLHz3viAHTbrHL4kzEJNclgXFHGUfEB-IU-cUmCVWnXPEaBFnlu_YabcgFdDAfz-bOy0W64TuUAYDzY_X8mv2fKXMoO0KOnMGZL3j0x08rK8kFzs9opczNFwmRjDQJySNVC16upa2HqYBk3S_O-uhC6DPmfm-5RBKXaNrz_MDFKc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66F4z1SdJVtMj3OHsYdu4K3zdVKynrutGbrtuCaz5eVIgAkO0CTJ5O_Wkch3XmvQNlW6xf_cuU7kSPpPHDf1BdRvBPzpKNGb7d4wf2kEwUTRGLhQaj6qh-jImuQ3XeA2cfEapc9Hp93lr=w3580-h1382-s-no-gm?authuser=2' ] example_previews = [ [thumbnails[0], 'Prompt: medieval castle'], [thumbnails[1], 'Prompt: parrot'], [thumbnails[2], 'Prompt: hoodie'], [thumbnails[3], 'Prompt: salad'], [thumbnails[4], 'Prompt: space helmet'], [thumbnails[5], 'Prompt: laptop'], [thumbnails[6], 'Prompt: antique greek vase'], [thumbnails[7], 'Prompt: sunglasses'], ] # Load models inpainting_models = OrderedDict([ ("Dreamshaper Inpainting V8", models.ds_inp.load_model()), ("Stable-Inpainting 2.0", models.sd2_inp.load_model()), ("Stable-Inpainting 1.5", models.sd15_inp.load_model()) ]) sr_model = models.sd2_sr.load_model(device='cuda:1') sam_predictor = models.sam.load_model(device='cuda:0') inp_model = inpainting_models[list(inpainting_models.keys())[0]] def set_model_from_name(inp_model_name): global inp_model print (f"Activating Inpaintng Model: {inp_model_name}") inp_model = inpainting_models[inp_model_name] def rasg_run(use_painta, prompt, input, seed, eta, negative_prompt, positive_prompt, ddim_steps, guidance_scale=7.5, batch_size=4): torch.cuda.empty_cache() seed = int(seed) batch_size = max(1, min(int(batch_size), 4)) image = IImage(input['image']).resize(512) mask = IImage(input['mask']).rgb().resize(512) method = ['rasg'] if use_painta: method.append('painta') inpainted_images = [] blended_images = [] for i in range(batch_size): inpainted_image = rasg.run( ddim = inp_model, method = '-'.join(method), prompt = prompt, image = image.padx(64), mask = mask.alpha().padx(64), seed = seed+i*1000, eta = eta, prefix = '{}', negative_prompt = negative_prompt, positive_prompt = f', {positive_prompt}', dt = 1000 // ddim_steps, guidance_scale = guidance_scale ).crop(image.size) blended_image = poisson_blend(orig_img = image.data[0], fake_img = inpainted_image.data[0], mask = mask.data[0], dilation = 12) blended_images.append(blended_image) inpainted_images.append(inpainted_image.numpy()[0]) return blended_images, inpainted_images def sd_run(use_painta, prompt, input, seed, eta, negative_prompt, positive_prompt, ddim_steps, guidance_scale=7.5, batch_size=4): torch.cuda.empty_cache() seed = int(seed) batch_size = max(1, min(int(batch_size), 4)) image = IImage(input['image']).resize(512) mask = IImage(input['mask']).rgb().resize(512) method = ['default'] if use_painta: method.append('painta') inpainted_images = [] blended_images = [] for i in range(batch_size): inpainted_image = sd.run( ddim = inp_model, method = '-'.join(method), prompt = prompt, image = image.padx(64), mask = mask.alpha().padx(64), seed = seed+i*1000, eta = eta, prefix = '{}', negative_prompt = negative_prompt, positive_prompt = f', {positive_prompt}', dt = 1000 // ddim_steps, guidance_scale = guidance_scale ).crop(image.size) blended_image = poisson_blend(orig_img = image.data[0], fake_img = inpainted_image.data[0], mask = mask.data[0], dilation = 12) blended_images.append(blended_image) inpainted_images.append(inpainted_image.numpy()[0]) return blended_images, inpainted_images def upscale_run( prompt, input, ddim_steps, seed, use_sam_mask, gallery, img_index, negative_prompt='', positive_prompt=', high resolution professional photo'): torch.cuda.empty_cache() seed = int(seed) img_index = int(img_index) img_index = 0 if img_index < 0 else img_index img_index = len(gallery) - 1 if img_index >= len(gallery) else img_index img_info = gallery[img_index if img_index >= 0 else 0] inpainted_image = image_from_url_text(img_info) lr_image = IImage(inpainted_image) hr_image = IImage(input['image']).resize(2048) hr_mask = IImage(input['mask']).resize(2048) output_image = sr.run(sr_model, sam_predictor, lr_image, hr_image, hr_mask, prompt=prompt + positive_prompt, noise_level=0, blend_trick=True, blend_output=True, negative_prompt=negative_prompt, seed=seed, use_sam_mask=use_sam_mask) return output_image.numpy()[0], output_image.numpy()[0] def switch_run(use_rasg, model_name, *args): set_model_from_name(model_name) if use_rasg: return rasg_run(*args) return sd_run(*args) with gr.Blocks(css='style.css') as demo: gr.HTML( """

🧑‍🎨 HD-Painter Demo

Hayk Manukyan1*, Andranik Sargsyan1*, Barsegh Atanyan1, Zhangyang Wang1,2, Shant Navasardyan1 and Humphrey Shi1,3

1Picsart AI Resarch (PAIR), 2UT Austin, 3Georgia Tech

[arXiv] [GitHub]

HD-Painter enables prompt-faithfull and high resolution (up to 2k) image inpainting upon any diffusion-based image inpainting method.

""") if on_huggingspace: gr.HTML("""

For faster inference without waiting in queue, you may duplicate the space and upgrade to the suggested GPU in settings.
Duplicate Space

""") with open('script.js', 'r') as f: js_str = f.read() demo.load(_js=js_str) with gr.Row(): with gr.Column(): model_picker = gr.Dropdown( list(inpainting_models.keys()), value=list(inpainting_models.keys())[0], label = "Please select a model!", ) with gr.Column(): use_painta = gr.Checkbox(value = True, label = "Use PAIntA") use_rasg = gr.Checkbox(value = True, label = "Use RASG") prompt = gr.Textbox(label = "Inpainting Prompt") with gr.Row(): with gr.Column(): input = gr.ImageMask(label = "Input Image", brush_color='#ff0000', elem_id="inputmask", type="pil") with gr.Row(): inpaint_btn = gr.Button("Inpaint", scale = 0) with gr.Accordion('Advanced options', open=False): guidance_scale = gr.Slider(minimum = 0, maximum = 30, value = 7.5, label = "Guidance Scale") eta = gr.Slider(minimum = 0, maximum = 1, value = 0.1, label = "eta") ddim_steps = gr.Slider(minimum = 10, maximum = 100, value = 50, step = 1, label = 'Number of diffusion steps') with gr.Row(): seed = gr.Number(value = 49123, label = "Seed") batch_size = gr.Number(value = 1, label = "Batch size", minimum=1, maximum=4) negative_prompt = gr.Textbox(value=negative_prompt_str, label = "Negative prompt", lines=3) positive_prompt = gr.Textbox(value=positive_prompt_str, label = "Positive prompt", lines=1) with gr.Column(): with gr.Row(): output_gallery = gr.Gallery( [], columns = 4, preview = True, allow_preview = True, object_fit='scale-down', elem_id='outputgallery' ) with gr.Row(): upscale_btn = gr.Button("Send to Inpainting-Specialized Super-Resolution (x4)", scale = 1) with gr.Row(): use_sam_mask = gr.Checkbox(value = False, label = "Use SAM mask for background preservation (for SR only, experimental feature)") with gr.Row(): hires_image = gr.Image(label = "Hi-res Image") label = gr.Markdown("## High-Resolution Generation Samples (2048px large side)") with gr.Column(): example_container = gr.Gallery( example_previews, columns = 4, preview = True, allow_preview = True, object_fit='scale-down' ) gr.Examples( [ example_inputs[i] + [[example_previews[i]]] for i in range(len(example_previews)) ], [input, prompt, example_container] ) mock_output_gallery = gr.Gallery([], columns = 4, visible=False) mock_hires = gr.Image(label = "__MHRO__", visible = False) html_info = gr.HTML(elem_id=f'html_info', elem_classes="infotext") inpaint_btn.click( fn=switch_run, inputs=[ use_rasg, model_picker, use_painta, prompt, input, seed, eta, negative_prompt, positive_prompt, ddim_steps, guidance_scale, batch_size ], outputs=[output_gallery, mock_output_gallery], api_name="inpaint" ) upscale_btn.click( fn=upscale_run, inputs=[ prompt, input, ddim_steps, seed, use_sam_mask, mock_output_gallery, html_info ], outputs=[hires_image, mock_hires], api_name="upscale", _js="function(a, b, c, d, e, f, g){ return [a, b, c, d, e, f, selected_gallery_index()] }", ) demo.queue(max_size=20) demo.launch(share=True, allowed_paths=[TMP_DIR])