File size: 4,455 Bytes
1fb65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from vis_common import *
import vis_utils as v_uts
import io_utils as io_uts

import os
from pickle import FALSE
import gradio as gr
from functools import partial
import json

# install gradio of 3.14
os.system("echo $BYTED_HOST_IP")

# Load the dataset change to your local path
root = "/home/mudehui/ChatEdit"
#prompt_version = "prompt_0_sd"
#prompt_version = "prompt_0_hd"
#prompt_version = "prompt_1_sd"
#prompt_version = "prompt_1_hd"
prompt_version = "prompt_0_rewrited_sd"

def load_json(file, existing_data=[]):
    if not os.path.exists(file):
        empty = {}
        return empty
    with open(file, "r") as f:
        stats = json.load(f)

    results = {name: score for name, score in stats.items() \
        if name not in existing_data}
    return results

all_items = f"{root}/full_val.jsonl"
all_samples = io_uts.load_jsonl(all_items)
all_samples = {f"{i:03}":all_samples[i] for i in range(len(all_samples))}

votes = {}
def update(name, picture_name, vote, start_idx=0, end_idx=1000):
    record_file = f"./output/{prompt_version}/{name}.json"
    v_uts.mkdir("", record_file)
    start_idx, end_idx = int(start_idx), int(end_idx)
    end_idx = min(end_idx, len(all_samples) - 1)
    items = list(all_samples.items())[start_idx:end_idx]
    label_samples = {name:prompt for name, prompt in items}

    if name == "":
        new_picture = None
        picture_name = None
        description = None
        message = "Please enter your lark username"

    elif picture_name in label_samples.keys() and vote is None:
        new_picture = None
        picture_name = None
        description = None
        message = "Please make selections! Click Next to continue..."

    else:
        # Read record
        existing_data = load_json(record_file)

        # Save record
        if (picture_name in label_samples.keys()):
            sample = label_samples[picture_name]
            sample["vote"] = vote
            existing_data[picture_name] = sample
            with open(record_file, "w") as f:
                json.dump(existing_data, f, indent=2)

        # Find Next example
        all_remaining = {}
        for i, name in enumerate(label_samples.keys()):
            if name in existing_data:
                continue
            else:
                all_remaining[name] = label_samples[name]

        if len(all_remaining) > 0:
            new_sample = list(all_remaining.items())[0]
            picture_name, data = new_sample
            description = f"input: {data['input']}<br>output: {data['output']}<br>edit: {data['edit']}"
            new_picture = f"{root}/{prompt_version}/{picture_name}.png"
            message = f"{len(all_remaining)} exmaples remaining"
        else:
            new_picture = None
            picture_name = None
            description = None
            message = "You have finished all exmaples! Thank you!"

    outputs = [new_picture, picture_name, message, description]
    print(outputs)
    return tuple(outputs)


with gr.Blocks() as demo:

    gr.Markdown("""
         - 输入用户名, 开始结束index,点击Next按钮开始, 你正在评价 {prompt}"
    """.format(prompt=prompt_version))
    with gr.Row():
        with gr.Column():
            picture_name = gr.Textbox(visible=FALSE)
            picture = gr.Image(label=f"Input Image from ")

        with gr.Column():
            name = gr.Textbox(label="User Name (enter and click Next to start)")
            start_idx = gr.Textbox(label="Start Index (max 292)", default="0")
            end_idx = gr.Textbox(label="End Index (max 292)", default="1000")
            message = gr.Markdown()
            description = gr.Markdown()
            vote = gr.Radio([
                ('1: Totally not related ', 1), 
                ('2: Not follow edit, there is some/little relation between the two images.', 2), 
                ('3: OK Pair data, not follow edit, image pair need some edit effort [flip etc.] to construct a good edit pair.', 3),
                ('4: Good pair data, can modify the instruction to form a good triplet', 4), 
                ('5: Perfectly follows the edit instruction.', 5)
            ], label="Score", min_width=400)
            greet_btn = gr.Button("Next")
    greet_btn.click(fn=update, 
                    inputs=[name,picture_name,vote, start_idx, end_idx], 
                    outputs=[picture,picture_name,message,description])

demo.queue(max_size=4)
demo.launch(share=True)