File size: 11,887 Bytes
f4d9912
 
 
 
 
 
cf46cef
f4d9912
 
 
cf46cef
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a29667
25cb6f6
f4d9912
 
 
 
cf46cef
 
f4d9912
 
 
cf46cef
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf46cef
 
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613e563
 
 
 
 
 
f4d9912
 
 
613e563
cf46cef
613e563
cf46cef
 
3a29667
cf46cef
 
 
 
 
5ca2cbd
 
 
 
 
 
 
bd474be
b554170
 
5ca2cbd
cf46cef
3a29667
 
 
 
cf46cef
 
 
 
 
 
3a29667
cf46cef
 
 
3a29667
f4d9912
cf46cef
f4d9912
cf46cef
f4d9912
cf46cef
 
 
f4d9912
 
 
 
 
3a29667
e8aefb6
bd474be
e8aefb6
3a29667
aec8a3d
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
e8aefb6
25cb6f6
e8aefb6
f4d9912
 
3a29667
bd474be
e2096ee
25cb6f6
f4d9912
674943e
f4d9912
 
 
 
 
 
 
bd474be
 
e73a532
25cb6f6
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
cf46cef
f4d9912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613e563
cf46cef
 
 
f4d9912
 
 
 
cf46cef
 
 
 
 
 
 
 
613e563
 
f4d9912
 
3a29667
 
f4d9912
 
 
3a29667
613e563
3a29667
613e563
f4d9912
 
3a29667
f4d9912
3a29667
 
f4d9912
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import gradio as gr
import yaml
import random
import os
import json
import time
import numpy as np
from pathlib import Path
from huggingface_hub import CommitScheduler, HfApi

from src.utils import load_words, load_example_images, load_csv_concepts, generate_random_ids
from src.style import css
from src.user import UserID

from datetime import datetime
from pathlib import Path
from uuid import uuid4
import json
from huggingface_hub import CommitScheduler

def main():
    config = yaml.safe_load(open("config/config.yaml"))
    class_names = config['dataset'][config['dataset']['name']]['class_names']
    data_dir = os.path.join(config['dataset']['path'], config['dataset']['name'])

    with gr.Blocks(theme=gr.themes.Glass(), css=css) as demo:
        # Main App Components
        title = gr.Markdown("# Saliency evaluation - experiment 2")
        user_state = gr.State(0)
        answers = gr.State([])
        random_answer_order = gr.State({})
        start_time = gr.State(time.time())

        target_img_label = gr.Markdown(f"Target class: **{class_names[user_state.value]}**")
        question = gr.Markdown()

        concepts = load_csv_concepts(data_dir)

        concept_checkboxes = gr.CheckboxGroup(
            ['c1, c2, c3', 'c4, c5, c6', 'c7, c8, c9'],
            label=f"Choose the concept set that better describes the target class",
            visible=False
        )
        
        gr.Markdown("### Image examples of the same class")
        with gr.Row():
            count = user_state if isinstance(user_state, int) else user_state.value
            images = load_example_images(count, data_dir)
            img1 = gr.Image(images[0])
            img2 = gr.Image(images[1])
            img3 = gr.Image(images[2])
            img4 = gr.Image(images[3])
            img5 = gr.Image(images[4])
            img6 = gr.Image(images[5])
            img7 = gr.Image(images[6])
            img8 = gr.Image(images[7])
            img9 = gr.Image(images[8])
            img10 = gr.Image(images[9])
            img11 = gr.Image(images[10])
            img12 = gr.Image(images[11])
            img13 = gr.Image(images[12])
            img14 = gr.Image(images[13])
            img15 = gr.Image(images[14])
            img16 = gr.Image(images[15])
            
        continue_button = gr.Button("Continue")
        submit_button = gr.Button("Submit", visible=False)
        finish_button = gr.Button("Finish", visible=False)
        
        def update_label(concept_checkboxes, user_state):

            count = user_state if isinstance(user_state, int) else user_state.value
            if count < config['dataset'][config['dataset']['name']]['n_classes']:

                # image examples
                images = load_example_images(count, data_dir)
                img1 = gr.Image(images[0])
                img2 = gr.Image(images[1])
                img3 = gr.Image(images[2])
                img4 = gr.Image(images[3])
                img5 = gr.Image(images[4])
                img6 = gr.Image(images[5])
                img7 = gr.Image(images[6])
                img8 = gr.Image(images[7])
                img9 = gr.Image(images[8])
                img10 = gr.Image(images[9])
                img11 = gr.Image(images[10])
                img12 = gr.Image(images[11])
                img13 = gr.Image(images[12])
                img14 = gr.Image(images[13])
                img15 = gr.Image(images[14])
                img16 = gr.Image(images[15])
                return img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16
            else:
                return  img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16

        def update_state(state):
            count = state if isinstance(state, int) else state.value
            return gr.State(count + 1)

        def update_img_label(state):
            count = state if isinstance(state, int) else state.value
            return f"### Target class: {class_names[count]}"

        def update_buttons():
            submit_button = gr.Button("Submit", visible=False)
            continue_button = gr.Button("Continue", visible=True)
            return continue_button, submit_button

        def update_continue_button(state):
            count = state if isinstance(state, int) else state.value
            max_images = config['dataset'][config['dataset']['name']]['n_classes']
            finish_button = gr.Button("Finish", visible=(count == max_images-1))
            submit_button = gr.Button("Submit", visible=(count != max_images-1))
            continue_button = gr.Button("Continue", visible=False)
            return continue_button, submit_button, finish_button


        def update_checkbox(user_state, random_answer_order):
            count = user_state if isinstance(user_state, int) else user_state.value
            # get row count from csv
            row = concepts.iloc[count]
            keys = concepts.keys()
            random_ids = generate_random_ids()
            tmp = []
            for i in range(3):
                t = []
                for j in range(3):
                    t.append(int(random_ids[i][j]))
                tmp.append(t)
            random_ids = tmp

            random_order = np.random.permutation(3)
            
            print('random_ids:', random_ids)
            print('random_order:', random_order)
            random_answer_order[count] = {
                "random_ids": random_ids,
                "random_order": random_order
            }
            concept_checkboxes = gr.CheckboxGroup(
                choices = [
                    (f'{row[keys[random_ids[random_order[0]][0]]]}, {row[keys[random_ids[random_order[0]][1]]]}, {row[keys[random_ids[random_order[0]][2]]]}', int(random_order[0])), 
                    (f'{row[keys[random_ids[random_order[1]][0]]]}, {row[keys[random_ids[random_order[1]][1]]]}, {row[keys[random_ids[random_order[1]][2]]]}', int(random_order[1])),
                    (f'{row[keys[random_ids[random_order[2]][0]]]}, {row[keys[random_ids[random_order[2]][1]]]}, {row[keys[random_ids[random_order[2]][2]]]}', int(random_order[2]))
                ],
                label=f"Choose the concept set that better describes the class {class_names[count]}",
                value=None,
                visible=True
            )
            return random_answer_order, concept_checkboxes

        def hide_checkbox():
            concept_checkboxes = gr.CheckboxGroup(
                choices = ['c10, c2, c3','c4, c5, c6','c7, c8, c9'],
                label=f"Choose the concept set that better describes the target class",
                value=None,
                visible=False
            )
            return concept_checkboxes

        def redirect():
            pass

        def save_results(answers, random_answer_order):
            rand_ids = [random_answer_order[i]['random_ids'] for i in range(len(random_answer_order))]

            rand_order = [random_answer_order[i]['random_order'] for i in range(len(random_answer_order))]

            api_token = os.getenv("HFTOKEN")
            if not api_token:
                raise ValueError("Hugging Face API token not found. Please set the HF_API_TOKEN environment variable.")
            
            json_file_results = config['results']['exp1_dir'] # 'exp1' 
            JSON_DATASET_DIR = Path("json_dataset")
            JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
            JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"
            scheduler = CommitScheduler(
                repo_id=f"results_{config['dataset']['name']}_{config['results']['exp2_dir']}",
                repo_type="dataset",
                folder_path=JSON_DATASET_DIR,
                path_in_repo="data",
                token=api_token  # Pass the token here
            )

            duration = time.time() - start_time.value

            info_to_push = {
                "user_id": time.time(), 
                "answer": {i: answer for i, answer in enumerate(answers)},
                "random_ids": {i: [list(elem) for elem in rand_id] for i, rand_id in enumerate(rand_ids)}, # 'random_ids': {0: [[np.int64(3), np.int64(4), np.int64(1)], [np.int64(6), np.int64(3), np.int64(9)], [np.int64(13), np.int64(14), np.int64(5)]], 1: [[np.int64(2), np.int64(1), np.int64(3)], [np.int64(6), np.int64(8), np.int64(5)], [np.int64(11), np.int64(10), np.int64(5)]]} -> it's not serializable
                "random_order": {i: [int(elem) for elem in rand_o] for i, rand_o in enumerate(rand_order)},
                "duration": duration,
            }
            print('INFO TO PUSH:', info_to_push)

            # Save the results into huggingface hub
            with scheduler.lock:
                with JSON_DATASET_PATH.open("a") as f:
                    json.dump({
                        "user_id": info_to_push["user_id"],
                        "answers": info_to_push["answer"],
                        # make it serializable not as it previously defined
                        "random_ids": {i: [list(elem) for elem in rand_id] for i, rand_id in enumerate(rand_ids)},
                        "random_order": info_to_push["random_order"],
                        "duration": info_to_push["duration"],
                        "datetime": datetime.now().isoformat()
                    }, f)
                    f.write("\n")
            scheduler.push_to_hub()
 
        def check_answer(concept_checkboxes):
            # check if there are multiple concepts selected, if yes return an error
            if len(concept_checkboxes) > 1:
                raise gr.Error("Please select only one concept set")
            if len(concept_checkboxes) == 0:
                raise gr.Error("Please select a concept set")

        def add_answer(concept_checkboxes, answers):
            answers.append(concept_checkboxes)
            print('ANSWERS:', answers, concept_checkboxes)
            return answers

        submit_button.click(
            check_answer, 
            inputs=concept_checkboxes
        ).success(
            update_state,
            inputs=user_state,
            outputs=user_state
        ).then(
            add_answer,
            inputs=[concept_checkboxes, answers],
            outputs=answers
        ).then(
            update_img_label,
            inputs=user_state,
            outputs=target_img_label
        ).then(
            update_buttons,
            outputs={continue_button, submit_button}
        ).then(
            hide_checkbox,
            outputs=concept_checkboxes
        ).then(
            update_label, 
            inputs=[concept_checkboxes, user_state], 
            outputs={img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16},
        )
        #.then(
        #    update_checkbox,
        #    outputs=concept_checkboxes
        #)

        continue_button.click(
            update_continue_button,
            inputs=user_state,
            outputs={continue_button, submit_button, finish_button}
        ).then(
            update_checkbox,
            inputs=[user_state, random_answer_order],
            outputs={random_answer_order, concept_checkboxes}
        )
       
        finish_button.click(
            check_answer, inputs=concept_checkboxes
        ).success(
            update_state, inputs=user_state, outputs=user_state
        ).then(
            add_answer, inputs=[concept_checkboxes, answers],outputs=answers
        ).then(
            save_results, inputs=[answers, random_answer_order]
        ).then(
            redirect, js="window.location = 'https://marcoparola.github.io/saliency-evaluation-app/end'"
        )

        demo.load()
    demo.launch()

if __name__ == "__main__":
    main()