emwebaze's picture
Update app.py
a5e4fec verified
import gradio as gr
import pandas as pd
import json
def process_df(df):
# Set the current date and time
df['date'] = pd.Timestamp.now().strftime('%Y-%m-%d %H:%M')
# Create clickable links in the "Model" column
df['Model'] = df.apply(lambda x: f'<a href="{x["Link"]}" target="_blank">{x["Model"]}</a>', axis=1)
# Drop the "Link" column
df = df.drop(['Link'], axis=1)
# Convert columns to float
cols = df.columns.drop(['Model', 'date'])
df[cols] = df[cols].astype(float)
# Highlight the minimum values in the columns
styled_df = df.style.highlight_min(color='lightgreen', axis=0, subset=cols)
return styled_df
def get_eval(jsonfile):
global leaderboard_df
# Load JSON data
data = json.load(open(jsonfile))
# Convert JSON data to DataFrame
df = pd.DataFrame.from_dict(data, orient='columns')
# Concatenate with the existing leaderboard
leaderboard_df = pd.concat([leaderboard_df, df], ignore_index=True)
# Process the dataframe
df = process_df(leaderboard_df)
return df
def refresh_leaderboard():
global leaderboard_df
if len(leaderboard_df) == 0:
return leaderboard_df
else:
return process_df(leaderboard_df)
# Usage markdown
usage = """
## Using the web interface
Run your model on the evaluation datasets and create a json file in this format:
```
[
{
"Model": "whisperbase",
"date": "",
"ucfd_eng": 102.27,
"ucfd_lug": "nan",
"sema_eng" : 42.86,
"sema_lug": "nan",
"trac_fm_lug": "nan",
"Link": "https://colab.research.google.com/drive/1MGGOotrjSXWkR3ljNEVvfEUResEx6OLV#scrollTo=Elwx1K3fEDjf"
},
{
"Model": "facebookmms",
"date": "2024-06-26",
"ucfd_eng": 92.91,
"ucfd_lug": "nan",
"sema_eng" : 63.39,
"sema_lug": "nan",
"trac_fm_lug": 69.29,
"Link": "https://colab.research.google.com/drive/1MGGOotrjSXWkR3ljNEVvfEUResEx6OLV#scrollTo=Elwx1K3fEDjf"
}
]
```
>Upload this to the leaderboard and it should be updated with your results.
## Using the API
You can use the following code to programmatically upload your results from a colab or python script.
```
from gradio_client import Client, handle_file
client = Client("sunbird/SB_ASR_Leaderboard")
result = client.predict(
jsonfile=handle_file([/path/to/eval_results.json]),
api_name="/get_eval"
)
```
>Refresh the leaderboard and your results should be visible.
"""
# Initialize an empty dataframe for the leaderboard
columns = ['Model', 'date', 'ucfd_eng', 'ucfd_lug',
'sema_eng', 'sema_lug', 'trac_fm_lug']
leaderboard_df = pd.DataFrame(columns=columns)
# Create Gradio interface
block = gr.Blocks()
with block:
gr.Markdown("## Leaderboard for Sunbird AI ASR")
with gr.Row():
input_json = gr.UploadButton("Upload a JSON file", file_count="single", scale=0)
with gr.Tabs():
with gr.TabItem("ASR Leaderboard"):
with gr.Row():
data = gr.DataFrame(datatype="markdown")
with gr.TabItem("Usage"):
with gr.Row():
gr.Markdown(usage)
input_json.upload(get_eval, inputs=input_json, outputs=data)
# Running the function on page load in addition to when the button is clicked
block.load(refresh_leaderboard, inputs=None, outputs=data)
block.launch(share=True)