File size: 4,949 Bytes
5416a13
 
 
 
9f4b472
9ba02a8
5416a13
9f4b472
5416a13
 
9ac3ff2
 
 
 
5416a13
 
 
 
9f4b472
5416a13
9f4b472
5416a13
9ac3ff2
 
 
 
 
 
 
98a92b9
9ac3ff2
5d39493
9ba02a8
 
5d39493
9f4b472
 
 
 
 
 
5416a13
741ba88
5416a13
 
5d39493
 
5416a13
 
 
5d39493
5416a13
741ba88
5416a13
9ba02a8
 
 
741ba88
5416a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d39493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f4b472
5d39493
 
 
 
9f4b472
5d39493
 
 
9f4b472
5d39493
5416a13
 
 
 
 
b1b1939
5416a13
 
 
 
 
 
5d39493
5416a13
 
 
 
 
1e3e225
5416a13
9ac3ff2
5b7da8b
98a92b9
 
5416a13
 
 
 
9ac3ff2
 
 
5416a13
2f1dd2d
 
 
 
 
 
5416a13
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from PIL import Image
import gradio as gr
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
import torch
torch.backends.cuda.matmul.allow_tf32 = True
import gc

controlnet = ControlNetModel.from_pretrained("ioclab/control_v1p_sd15_brightness", torch_dtype=torch.float16, use_safetensors=True)

pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    torch_dtype=torch.float16,
    safety_checker=None,
)

pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

pipe.enable_xformers_memory_efficient_attention()
pipe.enable_model_cpu_offload()
pipe.enable_attention_slicing()

def infer(
        prompt,
        negative_prompt,
        conditioning_image,
        num_inference_steps=30,
        size=768,
        guidance_scale=7.0,
        seed=1234,
):

    conditioning_image_raw = Image.fromarray(conditioning_image)
    conditioning_image = conditioning_image_raw.convert('L')

    g_cpu = torch.Generator()

    if seed == -1:
        generator = g_cpu.manual_seed(g_cpu.seed())
    else:
        generator = g_cpu.manual_seed(seed)

    output_image = pipe(
        prompt,
        conditioning_image,
        height=size,
        width=size,
        num_inference_steps=num_inference_steps,
        generator=generator,
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale,
        controlnet_conditioning_scale=1.0,
    ).images[0]

    del conditioning_image, conditioning_image_raw
    gc.collect()

    return output_image

with gr.Blocks() as demo:
    gr.Markdown(
        """
    # ControlNet on Brightness

    This is a demo on ControlNet based on brightness.
    """)

    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(
                label="Prompt",
            )
            negative_prompt = gr.Textbox(
                label="Negative Prompt",
            )
            conditioning_image = gr.Image(
                label="Conditioning Image",
            )
            with gr.Accordion('Advanced options', open=False):
                with gr.Row():
                    num_inference_steps = gr.Slider(
                        10, 40, 20,
                        step=1,
                        label="Steps",
                    )
                    size = gr.Slider(
                        256, 768, 512,
                        step=128,
                        label="Size",
                    )
                with gr.Row():
                    guidance_scale = gr.Slider(
                        label='Guidance Scale',
                        minimum=0.1,
                        maximum=30.0,
                        value=7.0,
                        step=0.1
                    )
                    seed = gr.Slider(
                        label='Seed',
                        value=-1,
                        minimum=-1,
                        maximum=2147483647,
                        step=1,
                        # randomize=True
                    )
            submit_btn = gr.Button(
                value="Submit",
                variant="primary"
            )
        with gr.Column(min_width=300):
            output = gr.Image(
                label="Result",
            )

    submit_btn.click(
        fn=infer,
        inputs=[
            prompt, negative_prompt, conditioning_image, num_inference_steps, size, guidance_scale, seed
        ],
        outputs=output
    )
    gr.Examples(
        examples=[
            ["a village in the mountains", "monochrome", "./conditioning_images/conditioning_image_1.jpg"],
            ["three people walking in an alleyway with hats and pants", "monochrome", "./conditioning_images/conditioning_image_2.jpg"],
            ["an anime character, natural skin", "monochrome, blue skin, grayscale", "./conditioning_images/conditioning_image_3.jpg"],
            ["a man in a black suit", "monochrome", "./conditioning_images/conditioning_image_4.jpg"],
            ["the forbidden city in beijing at sunset with a reflection in the water", "monochrome", "./conditioning_images/conditioning_image_5.jpg"],
            ["a man in a white shirt holding his hand out in front of", "monochrome", "./conditioning_images/conditioning_image_6.jpg"],
        ],
        inputs=[
            prompt, negative_prompt, conditioning_image
        ],
        outputs=output,
        fn=infer,
        cache_examples=True,
    )
    gr.Markdown(
        """
    * [Dataset](https://huggingface.co/datasets/ioclab/grayscale_image_aesthetic_3M)
    * [Diffusers model](https://huggingface.co/ioclab/control_v1p_sd15_brightness), [Web UI model](https://huggingface.co/ioclab/ioc-controlnet)
    * [Training Report](https://api.wandb.ai/links/ciaochaos/oot5cui2), [Doc(Chinese)](https://aigc.ioclab.com/sd-showcase/brightness-controlnet.html)
    """)

demo.launch()