Howieeeee's picture
Upload app.py
fa4bb08 verified
__all__ = ['block', 'make_clickable_model', 'make_clickable_user', 'get_submissions']
import os
import io
import gradio as gr
import pandas as pd
import json
import shutil
import tempfile
import datetime
import zipfile
import numpy as np
from constants import *
from huggingface_hub import Repository
HF_TOKEN = os.environ.get("HF_TOKEN")
global data_component, filter_component
def upload_file(files):
file_paths = [file.name for file in files]
return file_paths
def add_new_eval(
input_file,
model_type,
model_name_textbox,
model_ability,
revision_name_textbox,
access_type,
model_link,
team_name,
contact_email,
release_time,
model_resolution,
model_length,
model_fps,
model_frame,
model_link_optional,
):
if input_file is None:
return "Error! Empty file!"
if model_link == '' or model_name_textbox == '' or contact_email == '':
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
submission_repo = Repository(local_dir=SUBMISSION_NAME, clone_from=SUBMISSION_URL, use_auth_token=HF_TOKEN, repo_type="dataset")
submission_repo.git_pull()
filename = f"{model_name_textbox}_{model_type}_{model_ability}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}"
now = datetime.datetime.now()
update_time = now.strftime("%Y.%m.%d") # Capture update time
csv_data = pd.read_csv(CSV_DIR)
if revision_name_textbox == '':
col = csv_data.shape[0]
model_name = model_name_textbox.replace(',',' ')
else:
model_name = revision_name_textbox.replace(',',' ')
model_name_list = csv_data['Model Name']
name_list = [name.split(']')[0][1:] for name in model_name_list]
if revision_name_textbox not in name_list:
col = csv_data.shape[0]
else:
col = name_list.index(revision_name_textbox)
if csv_data[col]['Sampled by'] == "WorldScore" or csv_data[col]['Evaluated by'] == "WorldScore":
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
if model_link == '':
model_name = model_name # no url
else:
model_name = '[' + model_name + '](' + model_link + ')'
if model_link_optional == '':
model_link_optional = model_link
try:
with open(input_file, 'r') as f:
upload_data = json.load(f)
# add new data
print('upload_data:', upload_data)
new_data = [model_type, model_name, model_ability]
if team_name == '':
new_data.append(model_name)
new_data.append(model_name)
else:
new_data.append(team_name)
new_data.append(team_name)
new_data.append(access_type)
new_data.append(update_time)
for key in TASK_INFO:
value = COLNAME2KEY[key]
if value in upload_data:
new_data.append(upload_data[value])
else:
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
csv_data.loc[col] = new_data
csv_data = csv_data.to_csv(CSV_DIR, index=False)
with open(INFO_DIR,'a') as f:
f.write(f"{model_type.replace(',', ' ')}\t{model_name.replace(',', ' ')}\t{model_ability.replace(',', ' ')}\t{model_resolution.replace(',', ' ')}\t{model_length.replace(',', ' ')}\t{model_fps.replace(',', ' ')}\t{model_frame.replace(',', ' ')}\t{model_link_optional.replace(',', ' ')}\t{contact_email.replace(',', ' ')}\n")
submission_repo.push_to_hub()
print("success update", model_name)
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
except:
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
def get_baseline_df():
submission_repo = Repository(local_dir=SUBMISSION_NAME, clone_from=SUBMISSION_URL, use_auth_token=HF_TOKEN, repo_type="dataset")
submission_repo.git_pull()
df = pd.read_csv(CSV_DIR)
df = df.sort_values(by=DEFAULT_INFO[0], ascending=False)
return df
block = gr.Blocks(css="""
.container {
max-width: 80%;
margin: auto;
}
/* 添加以下样式来控制表格列宽 */
.gradio-dataframe {
overflow-x: auto !important;
}
.gradio-dataframe table {
width: 100% !important;
white-space: nowrap !important;
}
.gradio-dataframe td, .gradio-dataframe th {
min-width: fit-content !important;
max-width: none !important;
white-space: pre-wrap !important;
padding: 8px !important;
}
""")
with block:
with gr.Column(elem_classes="container"):
gr.Markdown(
LEADERBORAD_INTRODUCTION
)
with gr.Tabs(elem_classes="tab-buttons") as tabs:
# Table 0
with gr.TabItem("🏅 WorldScore Benchmark", elem_id="worldscore-tab-table", id=0):
with gr.Row():
with gr.Column(scale=0.3):
model_type_filter = gr.CheckboxGroup(
choices=MODEL_TYPE,
value=DEFAULT_MODEL_TYPE,
label="Model Type",
interactive=True
)
ability_filter = gr.CheckboxGroup(
choices=ABILITY,
value=DEFAULT_ABILITY,
label="Ability",
interactive=True
)
with gr.Column(scale=0.7):
sort_by_filter = gr.Radio(
choices=TASK_INFO,
value=DEFAULT_INFO[0],
label="Sort by",
interactive=True
)
df = get_baseline_df()
data_component = gr.components.Dataframe(
value=df,
headers=COLUMN_NAMES,
type="pandas",
datatype=DATA_TITILE_TYPE,
interactive=False,
visible=True,
wrap=True
)
def on_filter_change(model_types, abilities, sort_by):
df = get_baseline_df()
# Filter by selected model types
df = df[df['Model Type'].isin(model_types)]
# Filter by selected abilities
df = df[df['Ability'].isin(abilities)]
# Sort by selected sort by
df = df.sort_values(by=sort_by, ascending=False)
return gr.Dataframe(
value=df,
headers=COLUMN_NAMES,
type="pandas",
datatype=DATA_TITILE_TYPE,
interactive=False,
visible=True,
wrap=True
)
model_type_filter.change(
fn=on_filter_change,
inputs=[model_type_filter, ability_filter, sort_by_filter],
outputs=data_component
)
ability_filter.change(
fn=on_filter_change,
inputs=[model_type_filter, ability_filter, sort_by_filter],
outputs=data_component
)
sort_by_filter.change(
fn=on_filter_change,
inputs=[model_type_filter, ability_filter, sort_by_filter],
outputs=data_component
)
with gr.TabItem("🚀 Submit here! ", elem_id="submit-tab-table", id=1):
with gr.Row():
gr.Markdown(SUBMIT_INTRODUCTION, elem_classes="markdown-text")
with gr.Row():
gr.Markdown("# Submit your evaluation json file here!", elem_classes="markdown-text")
with gr.Row():
gr.Markdown("Here is a required field", elem_classes="markdown-text")
with gr.Row():
with gr.Column():
model_type = gr.Dropdown(["Video", "3D", "4D"], label="Model Type")
model_name_textbox = gr.Textbox(
label="Model name", placeholder="Your model name"
)
model_ability = gr.Dropdown(["I2V", "T2V"], label="Model Ability")
revision_name_textbox = gr.Textbox(
label="Revision Model Name (Optional)", placeholder="If you need to update the previous submissions, please fill in this line"
)
with gr.Column():
access_type = gr.Dropdown(["Open Source", "Ready to Open Source", "API", "Close"], label="Access")
model_link = gr.Textbox(
label="Link (Website/Paper Link/Github/HuggingFace)", placeholder="If filling in the wrong information, your results may be removed."
)
team_name = gr.Textbox(
label="Your Team Name", placeholder="If left blank, it will be your model name"
)
contact_email = gr.Textbox(
label="E-Mail (Will not be displayed)", placeholder="Contact email"
)
with gr.Row():
gr.Markdown("The following is optional and will be synced to [GitHub](https://github.com/haoyi-duan/WorldScore/blob/main/README.md#world-generation-models-info)", elem_classes="markdown-text")
with gr.Row():
release_time = gr.Textbox(label="Version", placeholder="2025.03.29")
model_resolution = gr.Textbox(label="Resolution", placeholder="WidthxHeight")
model_length = gr.Textbox(label="Video Length (s)", placeholder="float")
model_fps = gr.Textbox(label="FPS", placeholder="int")
model_frame = gr.Textbox(label="Frame Number", placeholder="int")
model_link_optional = gr.Textbox(label="Link", placeholder='optional')
with gr.Column():
input_file = gr.components.File(label = "Click to Upload a JSON File", file_count="single", file_types=[".json"])
submit_button = gr.Button("Submit Eval")
submit_succ_button = gr.Markdown("Submit Success! Please press refresh and return to WorldScore Benchmark!", visible=False)
fail_textbox = gr.Markdown('<span style="color:red;">Please ensure that the `Model Name`, `Project Page`, and `E-mail` are filled in correctly and the uploaded json file is valid.</span>', elem_classes="markdown-text",visible=False)
submission_result = gr.Markdown()
submit_button.click(
add_new_eval,
inputs = [
input_file,
model_type,
model_name_textbox,
model_ability,
revision_name_textbox,
access_type,
model_link,
team_name,
contact_email,
release_time,
model_resolution,
model_length,
model_fps,
model_frame,
model_link_optional,
],
outputs=[submit_button, submit_succ_button, fail_textbox]
)
def refresh_data():
model_type_filter.value = DEFAULT_MODEL_TYPE
ability_filter.value = DEFAULT_ABILITY
sort_by_filter.value = DEFAULT_INFO[0]
df = get_baseline_df()
df = df[df['Model Type'].isin(model_type_filter.value)]
df = df[df['Ability'].isin(ability_filter.value)]
df = df.sort_values(by=sort_by_filter.value, ascending=False)
data_component = gr.Dataframe(
value=df,
headers=COLUMN_NAMES,
type="pandas",
datatype=DATA_TITILE_TYPE,
interactive=False,
visible=True,
wrap=True
)
return data_component, model_type_filter, ability_filter, sort_by_filter
with gr.Row(elem_classes="container"):
with gr.Column():
data_run = gr.Button("Refresh")
data_run.click(
refresh_data,
inputs=[],
outputs=[data_component, model_type_filter, ability_filter, sort_by_filter]
)
block.launch()