Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import gradio as gr | |
from fuzzywuzzy import fuzz | |
from utils import submit_gradio_module | |
def search_leaderboard(df, model_name, columns_to_show, threshold=95): | |
""" | |
Search the leaderboard for models matching the search term using fuzzy matching. | |
Args: | |
df: The dataframe containing all leaderboard data | |
model_name: The search term to find models | |
columns_to_show: List of columns to include in the result | |
threshold: Minimum similarity threshold (default: 95) | |
Returns: | |
Filtered dataframe with only matching models and selected columns | |
""" | |
if not model_name.strip(): | |
return df.loc[:, columns_to_show] | |
search_name = model_name.lower() # compute once for efficiency | |
def calculate_similarity(row): | |
return fuzz.partial_ratio(search_name, row["Model"].lower()) | |
filtered_df = df.copy() | |
filtered_df["similarity"] = filtered_df.apply(calculate_similarity, axis=1) | |
filtered_df = filtered_df[filtered_df["similarity"] >= threshold].sort_values( | |
"similarity", ascending=False | |
) | |
filtered_df = filtered_df.drop("similarity", axis=1).loc[:, columns_to_show] | |
return filtered_df | |
def update_columns_to_show(df, columns_to_show): | |
""" | |
Update the displayed columns in the dataframe. | |
Args: | |
df: The dataframe to update | |
columns_to_show: List of columns to include | |
Returns: | |
gradio.update object with the updated dataframe | |
""" | |
dummy_df = df.loc[:, [col for col in df.columns if col in columns_to_show]] | |
columns_widths = [] | |
for col in dummy_df.columns: | |
if col == "Rank": | |
columns_widths.append(80) | |
elif col == "Model": | |
columns_widths.append(400) | |
else: | |
columns_widths.append(150) | |
return gr.update(value=dummy_df, column_widths=columns_widths) | |
def create_leaderboard_tab( | |
df, | |
initial_columns_to_show, | |
search_function, | |
update_function, | |
about_section, | |
task_type, | |
model_param_limit=2000, | |
): | |
""" | |
Create a complete leaderboard tab with search, column selection, and data display. | |
Args: | |
df: The dataframe containing the leaderboard data | |
initial_columns_to_show: Initial list of columns to display | |
search_function: Function to handle searching | |
update_function: Function to handle column updates | |
about_section: Markdown text for the About tab | |
task_type: Type of the task ("Retriever" or "Reranker") | |
Returns: | |
A gradio Tabs component with the complete leaderboard interface | |
""" | |
columns_widths = [ | |
80 if col == "Rank" else 400 if col == "Model" else 150 | |
for col in initial_columns_to_show | |
] | |
with gr.Tabs() as tabs: | |
with gr.Tab("π Leaderboard"): | |
with gr.Column(): | |
with gr.Row(equal_height=True): | |
search_box = gr.Textbox( | |
placeholder="Search for models...", | |
label="Search (You can also press Enter to search)", | |
scale=5, | |
) | |
search_button = gr.Button( | |
value="Search", variant="primary", scale=1 | |
) | |
columns_to_show_input = gr.CheckboxGroup( | |
label="Columns to Show", | |
choices=df.columns.tolist(), | |
value=initial_columns_to_show, | |
scale=4, | |
) | |
leaderboard = gr.Dataframe( | |
value=df.loc[:, initial_columns_to_show], | |
datatype="markdown", | |
wrap=True, | |
show_fullscreen_button=True, | |
interactive=False, | |
column_widths=columns_widths, | |
) | |
# Connect events | |
search_box.submit( | |
search_function, | |
inputs=[search_box, columns_to_show_input], | |
outputs=leaderboard, | |
) | |
columns_to_show_input.select( | |
update_function, inputs=columns_to_show_input, outputs=leaderboard | |
) | |
search_button.click( | |
search_function, | |
inputs=[search_box, columns_to_show_input], | |
outputs=leaderboard, | |
) | |
with gr.Tab("π΅οΈ Submit"): | |
submit_gradio_module(task_type, model_param_limit=model_param_limit) | |
with gr.Tab("βΉοΈ About"): | |
gr.Markdown(about_section) | |
return tabs | |