import gradio as gr from PIL import Image from torchvision import transforms from load_model import sample import torch import random import os device = "cuda" if torch.cuda.is_available() else "cpu" device = "mps" if torch.backends.mps.is_available() else device image_size = 128 def show_example_fn(): sketch = Image.open("examples/sketch.png") scribble_folder = "./examples/scribbles/" png_files = [f for f in os.listdir(scribble_folder) if f.lower().endswith(".png")] # get random scribble random_scribble = Image.open( os.path.join(scribble_folder, random.choice(png_files)) ) return [sketch, random_scribble] transform = transforms.Compose( [ transforms.Resize((image_size, image_size)), transforms.ToTensor(), transforms.Lambda(lambda t: (t * 2) - 1), ] ) def process_images( sketch, scribbles, sampling_steps, seed_nr, upscale, progress=gr.Progress(), ): w, h = sketch.size sketch = transform(sketch.convert("RGB")) scribbles = transform(scribbles.convert("RGB")) if upscale: return transforms.Resize((h, w))( sample(sketch, scribbles, sampling_steps, seed_nr, progress) ) else: return sample(sketch, scribbles, sampling_steps, seed_nr, progress) theme = gr.themes.Monochrome() with gr.Blocks(theme=theme) as demo: with gr.Row(): gr.Markdown( "
" "Notes: Depending on where you run this demo, it might take a while to generate the output. For the HF space it may take up to 20 minutes for 100 sampling steps. We recommend lowering the sampling steps to 10 for the HF space. Model trained using this dataset." "
" ) show_example.click( show_example_fn, inputs=[], outputs=[sketch_input, scribbles_input], concurrency_limit=1, trigger_mode="once", ) generate_button.click( process_images, inputs=[ sketch_input, scribbles_input, sampling_slider, seed_slider, upscale_button, ], outputs=output, concurrency_limit=1, trigger_mode="once", ) if __name__ == "__main__": demo.launch()