DontPlanToEnd commited on
Commit
38d6ba2
1 Parent(s): 24ddd2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+
4
+ # Define the columns for the UGI Leaderboard
5
+ UGI_COLS = [
6
+ '#P', 'Model', 'UGI 🏆', 'Willingness👍', 'QuActivities', 'Internet', 'CrimeStats', 'Stories/Jokes', 'Pol Contro'
7
+ ]
8
+
9
+ # Load the leaderboard data from a CSV file
10
+ def load_leaderboard_data(csv_file_path):
11
+ try:
12
+ df = pd.read_csv(csv_file_path)
13
+ # Create hyperlinks in the Model column using HTML <a> tags with inline CSS for styling
14
+ df['Model'] = df.apply(lambda row: f'<a href="{row["Link"]}" target="_blank" style="color: blue; text-decoration: none;">{row["Model"]}</a>' if pd.notna(row["Link"]) else row["Model"], axis=1)
15
+ # Drop the 'Link' column as it's no longer needed
16
+ df.drop(columns=['Link'], inplace=True)
17
+ return df
18
+ except Exception as e:
19
+ print(f"Error loading CSV file: {e}")
20
+ return pd.DataFrame(columns=UGI_COLS) # Return an empty dataframe with the correct columns
21
+
22
+ # Update the leaderboard table based on the search query and parameter range filters
23
+ def update_table(df: pd.DataFrame, query: str, param_ranges: dict) -> pd.DataFrame:
24
+ filtered_df = df
25
+ if any(param_ranges.values()):
26
+ conditions = []
27
+ for param_range, checked in param_ranges.items():
28
+ if checked:
29
+ if param_range == '~1.5':
30
+ conditions.append((filtered_df['Params'] < 2.5))
31
+ elif param_range == '~3':
32
+ conditions.append(((filtered_df['Params'] >= 2.5) & (filtered_df['Params'] < 6)))
33
+ elif param_range == '~7':
34
+ conditions.append(((filtered_df['Params'] >= 6) & (filtered_df['Params'] < 9.5)))
35
+ elif param_range == '~13':
36
+ conditions.append(((filtered_df['Params'] >= 9.5) & (filtered_df['Params'] < 16)))
37
+ elif param_range == '~20':
38
+ conditions.append(((filtered_df['Params'] >= 16) & (filtered_df['Params'] < 28)))
39
+ elif param_range == '~34':
40
+ conditions.append(((filtered_df['Params'] >= 28) & (filtered_df['Params'] < 40)))
41
+ elif param_range == '~50':
42
+ conditions.append(((filtered_df['Params'] >= 40) & (filtered_df['Params'] < 60)))
43
+ elif param_range == '~70+':
44
+ conditions.append((filtered_df['Params'] >= 60))
45
+
46
+ if all(param_ranges.values()):
47
+ conditions.append(filtered_df['Params'].isna())
48
+
49
+ filtered_df = filtered_df[pd.concat(conditions, axis=1).any(axis=1)]
50
+
51
+ if query:
52
+ filtered_df = filtered_df[filtered_df.apply(lambda row: query.lower() in row.to_string().lower(), axis=1)]
53
+
54
+ return filtered_df[UGI_COLS] # Return only the columns defined in UGI_COLS
55
+
56
+ # Define the Gradio interface
57
+ demo = gr.Blocks()
58
+
59
+ with demo:
60
+ gr.Markdown("## UGI Leaderboard", elem_classes="text-lg")
61
+ with gr.Column():
62
+ with gr.Row():
63
+ search_bar = gr.Textbox(placeholder=" 🔍 Search for a model...", show_label=False)
64
+ with gr.Row():
65
+ gr.Markdown("Model sizes (in billions of parameters)", elem_classes="text-sm")
66
+ param_range_1 = gr.Checkbox(label="~1.5", value=False)
67
+ param_range_2 = gr.Checkbox(label="~3", value=False)
68
+ param_range_3 = gr.Checkbox(label="~7", value=False)
69
+ param_range_4 = gr.Checkbox(label="~13", value=False)
70
+ param_range_5 = gr.Checkbox(label="~20", value=False)
71
+ param_range_6 = gr.Checkbox(label="~34", value=False)
72
+ param_range_7 = gr.Checkbox(label="~50", value=False)
73
+ param_range_8 = gr.Checkbox(label="~70+", value=False)
74
+
75
+ # Load the initial leaderboard data
76
+ leaderboard_df = load_leaderboard_data("ugi-leaderboard-data.csv")
77
+
78
+ # Define the datatypes for each column, setting 'Model' column to 'html'
79
+ datatypes = ['html' if col == 'Model' else 'str' for col in UGI_COLS]
80
+
81
+ leaderboard_table = gr.Dataframe(
82
+ value=leaderboard_df[UGI_COLS],
83
+ datatype=datatypes, # Specify the datatype for each column
84
+ interactive=False, # Set to False to make the leaderboard non-editable
85
+ visible=True,
86
+ elem_classes="text-sm" # Increase the font size of the leaderboard data
87
+ )
88
+
89
+ # Define the search and filter functionality
90
+ inputs = [
91
+ search_bar,
92
+ param_range_1,
93
+ param_range_2,
94
+ param_range_3,
95
+ param_range_4,
96
+ param_range_5,
97
+ param_range_6,
98
+ param_range_7,
99
+ param_range_8
100
+ ]
101
+
102
+ outputs = leaderboard_table
103
+
104
+ search_bar.change(
105
+ fn=lambda query, r1, r2, r3, r4, r5, r6, r7, r8: update_table(leaderboard_df, query, {
106
+ '~1.5': r1,
107
+ '~3': r2,
108
+ '~7': r3,
109
+ '~13': r4,
110
+ '~20': r5,
111
+ '~34': r6,
112
+ '~50': r7,
113
+ '~70+': r8
114
+ }),
115
+ inputs=inputs,
116
+ outputs=outputs
117
+ )
118
+
119
+ for param_range in inputs[1:]:
120
+ param_range.change(
121
+ fn=lambda query, r1, r2, r3, r4, r5, r6, r7, r8: update_table(leaderboard_df, query, {
122
+ '~1.5': r1,
123
+ '~3': r2,
124
+ '~7': r3,
125
+ '~13': r4,
126
+ '~20': r5,
127
+ '~34': r6,
128
+ '~50': r7,
129
+ '~70+': r8
130
+ }),
131
+ inputs=inputs,
132
+ outputs=outputs
133
+ )
134
+
135
+ # Launch the Gradio app
136
+ demo.launch()