BenCzechMark / app.py
Lakoc
v0.0.1
b66f230
raw
history blame
5.51 kB
import os
import gradio as gr
import pandas as pd
from gradio.themes.utils.sizes import text_md
from content import (HEADER_MARKDOWN, LEADERBOARD_TAB_TITLE_MARKDOWN, SUBMISSION_TAB_TITLE_MARKDOWN)
from server import LeaderboardServer
leaderboard_server = LeaderboardServer()
def on_submit_pressed():
return gr.update(value='Processing submission...', interactive=False)
def validate_submission_inputs(team_name, submission_id, link_to_model, submission_file):
if not team_name or not submission_id or not link_to_model or not submission_file:
raise ValueError('Please fill in all fields')
if not os.path.exists(submission_file):
raise ValueError('File does not exist')
def process_submission(team_name, submission_id, description, link_to_model, submission_file):
try:
validate_submission_inputs(team_name, submission_id, link_to_model, submission_file)
metadata = {
"team_name": team_name,
"submission_id": submission_id,
"description": description,
"link_to_model": link_to_model,
}
gr.Info('Submission valid, running local tournament...')
leaderboard_server.prepare_model_for_submission(submission_file, metadata)
except ValueError as err:
gr.Warning(str(err))
return gr.update(visible=False), gr.update(visible=True), gr.update(interactive=True,
visible=True), gr.update(
interactive=True, visible=True), gr.update(visible=True), gr.update(
value=leaderboard_server.get_leaderboard(leaderboard_server.pre_submit[0]), visible=True)
def submit_results():
leaderboard_server.save_pre_submit()
leaderboard_server.update_leaderboard()
gr.Info('Submission successful!')
return gr.update(value='Pre-submit model', visible=True, interactive=True), gr.update(
visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(
visible=False), gr.update(visible=False), gr.DataFrame(
value=leaderboard_server.get_leaderboard(), visible=True)
def erase_presubmit():
leaderboard_server.pre_submit = None
return gr.update(value='Pre-submit model', visible=True, interactive=True), gr.update(
visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(
visible=False), gr.update(visible=False)
with (gr.Blocks(theme=gr.themes.Soft(text_size=text_md), css="footer {visibility: hidden}") as main):
app_state = gr.State({})
with gr.Row():
with gr.Row():
gr.Markdown(HEADER_MARKDOWN)
with gr.Row():
with gr.Tab('Leaderboard') as leaderboards_tab:
gr.Markdown(LEADERBOARD_TAB_TITLE_MARKDOWN)
results_table = gr.DataFrame(leaderboard_server.get_leaderboard(), interactive=False, label=None,
visible=True)
with gr.Tab('Submission'):
with gr.Column():
gr.Markdown(SUBMISSION_TAB_TITLE_MARKDOWN)
with gr.Row():
submission_team_name_tb = gr.Textbox(label='Team Name')
submission_id_tb = gr.Textbox(label='Submission ID')
with gr.Row():
description_tb = gr.Textbox(label='Description', type='text')
link_to_model_tb = gr.Textbox(label='Link to model', type='text')
submission_file_path = gr.File(label='Upload your results', type='filepath')
pre_submission_btn = gr.Button(value='Pre-submit model', interactive=True)
submit_prompt = gr.Markdown(
"""
Do you really want to submit a model? This action is irreversible.
""",
visible=False
)
pre_submit_info = gr.Markdown(
"""
This is how will ranking look like after your submission:
""",
visible=False
)
pre_submit_table = gr.DataFrame(pd.DataFrame(), interactive=False, label=None, visible=False)
submission_btn_yes = gr.Button(value='Submit model', interactive=False, visible=False)
submission_btn_no = gr.Button(value='Reverse process', interactive=False, visible=False)
pre_submission_btn.click(
fn=on_submit_pressed,
outputs=[pre_submission_btn]
).then(
fn=process_submission,
inputs=[submission_team_name_tb, submission_id_tb, description_tb, link_to_model_tb,
submission_file_path],
outputs=[pre_submission_btn, submit_prompt, submission_btn_yes, submission_btn_no, pre_submit_info,
pre_submit_table]
)
submission_btn_yes.click(
fn=submit_results,
outputs=[pre_submission_btn, submission_btn_yes, submission_btn_no, submit_prompt, pre_submit_info,
pre_submit_table, results_table]
)
submission_btn_no.click(
fn=erase_presubmit,
outputs=[pre_submission_btn, submission_btn_yes, submission_btn_no, submit_prompt, pre_submit_info,
pre_submit_table]
)
main.launch()