import os import sys from pathlib import Path from collections import OrderedDict import spaces import gradio as gr import shutil import uuid import torch from PIL import Image demo_path = Path(__file__).resolve().parent root_path = demo_path sys.path.append(str(root_path)) from src import models from src.methods import rasg, sd, sr from src.utils import IImage, poisson_blend, image_from_url_text TMP_DIR = root_path / 'gradio_tmp' if TMP_DIR.exists(): shutil.rmtree(str(TMP_DIR)) TMP_DIR.mkdir(exist_ok=True, parents=True) os.environ['GRADIO_TEMP_DIR'] = str(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" examples_path = root_path / '__assets__/demo/examples' example_inputs = [ [f'{examples_path}/images_1024/a40.jpg', f'{examples_path}/images_2048/a40.jpg', 'medieval castle'], [f'{examples_path}/images_1024/a4.jpg', f'{examples_path}/images_2048/a4.jpg', 'parrot'], [f'{examples_path}/images_1024/a65.jpg', f'{examples_path}/images_2048/a65.jpg', 'hoodie'], [f'{examples_path}/images_1024/a54.jpg', f'{examples_path}/images_2048/a54.jpg', 'salad'], [f'{examples_path}/images_1024/a51.jpg', f'{examples_path}/images_2048/a51.jpg', 'space helmet'], [f'{examples_path}/images_1024/a46.jpg', f'{examples_path}/images_2048/a46.jpg', 'stack of books'], [f'{examples_path}/images_1024/a19.jpg', f'{examples_path}/images_2048/a19.jpg', 'antique greek vase'], [f'{examples_path}/images_1024/a2.jpg', f'{examples_path}/images_2048/a2.jpg', 'sunglasses'], ] thumbnails = [ 'https://lh3.googleusercontent.com/pw/ABLVV87bkFc_SRKrbXuk5BTp18dETNm18MLbjoJo6JvwbIkYtjZXrjU_H1dCJIP799OJjHTZmo19mYVyMCC1RLmwqzoZrgwQzfB-SCtxLa83IbXBQ23xzmKoZgsRlPztxNJD6gmXzFyatdLRzDxHIusBQLUz=w3580-h1150-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV85RWtrpTf1tMp2p3q37eg5DlFp5znifALK_JTjvxJua8UYMjytVoEy2GUW2cLXgBvQyYKg7GvrWXQ5hkdAsyih5Rf4rFnDq-JoiQYhVZHStCZLKxmeAlQna5ZwMPVTKG1TK63DH_OdK58gvSjWtF2ww=w3580-h1152-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV84dkaU6SQs9fyDjajpk1X9JkYp_zQBEnPVL67oi11_05U6-Ys5ydQpuny8GBQCMyVbFKxJ5unn9w__gmP9K0cKQ4_IVoT7Hvfmya71klDqSI7vu9Iy_5P2Il5-0giJFpumtffBA3kryn1xtJdR4vSA0=w2924-h1858-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV853ZyjvS4LvcPpVMY9BWz-232omt3-hgRiGcky_3ojE6WLKgtsrftsg1jSrUm2ccT_UOa279CulZy6fdnH_Xg1SunyRBxaRjOK0uxAkUFwb60rR1S4hI2MmhLV7KCi3tw1A-oiGi0f9JINyade-322A=w2622-h1858-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV86AJGUVGjb0i6CPg8zlJlWObNY0xdOzM1x5Bq9gKhP-ZWre5aaexRJDxQUO2gmJtRIyohD88FJDG_aVX2G5M0QOyGRWlZmx7tOVXLh-Kbesobxo9MfD-wqk9Ts9O8NUGtIwkWzo9SEs2opKdu83gB9F=w2528-h1858-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV87MplTciS7z-4i-eY3B3L0YhaK8UEQ3pTQD6W6uYVGR4hPD9u1WGEGyfg5ddqU-Bx2BrKskDhwxzF746cRhgFU5aPtbYA_-O7KfqXe9IsMxYCgUKxEHBm2ncqy64V-w-N8XOFgUMkAQqcuuNZ8Xapqp=w3580-h1186-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV877Esi6l2Kuw3akH5QBlmDAbWydZDZEEJqlZ_N-X7g33NQZU8nv_UKdAVETS7q23byTuldIAhW-q99zCycFB8Yfc-5e_WPNIM9icU0p3gd6DUVZR233ZNUtLca384MYGIhMGud9Y_Xed1I3PpiMhrpG=w2846-h1858-s-no-gm', 'https://lh3.googleusercontent.com/pw/ABLVV85hMQbSB6fCokdyut4ke7xTUqjERhuYygnj7T8IIA1k48e9GkaowDywPZzi5QJzZfj7wU3bgBHzjxop19qK1zOi5XDrjfXkn5bwj4MxicHa3TG-Rc-V-c1uyZVUyviyUlkGZ62FxuVROw2x0aGJIcr0=w3580-h1382-s-no-gm' ] 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: stack of books'], [thumbnails[6], 'Prompt: antique greek vase'], [thumbnails[7], 'Prompt: sunglasses'], ] # Load models models.pre_download_inpainting_models() inpainting_models = OrderedDict([ ("Dreamshaper Inpainting V8", 'ds8_inp'), ("Stable-Inpainting 2.0", 'sd2_inp'), ("Stable-Inpainting 1.5", 'sd15_inp') ]) sr_model = models.sd2_sr.load_model(device='cuda:0') sam_predictor = models.sam.load_model(device='cuda:0') for _name, _model_id in inpainting_models.items(): print(f'Preloading inpainting model: {_name}') models.load_inpainting_model(_model_id, device='cuda:0', cache=True) inp_model_name = list(inpainting_models.keys())[0] inp_model = models.load_inpainting_model( inpainting_models[inp_model_name], device='cuda:0', cache=True) def set_model_from_name(new_inp_model_name): global inp_model global inp_model_name if new_inp_model_name != inp_model_name: print (f"Activating Inpaintng Model: {new_inp_model_name}") inp_model = models.load_inpainting_model( inpainting_models[new_inp_model_name], device='cuda:0', cache=True) inp_model_name = new_inp_model_name def _resize_pil_max(img, max_side): if img is None: return None w, h = img.size longest = max(w, h) if longest <= max_side: return img.copy() scale = max_side / longest return img.resize( (max(1, int(round(w * scale))), max(1, int(round(h * scale)))), Image.LANCZOS, ) def _mask_from_editor(editor_value): # ImageMask (gr.ImageEditor subclass) returns {'background', 'layers', 'composite'}. # The 3.x-style binary mask is the alpha of the drawn brush layer. if not editor_value: return None bg = editor_value.get('background') layers = editor_value.get('layers') or [] if bg is None: return None if not layers: return Image.new('RGB', bg.size, (0, 0, 0)) layer = layers[0] if layer.mode != 'RGBA': return layer.convert('RGB') alpha = layer.split()[-1] return alpha.point(lambda p: 255 if p > 0 else 0).convert('RGB') def on_imagemask_upload(editor_value): bg = (editor_value or {}).get('background') if bg is None: return gr.update(), gr.update() return _resize_pil_max(bg, 2048), _resize_pil_max(bg, 1024) def save_user_session(hr_image, hr_mask, lr_results, prompt, session_id=None): if session_id == '': session_id = str(uuid.uuid4()) session_dir = TMP_DIR / session_id session_dir.mkdir(exist_ok=True, parents=True) hr_image.save(session_dir / 'hr_image.png') hr_mask.save(session_dir / 'hr_mask.png') lr_results_dir = session_dir / 'lr_results' if lr_results_dir.exists(): shutil.rmtree(lr_results_dir) lr_results_dir.mkdir(parents=True) for i, lr_result in enumerate(lr_results): lr_result.save(lr_results_dir / f'{i}.png') with open(session_dir / 'prompt.txt', 'w') as f: f.write(prompt) return session_id def recover_user_session(session_id): if session_id == '': return None, None, [], '' session_dir = TMP_DIR / session_id lr_results_dir = session_dir / 'lr_results' hr_image = Image.open(session_dir / 'hr_image.png') hr_mask = Image.open(session_dir / 'hr_mask.png') lr_result_paths = list(lr_results_dir.glob('*.png')) gallery = [] for lr_result_path in sorted(lr_result_paths): gallery.append(Image.open(lr_result_path)) with open(session_dir / 'prompt.txt', "r") as f: prompt = f.read() return hr_image, hr_mask, gallery, prompt @spaces.GPU def inpainting_run(model_name, use_rasg, use_painta, prompt, imageMask, hr_image, seed, eta, negative_prompt, positive_prompt, ddim_steps, guidance_scale=7.5, batch_size=1, session_id='' ): torch.cuda.empty_cache() set_model_from_name(model_name) method = ['default'] if use_painta: method.append('painta') if use_rasg: method.append('rasg') method = '-'.join(method) if use_rasg: inpainting_f = rasg.run else: inpainting_f = sd.run seed = int(seed) batch_size = max(1, min(int(batch_size), 4)) mask_pil = _mask_from_editor(imageMask) if mask_pil is None: return [], session_id image = IImage(hr_image).resize(512) mask = IImage(mask_pil).rgb().resize(512) method = ['default'] if use_painta: method.append('painta') method = '-'.join(method) inpainted_images = [] blended_images = [] for i in range(batch_size): seed = seed + i * 1000 inpainted_image = inpainting_f( ddim=inp_model, method=method, prompt=prompt, image=image, mask=mask, seed=seed, eta=eta, negative_prompt=negative_prompt, positive_prompt=positive_prompt, num_steps=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.pil()) session_id = save_user_session( hr_image, mask_pil, inpainted_images, prompt, session_id=session_id) return blended_images, session_id @spaces.GPU def upscale_run( ddim_steps, seed, use_sam_mask, session_id, img_index, negative_prompt='', positive_prompt='high resolution professional photo' ): hr_image, hr_mask, gallery, prompt = recover_user_session(session_id) if len(gallery) == 0: return Image.open(root_path / '__assets__/demo/sr_info.png') 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 inpainted_image = gallery[img_index if img_index >= 0 else 0] output_image = sr.run( sr_model, sam_predictor, inpainted_image, hr_image, hr_mask, prompt=f'{prompt}, {positive_prompt}', noise_level=20, blend_trick=True, blend_output=True, negative_prompt=negative_prompt, seed=seed, use_sam_mask=use_sam_mask ) return output_image with gr.Blocks(css=demo_path / 'style.css') as demo: gr.HTML( """
For faster inference without waiting in queue, you may duplicate the space and upgrade to the suggested GPU in settings.