Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import pandas as pd | |
class MultiURLData: | |
def __init__(self, file_path): | |
self.file_path = file_path | |
def fetch_data(self): | |
# Read URLs from a file, one per line | |
with open(self.file_path, 'r') as f: | |
file_urls = [line.strip() for line in f.readlines()] | |
dataframes = [] | |
for url in file_urls: | |
# Derive column names from the URLs | |
column_name = url.split('/')[-1].split('_')[0] | |
# Load data from URL | |
response = requests.get(url) | |
data = response.json() | |
# Convert data into a DataFrame | |
df = pd.DataFrame(data['results']).T | |
# Rename 'acc' column to respective file names | |
df = df.rename(columns={'acc': column_name}) | |
# Remove 'hendrycksTest-' from the index | |
df.index = df.index.str.replace('hendrycksTest-', '') | |
print(f"dype df {type(df)}") | |
dataframes.append(df[[column_name]]) # keep only the column of interest | |
# Merge the dataframes | |
# Merge the dataframes on index | |
data = pd.concat(dataframes, axis=1) | |
print(f"dype data {type(data)}") | |
# Transpose the dataframe to swap rows and columns | |
data = data.transpose() | |
# Select only columns 'moral_scenarios' and 'moral_disputes' | |
data = data[['moral_scenarios', 'moral_disputes']] | |
## add a column with the index of the dataframe | |
data['Model Name'] = data.index | |
# move the column to the front of the dataframe | |
cols = data.columns.tolist() | |
cols = cols[-1:] + cols[:-1] | |
data = data[cols] | |
return data | |
data_provider = MultiURLData("file_urls.txt") | |
block = gr.Blocks() | |
with block: | |
gr.Markdown("""Leaderboard""") | |
with gr.Tabs(): | |
with gr.TabItem("Leaderboard"): | |
with gr.Row(): | |
data = gr.outputs.Dataframe(type="pandas") | |
with gr.Row(): | |
data_run = gr.Button("Refresh") | |
data_run.click(data_provider.fetch_data, inputs=None, outputs=data) | |
# running the function on page load in addition to when the button is clicked | |
block.load(data_provider.fetch_data, inputs=None, outputs=data) | |
block.launch() | |