|
import gradio as gr |
|
import os |
|
from apscheduler.schedulers.background import BackgroundScheduler |
|
from huggingface_hub import HfApi |
|
from datetime import datetime, timedelta |
|
|
|
from src.assets.text_content import TITLE, INTRODUCTION_TEXT, CLEMSCORE_TEXT, MULTIMODAL_NAME, TEXT_NAME, HF_REPO |
|
from src.leaderboard_utils import query_search, get_github_data |
|
from src.plot_utils import split_models, plotly_plot, get_plot_df, update_open_models, update_closed_models |
|
from src.plot_utils import reset_show_all, reset_show_names, reset_show_legend, reset_mobile_view |
|
from src.version_utils import get_version_data |
|
from src.trend_utils import get_final_trend_plot |
|
|
|
""" |
|
CONSTANTS |
|
""" |
|
|
|
TIME = 43200 |
|
|
|
|
|
""" |
|
AUTO RESTART HF SPACE |
|
""" |
|
HF_TOKEN = os.environ.get("H4_TOKEN", None) |
|
api = HfApi() |
|
|
|
def restart_space(): |
|
api.restart_space(repo_id=HF_REPO, token=HF_TOKEN) |
|
|
|
|
|
""" |
|
GITHUB UTILS |
|
""" |
|
github_data = get_github_data() |
|
multimodal_leaderboard = github_data["multimodal"]["dataframes"][0] |
|
|
|
|
|
|
|
multimodal_leaderboard = multimodal_leaderboard.iloc[:, :4] |
|
|
|
|
|
""" |
|
VERSIONS UTILS |
|
""" |
|
versions_data = get_version_data() |
|
latest_version = versions_data['versions'][0]['name'] |
|
last_updated_date = versions_data['versions'][0]['last_updated'][0] |
|
version_names = [v['name'] for v in versions_data['versions']] |
|
|
|
global version_df |
|
version_df = versions_data['dataframes'][0] |
|
def select_version_df(name): |
|
for i, v in enumerate(versions_data['versions']): |
|
if v['name'] == name: |
|
return versions_data['dataframes'][i] |
|
|
|
""" |
|
MAIN APPLICATION |
|
""" |
|
hf_app = gr.Blocks() |
|
with hf_app: |
|
|
|
gr.HTML(TITLE) |
|
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text") |
|
|
|
with gr.Tabs(elem_classes="tab-buttons") as tabs: |
|
""" |
|
####################### FIRST TAB - MULTIMODAL LEADERBOARD ####################### |
|
""" |
|
with gr.TabItem(MULTIMODAL_NAME, elem_id="mm-llm-benchmark-tab-table", id=1): |
|
with gr.Row(): |
|
mm_search_bar = gr.Textbox( |
|
placeholder=" π Search for models - separate multiple queries with `;` and press ENTER...", |
|
show_label=False, |
|
elem_id="search-bar", |
|
) |
|
|
|
mm_leaderboard_table = gr.Dataframe( |
|
value=multimodal_leaderboard, |
|
elem_id="mm-leaderboard-table", |
|
interactive=False, |
|
visible=True |
|
) |
|
|
|
|
|
gr.HTML(CLEMSCORE_TEXT) |
|
gr.HTML(f"Last updated - {github_data['multimodal']['version_data'][0]['last_updated'][0]}") |
|
|
|
|
|
|
|
mm_dummy_leaderboard_table = gr.Dataframe( |
|
value=multimodal_leaderboard, |
|
elem_id="mm-leaderboard-table-dummy", |
|
interactive=False, |
|
visible=False |
|
) |
|
|
|
|
|
mm_search_bar.submit( |
|
query_search, |
|
[mm_dummy_leaderboard_table, mm_search_bar], |
|
mm_leaderboard_table, |
|
queue=True |
|
) |
|
|
|
""" |
|
####################### SECOND TAB - PLOTS - %PLAYED V/S QUALITY SCORE ####################### |
|
""" |
|
with gr.TabItem("π Plots", elem_id="plots", id=2): |
|
""" |
|
Accordion Groups to select individual models - Hidden by default |
|
""" |
|
with gr.Accordion("Select Open-weight Models π", open=False): |
|
open_models_selection = update_open_models() |
|
clear_button_1 = gr.ClearButton(open_models_selection) |
|
|
|
with gr.Accordion("Select Commercial Models π°", open=False): |
|
closed_models_selection = update_closed_models() |
|
clear_button_2 = gr.ClearButton(closed_models_selection) |
|
|
|
""" |
|
Checkbox group to control the layout of the plot |
|
""" |
|
with gr.Row(): |
|
with gr.Column(): |
|
show_all = gr.CheckboxGroup( |
|
["Select All Models"], |
|
label="Show plot for all models π€", |
|
value=[], |
|
elem_id="value-select-3", |
|
interactive=True, |
|
) |
|
|
|
with gr.Column(): |
|
show_names = gr.CheckboxGroup( |
|
["Show Names"], |
|
label="Show names of models on the plot π·οΈ", |
|
value=[], |
|
elem_id="value-select-4", |
|
interactive=True, |
|
) |
|
|
|
with gr.Column(): |
|
show_legend = gr.CheckboxGroup( |
|
["Show Legend"], |
|
label="Show legend on the plot π‘", |
|
value=[], |
|
elem_id="value-select-5", |
|
interactive=True, |
|
) |
|
with gr.Column(): |
|
mobile_view = gr.CheckboxGroup( |
|
["Mobile View"], |
|
label="View plot on smaller screens π±", |
|
value=[], |
|
elem_id="value-select-6", |
|
interactive=True, |
|
) |
|
|
|
""" |
|
PLOT BLOCK |
|
""" |
|
|
|
|
|
with gr.Row(): |
|
dummy_plot_df = gr.DataFrame( |
|
value=get_plot_df(), |
|
visible=False |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
plot_output = gr.Plot() |
|
|
|
""" |
|
PLOT CHANGE ACTIONS |
|
Toggle 'Select All Models' based on the values in Accordion checkbox groups |
|
""" |
|
open_models_selection.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
closed_models_selection.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
show_all.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
show_names.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
show_legend.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
mobile_view.change( |
|
plotly_plot, |
|
[dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, |
|
mobile_view], |
|
[plot_output], |
|
queue=True |
|
) |
|
|
|
open_models_selection.change( |
|
reset_show_all, |
|
outputs=[show_all], |
|
queue=True |
|
) |
|
|
|
closed_models_selection.change( |
|
reset_show_all, |
|
outputs=[show_all], |
|
queue=True |
|
) |
|
|
|
""" |
|
####################### THIRD TAB - TRENDS ####################### |
|
""" |
|
with gr.TabItem("πTrends", elem_id="trends-tab", id=3): |
|
with gr.Row(): |
|
mkd_text = gr.Markdown("### Commercial v/s Open-Weight models - clemscore over time. The size of the circles represents the scaled value of the parameters of the models. Larger circles indicate higher parameter values.") |
|
|
|
with gr.Row(): |
|
trend_plot = gr.Plot(get_final_trend_plot(False, 1200), show_label=False) |
|
|
|
with gr.Row(): |
|
mobile_view = gr.CheckboxGroup( |
|
choices=["Mobile View"], |
|
value=[], |
|
label="View plot on smaller screens π±", |
|
elem_id="value-select-8", |
|
interactive=True, |
|
) |
|
|
|
mobile_view.change( |
|
get_final_trend_plot, |
|
[mobile_view], |
|
[trend_plot], |
|
queue=True |
|
) |
|
|
|
|
|
""" |
|
####################### FOURTH TAB - VERSIONS AND DETAILS ####################### |
|
""" |
|
with gr.TabItem("π Versions and Details", elem_id="versions-details-tab", id=4): |
|
with gr.Row(): |
|
version_select = gr.Dropdown( |
|
version_names, label="Select Version πΉοΈ", value=latest_version |
|
) |
|
with gr.Row(): |
|
search_bar_prev = gr.Textbox( |
|
placeholder=" π Search for models - separate multiple queries with `;` and press ENTER...", |
|
show_label=False, |
|
elem_id="search-bar-3", |
|
) |
|
|
|
prev_table = gr.Dataframe( |
|
value=version_df, |
|
elem_id="version-leaderboard-table", |
|
interactive=False, |
|
visible=True |
|
) |
|
|
|
dummy_prev_table = gr.Dataframe( |
|
value=version_df, |
|
elem_id="version-dummy-leaderboard-table", |
|
interactive=False, |
|
visible=False |
|
) |
|
|
|
gr.HTML(CLEMSCORE_TEXT) |
|
gr.HTML(f"Last updated - {last_updated_date}") |
|
|
|
search_bar_prev.submit( |
|
query_search, |
|
[dummy_prev_table, search_bar_prev], |
|
prev_table, |
|
queue=True |
|
) |
|
|
|
version_select.change( |
|
select_version_df, |
|
[version_select], |
|
prev_table, |
|
queue=True |
|
) |
|
|
|
|
|
version_select.change( |
|
select_version_df, |
|
[version_select], |
|
dummy_prev_table, |
|
queue=True |
|
) |
|
|
|
hf_app.load() |
|
hf_app.queue() |
|
|
|
|
|
scheduler = BackgroundScheduler() |
|
scheduler.add_job(restart_space, 'interval', seconds=TIME) |
|
scheduler.start() |
|
|
|
|
|
print(datetime.now()) |
|
print(f"Scheduled restart at {datetime.now() + timedelta(seconds=TIME)}") |
|
|
|
hf_app.launch() |
|
|