shunshao commited on
Commit
32dbb08
Β·
verified Β·
1 Parent(s): 0f76534

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -0
app.py CHANGED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gzip
3
+ import gradio as gr
4
+ from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
5
+ import pandas as pd
6
+ from apscheduler.schedulers.background import BackgroundScheduler
7
+ from huggingface_hub import snapshot_download
8
+ from io import StringIO
9
+
10
+ from src.about import (
11
+ CITATION_BUTTON_LABEL,
12
+ CITATION_BUTTON_TEXT,
13
+ EVALUATION_QUEUE_TEXT,
14
+ INTRODUCTION_TEXT,
15
+ LLM_BENCHMARKS_TEXT,
16
+ TITLE,
17
+ )
18
+ from src.display.css_html_js import custom_css
19
+ from src.display.utils import (
20
+ BENCHMARK_COLS,
21
+ BENCHMARK_COLS_MULTIMODAL,
22
+ BENCHMARK_COLS_MIB,
23
+ COLS,
24
+ COLS_MIB,
25
+ COLS_MULTIMODAL,
26
+ EVAL_COLS,
27
+ EVAL_TYPES,
28
+ AutoEvalColumn,
29
+ AutoEvalColumn_mib,
30
+ fields,
31
+ )
32
+ from src.envs import API, EVAL_REQUESTS_PATH, EVAL_RESULTS_PATH, QUEUE_REPO, REPO_ID, RESULTS_REPO, TOKEN, RESULTS_REPO_MIB_SUBGRAPH, EVAL_RESULTS_MIB_SUBGRAPH_PATH
33
+ from src.populate import get_evaluation_queue_df, get_leaderboard_df, get_leaderboard_df_mib
34
+ from src.submission.submit import add_new_eval
35
+
36
+ print("restart_space ")
37
+
38
+ def restart_space():
39
+ API.restart_space(repo_id=REPO_ID)
40
+
41
+ print("end restart_space")
42
+
43
+
44
+ print("Space initialisation ")
45
+ ### Space initialisation
46
+ print("EVAL_REQUESTS_PATH")
47
+ try:
48
+ print(EVAL_REQUESTS_PATH)
49
+ snapshot_download(
50
+ repo_id=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
51
+ )
52
+ except Exception:
53
+ restart_space()
54
+
55
+ print("EVAL_RESULTS_PATH")
56
+ try:
57
+ print(EVAL_RESULTS_PATH)
58
+ snapshot_download(
59
+ repo_id=RESULTS_REPO, local_dir=EVAL_RESULTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
60
+ )
61
+ except Exception:
62
+ restart_space()
63
+
64
+ print("RESULTS_REPO_MIB_SUBGRAPH")
65
+ try:
66
+ print(RESULTS_REPO_MIB_SUBGRAPH)
67
+ snapshot_download(
68
+ repo_id=RESULTS_REPO_MIB_SUBGRAPH, local_dir=EVAL_RESULTS_MIB_SUBGRAPH_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
69
+ )
70
+ except Exception:
71
+ restart_space()
72
+
73
+ print("End Space initialisation ")
74
+
75
+
76
+ LEADERBOARD_DF_MIB_SUBGRAPH = get_leaderboard_df_mib(EVAL_RESULTS_MIB_SUBGRAPH_PATH, EVAL_REQUESTS_PATH, COLS_MIB, BENCHMARK_COLS_MIB)
77
+
78
+ # LEADERBOARD_DF = get_leaderboard_df(EVAL_RESULTS_PATH, EVAL_REQUESTS_PATH, COLS, BENCHMARK_COLS)
79
+ # LEADERBOARD_DF_MULTIMODAL = get_leaderboard_df(EVAL_RESULTS_PATH, EVAL_REQUESTS_PATH, COLS_MULTIMODAL, BENCHMARK_COLS_MULTIMODAL)
80
+
81
+ (
82
+ finished_eval_queue_df,
83
+ running_eval_queue_df,
84
+ pending_eval_queue_df,
85
+ ) = get_evaluation_queue_df(EVAL_REQUESTS_PATH, EVAL_COLS)
86
+
87
+
88
+ def init_leaderboard_mib(dataframe, track):
89
+ print(f"init_leaderboard_mib: dataframe head before loc is {dataframe.head()}\n")
90
+
91
+ if dataframe is None or dataframe.empty:
92
+ raise ValueError("Leaderboard DataFrame is empty or None.")
93
+
94
+ # filter for correct track
95
+ # dataframe = dataframe.loc[dataframe["Track"] == track]
96
+
97
+ print(f"init_leaderboard_mib: dataframe head after loc is {dataframe.head()}\n")
98
+
99
+ return Leaderboard(
100
+ value=dataframe,
101
+ datatype=[c.type for c in fields(AutoEvalColumn_mib)],
102
+ select_columns=SelectColumns(
103
+ default_selection=[c.name for c in fields(AutoEvalColumn_mib) if c.displayed_by_default],
104
+ cant_deselect=[c.name for c in fields(AutoEvalColumn_mib) if c.never_hidden],
105
+ label="Select Columns to Display:",
106
+ ),
107
+ search_columns=["Method"], # Changed from AutoEvalColumn_mib.model.name to "Method"
108
+ hide_columns=[c.name for c in fields(AutoEvalColumn_mib) if c.hidden],
109
+ bool_checkboxgroup_label="Hide models",
110
+ interactive=False,
111
+ )
112
+
113
+ def init_leaderboard(dataframe, track):
114
+ if dataframe is None or dataframe.empty:
115
+ raise ValueError("Leaderboard DataFrame is empty or None.")
116
+ # filter for correct track
117
+ dataframe = dataframe.loc[dataframe["Track"] == track]
118
+
119
+ # print(f"\n\n\n dataframe is {dataframe}\n\n\n")
120
+
121
+ return Leaderboard(
122
+ value=dataframe,
123
+ datatype=[c.type for c in fields(AutoEvalColumn)],
124
+ select_columns=SelectColumns(
125
+ default_selection=[c.name for c in fields(AutoEvalColumn) if c.displayed_by_default],
126
+ cant_deselect=[c.name for c in fields(AutoEvalColumn) if c.never_hidden],
127
+ label="Select Columns to Display:",
128
+ ),
129
+ search_columns=[AutoEvalColumn.model.name],
130
+ hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden],
131
+ bool_checkboxgroup_label="Hide models",
132
+ interactive=False,
133
+ )
134
+
135
+ def process_json(temp_file):
136
+ if temp_file is None:
137
+ return {}
138
+
139
+ # Handle file upload
140
+ try:
141
+ file_path = temp_file.name
142
+ if file_path.endswith('.gz'):
143
+ with gzip.open(file_path, 'rt') as f:
144
+ data = json.load(f)
145
+ else:
146
+ with open(file_path, 'r') as f:
147
+ data = json.load(f)
148
+ except Exception as e:
149
+ raise gr.Error(f"Error processing file: {str(e)}")
150
+
151
+ gr.Markdown("Upload successful!")
152
+ return data
153
+
154
+
155
+ demo = gr.Blocks(css=custom_css)
156
+ with demo:
157
+ gr.HTML(TITLE)
158
+ gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
159
+
160
+ with gr.Tabs(elem_classes="tab-buttons") as tabs:
161
+ # with gr.TabItem("Strict", elem_id="strict-benchmark-tab-table", id=0):
162
+ # leaderboard = init_leaderboard(LEADERBOARD_DF, "strict")
163
+ # with gr.TabItem("Strict-small", elem_id="strict-small-benchmark-tab-table", id=1):
164
+ # leaderboard = init_leaderboard(LEADERBOARD_DF, "strict-small")
165
+ # with gr.TabItem("Multimodal", elem_id="multimodal-benchmark-tab-table", id=2):
166
+ # leaderboard = init_leaderboard(LEADERBOARD_DF_MULTIMODAL, "multimodal")
167
+
168
+ # with gr.TabItem("πŸ“ About", elem_id="llm-benchmark-tab-table", id=4):
169
+ # gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
170
+
171
+ # with gr.TabItem("πŸ‘Ά Submit", elem_id="llm-benchmark-tab-table", id=5):
172
+ # with gr.Column():
173
+ # with gr.Row():
174
+ # gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
175
+
176
+ with gr.TabItem("Subgraph", elem_id="subgraph", id=0):
177
+ leaderboard = init_leaderboard_mib(LEADERBOARD_DF_MIB_SUBGRAPH, "Subgraph")
178
+ # leaderboard = init_leaderboard_mib(LEADERBOARD_DF, "mib")
179
+
180
+ with gr.TabItem("Causal Graph", elem_id="causalgraph", id=1):
181
+ leaderboard = init_leaderboard_mib(LEADERBOARD_DF_MIB_SUBGRAPH, "Causal Graph")
182
+
183
+ # with gr.Row():
184
+ # with gr.Accordion("πŸ“™ Citation", open=False):
185
+ # citation_button = gr.Textbox(
186
+ # value=CITATION_BUTTON_TEXT,
187
+ # label=CITATION_BUTTON_LABEL,
188
+ # lines=20,
189
+ # elem_id="citation-button",
190
+ # show_copy_button=True,
191
+ # )
192
+
193
+ scheduler = BackgroundScheduler()
194
+ scheduler.add_job(restart_space, "interval", seconds=1800)
195
+ scheduler.start()
196
+ demo.launch(share=True, ssr_mode=False)