Spaces:
Runtime error
Runtime error
Meant to do last commit on run.py not app.py
Browse files
app.py
CHANGED
@@ -1,235 +1,311 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
-
from pathlib import Path
|
5 |
-
import openai
|
6 |
-
import torch
|
7 |
-
import zlib
|
8 |
-
import statistics
|
9 |
-
from torch.utils.data import DataLoader
|
10 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
11 |
-
from tqdm import tqdm
|
12 |
-
import math
|
13 |
-
import numpy as np
|
14 |
-
from datasets import load_dataset
|
15 |
-
from options import Options
|
16 |
-
from ipdb import set_trace as bp
|
17 |
-
from eval import *
|
18 |
-
from utils import evaluate_model
|
19 |
-
from analyze import analyze_data
|
20 |
-
import argparse
|
21 |
import os
|
22 |
import sys
|
23 |
-
import
|
24 |
-
import
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
else:
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
import sys
|
5 |
+
import time
|
6 |
+
import pandas as pd
|
7 |
+
from threading import Thread
|
8 |
+
|
9 |
+
# Add the path to the "src" directory of detect-pretrain-code-contamination to the sys.path
|
10 |
+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "detect-pretrain-code-contamination"))
|
11 |
+
src_dir = os.path.join(project_root, "src")
|
12 |
+
sys.path.insert(0, src_dir)
|
13 |
+
|
14 |
+
import run as evaluator # Import the run module
|
15 |
+
from src.css_html import custom_css
|
16 |
+
from src.text_content import ABOUT_TEXT, SUBMISSION_TEXT, SUBMISSION_TEXT_2
|
17 |
+
from src.envs import API, H4_TOKEN, REPO_ID
|
18 |
+
from huggingface_hub import HfApi
|
19 |
+
from src.utils import (
|
20 |
+
AutoEvalColumn,
|
21 |
+
fields,
|
22 |
+
is_model_on_hub,
|
23 |
+
make_clickable_names,
|
24 |
+
styled_error,
|
25 |
+
styled_message,
|
26 |
+
)
|
27 |
+
|
28 |
+
COLS = [c.name for c in fields(AutoEvalColumn) if not c.hidden]
|
29 |
+
TYPES = [c.type for c in fields(AutoEvalColumn) if not c.hidden]
|
30 |
+
COLS_LITE = [c.name for c in fields(AutoEvalColumn) if c.displayed_by_default and not c.hidden]
|
31 |
+
TYPES_LITE = [c.type for c in fields(AutoEvalColumn) if c.displayed_by_default and not c.hidden]
|
32 |
+
|
33 |
+
# CONFIGURATION:
|
34 |
+
ref_model = "huggyllama/llama-7b"
|
35 |
+
test_datasets = ["truthful_qa","cais/mmlu","ai2_arc","gsm8k","Rowan/hellaswag","winogrande"]
|
36 |
+
modelQueue = []
|
37 |
+
|
38 |
+
def restart_space(): #Most dumbest update function to ever exist, I'm sobbing in tears as I've tried to make gradio update the leaderboard literally any other way.
|
39 |
+
API.restart_space(repo_id=REPO_ID, token=H4_TOKEN)
|
40 |
+
|
41 |
+
|
42 |
+
def save_to_txt(model, results, model_type):
|
43 |
+
file_path = "data/code_eval_board.csv"
|
44 |
+
|
45 |
+
with open(file_path, "a") as f:
|
46 |
+
f.write(f"\n{model_type},{model}," + str(results["arc"]) + "," + str(results["hellaswag"]) + "," + str(results["mmlu"]) + "," + str(results["truthfulQA"]) + "," + str(results["winogrande"]) + "," + str(results["gsm8k"]))
|
47 |
+
f.close()
|
48 |
+
|
49 |
+
restart_space()
|
50 |
+
|
51 |
+
def run_test(model,ref_model,data):
|
52 |
+
print(f"|| TESTING {data} ||")
|
53 |
+
return evaluator.main(
|
54 |
+
target_model=f"{model}",
|
55 |
+
ref_model=f"{ref_model}",
|
56 |
+
output_dir="out",
|
57 |
+
data=f"{data}",
|
58 |
+
length=64,
|
59 |
+
key_name="input",
|
60 |
+
ratio_gen=0.4
|
61 |
+
) # Call the main function in detect-pretrain-code-contamination/src/run.py
|
62 |
+
|
63 |
+
def evaluate(model,model_type):
|
64 |
+
global ref_model
|
65 |
+
print(f"|| EVALUATING {model} ||")
|
66 |
+
results = {
|
67 |
+
"arc": run_test(model, ref_model, test_datasets[2]),
|
68 |
+
"hellaswag": run_test(model, ref_model, test_datasets[4]),
|
69 |
+
"mmlu": run_test(model, ref_model, test_datasets[1]),
|
70 |
+
"truthfulQA": run_test(model, ref_model, test_datasets[0]),
|
71 |
+
"winogrande": run_test(model, ref_model, test_datasets[5]),
|
72 |
+
"gsm8k": run_test(model, ref_model, test_datasets[3]),
|
73 |
+
"ref_model": ref_model,
|
74 |
+
}
|
75 |
+
|
76 |
+
# Save to .txt file in /Evaluations/{model}
|
77 |
+
save_to_txt(model, results, model_type)
|
78 |
+
return "\n".join([f"{k}:{results[k]}" for k in results])
|
79 |
+
|
80 |
+
def worker_thread():
|
81 |
+
global modelQueue, server
|
82 |
+
while True:
|
83 |
+
for submission in modelQueue:
|
84 |
+
evaluate(submission[0],submission[1].split(" ")[0])
|
85 |
+
modelQueue.pop(modelQueue.index(submission))
|
86 |
+
time.sleep(1)
|
87 |
+
time.sleep(1)
|
88 |
+
|
89 |
+
def queue(model,model_type):
|
90 |
+
global modelQueue
|
91 |
+
modelQueue.append([model,model_type])
|
92 |
+
print(f"QUEUE:\n{modelQueue}")
|
93 |
+
|
94 |
+
|
95 |
+
### bigcode/bigcode-models-leaderboard
|
96 |
+
def add_new_eval(
|
97 |
+
model: str,
|
98 |
+
revision: str,
|
99 |
+
precision: str,
|
100 |
+
model_type: str,
|
101 |
+
):
|
102 |
+
precision = precision
|
103 |
+
|
104 |
+
if model_type is None or model_type == "" or model_type == []:
|
105 |
+
return styled_error("Please select a model type.")
|
106 |
+
print(model_type)
|
107 |
+
# check the model actually exists before adding the eval
|
108 |
+
if revision == "":
|
109 |
+
revision = "main"
|
110 |
+
|
111 |
+
model_on_hub, error = is_model_on_hub(model, revision)
|
112 |
+
if not model_on_hub:
|
113 |
+
return styled_error(f'Model "{model}" {error}')
|
114 |
+
|
115 |
+
print("Adding new eval")
|
116 |
+
queue(model,model_type)
|
117 |
+
return styled_message("Your request has been submitted to the evaluation queue!\n")
|
118 |
+
|
119 |
+
def select_columns(df, columns):
|
120 |
+
always_here_cols = [
|
121 |
+
AutoEvalColumn.model_type_symbol.name,
|
122 |
+
AutoEvalColumn.model.name,
|
123 |
+
]
|
124 |
+
# We use COLS to maintain sorting
|
125 |
+
filtered_df = df[
|
126 |
+
always_here_cols + [c for c in COLS if c in df.columns and c in columns]
|
127 |
+
]
|
128 |
+
return filtered_df
|
129 |
+
|
130 |
+
|
131 |
+
def filter_items(df, leaderboard_table, query):
|
132 |
+
if query == "All":
|
133 |
+
return df[leaderboard_table.columns]
|
134 |
else:
|
135 |
+
query = query[0] # take only the emoji character
|
136 |
+
filtered_df = df[(df["T"] == query)]
|
137 |
+
return filtered_df[leaderboard_table.columns]
|
138 |
+
|
139 |
+
def search_table(df, leaderboard_table, query):
|
140 |
+
filtered_df = df[(df["Models"].str.contains(query, case=False))]
|
141 |
+
return filtered_df[leaderboard_table.columns]
|
142 |
+
|
143 |
+
demo = gr.Blocks(css=custom_css)
|
144 |
+
with demo:
|
145 |
+
with gr.Row():
|
146 |
+
gr.Markdown(
|
147 |
+
"""<div style="text-align: center;"><h1> π LLM Contamination Detector </h1></div>\
|
148 |
+
<br>\
|
149 |
+
<p>Inspired from the <a href="https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard">π€ Open LLM Leaderboard</a> and <a href="https://huggingface.co/spaces/bigcode/bigcode-models-leaderboard">π€ Big Code Models Leaderboard οΏ½οΏ½οΏ½</a>, we use an implementation of <a href="https://huggingface.co/papers/2310.16789">Detecting Pretraining Data from Large Language Models</a> paper found in <a href="https://github.com/swj0419/detect-pretrain-code-contamination/tree/master">this github repo</a>, to provide contamination scores for LLMs on the datasets used by Open LLM Leaderboard.\
|
150 |
+
This space should NOT be used to flag or accuse models of cheating / being contamined, instead, it should form part of a holistic assesment by the parties involved.</p>""",
|
151 |
+
elem_classes="markdown-text",
|
152 |
+
)
|
153 |
+
|
154 |
+
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
155 |
+
with gr.Column():
|
156 |
+
with gr.Tabs(elem_classes="A100-tabs") as A100_tabs:
|
157 |
+
with gr.TabItem("π Evaluations", id=0):
|
158 |
+
with gr.Column():
|
159 |
+
with gr.Accordion("β‘οΈ See filters", open=False):
|
160 |
+
shown_columns = gr.CheckboxGroup(
|
161 |
+
choices=[
|
162 |
+
c
|
163 |
+
for c in COLS
|
164 |
+
if c
|
165 |
+
not in [
|
166 |
+
AutoEvalColumn.dummy.name,
|
167 |
+
AutoEvalColumn.model.name,
|
168 |
+
AutoEvalColumn.model_type_symbol.name,
|
169 |
+
]
|
170 |
+
],
|
171 |
+
value=[
|
172 |
+
c
|
173 |
+
for c in COLS_LITE
|
174 |
+
if c
|
175 |
+
not in [
|
176 |
+
AutoEvalColumn.dummy.name,
|
177 |
+
AutoEvalColumn.model.name,
|
178 |
+
AutoEvalColumn.model_type_symbol.name,
|
179 |
+
]
|
180 |
+
],
|
181 |
+
label="",
|
182 |
+
elem_id="column-select",
|
183 |
+
interactive=True,
|
184 |
+
)
|
185 |
+
# with gr.Column(min_width=780):
|
186 |
+
with gr.Row():
|
187 |
+
search_bar = gr.Textbox(
|
188 |
+
placeholder="π Search for a model and press ENTER...",
|
189 |
+
show_label=False,
|
190 |
+
elem_id="search-bar",
|
191 |
+
)
|
192 |
+
filter_columns = gr.Radio(
|
193 |
+
label="β Filter model types",
|
194 |
+
choices=["All", "π’ Base", "πΆ Finetuned"],
|
195 |
+
value="All",
|
196 |
+
elem_id="filter-columns",
|
197 |
+
)
|
198 |
+
|
199 |
+
df = pd.read_csv("data/code_eval_board.csv")
|
200 |
+
leaderboard_df = gr.components.Dataframe(
|
201 |
+
value=df[
|
202 |
+
[
|
203 |
+
AutoEvalColumn.model_type_symbol.name,
|
204 |
+
AutoEvalColumn.model.name,
|
205 |
+
]
|
206 |
+
+ shown_columns.value
|
207 |
+
],
|
208 |
+
headers=[
|
209 |
+
AutoEvalColumn.model_type_symbol.name,
|
210 |
+
AutoEvalColumn.model.name,
|
211 |
+
]
|
212 |
+
+ shown_columns.value,
|
213 |
+
datatype=TYPES,
|
214 |
+
elem_id="leaderboard-table",
|
215 |
+
interactive=False,
|
216 |
+
)
|
217 |
+
|
218 |
+
hidden_leaderboard_df = gr.components.Dataframe(
|
219 |
+
value=df,
|
220 |
+
headers=COLS,
|
221 |
+
datatype=["str" for _ in range(len(COLS))],
|
222 |
+
visible=False,
|
223 |
+
)
|
224 |
+
|
225 |
+
search_bar.submit(
|
226 |
+
search_table,
|
227 |
+
[hidden_leaderboard_df, leaderboard_df, search_bar],
|
228 |
+
leaderboard_df,
|
229 |
+
)
|
230 |
+
|
231 |
+
filter_columns.change(
|
232 |
+
filter_items,
|
233 |
+
[hidden_leaderboard_df, leaderboard_df, filter_columns],
|
234 |
+
leaderboard_df,
|
235 |
+
)
|
236 |
+
|
237 |
+
shown_columns.change(
|
238 |
+
select_columns,
|
239 |
+
[hidden_leaderboard_df, shown_columns],
|
240 |
+
leaderboard_df,
|
241 |
+
)
|
242 |
+
|
243 |
+
gr.Markdown(
|
244 |
+
"""
|
245 |
+
**Notes:**
|
246 |
+
- The Huggingface team is working on their own implementation of this paper as a space, I'll be leaving this space up until that's available.
|
247 |
+
- Some scores may not be entirely accurate according to the paper cited as I still work out the kinks and innacuracies of this implementation.
|
248 |
+
- For any issues, questions, or comments either open a discussion in this space's community tab or message me directly to my discord: yeyito777.
|
249 |
+
- Make sure to check the pinned discussion in this space's community tab for implementation details I'm not 100% about.
|
250 |
+
""",
|
251 |
+
elem_classes="markdown-text",
|
252 |
+
)
|
253 |
+
|
254 |
+
with gr.TabItem("π About", id=2):
|
255 |
+
gr.Markdown(ABOUT_TEXT, elem_classes="markdown-text")
|
256 |
+
with gr.TabItem("π οΈ Submit models", id=3):
|
257 |
+
gr.Markdown(SUBMISSION_TEXT)
|
258 |
+
gr.Markdown(
|
259 |
+
"## π€ Submit a model here:", elem_classes="markdown-text"
|
260 |
+
)
|
261 |
+
with gr.Column():
|
262 |
+
with gr.Row():
|
263 |
+
model_name = gr.Textbox(label="Model name")
|
264 |
+
revision_name = gr.Textbox(
|
265 |
+
label="revision", placeholder="main"
|
266 |
+
)
|
267 |
+
with gr.Row():
|
268 |
+
precision = gr.Dropdown(
|
269 |
+
choices=[
|
270 |
+
"float16",
|
271 |
+
"bfloat16",
|
272 |
+
"8bit",
|
273 |
+
"4bit",
|
274 |
+
],
|
275 |
+
label="Precision",
|
276 |
+
multiselect=False,
|
277 |
+
value="float16",
|
278 |
+
interactive=True,
|
279 |
+
)
|
280 |
+
model_type = gr.Dropdown(
|
281 |
+
choices=["π’ base", "πΆ instruction-tuned"],
|
282 |
+
label="Model type",
|
283 |
+
multiselect=False,
|
284 |
+
value=None,
|
285 |
+
interactive=True,
|
286 |
+
)
|
287 |
+
submit_button = gr.Button("Submit Eval")
|
288 |
+
submission_result = gr.Markdown()
|
289 |
+
submit_button.click(
|
290 |
+
add_new_eval,
|
291 |
+
inputs=[model_name, revision_name, precision, model_type],
|
292 |
+
outputs=[submission_result],
|
293 |
+
)
|
294 |
+
gr.Markdown(SUBMISSION_TEXT_2)
|
295 |
+
|
296 |
+
thread = Thread(target=worker_thread)
|
297 |
+
thread.start()
|
298 |
+
demo.launch(share=True)
|
299 |
+
|
300 |
+
# Some worries:
|
301 |
+
# 1. Am I testing things correctly in eval.py, following the template format?
|
302 |
+
|
303 |
+
# 2. Am I choosing the correct splits in run.py? The higherarchy I use is: test > val > train
|
304 |
+
# (As in: if test exists, I go with that, then validation, then default)
|
305 |
+
|
306 |
+
# 3. I decided to go with winogrande_debiased instead of winogrande_l arbitrarily.
|
307 |
+
# (Not sure which one open llm leaderboard uses, or what is the standard)
|
308 |
+
|
309 |
+
# 4. I'm unsure why in eval.py we append the output at the end of the input.
|
310 |
|
311 |
+
# 5. Currently I'm using huggyllama/llama-7b as ref_model, should I switch to llama2-7B? Maybe Mistral-7B?
|