Corey Morris commited on
Commit
f9b88e9
·
1 Parent(s): cd2dfb2

updated app.py to a sortable dataframe

Browse files
Files changed (1) hide show
  1. app.py +56 -33
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
- # Read URLs from a file, one per line
8
- with open('file_urls.txt', 'r') as f:
9
- file_urls = [line.strip() for line in f.readlines()]
10
 
11
- dataframes = []
 
12
 
13
- for url in file_urls:
14
- # Derive column names from the URLs
15
- column_name = url.split('/')[-1].split('_')[0]
16
 
17
- # Load data from URL
18
- response = requests.get(url)
19
- data = response.json()
20
 
21
- # Convert data into a DataFrame
22
- df = pd.DataFrame(data['results']).T
 
 
23
 
24
- # Rename 'acc' column to respective file names
25
- df = df.rename(columns={'acc': column_name})
26
 
27
- # Remove 'hendrycksTest-' from the index
28
- df.index = df.index.str.replace('hendrycksTest-', '')
29
 
30
- dataframes.append(df[[column_name]]) # keep only the column of interest
 
31
 
32
- # Merge the dataframes on index
33
- data = pd.concat(dataframes, axis=1)
 
 
34
 
35
- # Transpose the dataframe to swap rows and columns
36
- data = data.transpose()
37
 
38
- # Select only columns 'moral_scenarios' and 'moral_disputes'
39
- data = data[['moral_scenarios', 'moral_disputes']]
40
 
41
- def show_leaderboard():
42
- # Convert dataframe to html so that it can be displayed properly in Gradio
43
- return data.to_html()
44
 
45
- iface = gr.Interface(fn=show_leaderboard, inputs=[], outputs="html")
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Run the interface.
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()