awacke1 commited on
Commit
c57e896
·
verified ·
1 Parent(s): f14ef95

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +227 -0
app.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import MEGABenchEvalDataLoader
3
+ import os
4
+ from constants import *
5
+ from urllib.parse import quote
6
+
7
+ # ------------------------------------------------------------------------------
8
+ # Load CSS files
9
+ # ------------------------------------------------------------------------------
10
+ current_dir = os.path.dirname(os.path.abspath(__file__))
11
+ base_css_file = os.path.join(current_dir, "static", "css", "style.css")
12
+ table_css_file = os.path.join(current_dir, "static", "css", "table.css")
13
+
14
+ with open(base_css_file, "r") as f:
15
+ base_css = f.read()
16
+ with open(table_css_file, "r") as f:
17
+ table_css = f.read()
18
+
19
+ # ------------------------------------------------------------------------------
20
+ # Initialize data loaders
21
+ # ------------------------------------------------------------------------------
22
+ default_loader = MEGABenchEvalDataLoader("./static/eval_results/Default")
23
+ si_loader = MEGABenchEvalDataLoader("./static/eval_results/SI")
24
+
25
+ # ------------------------------------------------------------------------------
26
+ # Helper Functions
27
+ # ------------------------------------------------------------------------------
28
+
29
+ def generate_search_links(model_name):
30
+ """For a given model name, generate a set of search links as HTML."""
31
+ search_urls = {
32
+ "📚📖ArXiv": lambda k: f"https://arxiv.org/search/?query={quote(k)}&searchtype=all",
33
+ "🔮Google": lambda k: f"https://www.google.com/search?q={quote(k)}",
34
+ "📺Youtube": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
35
+ "🔭Bing": lambda k: f"https://www.bing.com/search?q={quote(k)}",
36
+ "💡Truth": lambda k: f"https://truthsocial.com/search?q={quote(k)}",
37
+ "📱X": lambda k: f"https://twitter.com/search?q={quote(k)}",
38
+ }
39
+ # Build a set of inline HTML links (each opens in a new tab)
40
+ links = " ".join(
41
+ [f'<a href="{url(model_name)}" target="_blank">{emoji}</a>' for emoji, url in search_urls.items()]
42
+ )
43
+ return links
44
+
45
+ def add_search_links_to_table(headers, data):
46
+ """
47
+ Append a "Search Links" column to the table.
48
+ (Assumes that each row’s column index 1 holds the model name.)
49
+ """
50
+ new_headers = headers.copy()
51
+ new_headers.append("Search Links")
52
+ new_data = []
53
+ for row in data:
54
+ new_row = row.copy()
55
+ # Assume the model name is in the second column (index 1)
56
+ model_name = new_row[1] if len(new_row) > 1 else ""
57
+ new_row.append(generate_search_links(model_name))
58
+ new_data.append(new_row)
59
+ return new_headers, new_data
60
+
61
+ def clean_choice(choice):
62
+ """
63
+ Remove a leading emoji and space (if present) from a choice string.
64
+ For example, "📊 Default" becomes "Default".
65
+ """
66
+ parts = choice.split(" ", 1)
67
+ return parts[1] if len(parts) > 1 else choice
68
+
69
+ def update_table_and_caption(table_type, super_group, model_group):
70
+ """
71
+ Called when any selector changes. Cleans the emoji‐prefixed values, loads new data,
72
+ appends a Search Links column, and returns a new Dataframe component (with a smaller max height),
73
+ an updated caption, and the CSS style.
74
+ """
75
+ table_type_clean = clean_choice(table_type)
76
+ super_group_clean = clean_choice(super_group)
77
+ model_group_clean = clean_choice(model_group)
78
+
79
+ if table_type_clean == "Default":
80
+ headers, data = default_loader.get_leaderboard_data(super_group_clean, model_group_clean)
81
+ caption = default_caption
82
+ else: # "Single Image"
83
+ headers, data = si_loader.get_leaderboard_data(super_group_clean, model_group_clean)
84
+ caption = single_image_caption
85
+
86
+ # Append search links column to the table data
87
+ headers, data = add_search_links_to_table(headers, data)
88
+ n = len(headers)
89
+ # Assume first column is a number, second (model name) is HTML, the intermediate ones are numbers, and the last (links) is HTML
90
+ datatype = ["number", "html"] + ["number"] * (n - 3) + ["html"]
91
+ # Adjust column widths as needed (example: first two fixed, last a bit wider)
92
+ column_widths = ["100px", "240px"] + ["160px"] * (n - 3) + ["210px"]
93
+
94
+ dataframe_component = gr.Dataframe(
95
+ value=data,
96
+ headers=headers,
97
+ datatype=datatype,
98
+ interactive=False,
99
+ column_widths=column_widths,
100
+ max_height=600, # smaller height to show the full table in a compact area
101
+ elem_classes="custom-dataframe"
102
+ )
103
+
104
+ return [dataframe_component, caption, f"<style>{base_css}\n{table_css}</style>"]
105
+
106
+ def update_selectors(table_type):
107
+ """
108
+ When the table selector changes, update the other radio choices.
109
+ (Also adds an emoji prefix to each choice.)
110
+ """
111
+ table_type_clean = clean_choice(table_type)
112
+ loader = default_loader if table_type_clean == "Default" else si_loader
113
+ super_group_choices = [f"🔍 {group}" for group in list(loader.SUPER_GROUPS.keys())]
114
+ model_group_choices = [f"🤖 {group}" for group in list(loader.MODEL_GROUPS.keys())]
115
+ return [super_group_choices, model_group_choices]
116
+
117
+ # ------------------------------------------------------------------------------
118
+ # Build Gradio App Layout
119
+ # ------------------------------------------------------------------------------
120
+ with gr.Blocks() as block:
121
+ # Add CSS via an invisible HTML component
122
+ css_style = gr.HTML(f"<style>{base_css}\n{table_css}</style>", visible=False)
123
+
124
+ # NOTE: The original top-level introduction markdown has been removed here.
125
+ # It is now placed at the bottom of the MEGA-Bench tab in a collapsed Accordion.
126
+ with gr.Tabs(elem_classes="tab-buttons") as tabs:
127
+
128
+ # -------------------- Tab 1: MEGA-Bench --------------------
129
+ with gr.TabItem("📊 MEGA-Bench", elem_id="qa-tab-table1", id=1):
130
+ # A Citation accordion (collapsed by default)
131
+ with gr.Row():
132
+ with gr.Accordion("Citation", open=False):
133
+ citation_button = gr.Textbox(
134
+ value=CITATION_BUTTON_TEXT,
135
+ label=CITATION_BUTTON_LABEL,
136
+ elem_id="citation-button",
137
+ lines=10,
138
+ )
139
+ gr.Markdown(TABLE_INTRODUCTION)
140
+
141
+ # ---- Top-left “button‐bar” of radio selectors with emoji labels ----
142
+ with gr.Row():
143
+ table_selector = gr.Radio(
144
+ choices=["📊 Default", "🖼️ Single Image"],
145
+ label="Select table to display",
146
+ value="📊 Default"
147
+ )
148
+ with gr.Row():
149
+ super_group_selector = gr.Radio(
150
+ choices=[f"🔍 {group}" for group in list(default_loader.SUPER_GROUPS.keys())],
151
+ label="Select a dimension",
152
+ value=f"🔍 {list(default_loader.SUPER_GROUPS.keys())[0]}"
153
+ )
154
+ model_group_selector = gr.Radio(
155
+ choices=[f"🤖 {group}" for group in list(BASE_MODEL_GROUPS.keys())],
156
+ label="Select a model group",
157
+ value="🤖 All"
158
+ )
159
+
160
+ # A caption component for the table
161
+ caption_component = gr.Markdown(
162
+ value=default_caption,
163
+ elem_classes="table-caption",
164
+ latex_delimiters=[{"left": "$", "right": "$", "display": False}],
165
+ )
166
+
167
+ # ---- Initial Dataframe (with search links appended) ----
168
+ initial_headers, initial_data = default_loader.get_leaderboard_data(
169
+ list(default_loader.SUPER_GROUPS.keys())[0], "All"
170
+ )
171
+ initial_headers, initial_data = add_search_links_to_table(initial_headers, initial_data)
172
+ n = len(initial_headers)
173
+ initial_datatype = ["number", "html"] + ["number"] * (n - 3) + ["html"]
174
+ initial_column_widths = ["100px", "240px"] + ["160px"] * (n - 3) + ["210px"]
175
+
176
+ data_component = gr.Dataframe(
177
+ value=initial_data,
178
+ headers=initial_headers,
179
+ datatype=initial_datatype,
180
+ interactive=False,
181
+ elem_classes="custom-dataframe",
182
+ max_height=600,
183
+ column_widths=initial_column_widths
184
+ )
185
+
186
+ # ---- Controls to update the table ----
187
+ refresh_button = gr.Button("Refresh")
188
+ refresh_button.click(
189
+ fn=update_table_and_caption,
190
+ inputs=[table_selector, super_group_selector, model_group_selector],
191
+ outputs=[data_component, caption_component, css_style]
192
+ )
193
+ super_group_selector.change(
194
+ fn=update_table_and_caption,
195
+ inputs=[table_selector, super_group_selector, model_group_selector],
196
+ outputs=[data_component, caption_component, css_style]
197
+ )
198
+ model_group_selector.change(
199
+ fn=update_table_and_caption,
200
+ inputs=[table_selector, super_group_selector, model_group_selector],
201
+ outputs=[data_component, caption_component, css_style]
202
+ )
203
+ table_selector.change(
204
+ fn=update_selectors,
205
+ inputs=[table_selector],
206
+ outputs=[super_group_selector, model_group_selector]
207
+ ).then(
208
+ fn=update_table_and_caption,
209
+ inputs=[table_selector, super_group_selector, model_group_selector],
210
+ outputs=[data_component, caption_component, css_style]
211
+ )
212
+
213
+ # ---- Move the introductory text to a collapsed accordion at the bottom ----
214
+ with gr.Accordion("Introduction", open=False):
215
+ gr.Markdown(LEADERBOARD_INTRODUCTION)
216
+
217
+ # -------------------- Tab 2: Data Information --------------------
218
+ with gr.TabItem("📝 Data Information", elem_id="qa-tab-table2", id=2):
219
+ gr.Markdown(DATA_INFO, elem_classes="markdown-text")
220
+
221
+ # -------------------- Tab 3: Submit --------------------
222
+ with gr.TabItem("🚀 Submit", elem_id="submit-tab", id=3):
223
+ with gr.Row():
224
+ gr.Markdown(SUBMIT_INTRODUCTION, elem_classes="markdown-text")
225
+
226
+ if __name__ == "__main__":
227
+ block.launch(share=True)