File size: 5,037 Bytes
cc7235c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os, os.path as osp
import time
import glob
import cv2
from PIL import Image

import hashlib
import shutil
import os, sys, os.path as osp
import csv
import random
import json
from huggingface_hub import HfApi, repo_exists, file_exists
from huggingface_hub.hf_api import CommitOperationAdd


def calc_file_md5(fpath):
    with open(fpath, "rb") as f:
        file_hash = hashlib.md5()
        while chunk := f.read(8192):
            file_hash.update(chunk)
    return file_hash.hexdigest()[:6]


finfo = [
    json.load(open("f1/coyo25m-0-000000.tar.json")),
    json.load(open("f2/coyo25m-0-000000.tar.json")),
    json.load(open("f3/coyo25m-0-000000.tar.json")),
    json.load(open("f3/coyo25m-0-000000.tar.json")),
]

keys = list(finfo[0].keys())

api = HfApi()


def get_random_captino(k):
    indexs = random.sample(list(range(5)), k=2)

    output = []
    idxs = []
    for i in indexs:
        if i == 4:
            output.append(finfo[0][k]["orig_text"])
        else:
            output.append(finfo[i][k]["output"])
        idxs.append(i)
    return output, idxs


def load_image(idx):
    k = keys[idx]
    infos, indexs = get_random_captino(k)
    return k, f"{k}", infos[0], infos[1], str(indexs)


def random_image(idx):
    k = random.choice(keys)
    index = keys.index(k)
    infos, indexs = get_random_captino(k)
    return k, index, f"{k}", infos[0], infos[1], str(indexs)


def save_labeling(url, cap1, cap2, labeler, indexs, preference="left"):
    os.makedirs("flagged", exist_ok=True)
    output_info = {
        "cap1": cap1,
        "cap2": cap2,
        "preference": preference,
        "indexs": indexs,
        "labeler": labeler,
    }
    # print(url)
    lid = (
        labeler.replace(" ", "_").replace("@", "_").replace(".", "_").replace("/", "-")
    )
    output_path = osp.join(f"flagged", url.replace("/", "--") + f".{lid}.json")
    with open(output_path, "w") as fp:
        json.dump(output_info, fp, indent=2)

    if "RUNNING_ON_SPACE" in os.environ:

        if not api.repo_exists(
            "Efficient-Large-Model/VILA-S-Human-Test", repo_type="dataset"
        ):
            api.create_repo(
                "Efficient-Large-Model/VILA-S-Human-Test",
                repo_type="dataset",
                private=True,
            )

        operation = CommitOperationAdd(
            path_or_fileobj=output_path,
            path_in_repo=osp.basename(output_path),
        )
        print("uploading ", output_path)
        commit_info = api.create_commit(
            repo_id="Efficient-Large-Model/VILA-S-Human-Test",
            repo_type="dataset",
            operations=[
                operation,
            ],
            commit_message=f"update {output_path}",
        )
        output_path = commit_info
    return output_path + "\n" + json.dumps(output_info, indent=2)


with gr.Blocks(
    title="VILA Video Benchmark",
) as demo:
    with gr.Row():
        slider = gr.Slider(maximum=len(keys), label="Video Index", value=0)

    with gr.Row():
        with gr.Column(scale=4):
            image_input = gr.Image(
                label="Video Preview ",
                height=360,
                value="https://github.com/NVlabs/VILA/raw/main/demo_images/vila-logo.jpg",
            )
        with gr.Column(scale=1):
            random_img = gr.Button(value="Random Image")
            labeler = gr.Text(
                value="placeholder",
                label="Labeler ID (your name or email)",
                interactive=True,
            )
            logging = gr.Markdown(label="Logging info")
    with gr.Row():
        btn_left = gr.Button("Left better")
        btn_tie = gr.Button("tie")
        btn_right = gr.Button("Right better")
    with gr.Row():
        vcap1 = gr.Textbox(label="Anoymous Caption 1")
        vcap2 = gr.Textbox(label="Anoymous Caption 2")
    cap_res = gr.Textbox(label="Caption Saving Results")
    tmp_info = gr.Textbox(label="Temp Info", visible=False)
    from functools import partial

    btn_left.click(
        partial(save_labeling, preference="left"),
        inputs=[logging, vcap1, vcap2, labeler, tmp_info],
        outputs=[cap_res],
    )

    btn_tie.click(
        partial(save_labeling, preference="tie"),
        inputs=[logging, vcap1, vcap2, labeler, tmp_info],
        outputs=[cap_res],
    )

    btn_right.click(
        partial(save_labeling, preference="right"),
        inputs=[logging, vcap1, vcap2, labeler, tmp_info],
        outputs=[cap_res],
    )

    slider.change(
        load_image,
        inputs=[slider],
        outputs=[image_input, logging, vcap1, vcap2, tmp_info],
    )
    random_img.click(
        random_image,
        inputs=[random_img],
        outputs=[image_input, slider, logging, vcap1, vcap2, tmp_info],
    )

    # btn_save.click(
    #     save_labeling,
    #     inputs=[video_path, _vtag, _vcap, vtag, vcap, uid],
    #     outputs=[
    #         cap_res,
    #     ],
    # )


demo.queue()
if __name__ == "__main__":
    demo.launch()