File size: 5,310 Bytes
c5574cd
 
 
 
 
 
 
 
 
 
 
 
3cf9b05
eeef8ff
0eba6ac
 
3cf9b05
95e8941
c5574cd
 
3cf9b05
95e8941
c5574cd
 
 
 
 
 
 
 
 
 
 
 
b000a3c
 
 
 
0566513
b000a3c
 
 
 
0566513
b000a3c
 
c5574cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6ff29a
c5574cd
c6ff29a
0eba6ac
 
 
 
 
c5574cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3519392
c5574cd
 
 
 
 
 
 
 
 
 
 
 
b000a3c
 
c5574cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b000a3c
c5574cd
 
 
 
 
 
 
b000a3c
c5574cd
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
147
148
149
150
151
import gradio as gr
import random


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, 100000000)
    return seed


examples = [
    [
        "condition/example/t2i/landscape.jpg",
        "Landscape photos with snow on the mountains in the distance and clear reflections in the lake near by",
    ],
    [
        "condition/example/t2i/girl.jpg",
        "A girl with blue hair",
    ],
    [
        "condition/example/t2i/eye.jpg",
        "A vivid drawing of an eye with a few pencils nearby",
    ],
]


def create_demo(process):
    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column():
                image = gr.Image()
                prompt = gr.Textbox(label="Prompt")
                run_button = gr.Button("Run")
                with gr.Accordion("Advanced options", open=False):
                    preprocessor_name = gr.Radio(
                        label="Preprocessor",
                        choices=[
                            "Hed",
                            "Canny",
                            "Lineart",
                            "No preprocess",
                        ],
                        type="value",
                        value="Hed",
                        info='Edge type.',
                    )
                    canny_low_threshold = gr.Slider(
                        label="Canny low threshold",
                        minimum=0,
                        maximum=255,
                        value=100,
                        step=50)
                    canny_high_threshold = gr.Slider(
                        label="Canny high threshold",
                        minimum=0,
                        maximum=255,
                        value=200,
                        step=50)
                    cfg_scale = gr.Slider(label="Guidance scale",
                                          minimum=0.1,
                                          maximum=30.0,
                                          value=4,
                                          step=0.1)
                    control_strength = gr.Slider(minimum=0., maximum=1.0, step=0.1, value=0.6, label="control_strength")
                    # relolution = gr.Slider(label="(H, W)",
                    #                        minimum=384,
                    #                        maximum=768,
                    #                        value=512,
                    #                        step=16)
                    top_k = gr.Slider(minimum=1,
                                      maximum=16384,
                                      step=1,
                                      value=2000,
                                      label='Top-K')
                    top_p = gr.Slider(minimum=0.,
                                      maximum=1.0,
                                      step=0.1,
                                      value=1.0,
                                      label="Top-P")
                    temperature = gr.Slider(minimum=0.,
                                            maximum=1.0,
                                            step=0.1,
                                            value=1.0,
                                            label='Temperature')
                    seed = gr.Slider(label="Seed",
                                     minimum=0,
                                     maximum=100000000,
                                     step=1,
                                     value=0)
                    randomize_seed = gr.Checkbox(label="Randomize seed",
                                                 value=True)
            with gr.Column():
                result = gr.Gallery(label="Output",
                                    show_label=False,
                                    height='800px',
                                    columns=2,
                                    object_fit="scale-down")
        gr.Examples(
            examples=examples,
            inputs=[
                image,
                prompt,
                # relolution,
            ]
        )
        inputs = [
            image,
            prompt,
            cfg_scale,
            temperature,
            top_k,
            top_p,
            seed,
            canny_low_threshold,
            canny_high_threshold,
            control_strength,
            preprocessor_name,
        ]
        # prompt.submit(
        #     fn=randomize_seed_fn,
        #     inputs=[seed, randomize_seed],
        #     outputs=seed,
        #     queue=False,
        #     api_name=False,
        # ).then(
        #     fn=process,
        #     inputs=inputs,
        #     outputs=result,
        #     api_name=False,
        # )
        run_button.click(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=process,
            inputs=inputs,
            outputs=result,
            api_name="edge",
        )
    return demo


if __name__ == "__main__":
    from model import Model
    model = Model()
    demo = create_demo(model.process_edge)
    demo.queue().launch(share=False, server_name="0.0.0.0")