File size: 3,178 Bytes
6eb1be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import torch
from diffusion import DiffusionPipeline

auth_token = os.environ.get("API_TOKEN") or True

device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = DiffusionPipeline(device)

def predict(input, diffusion_step, binoising_step, grid_size):
    for output in pipe(input, diffusion_step, binoising_step, grid_size):
        yield output[0], output[1]

def read_content(file_path: str) -> str:
    """read the content of target file
    """
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    return content


css = '''
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
#image_upload{min-height:256px}
#image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 256px}
#mask_radio .gr-form{background:transparent; border: none}
#word_mask{margin-top: .75em !important}
#word_mask textarea:disabled{opacity: 0.3}
.footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
.footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
.dark .footer {border-color: #303030}
.dark .footer>p {background: #0b0f19}
.acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
#image_upload .touch-none{display: flex}
@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}
'''

image_blocks = gr.Blocks(css=css)
with image_blocks as demo:
    gr.HTML(read_content("header.html"))
    with gr.Group():
        with gr.Box():
            with gr.Row():
                with gr.Column():
                    image = gr.Image(source='upload', elem_id="image_upload", type="pil", image_mode="L", label="Gray Image").style(height=256)

                    diffusion_step = gr.Slider(minimum=10, maximum=200, step=5, value=50, label="Diffusion Time Step")
                    binoising_step = gr.Slider(minimum=1, maximum=50, step=1, value=50, label="Bi-Noising Start Step")
                    grid_size = gr.Slider(minimum=1, maximum=16, step=1, value=2, label="Grid Size")

                    with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
                        btn = gr.Button("Colorization").style(
                            margin=False,
                            full_width=True,
                        )
                with gr.Column():
                    diffusion_result = gr.Image(elem_id="output-simg", label="Diffusion Result").style(height=256)
                    bidiffusion_result = gr.Image(elem_id="output-bimg", label="Bi-Noising Diffsuion Result").style(height=256)

                # with gr.Column():


            with gr.Row():
                gr.Examples(examples=[
                    'examples/00015.jpg',
                    'examples/00065.jpg'
                ], inputs=[image])

            btn.click(fn=predict, inputs=[image, diffusion_step, binoising_step, grid_size], outputs=[diffusion_result, bidiffusion_result])

image_blocks.queue()
image_blocks.launch(enable_queue=True, share=False, debug=False)