Spaces:
Sleeping
Sleeping
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() | |