File size: 1,976 Bytes
b8d9f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

from __future__ import annotations

import json
import shlex
import subprocess

import gradio as gr


def run(image_path: str, class_index: int, sigma_y: float) -> str:
    out_name = image_path.split('/')[-1].split('.')[0]
    subprocess.run(shlex.split(
        f'python main.py --config confs/inet256.yml --deg colorization --scale 1 --class {class_index} --path_y {image_path} --save_path {out_name} --sigma_y {sigma_y}'
    ),
                   cwd='DDNM/hq_demo')
    return f'DDNM/hq_demo/results/{out_name}/final/00000.png'


def create_demo():
    examples = [
        [
            'sample_images/monarch_gray.png',
            'monarch, monarch butterfly, milkweed butterfly, Danaus plexippus',
            0,
        ],
        [
            'sample_images/tiger_gray.png',
            'tiger, Panthera tigris',
            0,
        ],
    ]

    with open('imagenet_classes.json') as f:
        imagenet_class_names = json.load(f)

    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column():
                image = gr.Image(label='Input image', type='filepath')
                class_index = gr.Dropdown(label='Class name',
                                          choices=imagenet_class_names,
                                          type='index',
                                          value=950)
                sigma_y = gr.Number(label='sigma_y', value=0, precision=2)
                run_button = gr.Button('Run')
            with gr.Column():
                result = gr.Image(label='Result', type='filepath')

        gr.Examples(
            examples=examples,
            inputs=[
                image,
                class_index,
                sigma_y,
            ],
        )

        run_button.click(
            fn=run,
            inputs=[
                image,
                class_index,
                sigma_y,
            ],
            outputs=result,
        )
    return demo