Spaces:
Runtime error
Runtime error
Corey Morris
commited on
Commit
·
f9b88e9
1
Parent(s):
cd2dfb2
updated app.py to a sortable dataframe
Browse files
app.py
CHANGED
@@ -1,49 +1,72 @@
|
|
1 |
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
import json
|
5 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
file_urls = [line.strip() for line in f.readlines()]
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
response = requests.get(url)
|
19 |
-
data = response.json()
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
|
|
31 |
|
32 |
-
#
|
33 |
-
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
data = data.transpose()
|
37 |
|
38 |
-
|
39 |
-
data = data[['moral_scenarios', 'moral_disputes']]
|
40 |
|
41 |
-
|
42 |
-
# Convert dataframe to html so that it can be displayed properly in Gradio
|
43 |
-
return data.to_html()
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
# Note: you don't need to use .launch() in Hugging Face Spaces, this is for local testing.
|
49 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import requests
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
class MultiURLData:
|
6 |
+
def __init__(self, file_path):
|
7 |
+
self.file_path = file_path
|
8 |
+
|
9 |
+
def fetch_data(self):
|
10 |
+
# Read URLs from a file, one per line
|
11 |
+
with open(self.file_path, 'r') as f:
|
12 |
+
file_urls = [line.strip() for line in f.readlines()]
|
13 |
+
|
14 |
+
dataframes = []
|
15 |
+
for url in file_urls:
|
16 |
+
# Derive column names from the URLs
|
17 |
+
column_name = url.split('/')[-1].split('_')[0]
|
18 |
+
|
19 |
+
# Load data from URL
|
20 |
+
response = requests.get(url)
|
21 |
+
data = response.json()
|
22 |
|
23 |
+
# Convert data into a DataFrame
|
24 |
+
df = pd.DataFrame(data['results']).T
|
|
|
25 |
|
26 |
+
# Rename 'acc' column to respective file names
|
27 |
+
df = df.rename(columns={'acc': column_name})
|
28 |
|
29 |
+
# Remove 'hendrycksTest-' from the index
|
30 |
+
df.index = df.index.str.replace('hendrycksTest-', '')
|
31 |
+
print(f"dype df {type(df)}")
|
32 |
|
33 |
+
dataframes.append(df[[column_name]]) # keep only the column of interest
|
|
|
|
|
34 |
|
35 |
+
# Merge the dataframes
|
36 |
+
# Merge the dataframes on index
|
37 |
+
data = pd.concat(dataframes, axis=1)
|
38 |
+
print(f"dype data {type(data)}")
|
39 |
|
40 |
+
# Transpose the dataframe to swap rows and columns
|
41 |
+
data = data.transpose()
|
42 |
|
43 |
+
# Select only columns 'moral_scenarios' and 'moral_disputes'
|
44 |
+
data = data[['moral_scenarios', 'moral_disputes']]
|
45 |
|
46 |
+
## add a column with the index of the dataframe
|
47 |
+
data['Model Name'] = data.index
|
48 |
|
49 |
+
# move the column to the front of the dataframe
|
50 |
+
cols = data.columns.tolist()
|
51 |
+
cols = cols[-1:] + cols[:-1]
|
52 |
+
data = data[cols]
|
53 |
|
54 |
+
return data
|
|
|
55 |
|
56 |
+
data_provider = MultiURLData("file_urls.txt")
|
|
|
57 |
|
58 |
+
block = gr.Blocks()
|
|
|
|
|
59 |
|
60 |
+
with block:
|
61 |
+
gr.Markdown("""Leaderboard""")
|
62 |
+
with gr.Tabs():
|
63 |
+
with gr.TabItem("Leaderboard"):
|
64 |
+
with gr.Row():
|
65 |
+
data = gr.outputs.Dataframe(type="pandas")
|
66 |
+
with gr.Row():
|
67 |
+
data_run = gr.Button("Refresh")
|
68 |
+
data_run.click(data_provider.fetch_data, inputs=None, outputs=data)
|
69 |
+
# running the function on page load in addition to when the button is clicked
|
70 |
+
block.load(data_provider.fetch_data, inputs=None, outputs=data)
|
71 |
|
72 |
+
block.launch()
|
|
|
|