Spaces:
Runtime error
Runtime error
- README.md +16 -16
- app.py +205 -81
- main_backend.py +127 -0
- requirements.txt +15 -14
- scripts/create_request_file.py +110 -0
- tests/test_evaluate_model.py +87 -0
- tests/test_evaluator.py +59 -0
- tests/test_main_backend.py +54 -0
- tests/test_summary_generator.py +68 -0
README.md
CHANGED
@@ -1,20 +1,29 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 🥇
|
4 |
colorFrom: green
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
|
|
7 |
app_file: app.py
|
8 |
pinned: true
|
9 |
-
license:
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
# Start the configuration
|
13 |
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
{
|
19 |
"config": {
|
20 |
"model_dtype": "torch.float16", # or torch.bfloat16 or 8bit or 4bit
|
@@ -32,13 +41,4 @@ Results files should have the following format and be stored as json files:
|
|
32 |
}
|
33 |
```
|
34 |
|
35 |
-
Request files are created automatically by this tool.
|
36 |
-
|
37 |
-
If you encounter problem on the space, don't hesitate to restart it to remove the create eval-queue, eval-queue-bk, eval-results and eval-results-bk created folder.
|
38 |
-
|
39 |
-
# Code logic for more complex edits
|
40 |
-
|
41 |
-
You'll find
|
42 |
-
- the main table' columns names and properties in `src/display/utils.py`
|
43 |
-
- the logic to read all results and request files, then convert them in dataframe lines, in `src/leaderboard/read_evals.py`, and `src/populate.py`
|
44 |
-
- teh logic to allow or filter submissions in `src/submission/submit.py` and `src/submission/check_validity.py`
|
|
|
1 |
---
|
2 |
+
title: HHEM Leaderboard
|
3 |
emoji: 🥇
|
4 |
colorFrom: green
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.37.1
|
8 |
app_file: app.py
|
9 |
pinned: true
|
10 |
+
license: apache-2.0
|
11 |
+
tags:
|
12 |
+
- leaderboard
|
13 |
---
|
14 |
|
|
|
15 |
|
16 |
+
python>3.10
|
17 |
+
pip spacy
|
18 |
+
python -m spacy download en_core_web_sm
|
19 |
+
pip install google.generativeai
|
20 |
|
21 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
22 |
+
|
23 |
+
Most of the variables to change for a default leaderboard are in env (replace the path for your leaderboard) and src/display/about.
|
24 |
+
|
25 |
+
Results files should have the following format:
|
26 |
+
```
|
27 |
{
|
28 |
"config": {
|
29 |
"model_dtype": "torch.float16", # or torch.bfloat16 or 8bit or 4bit
|
|
|
41 |
}
|
42 |
```
|
43 |
|
44 |
+
Request files are created automatically by this tool.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -1,110 +1,234 @@
|
|
1 |
import gradio as gr
|
2 |
-
from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
|
3 |
import pandas as pd
|
4 |
from apscheduler.schedulers.background import BackgroundScheduler
|
5 |
from huggingface_hub import snapshot_download
|
6 |
|
7 |
-
|
8 |
-
CITATION_BUTTON_LABEL,
|
9 |
-
CITATION_BUTTON_TEXT,
|
10 |
-
EVALUATION_QUEUE_TEXT,
|
11 |
-
INTRODUCTION_TEXT,
|
12 |
-
LLM_BENCHMARKS_TEXT,
|
13 |
-
TITLE,
|
14 |
-
)
|
15 |
from src.display.css_html_js import custom_css
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
EVAL_TYPES,
|
21 |
-
AutoEvalColumn,
|
22 |
-
ModelType,
|
23 |
-
fields,
|
24 |
-
WeightType,
|
25 |
-
Precision
|
26 |
-
)
|
27 |
-
from src.envs import API, EVAL_REQUESTS_PATH, EVAL_RESULTS_PATH, QUEUE_REPO, REPO_ID, RESULTS_REPO, TOKEN
|
28 |
-
from src.populate import get_evaluation_queue_df, get_leaderboard_df
|
29 |
-
from src.submission.submit import add_new_eval
|
30 |
|
31 |
|
32 |
def restart_space():
|
33 |
-
API.restart_space(repo_id=REPO_ID)
|
34 |
|
35 |
-
### Space initialisation
|
36 |
try:
|
37 |
-
print(EVAL_REQUESTS_PATH)
|
38 |
snapshot_download(
|
39 |
-
repo_id=QUEUE_REPO, local_dir=EVAL_REQUESTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30
|
40 |
)
|
41 |
except Exception:
|
42 |
restart_space()
|
43 |
try:
|
44 |
-
print(EVAL_RESULTS_PATH)
|
45 |
snapshot_download(
|
46 |
-
repo_id=RESULTS_REPO, local_dir=EVAL_RESULTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30
|
47 |
)
|
48 |
except Exception:
|
49 |
restart_space()
|
50 |
|
51 |
-
|
52 |
-
|
53 |
|
54 |
(
|
55 |
finished_eval_queue_df,
|
56 |
running_eval_queue_df,
|
57 |
pending_eval_queue_df,
|
58 |
-
) = get_evaluation_queue_df(EVAL_REQUESTS_PATH, EVAL_COLS)
|
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 |
demo = gr.Blocks(css=custom_css)
|
93 |
with demo:
|
94 |
-
gr.HTML(TITLE)
|
95 |
-
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
96 |
|
97 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
98 |
with gr.TabItem("🏅 LLM Benchmark", elem_id="llm-benchmark-tab-table", id=0):
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
with gr.TabItem("📝 About", elem_id="llm-benchmark-tab-table", id=2):
|
102 |
-
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
103 |
|
104 |
with gr.TabItem("🚀 Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
|
105 |
with gr.Column():
|
106 |
with gr.Row():
|
107 |
-
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
108 |
|
109 |
with gr.Column():
|
110 |
with gr.Accordion(
|
@@ -114,8 +238,8 @@ with demo:
|
|
114 |
with gr.Row():
|
115 |
finished_eval_table = gr.components.Dataframe(
|
116 |
value=finished_eval_queue_df,
|
117 |
-
headers=EVAL_COLS,
|
118 |
-
datatype=EVAL_TYPES,
|
119 |
row_count=5,
|
120 |
)
|
121 |
with gr.Accordion(
|
@@ -125,8 +249,8 @@ with demo:
|
|
125 |
with gr.Row():
|
126 |
running_eval_table = gr.components.Dataframe(
|
127 |
value=running_eval_queue_df,
|
128 |
-
headers=EVAL_COLS,
|
129 |
-
datatype=EVAL_TYPES,
|
130 |
row_count=5,
|
131 |
)
|
132 |
|
@@ -137,8 +261,8 @@ with demo:
|
|
137 |
with gr.Row():
|
138 |
pending_eval_table = gr.components.Dataframe(
|
139 |
value=pending_eval_queue_df,
|
140 |
-
headers=EVAL_COLS,
|
141 |
-
datatype=EVAL_TYPES,
|
142 |
row_count=5,
|
143 |
)
|
144 |
with gr.Row():
|
@@ -149,7 +273,7 @@ with demo:
|
|
149 |
model_name_textbox = gr.Textbox(label="Model name")
|
150 |
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
151 |
model_type = gr.Dropdown(
|
152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
153 |
label="Model type",
|
154 |
multiselect=False,
|
155 |
value=None,
|
@@ -158,14 +282,14 @@ with demo:
|
|
158 |
|
159 |
with gr.Column():
|
160 |
precision = gr.Dropdown(
|
161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
162 |
label="Precision",
|
163 |
multiselect=False,
|
164 |
value="float16",
|
165 |
interactive=True,
|
166 |
)
|
167 |
weight_type = gr.Dropdown(
|
168 |
-
choices=[i.value.name for i in WeightType],
|
169 |
label="Weights type",
|
170 |
multiselect=False,
|
171 |
value="Original",
|
@@ -176,7 +300,7 @@ with demo:
|
|
176 |
submit_button = gr.Button("Submit Eval")
|
177 |
submission_result = gr.Markdown()
|
178 |
submit_button.click(
|
179 |
-
add_new_eval,
|
180 |
[
|
181 |
model_name_textbox,
|
182 |
base_model_name_textbox,
|
@@ -191,8 +315,8 @@ with demo:
|
|
191 |
with gr.Row():
|
192 |
with gr.Accordion("📙 Citation", open=False):
|
193 |
citation_button = gr.Textbox(
|
194 |
-
value=CITATION_BUTTON_TEXT,
|
195 |
-
label=CITATION_BUTTON_LABEL,
|
196 |
lines=20,
|
197 |
elem_id="citation-button",
|
198 |
show_copy_button=True,
|
@@ -201,4 +325,4 @@ with demo:
|
|
201 |
scheduler = BackgroundScheduler()
|
202 |
scheduler.add_job(restart_space, "interval", seconds=1800)
|
203 |
scheduler.start()
|
204 |
-
demo.queue(default_concurrency_limit=40).launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
from apscheduler.schedulers.background import BackgroundScheduler
|
4 |
from huggingface_hub import snapshot_download
|
5 |
|
6 |
+
import src.display.about as about
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from src.display.css_html_js import custom_css
|
8 |
+
import src.display.utils as utils
|
9 |
+
import src.envs as envs
|
10 |
+
import src.populate as populate
|
11 |
+
import src.submission.submit as submit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
def restart_space():
|
15 |
+
envs.API.restart_space(repo_id=envs.REPO_ID, token=envs.TOKEN)
|
16 |
|
|
|
17 |
try:
|
18 |
+
print(envs.EVAL_REQUESTS_PATH)
|
19 |
snapshot_download(
|
20 |
+
repo_id=envs.QUEUE_REPO, local_dir=envs.EVAL_REQUESTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30
|
21 |
)
|
22 |
except Exception:
|
23 |
restart_space()
|
24 |
try:
|
25 |
+
print(envs.EVAL_RESULTS_PATH)
|
26 |
snapshot_download(
|
27 |
+
repo_id=envs.RESULTS_REPO, local_dir=envs.EVAL_RESULTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30
|
28 |
)
|
29 |
except Exception:
|
30 |
restart_space()
|
31 |
|
32 |
+
raw_data, original_df = populate.get_leaderboard_df(envs.EVAL_RESULTS_PATH, envs.EVAL_REQUESTS_PATH, utils.COLS, utils.BENCHMARK_COLS)
|
33 |
+
leaderboard_df = original_df.copy()
|
34 |
|
35 |
(
|
36 |
finished_eval_queue_df,
|
37 |
running_eval_queue_df,
|
38 |
pending_eval_queue_df,
|
39 |
+
) = populate.get_evaluation_queue_df(envs.EVAL_REQUESTS_PATH, utils.EVAL_COLS)
|
40 |
+
|
41 |
+
|
42 |
+
# Searching and filtering
|
43 |
+
def update_table(
|
44 |
+
hidden_df: pd.DataFrame,
|
45 |
+
columns: list,
|
46 |
+
type_query: list,
|
47 |
+
precision_query: str,
|
48 |
+
size_query: list,
|
49 |
+
show_deleted: bool,
|
50 |
+
query: str,
|
51 |
+
):
|
52 |
+
filtered_df = filter_models(hidden_df, type_query, size_query, precision_query, show_deleted)
|
53 |
+
filtered_df = filter_queries(query, filtered_df)
|
54 |
+
df = select_columns(filtered_df, columns)
|
55 |
+
return df
|
56 |
+
|
57 |
+
|
58 |
+
def search_table(df: pd.DataFrame, query: str) -> pd.DataFrame:
|
59 |
+
return df[(df[utils.AutoEvalColumn.dummy.name].str.contains(query, case=False))]
|
60 |
+
|
61 |
+
|
62 |
+
def select_columns(df: pd.DataFrame, columns: list) -> pd.DataFrame:
|
63 |
+
always_here_cols = [
|
64 |
+
utils.AutoEvalColumn.model_type_symbol.name,
|
65 |
+
utils.AutoEvalColumn.model.name,
|
66 |
+
]
|
67 |
+
# We use COLS to maintain sorting
|
68 |
+
filtered_df = df[
|
69 |
+
always_here_cols + [c for c in utils.COLS if c in df.columns and c in columns] + [utils.AutoEvalColumn.dummy.name]
|
70 |
+
]
|
71 |
+
return filtered_df
|
72 |
+
|
73 |
+
|
74 |
+
def filter_queries(query: str, filtered_df: pd.DataFrame) -> pd.DataFrame:
|
75 |
+
final_df = []
|
76 |
+
if query != "":
|
77 |
+
queries = [q.strip() for q in query.split(";")]
|
78 |
+
for _q in queries:
|
79 |
+
_q = _q.strip()
|
80 |
+
if _q != "":
|
81 |
+
temp_filtered_df = search_table(filtered_df, _q)
|
82 |
+
if len(temp_filtered_df) > 0:
|
83 |
+
final_df.append(temp_filtered_df)
|
84 |
+
if len(final_df) > 0:
|
85 |
+
filtered_df = pd.concat(final_df)
|
86 |
+
filtered_df = filtered_df.drop_duplicates(
|
87 |
+
subset=[utils.AutoEvalColumn.model.name, utils.AutoEvalColumn.precision.name, utils.AutoEvalColumn.revision.name]
|
88 |
+
)
|
89 |
+
|
90 |
+
return filtered_df
|
91 |
+
|
92 |
+
|
93 |
+
def filter_models(
|
94 |
+
df: pd.DataFrame, type_query: list, size_query: list, precision_query: list, show_deleted: bool
|
95 |
+
) -> pd.DataFrame:
|
96 |
+
# Show all models
|
97 |
+
# if show_deleted:
|
98 |
+
# filtered_df = df
|
99 |
+
# else: # Show only still on the hub models
|
100 |
+
# filtered_df = df[df[utils.AutoEvalColumn.still_on_hub.name]]
|
101 |
+
|
102 |
+
filtered_df = df
|
103 |
+
|
104 |
+
type_emoji = [t[0] for t in type_query]
|
105 |
+
filtered_df = filtered_df.loc[df[utils.AutoEvalColumn.model_type_symbol.name].isin(type_emoji)]
|
106 |
+
filtered_df = filtered_df.loc[df[utils.AutoEvalColumn.precision.name].isin(precision_query + ["None"])]
|
107 |
+
|
108 |
+
numeric_interval = pd.IntervalIndex(sorted([utils.NUMERIC_INTERVALS[s] for s in size_query]))
|
109 |
+
params_column = pd.to_numeric(df[utils.AutoEvalColumn.params.name], errors="coerce")
|
110 |
+
mask = params_column.apply(lambda x: any(numeric_interval.contains(x)))
|
111 |
+
filtered_df = filtered_df.loc[mask]
|
112 |
+
|
113 |
+
return filtered_df
|
114 |
|
115 |
|
116 |
demo = gr.Blocks(css=custom_css)
|
117 |
with demo:
|
118 |
+
gr.HTML(about.TITLE)
|
119 |
+
gr.Markdown(about.INTRODUCTION_TEXT, elem_classes="markdown-text")
|
120 |
|
121 |
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
122 |
with gr.TabItem("🏅 LLM Benchmark", elem_id="llm-benchmark-tab-table", id=0):
|
123 |
+
with gr.Row():
|
124 |
+
with gr.Column():
|
125 |
+
with gr.Row():
|
126 |
+
search_bar = gr.Textbox(
|
127 |
+
placeholder=" 🔍 Search for your model (separate multiple queries with `;`) and press ENTER...",
|
128 |
+
show_label=False,
|
129 |
+
elem_id="search-bar",
|
130 |
+
)
|
131 |
+
with gr.Row():
|
132 |
+
shown_columns = gr.CheckboxGroup(
|
133 |
+
choices=[
|
134 |
+
c.name
|
135 |
+
for c in utils.fields(utils.AutoEvalColumn)
|
136 |
+
if not c.hidden and not c.never_hidden and not c.dummy
|
137 |
+
],
|
138 |
+
value=[
|
139 |
+
c.name
|
140 |
+
for c in utils.fields(utils.AutoEvalColumn)
|
141 |
+
if c.displayed_by_default and not c.hidden and not c.never_hidden
|
142 |
+
],
|
143 |
+
label="Select columns to show",
|
144 |
+
elem_id="column-select",
|
145 |
+
interactive=True,
|
146 |
+
)
|
147 |
+
with gr.Row():
|
148 |
+
deleted_models_visibility = gr.Checkbox(
|
149 |
+
value=False, label="Show gated/private/deleted models", interactive=True
|
150 |
+
)
|
151 |
+
with gr.Column(min_width=320):
|
152 |
+
#with gr.Box(elem_id="box-filter"):
|
153 |
+
filter_columns_type = gr.CheckboxGroup(
|
154 |
+
label="Model types",
|
155 |
+
choices=[t.to_str() for t in utils.ModelType],
|
156 |
+
value=[t.to_str() for t in utils.ModelType],
|
157 |
+
interactive=True,
|
158 |
+
elem_id="filter-columns-type",
|
159 |
+
)
|
160 |
+
filter_columns_precision = gr.CheckboxGroup(
|
161 |
+
label="Precision",
|
162 |
+
choices=[i.value.name for i in utils.Precision],
|
163 |
+
value=[i.value.name for i in utils.Precision],
|
164 |
+
interactive=True,
|
165 |
+
elem_id="filter-columns-precision",
|
166 |
+
)
|
167 |
+
filter_columns_size = gr.CheckboxGroup(
|
168 |
+
label="Model sizes (in billions of parameters)",
|
169 |
+
choices=list(utils.NUMERIC_INTERVALS.keys()),
|
170 |
+
value=list(utils.NUMERIC_INTERVALS.keys()),
|
171 |
+
interactive=True,
|
172 |
+
elem_id="filter-columns-size",
|
173 |
+
)
|
174 |
+
|
175 |
+
leaderboard_table = gr.components.Dataframe(
|
176 |
+
value=leaderboard_df[
|
177 |
+
[c.name for c in utils.fields(utils.AutoEvalColumn) if c.never_hidden]
|
178 |
+
+ shown_columns.value
|
179 |
+
+ [utils.AutoEvalColumn.dummy.name]
|
180 |
+
],
|
181 |
+
headers=[c.name for c in utils.fields(utils.AutoEvalColumn) if c.never_hidden] + shown_columns.value,
|
182 |
+
datatype=utils.TYPES,
|
183 |
+
elem_id="leaderboard-table",
|
184 |
+
interactive=False,
|
185 |
+
visible=True,
|
186 |
+
column_widths=["2%", "33%"]
|
187 |
+
)
|
188 |
+
|
189 |
+
# Dummy leaderboard for handling the case when the user uses backspace key
|
190 |
+
hidden_leaderboard_table_for_search = gr.components.Dataframe(
|
191 |
+
value=original_df[utils.COLS],
|
192 |
+
headers=utils.COLS,
|
193 |
+
datatype=utils.TYPES,
|
194 |
+
visible=False,
|
195 |
+
)
|
196 |
+
search_bar.submit(
|
197 |
+
update_table,
|
198 |
+
[
|
199 |
+
hidden_leaderboard_table_for_search,
|
200 |
+
shown_columns,
|
201 |
+
filter_columns_type,
|
202 |
+
filter_columns_precision,
|
203 |
+
filter_columns_size,
|
204 |
+
deleted_models_visibility,
|
205 |
+
search_bar,
|
206 |
+
],
|
207 |
+
leaderboard_table,
|
208 |
+
)
|
209 |
+
for selector in [shown_columns, filter_columns_type, filter_columns_precision, filter_columns_size, deleted_models_visibility]:
|
210 |
+
selector.change(
|
211 |
+
update_table,
|
212 |
+
[
|
213 |
+
hidden_leaderboard_table_for_search,
|
214 |
+
shown_columns,
|
215 |
+
filter_columns_type,
|
216 |
+
filter_columns_precision,
|
217 |
+
filter_columns_size,
|
218 |
+
deleted_models_visibility,
|
219 |
+
search_bar,
|
220 |
+
],
|
221 |
+
leaderboard_table,
|
222 |
+
queue=True,
|
223 |
+
)
|
224 |
|
225 |
with gr.TabItem("📝 About", elem_id="llm-benchmark-tab-table", id=2):
|
226 |
+
gr.Markdown(about.LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
227 |
|
228 |
with gr.TabItem("🚀 Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
|
229 |
with gr.Column():
|
230 |
with gr.Row():
|
231 |
+
gr.Markdown(about.EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
232 |
|
233 |
with gr.Column():
|
234 |
with gr.Accordion(
|
|
|
238 |
with gr.Row():
|
239 |
finished_eval_table = gr.components.Dataframe(
|
240 |
value=finished_eval_queue_df,
|
241 |
+
headers=utils.EVAL_COLS,
|
242 |
+
datatype=utils.EVAL_TYPES,
|
243 |
row_count=5,
|
244 |
)
|
245 |
with gr.Accordion(
|
|
|
249 |
with gr.Row():
|
250 |
running_eval_table = gr.components.Dataframe(
|
251 |
value=running_eval_queue_df,
|
252 |
+
headers=utils.EVAL_COLS,
|
253 |
+
datatype=utils.EVAL_TYPES,
|
254 |
row_count=5,
|
255 |
)
|
256 |
|
|
|
261 |
with gr.Row():
|
262 |
pending_eval_table = gr.components.Dataframe(
|
263 |
value=pending_eval_queue_df,
|
264 |
+
headers=utils.EVAL_COLS,
|
265 |
+
datatype=utils.EVAL_TYPES,
|
266 |
row_count=5,
|
267 |
)
|
268 |
with gr.Row():
|
|
|
273 |
model_name_textbox = gr.Textbox(label="Model name")
|
274 |
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
275 |
model_type = gr.Dropdown(
|
276 |
+
choices=[t.to_str(" : ") for t in utils.ModelType if t != utils.ModelType.Unknown],
|
277 |
label="Model type",
|
278 |
multiselect=False,
|
279 |
value=None,
|
|
|
282 |
|
283 |
with gr.Column():
|
284 |
precision = gr.Dropdown(
|
285 |
+
choices=[i.value.name for i in utils.Precision if i != utils.Precision.Unknown],
|
286 |
label="Precision",
|
287 |
multiselect=False,
|
288 |
value="float16",
|
289 |
interactive=True,
|
290 |
)
|
291 |
weight_type = gr.Dropdown(
|
292 |
+
choices=[i.value.name for i in utils.WeightType],
|
293 |
label="Weights type",
|
294 |
multiselect=False,
|
295 |
value="Original",
|
|
|
300 |
submit_button = gr.Button("Submit Eval")
|
301 |
submission_result = gr.Markdown()
|
302 |
submit_button.click(
|
303 |
+
submit.add_new_eval,
|
304 |
[
|
305 |
model_name_textbox,
|
306 |
base_model_name_textbox,
|
|
|
315 |
with gr.Row():
|
316 |
with gr.Accordion("📙 Citation", open=False):
|
317 |
citation_button = gr.Textbox(
|
318 |
+
value=about.CITATION_BUTTON_TEXT,
|
319 |
+
label=about.CITATION_BUTTON_LABEL,
|
320 |
lines=20,
|
321 |
elem_id="citation-button",
|
322 |
show_copy_button=True,
|
|
|
325 |
scheduler = BackgroundScheduler()
|
326 |
scheduler.add_job(restart_space, "interval", seconds=1800)
|
327 |
scheduler.start()
|
328 |
+
demo.queue(default_concurrency_limit=40).launch()
|
main_backend.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import logging
|
3 |
+
import pprint
|
4 |
+
import os
|
5 |
+
|
6 |
+
from huggingface_hub import snapshot_download
|
7 |
+
|
8 |
+
import src.backend.run_eval_suite as run_eval_suite
|
9 |
+
import src.backend.manage_requests as manage_requests
|
10 |
+
import src.backend.sort_queue as sort_queue
|
11 |
+
import src.envs as envs
|
12 |
+
|
13 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
|
14 |
+
|
15 |
+
logging.basicConfig(level=logging.ERROR)
|
16 |
+
pp = pprint.PrettyPrinter(width=80)
|
17 |
+
|
18 |
+
PENDING_STATUS = "PENDING"
|
19 |
+
RUNNING_STATUS = "RUNNING"
|
20 |
+
FINISHED_STATUS = "FINISHED"
|
21 |
+
FAILED_STATUS = "FAILED"
|
22 |
+
# import os
|
23 |
+
# os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
24 |
+
# snapshot_download(repo_id=envs.RESULTS_REPO, revision="main",
|
25 |
+
# local_dir=envs.EVAL_RESULTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
|
26 |
+
|
27 |
+
# snapshot_download(repo_id=envs.QUEUE_REPO, revision="main",
|
28 |
+
# local_dir=envs.EVAL_REQUESTS_PATH_BACKEND, repo_type="dataset", max_workers=60)
|
29 |
+
# exit()
|
30 |
+
|
31 |
+
def run_auto_eval(args):
|
32 |
+
if not args.reproduce:
|
33 |
+
current_pending_status = [PENDING_STATUS]
|
34 |
+
print('_________________')
|
35 |
+
manage_requests.check_completed_evals(
|
36 |
+
api=envs.API,
|
37 |
+
checked_status=RUNNING_STATUS,
|
38 |
+
completed_status=FINISHED_STATUS,
|
39 |
+
failed_status=FAILED_STATUS,
|
40 |
+
hf_repo=envs.QUEUE_REPO,
|
41 |
+
local_dir=envs.EVAL_REQUESTS_PATH_BACKEND,
|
42 |
+
hf_repo_results=envs.RESULTS_REPO,
|
43 |
+
local_dir_results=envs.EVAL_RESULTS_PATH_BACKEND
|
44 |
+
)
|
45 |
+
logging.info("Checked completed evals")
|
46 |
+
eval_requests = manage_requests.get_eval_requests(job_status=current_pending_status,
|
47 |
+
hf_repo=envs.QUEUE_REPO,
|
48 |
+
local_dir=envs.EVAL_REQUESTS_PATH_BACKEND)
|
49 |
+
logging.info("Got eval requests")
|
50 |
+
eval_requests = sort_queue.sort_models_by_priority(api=envs.API, models=eval_requests)
|
51 |
+
logging.info("Sorted eval requests")
|
52 |
+
|
53 |
+
print(f"Found {len(eval_requests)} {','.join(current_pending_status)} eval requests")
|
54 |
+
print(eval_requests)
|
55 |
+
if len(eval_requests) == 0:
|
56 |
+
print("No eval requests found. Exiting.")
|
57 |
+
return
|
58 |
+
|
59 |
+
if args.model is not None:
|
60 |
+
eval_request = manage_requests.EvalRequest(
|
61 |
+
model=args.model,
|
62 |
+
status=PENDING_STATUS,
|
63 |
+
precision=args.precision
|
64 |
+
)
|
65 |
+
pp.pprint(eval_request)
|
66 |
+
else:
|
67 |
+
eval_request = eval_requests[0]
|
68 |
+
pp.pprint(eval_request)
|
69 |
+
|
70 |
+
# manage_requests.set_eval_request(
|
71 |
+
# api=envs.API,
|
72 |
+
# eval_request=eval_request,
|
73 |
+
# new_status=RUNNING_STATUS,
|
74 |
+
# hf_repo=envs.QUEUE_REPO,
|
75 |
+
# local_dir=envs.EVAL_REQUESTS_PATH_BACKEND
|
76 |
+
# )
|
77 |
+
# logging.info("Set eval request to running, now running eval")
|
78 |
+
|
79 |
+
run_eval_suite.run_evaluation(
|
80 |
+
eval_request=eval_request,
|
81 |
+
local_dir=envs.EVAL_RESULTS_PATH_BACKEND,
|
82 |
+
results_repo=envs.RESULTS_REPO,
|
83 |
+
batch_size=1,
|
84 |
+
device=envs.DEVICE,
|
85 |
+
no_cache=True,
|
86 |
+
need_check=not args.publish,
|
87 |
+
write_results=args.update
|
88 |
+
)
|
89 |
+
logging.info("Eval finished, now setting status to finished")
|
90 |
+
else:
|
91 |
+
eval_request = manage_requests.EvalRequest(
|
92 |
+
model=args.model,
|
93 |
+
status=PENDING_STATUS,
|
94 |
+
precision=args.precision
|
95 |
+
)
|
96 |
+
pp.pprint(eval_request)
|
97 |
+
logging.info("Running reproducibility eval")
|
98 |
+
|
99 |
+
run_eval_suite.run_evaluation(
|
100 |
+
eval_request=eval_request,
|
101 |
+
local_dir=envs.EVAL_RESULTS_PATH_BACKEND,
|
102 |
+
results_repo=envs.RESULTS_REPO,
|
103 |
+
batch_size=1,
|
104 |
+
device=envs.DEVICE,
|
105 |
+
need_check=not args.publish,
|
106 |
+
write_results=args.update
|
107 |
+
)
|
108 |
+
logging.info("Reproducibility eval finished")
|
109 |
+
|
110 |
+
|
111 |
+
def main():
|
112 |
+
parser = argparse.ArgumentParser(description="Run auto evaluation with optional reproducibility feature")
|
113 |
+
|
114 |
+
# Optional arguments
|
115 |
+
parser.add_argument("--reproduce", type=bool, default=True, help="Reproduce the evaluation results")
|
116 |
+
parser.add_argument("--model", type=str, default=None, help="Your Model ID")
|
117 |
+
parser.add_argument("--precision", type=str, default="float16", help="Precision of your model")
|
118 |
+
parser.add_argument("--publish", type=bool, default=False, help="whether directly publish the evaluation results on HF")
|
119 |
+
parser.add_argument("--update", type=bool, default=False, help="whether to update google drive files")
|
120 |
+
|
121 |
+
args = parser.parse_args()
|
122 |
+
|
123 |
+
run_auto_eval(args)
|
124 |
+
|
125 |
+
|
126 |
+
if __name__ == "__main__":
|
127 |
+
main()
|
requirements.txt
CHANGED
@@ -1,16 +1,17 @@
|
|
1 |
-
APScheduler
|
2 |
-
black
|
3 |
-
|
4 |
-
|
5 |
-
gradio
|
6 |
-
|
7 |
-
gradio_client
|
8 |
huggingface-hub>=0.18.0
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
tokenizers>=0.15.0
|
16 |
-
|
|
|
1 |
+
APScheduler==3.10.1
|
2 |
+
black==23.11.0
|
3 |
+
click==8.1.3
|
4 |
+
datasets==2.14.5
|
5 |
+
gradio==4.4.0
|
6 |
+
gradio_client==0.7.0
|
|
|
7 |
huggingface-hub>=0.18.0
|
8 |
+
litellm==1.15.1
|
9 |
+
matplotlib==3.7.1
|
10 |
+
numpy==1.24.2
|
11 |
+
pandas==2.0.0
|
12 |
+
python-dateutil==2.8.2
|
13 |
+
requests==2.28.2
|
14 |
+
tqdm==4.65.0
|
15 |
+
transformers==4.35.2
|
16 |
tokenizers>=0.15.0
|
17 |
+
sentence-transformers==2.2.2
|
scripts/create_request_file.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import pprint
|
4 |
+
import re
|
5 |
+
from datetime import datetime, timezone
|
6 |
+
|
7 |
+
import click
|
8 |
+
from colorama import Fore
|
9 |
+
from huggingface_hub import HfApi, snapshot_download
|
10 |
+
|
11 |
+
from src.envs import QUEUE_REPO, EVAL_REQUESTS_PATH
|
12 |
+
|
13 |
+
|
14 |
+
precisions = ("float16", "bfloat16", "8bit (LLM.int8)", "4bit (QLoRA / FP4)", "GPTQ")
|
15 |
+
model_types = ("pretrained", "fine-tuned", "RL-tuned", "instruction-tuned")
|
16 |
+
weight_types = ("Original", "Delta", "Adapter")
|
17 |
+
|
18 |
+
|
19 |
+
def get_model_size(model_info, precision: str):
|
20 |
+
size_pattern = size_pattern = re.compile(r"(\d\.)?\d+(b|m)")
|
21 |
+
try:
|
22 |
+
model_size = round(model_info.safetensors["total"] / 1e9, 3)
|
23 |
+
except (AttributeError, TypeError):
|
24 |
+
try:
|
25 |
+
size_match = re.search(size_pattern, model_info.modelId.lower())
|
26 |
+
model_size = size_match.group(0)
|
27 |
+
model_size = round(float(model_size[:-1]) if model_size[-1] == "b"
|
28 |
+
else float(model_size[:-1]) / 1e3, 3)
|
29 |
+
except AttributeError:
|
30 |
+
return 0 # Unknown model sizes are indicated as 0, see NUMERIC_INTERVALS in app.py
|
31 |
+
|
32 |
+
size_factor = 8 if (precision == "GPTQ" or "gptq" in model_info.modelId.lower()) else 1
|
33 |
+
model_size = size_factor * model_size
|
34 |
+
return model_size
|
35 |
+
|
36 |
+
|
37 |
+
def main():
|
38 |
+
api = HfApi()
|
39 |
+
current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
40 |
+
snapshot_download(repo_id=QUEUE_REPO, revision="main", local_dir=EVAL_REQUESTS_PATH,
|
41 |
+
repo_type="dataset")
|
42 |
+
|
43 |
+
model_name = click.prompt("Enter model name")
|
44 |
+
revision = click.prompt("Enter revision", default="main")
|
45 |
+
precision = click.prompt("Enter precision", default="float16", type=click.Choice(precisions))
|
46 |
+
model_type = click.prompt("Enter model type", type=click.Choice(model_types))
|
47 |
+
weight_type = click.prompt("Enter weight type", default="Original",
|
48 |
+
type=click.Choice(weight_types))
|
49 |
+
base_model = click.prompt("Enter base model", default="")
|
50 |
+
status = click.prompt("Enter status", default="FINISHED")
|
51 |
+
|
52 |
+
try:
|
53 |
+
model_info = api.model_info(repo_id=model_name, revision=revision)
|
54 |
+
except Exception as e:
|
55 |
+
print(f"{Fore.RED}Could not find model info for {model_name} on the Hub\n{e}{Fore.RESET}")
|
56 |
+
return 1
|
57 |
+
|
58 |
+
model_size = get_model_size(model_info=model_info, precision=precision)
|
59 |
+
|
60 |
+
try:
|
61 |
+
license = model_info.cardData["license"]
|
62 |
+
except Exception:
|
63 |
+
license = "?"
|
64 |
+
|
65 |
+
eval_entry = {
|
66 |
+
"model": model_name,
|
67 |
+
"base_model": base_model,
|
68 |
+
"revision": revision,
|
69 |
+
"private": False,
|
70 |
+
"precision": precision,
|
71 |
+
"weight_type": weight_type,
|
72 |
+
"status": status,
|
73 |
+
"submitted_time": current_time,
|
74 |
+
"model_type": model_type,
|
75 |
+
"likes": model_info.likes,
|
76 |
+
"params": model_size,
|
77 |
+
"license": license,
|
78 |
+
}
|
79 |
+
|
80 |
+
user_name = ""
|
81 |
+
model_path = model_name
|
82 |
+
if "/" in model_name:
|
83 |
+
user_name = model_name.split("/")[0]
|
84 |
+
model_path = model_name.split("/")[1]
|
85 |
+
|
86 |
+
pprint.pprint(eval_entry)
|
87 |
+
|
88 |
+
if click.confirm("Do you want to continue? This request file will be pushed to the hub"):
|
89 |
+
click.echo("continuing...")
|
90 |
+
|
91 |
+
out_dir = f"{EVAL_REQUESTS_PATH}/{user_name}"
|
92 |
+
os.makedirs(out_dir, exist_ok=True)
|
93 |
+
out_path = f"{out_dir}/{model_path}_eval_request_{False}_{precision}_{weight_type}.json"
|
94 |
+
|
95 |
+
with open(out_path, "w") as f:
|
96 |
+
f.write(json.dumps(eval_entry))
|
97 |
+
|
98 |
+
api.upload_file(
|
99 |
+
path_or_fileobj=out_path,
|
100 |
+
path_in_repo=out_path.split(f"{EVAL_REQUESTS_PATH}/")[1],
|
101 |
+
repo_id=QUEUE_REPO,
|
102 |
+
repo_type="dataset",
|
103 |
+
commit_message=f"Add {model_name} to eval queue",
|
104 |
+
)
|
105 |
+
else:
|
106 |
+
click.echo("aborting...")
|
107 |
+
|
108 |
+
|
109 |
+
if __name__ == "__main__":
|
110 |
+
main()
|
tests/test_evaluate_model.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
from unittest.mock import patch
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
import src.backend.evaluate_model as evaluate_model
|
7 |
+
import src.envs as envs
|
8 |
+
|
9 |
+
|
10 |
+
class TestEvaluator(unittest.TestCase):
|
11 |
+
|
12 |
+
def setUp(self):
|
13 |
+
self.model_name = 'test_model'
|
14 |
+
self.revision = 'test_revision'
|
15 |
+
self.precision = 'test_precision'
|
16 |
+
self.batch_size = 10
|
17 |
+
self.device = 'test_device'
|
18 |
+
self.no_cache = False
|
19 |
+
self.limit = 10
|
20 |
+
|
21 |
+
@patch('src.backend.evaluate_model.SummaryGenerator')
|
22 |
+
@patch('src.backend.evaluate_model.EvaluationModel')
|
23 |
+
def test_evaluator_initialization(self, mock_eval_model, mock_summary_generator):
|
24 |
+
evaluator = evaluate_model.Evaluator(self.model_name, self.revision,
|
25 |
+
self.precision, self.batch_size,
|
26 |
+
self.device, self.no_cache, self.limit)
|
27 |
+
|
28 |
+
mock_summary_generator.assert_called_once_with(self.model_name, self.revision)
|
29 |
+
mock_eval_model.assert_called_once_with(envs.HEM_PATH)
|
30 |
+
self.assertEqual(evaluator.model, self.model_name)
|
31 |
+
|
32 |
+
@patch('src.backend.evaluate_model.EvaluationModel')
|
33 |
+
@patch('src.backend.evaluate_model.SummaryGenerator')
|
34 |
+
def test_evaluator_initialization_error(self, mock_summary_generator, mock_eval_model):
|
35 |
+
mock_eval_model.side_effect = Exception('test_exception')
|
36 |
+
with self.assertRaises(Exception):
|
37 |
+
evaluate_model.Evaluator(self.model_name, self.revision,
|
38 |
+
self.precision, self.batch_size,
|
39 |
+
self.device, self.no_cache, self.limit)
|
40 |
+
|
41 |
+
@patch('src.backend.evaluate_model.SummaryGenerator')
|
42 |
+
@patch('src.backend.evaluate_model.EvaluationModel')
|
43 |
+
@patch('src.backend.evaluate_model.pd.read_csv')
|
44 |
+
@patch('src.backend.util.format_results')
|
45 |
+
def test_evaluate_method(self, mock_format_results, mock_read_csv, mock_eval_model,
|
46 |
+
mock_summary_generator):
|
47 |
+
evaluator = evaluate_model.Evaluator(self.model_name, self.revision,
|
48 |
+
self.precision, self.batch_size,
|
49 |
+
self.device, self.no_cache, self.limit)
|
50 |
+
|
51 |
+
# Mock setup
|
52 |
+
mock_format_results.return_value = {'test': 'result'}
|
53 |
+
mock_read_csv.return_value = pd.DataFrame({'column1': ['data1', 'data2']})
|
54 |
+
mock_summary_generator.return_value.generate_summaries.return_value = pd.DataFrame({'column1': ['summary1', 'summary2']})
|
55 |
+
mock_summary_generator.return_value.avg_length = 100
|
56 |
+
mock_summary_generator.return_value.answer_rate = 1.0
|
57 |
+
mock_summary_generator.return_value.error_rate = 0.0
|
58 |
+
mock_eval_model.return_value.compute_accuracy.return_value = 1.0
|
59 |
+
mock_eval_model.return_value.hallucination_rate = 0.0
|
60 |
+
mock_eval_model.return_value.evaluate_hallucination.return_value = [0.5]
|
61 |
+
|
62 |
+
# Method call and assertions
|
63 |
+
results = evaluator.evaluate()
|
64 |
+
mock_format_results.assert_called_once_with(model_name=self.model_name,
|
65 |
+
revision=self.revision,
|
66 |
+
precision=self.precision,
|
67 |
+
accuracy=1.0, hallucination_rate=0.0,
|
68 |
+
answer_rate=1.0, avg_summary_len=100,
|
69 |
+
error_rate=0.0)
|
70 |
+
mock_read_csv.assert_called_once_with(envs.SOURCE_PATH)
|
71 |
+
|
72 |
+
@patch('src.backend.evaluate_model.SummaryGenerator')
|
73 |
+
@patch('src.backend.evaluate_model.EvaluationModel')
|
74 |
+
@patch('src.backend.evaluate_model.pd.read_csv')
|
75 |
+
def test_evaluate_with_file_not_found(self, mock_read_csv, mock_eval_model,
|
76 |
+
mock_summary_generator):
|
77 |
+
mock_read_csv.side_effect = FileNotFoundError('test_exception')
|
78 |
+
evaluator = evaluate_model.Evaluator(self.model_name, self.revision,
|
79 |
+
self.precision, self.batch_size,
|
80 |
+
self.device, self.no_cache, self.limit)
|
81 |
+
|
82 |
+
with self.assertRaises(FileNotFoundError):
|
83 |
+
evaluator.evaluate()
|
84 |
+
|
85 |
+
|
86 |
+
if __name__ == '__main__':
|
87 |
+
unittest.main()
|
tests/test_evaluator.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
from unittest.mock import patch
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
import src.backend.model_operations as model_operations
|
7 |
+
|
8 |
+
|
9 |
+
class TestEvaluator(unittest.TestCase):
|
10 |
+
|
11 |
+
def setUp(self):
|
12 |
+
self.model_path = "test_model"
|
13 |
+
|
14 |
+
@patch("src.backend.model_operations.load_evaluation_model")
|
15 |
+
def test_init(self, mock_load_evaluation_model):
|
16 |
+
model_operations.EvaluationModel(self.model_path)
|
17 |
+
mock_load_evaluation_model.assert_called_once_with(self.model_path)
|
18 |
+
|
19 |
+
@patch("src.backend.model_operations.load_evaluation_model")
|
20 |
+
def test_evaluate_hallucination(self, mock_load_evaluation_model):
|
21 |
+
model = model_operations.EvaluationModel(self.model_path)
|
22 |
+
df = pd.DataFrame({'source': ['source1', 'source2'], 'summary': ['summary1', 'summary2']})
|
23 |
+
|
24 |
+
mock_load_evaluation_model.return_value.predict.return_value = [0.8, 0.2]
|
25 |
+
|
26 |
+
scores = model.evaluate_hallucination(df)
|
27 |
+
self.assertEqual(scores, [0.8, 0.2])
|
28 |
+
|
29 |
+
@patch("src.backend.model_operations.load_evaluation_model")
|
30 |
+
def test_evaluate_hallucination_exception(self, mock_load_evaluation_model):
|
31 |
+
model = model_operations.EvaluationModel(self.model_path)
|
32 |
+
df = pd.DataFrame({'source': ['source1', 'source2'], 'summary': ['summary1', 'summary2']})
|
33 |
+
|
34 |
+
mock_load_evaluation_model.return_value.predict.side_effect = Exception("Test exception")
|
35 |
+
|
36 |
+
with self.assertRaises(Exception):
|
37 |
+
scores = model.evaluate_hallucination(df)
|
38 |
+
|
39 |
+
@patch("src.backend.model_operations.load_evaluation_model")
|
40 |
+
def test_compute_accuracy(self, mock_load_evaluation_model):
|
41 |
+
model = model_operations.EvaluationModel(self.model_path)
|
42 |
+
model.scores = [0.8, 0.2]
|
43 |
+
|
44 |
+
accuracy = model.compute_accuracy()
|
45 |
+
expected_accuracy = 50.0
|
46 |
+
self.assertEqual(accuracy, expected_accuracy)
|
47 |
+
|
48 |
+
|
49 |
+
class TestLoadEvaluationModel(unittest.TestCase):
|
50 |
+
|
51 |
+
@patch("src.backend.model_operations.CrossEncoder")
|
52 |
+
def test_load_evaluation_model(self, mock_cross_encoder):
|
53 |
+
model_path = 'test_model_path'
|
54 |
+
model_operations.load_evaluation_model(model_path)
|
55 |
+
mock_cross_encoder.assert_called_once_with(model_path)
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == '__main__':
|
59 |
+
unittest.main()
|
tests/test_main_backend.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
from unittest.mock import patch
|
3 |
+
|
4 |
+
import main_backend
|
5 |
+
import src.backend.manage_requests as manage_requests
|
6 |
+
|
7 |
+
|
8 |
+
class TestMainBackend(unittest.TestCase):
|
9 |
+
|
10 |
+
@patch('src.backend.manage_requests.check_completed_evals')
|
11 |
+
@patch('src.backend.manage_requests.get_eval_requests')
|
12 |
+
@patch('src.backend.sort_queue.sort_models_by_priority')
|
13 |
+
@patch('src.backend.manage_requests.set_eval_request')
|
14 |
+
@patch('src.backend.run_eval_suite.run_evaluation')
|
15 |
+
def test_run_auto_eval_with_pending_requests(self, mock_run_evaluation, mock_set_eval_request,
|
16 |
+
mock_sort_models_by_priority, mock_get_eval_requests,
|
17 |
+
mock_check_completed_evals):
|
18 |
+
mock_sort_models_by_priority.return_value = [manage_requests.EvalRequest(
|
19 |
+
model="test_model",
|
20 |
+
private=True,
|
21 |
+
status="PENDING",
|
22 |
+
json_filepath="test_filepath",
|
23 |
+
weight_type="test_weight_type",
|
24 |
+
precision="test_precision",
|
25 |
+
base_model="test_base_model",
|
26 |
+
revision="test_revision",
|
27 |
+
)]
|
28 |
+
|
29 |
+
main_backend.run_auto_eval()
|
30 |
+
|
31 |
+
# Assertions
|
32 |
+
mock_check_completed_evals.assert_called()
|
33 |
+
mock_get_eval_requests.assert_called()
|
34 |
+
mock_sort_models_by_priority.assert_called()
|
35 |
+
mock_set_eval_request.assert_called()
|
36 |
+
mock_run_evaluation.assert_called()
|
37 |
+
|
38 |
+
@patch('builtins.print')
|
39 |
+
@patch('src.backend.manage_requests.check_completed_evals')
|
40 |
+
@patch('src.backend.manage_requests.get_eval_requests')
|
41 |
+
def test_run_auto_eval_with_no_pending_requests(self, mock_get_eval_requests,
|
42 |
+
mock_check_completed_evals, mock_print):
|
43 |
+
mock_get_eval_requests.return_value = []
|
44 |
+
|
45 |
+
main_backend.run_auto_eval()
|
46 |
+
|
47 |
+
# Assertions
|
48 |
+
mock_check_completed_evals.assert_called()
|
49 |
+
mock_get_eval_requests.assert_called()
|
50 |
+
mock_print.assert_any_call("No eval requests found. Exiting.")
|
51 |
+
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
unittest.main()
|
tests/test_summary_generator.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
from unittest.mock import patch
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
import src.backend.evaluate_model as evaluate_model
|
7 |
+
|
8 |
+
|
9 |
+
class TestSummaryGenerator(unittest.TestCase):
|
10 |
+
|
11 |
+
def setUp(self):
|
12 |
+
self.model_id = "test_model"
|
13 |
+
self.revision = "test_revision"
|
14 |
+
|
15 |
+
@patch("src.backend.model_operations.AutoTokenizer")
|
16 |
+
@patch("src.backend.model_operations.AutoModelForCausalLM")
|
17 |
+
def test_init(self, mock_model, mock_tokenizer):
|
18 |
+
evaluate_model.SummaryGenerator(self.model_id, self.revision)
|
19 |
+
mock_tokenizer.from_pretrained.assert_called_once_with(self.model_id,
|
20 |
+
self.revision)
|
21 |
+
mock_model.from_pretrained.assert_called_once_with(self.model_id,
|
22 |
+
self.revision)
|
23 |
+
|
24 |
+
@patch("src.backend.model_operations.nlp")
|
25 |
+
@patch("src.backend.model_operations.AutoTokenizer")
|
26 |
+
@patch("src.backend.model_operations.AutoModelForCausalLM")
|
27 |
+
def test_generate_summaries(self, mock_model, mock_tokenizer, mock_nlp):
|
28 |
+
df = pd.DataFrame({'text': ['text1', 'text2'],
|
29 |
+
'dataset': ['dataset1', 'dataset2']})
|
30 |
+
|
31 |
+
generator = evaluate_model.SummaryGenerator(self.model_id, self.revision)
|
32 |
+
generator.generate_summaries(df)
|
33 |
+
|
34 |
+
self.assertEqual(len(generator.summaries_df), len(df))
|
35 |
+
|
36 |
+
@patch("src.backend.model_operations.AutoTokenizer")
|
37 |
+
@patch("src.backend.model_operations.AutoModelForCausalLM")
|
38 |
+
def test_compute_avg_length(self, mock_model, mock_tokenizer):
|
39 |
+
generator = evaluate_model.SummaryGenerator(self.model_id, self.revision)
|
40 |
+
test_df = pd.DataFrame({'source': ['text'], 'summary': ['This is a test.'],
|
41 |
+
'dataset': ['dataset']})
|
42 |
+
generator.summaries_df = test_df
|
43 |
+
generator._compute_avg_length()
|
44 |
+
self.assertEqual(generator.avg_length, 4)
|
45 |
+
|
46 |
+
@patch("src.backend.model_operations.AutoTokenizer")
|
47 |
+
@patch("src.backend.model_operations.AutoModelForCausalLM")
|
48 |
+
def test_compute_answer_rate(self, mock_model, mock_tokenizer):
|
49 |
+
generator = evaluate_model.SummaryGenerator(self.model_id, self.revision)
|
50 |
+
test_df = pd.DataFrame({'source': ['text'], 'summary': ['This is a test.'],
|
51 |
+
'dataset': ['dataset']})
|
52 |
+
generator.summaries_df = test_df
|
53 |
+
generator._compute_answer_rate()
|
54 |
+
self.assertEqual(generator.answer_rate, 1)
|
55 |
+
|
56 |
+
@patch("src.backend.model_operations.AutoTokenizer")
|
57 |
+
@patch("src.backend.model_operations.AutoModelForCausalLM")
|
58 |
+
def test_error_rate(self, mock_model, mock_tokenizer):
|
59 |
+
generator = evaluate_model.SummaryGenerator(self.model_id, self.revision)
|
60 |
+
test_df = pd.DataFrame({'source': ['text'], 'summary': ['This is a test.'],
|
61 |
+
'dataset': ['dataset']})
|
62 |
+
generator.summaries_df = test_df
|
63 |
+
generator._compute_error_rate(0)
|
64 |
+
self.assertEqual(generator.error_rate, 0)
|
65 |
+
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
unittest.main()
|