Spaces:
Sleeping
Sleeping
File size: 10,266 Bytes
70030b8 ca6133f 70030b8 9bdb26d f75a860 9bdb26d 9334c2e 55365a8 22e71e9 17a2ec6 22e71e9 9bdb26d 22e71e9 433d823 9bdb26d 0dd3f3d 707142e 2fab3b0 707142e 0dd3f3d 9bdb26d 70030b8 64e9265 556bb4d 70030b8 291318b 556bb4d 70030b8 0dd3f3d ca6133f 45376e5 0586ee3 0dd3f3d 0586ee3 55365a8 45376e5 36d6ca9 0586ee3 70030b8 45376e5 55365a8 0586ee3 55365a8 ca6133f 45376e5 55365a8 0586ee3 ca6133f 5d32525 0586ee3 ca6133f b763faa 0586ee3 0dd3f3d 0586ee3 ca6133f 55365a8 0586ee3 b763faa 0586ee3 b763faa 5d32525 0586ee3 70030b8 5d32525 22e71e9 9da963f 8c64992 9da963f 22e71e9 9da963f 8c64992 9da963f 17a2ec6 9da963f 8c64992 0dd3f3d 9bdb26d b7d2352 64e9265 9bdb26d acd6e98 9bdb26d acd6e98 9bdb26d 5d32525 70030b8 5d32525 70030b8 9bdb26d 581565d 9bdb26d 581565d 9bdb26d |
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import os
import re
import json
import base64
import gradio as gr
import random
from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
import pandas as pd
from apscheduler.schedulers.background import BackgroundScheduler
from huggingface_hub import snapshot_download, login
from collections import defaultdict
from datasets import Dataset, DatasetDict
from datasets import load_dataset
from huggingface_hub import HfApi
from src.about import (
CITATION_BUTTON_LABEL,
CITATION_BUTTON_TEXT,
EVALUATION_QUEUE_TEXT,
INTRODUCTION_TEXT,
LLM_BENCHMARKS_TEXT,
TITLE,
)
from src.display.css_html_js import custom_css
from src.display.utils import (
BENCHMARK_COLS,
COLS,
EVAL_COLS,
EVAL_TYPES,
AutoEvalColumn,
ModelType,
fields,
WeightType,
Precision
)
from src.envs import API, EVAL_REQUESTS_PATH, EVAL_RESULTS_PATH, QUEUE_REPO, REPO_ID, RESULTS_REPO, TOKEN
from src.populate import get_evaluation_queue_df, get_leaderboard_df
from src.submission.submit import add_new_eval
HF_DATASET_REPO = "JunJiaGuo/Vid_result"
HF_TOKEN = os.getenv("HF_TOKEN")
login(HF_TOKEN)
def restart_space():
API.restart_space(repo_id=REPO_ID)
### Space initialisation
try:
print(EVAL_REQUESTS_PATH)
snapshot_download(
repo_id=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
)
except Exception:
restart_space()
try:
print(EVAL_RESULTS_PATH)
snapshot_download(
repo_id=RESULTS_REPO, local_dir=EVAL_RESULTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
)
except Exception:
restart_space()
LEADERBOARD_DF = get_leaderboard_df(EVAL_RESULTS_PATH, EVAL_REQUESTS_PATH, COLS, BENCHMARK_COLS)
(
finished_eval_queue_df,
running_eval_queue_df,
pending_eval_queue_df,
) = get_evaluation_queue_df(EVAL_REQUESTS_PATH, EVAL_COLS)
def init_leaderboard(dataframe):
if dataframe is None or dataframe.empty:
raise ValueError("Leaderboard DataFrame is empty or None.")
return Leaderboard(
value=dataframe,
datatype=[c.type for c in fields(AutoEvalColumn)],
select_columns=SelectColumns(
default_selection=[c.name for c in fields(AutoEvalColumn) if c.displayed_by_default],
cant_deselect=[c.name for c in fields(AutoEvalColumn) if c.never_hidden],
label="Select Columns to Display:",
),
search_columns=[AutoEvalColumn.model.name, AutoEvalColumn.license.name],
hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden],
filter_columns=[
ColumnFilter(AutoEvalColumn.model_type.name, type="checkboxgroup", label="Model types"),
ColumnFilter(AutoEvalColumn.precision.name, type="checkboxgroup", label="Precision"),
ColumnFilter(
AutoEvalColumn.params.name,
type="slider",
min=0.01,
max=150,
label="Select the number of parameters (B)",
),
ColumnFilter(
AutoEvalColumn.still_on_hub.name, type="boolean", label="Deleted/incomplete", default=True
),
],
bool_checkboxgroup_label="Hide models",
interactive=False,
)
current_dir = os.path.dirname(os.path.abspath(__file__))
print(current_dir)
CSV_FILE = os.path.join(current_dir, "acc.csv")
CLASS_LIST = [
"script_matching", "plot_ordering", "background_perception", "scene_counting", "lighting_perception",
"character_counting", "action_perception", "CMP_perception", "emotion_perception",
"art_style", "special_effect", "cut_counting", "camera_movement", "camera_angle", "shot_size",
"Narrative", "Scene", "Character", "Making", "Cinematography"
]
CATEGORY_MAPPING = {
"Narrative": ["script_matching", "plot_ordering"],
"Scene": ["background_perception", "scene_counting", "lighting_perception"],
"Character": ["character_counting", "action_perception", "CMP_perception", "emotion_perception"],
"Making": ["art_style", "special_effect", "cut_counting"],
"Cinematography": ["camera_movement", "camera_angle", "shot_size"]
}
def load_id_answer_mapping():
id_answer_mapping = os.getenv("ID_ANSWER_MAPPING")
if not id_answer_mapping:
raise ValueError("ID_ANSWER_MAPPING secret not found!")
# print(id_answer_mapping)
# print(type(id_answer_mapping))
return json.loads(id_answer_mapping)
def answer_matching(text):
if isinstance(text, list):
text = text[0] if text else random.choice(['A', 'B', 'C', 'D'])
if not isinstance(text, str):
return random.choice(['A', 'B', 'C', 'D'])
patterns = [
r'\((A|B|C|D)\)',
r'^(A|B|C|D)[\s\W]*',
r'\b[A-D]\b',
r'\((a|b|c|d)\)',
r'\b(A|B|C|D)\.',
]
for pattern in patterns:
match = re.search(pattern, text)
if match:
return match.group(1).upper()
letters = re.findall(r'[a-zA-Z]', text)
return letters[0].upper() if len(letters) == 1 else random.choice(['A', 'B', 'C', 'D'])
def evaluate_uploaded_json(
user_file: str,
model_name: str,
multi_choice_file: str = "multi_choice.json",
):
print(f"Model Name: {model_name}")
print(f"Uploaded File: {user_file}")
id_answer_mapping = load_id_answer_mapping()
with open(multi_choice_file, "r", encoding="utf-8") as f:
mc_data = json.load(f)
id_class_mapping = {q["id"]: q["class"] for q in mc_data}
with open(user_file, "r", encoding="utf-8") as f:
user_data = json.load(f)
correct = 0
total = 0
class_correct = defaultdict(int)
class_total = defaultdict(int)
for item in user_data:
question_id = item["id"]
raw_user_answer = item.get("model_answer", "")
user_answer = answer_matching(raw_user_answer)
question_class = id_class_mapping.get(question_id, "Unknown")
class_total[question_class] += 1
total += 1
if id_answer_mapping.get(question_id) == user_answer:
class_correct[question_class] += 1
correct += 1
subclass_data = []
subclass_result = {}
for cls in CLASS_LIST[:-5]:
acc = class_correct[cls] / class_total[cls] if class_total[cls] > 0 else 0
subclass_data.append({
"Subclass": cls,
"Accuracy": f"{acc:.2%}",
"Correct/Total": f"{class_correct[cls]}/{class_total[cls]}"
})
subclass_result[cls] = acc
category_data = []
for category, sub_classes in CATEGORY_MAPPING.items():
cat_correct = sum(class_correct.get(sub_cls, 0) for sub_cls in sub_classes)
cat_total = sum(class_total.get(sub_cls, 0) for sub_cls in sub_classes)
acc = cat_correct / cat_total if cat_total > 0 else 0
category_data.append({
"Category": category,
"Accuracy": f"{acc:.2%}",
"Correct/Total": f"{cat_correct}/{cat_total}"
})
subclass_result[category] = acc
overall_accuracy = f"{correct / total:.2%} ({correct}/{total} correct)"
subclass_df = pd.DataFrame(subclass_data)
category_df = pd.DataFrame(category_data)
save_class_accuracy_to_hf_dataset(model_name, subclass_result)
return overall_accuracy, category_df, subclass_df
def save_class_accuracy_to_hf_dataset(model_name, class_accuracy):
new_data = {"Model Name": model_name}
for cls in CLASS_LIST:
new_data[cls] = class_accuracy.get(cls, 0)
new_df = pd.DataFrame([new_data])
try:
dataset = load_dataset(HF_DATASET_REPO, split="train")
existing_df = dataset.to_pandas()
print(existing_df)
updated_df = pd.concat([existing_df, new_df], ignore_index=True)
except:
updated_df = new_df
updated_dataset = Dataset.from_pandas(updated_df)
updated_dataset.push_to_hub(HF_DATASET_REPO, split="train", token=HF_TOKEN)
demo = gr.Blocks(css=custom_css)
with demo:
gr.HTML('<h1 style="text-align: center;">Vid-Composition</h1>')
# gr.Markdown("Vid-Composition", elem_classes="markdown-text")
with gr.Tabs(elem_classes="tab-buttons") as tabs:
# with gr.TabItem("π
LLM Benchmark", elem_id="llm-benchmark-tab-table", id=0):
# leaderboard = init_leaderboard(LEADERBOARD_DF)
# with gr.TabItem("π About", elem_id="llm-benchmark-tab-table", id=2):
# gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
with gr.TabItem("π Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
with gr.Row():
json_file = gr.File(label="Upload JSON File")
model_name = gr.Textbox(label="Model Name", placeholder="Enter your model name here")
with gr.Row():
overall_acc = gr.Textbox(label="Overall Accuracy")
with gr.Row():
category_df = gr.Dataframe(label="Category Accuracy")
subclass_df = gr.Dataframe(label="Subclass Accuracy")
json_eval_button = gr.Button("Evaluate")
json_eval_button.click(
fn=evaluate_uploaded_json,
inputs=[json_file, model_name],
outputs=[overall_acc, category_df, subclass_df]
)
with gr.Row():
with gr.Accordion("π Citation", open=False):
citation_button = gr.Textbox(
value="""@article{tang2024vidcompostion,
title = {VidComposition: Can MLLMs Analyze Compositions in Compiled Videos?},
author = {Tang, Yunlong and Guo, Junjia and Hua, Hang and Liang, Susan and Feng, Mingqian and Li, Xinyang and Mao, Rui and Huang, Chao and Bi, Jing and Zhang, Zeliang and Fazli, Pooyan and Xu, Chenliang},
journal = {arXiv preprint arXiv:2411.10979},
year = {2024}
}""",
label="BibTeX Citation",
lines=20,
elem_id="citation-button",
show_copy_button=True,
)
scheduler = BackgroundScheduler()
scheduler.add_job(restart_space, "interval", seconds=1800)
scheduler.start()
demo.queue(default_concurrency_limit=40).launch() |