bhaskartripathi's picture
Update app.py
16eefe3 verified
raw
history blame
3.66 kB
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr
import io
BASE_URL = "https://scale.com/leaderboard"
LEADERBOARDS = {
"Coding": "/coding",
"Adversarial Robustness": "/adversarial_robustness",
"Instruction Following": "/instruction_following",
"Math": "/math"
}
def scrape_leaderboard(leaderboard):
url = BASE_URL + LEADERBOARDS[leaderboard]
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
leaderboard_div = soup.find('div', class_='flex flex-col gap-4 sticky top-20')
if not leaderboard_div:
raise ValueError("Leaderboard div not found. The page structure might have changed.")
table = leaderboard_div.find('table', class_='w-full caption-bottom text-sm')
if not table:
raise ValueError("Leaderboard table not found within the div.")
data = []
for row in table.find('tbody').find_all('tr'):
cols = row.find_all('td')
rank = cols[0].find('div', class_='flex').text.strip().split()[0]
model = cols[0].find('a').text.strip()
score = cols[1].text.strip()
confidence = cols[2].text.strip()
data.append([rank, model, score, confidence])
df = pd.DataFrame(data, columns=['Rank', 'Model', 'Score', '95% Confidence'])
return df
def update_leaderboard(leaderboard):
try:
df = scrape_leaderboard(leaderboard)
return df, create_interactive_table(df)
except Exception as e:
return None, f"An error occurred: {str(e)}"
def create_interactive_table(df):
html = f"""
<script src="https://cdn.jsdelivr.net/npm/ag-grid-community/dist/ag-grid-community.min.js"></script>
<div id="myGrid" style="height: 500px; width: 100%;" class="ag-theme-alpine"></div>
<script>
var gridOptions = {{
columnDefs: [
{{field: "Rank", sortable: true, filter: true}},
{{field: "Model", sortable: true, filter: true}},
{{field: "Score", sortable: true, filter: true}},
{{field: "95% Confidence", sortable: true, filter: true}}
],
rowData: {df.to_dict(orient='records')},
defaultColDef: {{
flex: 1,
minWidth: 100,
resizable: true,
}},
domLayout: 'autoHeight'
}};
document.addEventListener('DOMContentLoaded', function() {{
var gridDiv = document.querySelector('#myGrid');
new agGrid.Grid(gridDiv, gridOptions);
}});
</script>
"""
return html
def export_to_excel(df):
if df is not None:
output = io.BytesIO()
with pd.ExcelWriter(output, engine='openpyxl') as writer:
df.to_excel(writer, index=False, sheet_name='Leaderboard')
output.seek(0)
return output
return None
# Create Gradio interface
with gr.Blocks() as iface:
gr.Markdown("# Scale AI Leaderboard Viewer")
with gr.Row():
dropdown = gr.Dropdown(choices=list(LEADERBOARDS.keys()), label="Select Leaderboard", value="Coding")
export_button = gr.Button("Export to Excel")
table_output = gr.HTML()
df_state = gr.State()
def on_load():
df, html = update_leaderboard("Coding")
return df, html
dropdown.change(update_leaderboard, inputs=[dropdown], outputs=[df_state, table_output])
export_button.click(export_to_excel, inputs=[df_state], outputs=[gr.File(label="Download Excel")])
iface.load(on_load, outputs=[df_state, table_output])
# Launch the app
iface.launch()