HIST/ControlNet
0
1# This file is adapted from https://github.com/lllyasviel/ControlNet/blob/f4748e3630d8141d7765e2bd9b1e348f47847707/gradio_scribble2image_interactive.py2# The original license file is LICENSE.ControlNet this repo.3import gradio as gr4import numpy as np5 6 7def create_canvas(w, h):8 return np.zeros(shape=(h, w, 3), dtype=np.uint8) + 2559 10 11def create_demo(process):12 with gr.Blocks() as demo:13 with gr.Row():14 gr.Markdown(15 '## Control Stable Diffusion with Interactive Scribbles')16 with gr.Row():17 with gr.Column():18 canvas_width = gr.Slider(label='Canvas Width',19 minimum=256,20 maximum=1024,21 value=512,22 step=1)23 canvas_height = gr.Slider(label='Canvas Height',24 minimum=256,25 maximum=1024,26 value=512,27 step=1)28 create_button = gr.Button(label='Start',29 value='Open drawing canvas!')30 input_image = gr.Image(source='upload',31 type='numpy',32 tool='sketch')33 gr.Markdown(34 value=35 'Do not forget to change your brush width to make it thinner. (Gradio do not allow developers to set brush width so you need to do it manually.) '36 'Just click on the small pencil icon in the upper right corner of the above block.'37 )38 create_button.click(fn=create_canvas,39 inputs=[canvas_width, canvas_height],40 outputs=[input_image])41 prompt = gr.Textbox(label='Prompt')42 run_button = gr.Button(label='Run')43 with gr.Accordion('Advanced options', open=False):44 num_samples = gr.Slider(label='Images',45 minimum=1,46 maximum=12,47 value=1,48 step=1)49 image_resolution = gr.Slider(label='Image Resolution',50 minimum=256,51 maximum=768,52 value=512,53 step=256)54 ddim_steps = gr.Slider(label='Steps',55 minimum=1,56 maximum=100,57 value=20,58 step=1)59 scale = gr.Slider(label='Guidance Scale',60 minimum=0.1,61 maximum=30.0,62 value=9.0,63 step=0.1)64 seed = gr.Slider(label='Seed',65 minimum=-1,66 maximum=2147483647,67 step=1,68 randomize=True)69 eta = gr.Number(label='eta (DDIM)', value=0.0)70 a_prompt = gr.Textbox(71 label='Added Prompt',72 value='best quality, extremely detailed')73 n_prompt = gr.Textbox(74 label='Negative Prompt',75 value=76 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'77 )78 with gr.Column():79 result_gallery = gr.Gallery(label='Output',80 show_label=False,81 elem_id='gallery').style(82 grid=2, height='auto')83 ips = [84 input_image, prompt, a_prompt, n_prompt, num_samples,85 image_resolution, ddim_steps, scale, seed, eta86 ]87 run_button.click(fn=process, inputs=ips, outputs=[result_gallery])88 return demo89 