ZeroCommand commited on
Commit
088f179
1 Parent(s): 87119af

move files to utils and delete unused functions

Browse files
app.py CHANGED
@@ -5,7 +5,7 @@ import gradio as gr
5
  from app_debug import get_demo as get_demo_debug
6
  from app_leaderboard import get_demo as get_demo_leaderboard
7
  from app_text_classification import get_demo as get_demo_text_classification
8
- from run_jobs import start_process_run_job, stop_thread
9
 
10
  try:
11
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
 
5
  from app_debug import get_demo as get_demo_debug
6
  from app_leaderboard import get_demo as get_demo_leaderboard
7
  from app_text_classification import get_demo as get_demo_text_classification
8
+ from utils.run_jobs import start_process_run_job, stop_thread
9
 
10
  try:
11
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
app_debug.py CHANGED
@@ -4,8 +4,8 @@ import html
4
 
5
  import gradio as gr
6
 
7
- import pipe
8
- from io_utils import get_logs_file
9
 
10
  LOG_PATH = "./tmp"
11
  CONFIG_PATH = "./cicd/configs/"
 
4
 
5
  import gradio as gr
6
 
7
+ import utils.pipe as pipe
8
+ from utils.io_utils import get_logs_file
9
 
10
  LOG_PATH = "./tmp"
11
  CONFIG_PATH = "./cicd/configs/"
app_leaderboard.py CHANGED
@@ -5,10 +5,10 @@ import gradio as gr
5
  import pandas as pd
6
  import datetime
7
 
8
- from fetch_utils import (check_dataset_and_get_config,
9
  check_dataset_and_get_split)
10
 
11
- import leaderboard
12
  logger = logging.getLogger(__name__)
13
  global update_time
14
  update_time = datetime.datetime.fromtimestamp(0)
 
5
  import pandas as pd
6
  import datetime
7
 
8
+ from utils.fetch_utils import (check_dataset_and_get_config,
9
  check_dataset_and_get_split)
10
 
11
+ import utils.leaderboard as leaderboard
12
  logger = logging.getLogger(__name__)
13
  global update_time
14
  update_time = datetime.datetime.fromtimestamp(0)
app_text_classification.py CHANGED
@@ -2,22 +2,21 @@ import uuid
2
 
3
  import gradio as gr
4
 
5
- from io_utils import read_scanners, write_scanners
6
- from text_classification_ui_helpers import (
7
  get_related_datasets_from_leaderboard,
8
  align_columns_and_show_prediction,
9
  check_dataset,
 
10
  precheck_model_ds_enable_example_btn,
11
  try_submit,
 
12
  write_column_mapping_to_config,
 
13
  )
14
 
15
- from text_classification import (
16
- get_example_prediction,
17
- check_hf_token_validity,
18
- HuggingFaceInferenceAPIResponse
19
- )
20
- from wordings import (
21
  CONFIRM_MAPPING_DETAILS_MD,
22
  INTRODUCTION_MD,
23
  USE_INFERENCE_API_TIP,
@@ -30,7 +29,7 @@ MAX_FEATURES = 20
30
 
31
  EXAMPLE_MODEL_ID = "cardiffnlp/twitter-roberta-base-sentiment-latest"
32
  CONFIG_PATH = "./config.yaml"
33
-
34
 
35
  def get_demo():
36
  with gr.Row():
@@ -40,7 +39,7 @@ def get_demo():
40
  )
41
  with gr.Row():
42
  model_id_input = gr.Textbox(
43
- label="Hugging Face model id",
44
  placeholder=EXAMPLE_MODEL_ID + " (press enter to confirm)",
45
  )
46
 
@@ -57,12 +56,12 @@ def get_demo():
57
  dataset_split_input = gr.Dropdown(label="Dataset Split", visible=False, allow_custom_value=True)
58
 
59
  with gr.Row():
60
- first_line_ds = gr.DataFrame(label="Dataset preview", visible=False)
61
  with gr.Row():
62
  loading_status = gr.HTML(visible=True)
63
  with gr.Row():
64
  example_btn = gr.Button(
65
- "Validate model & dataset",
66
  visible=True,
67
  variant="primary",
68
  interactive=False,
@@ -104,7 +103,7 @@ def get_demo():
104
  inference_token_info = gr.HTML(value=HF_TOKEN_INVALID_STYLED, visible=False)
105
 
106
  inference_token.change(
107
- lambda token: gr.update(visible=lambda: check_hf_token_validity(token)),
108
  inputs=[inference_token],
109
  outputs=[inference_token_info],
110
  )
@@ -160,6 +159,12 @@ def get_demo():
160
  outputs=[dataset_config_input, dataset_split_input, loading_status]
161
  )
162
 
 
 
 
 
 
 
163
  gr.on(
164
  triggers=[label.change for label in column_mappings],
165
  fn=write_column_mapping_to_config,
@@ -237,21 +242,6 @@ def get_demo():
237
  outputs=[run_btn, logs, uid_label],
238
  )
239
 
240
- def enable_run_btn(run_inference, inference_token, model_id, dataset_id, dataset_config, dataset_split):
241
- if not run_inference or inference_token == "":
242
- return gr.update(interactive=False)
243
- if model_id == "" or dataset_id == "" or dataset_config == "" or dataset_split == "":
244
- return gr.update(interactive=False)
245
- if not column_mapping_accordion.visible:
246
- return gr.update(interactive=False)
247
- _, prediction_response = get_example_prediction(
248
- model_id, dataset_id, dataset_config, dataset_split, inference_token
249
- )
250
- if not isinstance(prediction_response, HuggingFaceInferenceAPIResponse):
251
- gr.warning("Your HF token is invalid. Please check your token.")
252
- return gr.update(interactive=False)
253
- return gr.update(interactive=True)
254
-
255
  gr.on(
256
  triggers=[
257
  run_inference.input,
@@ -260,6 +250,7 @@ def get_demo():
260
  ],
261
  fn=enable_run_btn,
262
  inputs=[
 
263
  run_inference,
264
  inference_token,
265
  model_id_input,
@@ -274,6 +265,7 @@ def get_demo():
274
  triggers=[label.input for label in column_mappings],
275
  fn=enable_run_btn,
276
  inputs=[
 
277
  run_inference,
278
  inference_token,
279
  model_id_input,
 
2
 
3
  import gradio as gr
4
 
5
+ from utils.io_utils import read_scanners, write_scanners
6
+ from utils.ui_helpers import (
7
  get_related_datasets_from_leaderboard,
8
  align_columns_and_show_prediction,
9
  check_dataset,
10
+ show_hf_token_info,
11
  precheck_model_ds_enable_example_btn,
12
  try_submit,
13
+ empty_column_mapping,
14
  write_column_mapping_to_config,
15
+ enable_run_btn,
16
  )
17
 
18
+ import logging
19
+ from utils.wordings import (
 
 
 
 
20
  CONFIRM_MAPPING_DETAILS_MD,
21
  INTRODUCTION_MD,
22
  USE_INFERENCE_API_TIP,
 
29
 
30
  EXAMPLE_MODEL_ID = "cardiffnlp/twitter-roberta-base-sentiment-latest"
31
  CONFIG_PATH = "./config.yaml"
32
+ logger = logging.getLogger(__name__)
33
 
34
  def get_demo():
35
  with gr.Row():
 
39
  )
40
  with gr.Row():
41
  model_id_input = gr.Textbox(
42
+ label="Hugging Face Model id",
43
  placeholder=EXAMPLE_MODEL_ID + " (press enter to confirm)",
44
  )
45
 
 
56
  dataset_split_input = gr.Dropdown(label="Dataset Split", visible=False, allow_custom_value=True)
57
 
58
  with gr.Row():
59
+ first_line_ds = gr.DataFrame(label="Dataset Preview", visible=False)
60
  with gr.Row():
61
  loading_status = gr.HTML(visible=True)
62
  with gr.Row():
63
  example_btn = gr.Button(
64
+ "Validate Model & Dataset",
65
  visible=True,
66
  variant="primary",
67
  interactive=False,
 
103
  inference_token_info = gr.HTML(value=HF_TOKEN_INVALID_STYLED, visible=False)
104
 
105
  inference_token.change(
106
+ fn=show_hf_token_info,
107
  inputs=[inference_token],
108
  outputs=[inference_token_info],
109
  )
 
159
  outputs=[dataset_config_input, dataset_split_input, loading_status]
160
  )
161
 
162
+ gr.on(
163
+ triggers=[model_id_input.change, dataset_id_input.change, dataset_config_input.change],
164
+ fn=empty_column_mapping,
165
+ inputs=[uid_label]
166
+ )
167
+
168
  gr.on(
169
  triggers=[label.change for label in column_mappings],
170
  fn=write_column_mapping_to_config,
 
242
  outputs=[run_btn, logs, uid_label],
243
  )
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  gr.on(
246
  triggers=[
247
  run_inference.input,
 
250
  ],
251
  fn=enable_run_btn,
252
  inputs=[
253
+ uid_label,
254
  run_inference,
255
  inference_token,
256
  model_id_input,
 
265
  triggers=[label.input for label in column_mappings],
266
  fn=enable_run_btn,
267
  inputs=[
268
+ uid_label,
269
  run_inference,
270
  inference_token,
271
  model_id_input,
isolated_env.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import subprocess
3
 
4
- from io_utils import write_log_to_user_file
5
 
6
 
7
  def prepare_venv(execution_id, deps):
 
1
  import os
2
  import subprocess
3
 
4
+ from utils.io_utils import write_log_to_user_file
5
 
6
 
7
  def prepare_venv(execution_id, deps):
utils.py DELETED
@@ -1,29 +0,0 @@
1
- import sys
2
-
3
- import yaml
4
-
5
-
6
- # read scanners from yaml file
7
- # return a list of scanners
8
- def read_scanners(path):
9
- scanners = []
10
- with open(path, "r") as f:
11
- config = yaml.load(f, Loader=yaml.FullLoader)
12
- scanners = config.get("detectors", None)
13
- return scanners
14
-
15
-
16
- # convert a list of scanners to yaml file
17
- def write_scanners(scanners):
18
- with open("./scan_config.yaml", "w") as f:
19
- # save scanners to detectors in yaml
20
- yaml.dump({"detectors": scanners}, f)
21
-
22
-
23
- # convert column mapping dataframe to json
24
- def convert_column_mapping_to_json(df, label=""):
25
- column_mapping = {}
26
- column_mapping[label] = []
27
- for _, row in df.iterrows():
28
- column_mapping[label].append(row.tolist())
29
- return column_mapping
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fetch_utils.py → utils/fetch_utils.py RENAMED
File without changes
io_utils.py → utils/io_utils.py RENAMED
File without changes
leaderboard.py → utils/leaderboard.py RENAMED
File without changes
pipe.py → utils/pipe.py RENAMED
File without changes
run_jobs.py → utils/run_jobs.py RENAMED
@@ -6,7 +6,7 @@ import threading
6
  import time
7
  from pathlib import Path
8
 
9
- import pipe
10
  from app_env import (
11
  HF_GSK_HUB_HF_TOKEN,
12
  HF_GSK_HUB_KEY,
@@ -17,9 +17,9 @@ from app_env import (
17
  HF_SPACE_ID,
18
  HF_WRITE_TOKEN,
19
  )
20
- from io_utils import LOG_FILE, get_yaml_path, write_log_to_user_file
21
  from isolated_env import prepare_venv
22
- from leaderboard import LEADERBOARD
23
 
24
  is_running = False
25
 
 
6
  import time
7
  from pathlib import Path
8
 
9
+ import utils.pipe as pipe
10
  from app_env import (
11
  HF_GSK_HUB_HF_TOKEN,
12
  HF_GSK_HUB_KEY,
 
17
  HF_SPACE_ID,
18
  HF_WRITE_TOKEN,
19
  )
20
+ from utils.io_utils import LOG_FILE, get_yaml_path, write_log_to_user_file
21
  from isolated_env import prepare_venv
22
+ from utils.leaderboard import LEADERBOARD
23
 
24
  is_running = False
25
 
text_classification.py → utils/text_classification.py RENAMED
@@ -1,17 +1,14 @@
1
- import json
2
  import logging
3
 
4
  import datasets
5
  import huggingface_hub
6
- import pandas as pd
7
- from transformers import pipeline
8
  import requests
9
  import os
10
 
11
- logger = logging.getLogger(__name__)
12
- HF_WRITE_TOKEN = "HF_WRITE_TOKEN"
13
 
14
- logger = logging.getLogger(__file__)
 
15
 
16
  class HuggingFaceInferenceAPIResponse:
17
  def __init__(self, message):
@@ -93,165 +90,6 @@ def preload_hf_inference_api(model_id):
93
  hf_token = os.environ.get(HF_WRITE_TOKEN, default="")
94
  hf_inference_api(model_id, hf_token, payload)
95
 
96
- def check_model_pipeline(model_id):
97
- try:
98
- task = huggingface_hub.model_info(model_id).pipeline_tag
99
- except Exception:
100
- return None
101
-
102
- try:
103
- ppl = pipeline(task=task, model=model_id)
104
-
105
- return ppl
106
- except Exception:
107
- return None
108
-
109
-
110
- def text_classificaiton_match_label_case_unsensative(id2label_mapping, label):
111
- for model_label in id2label_mapping.keys():
112
- if model_label.upper() == label.upper():
113
- return model_label, label
114
- return None, label
115
-
116
-
117
- def text_classification_map_model_and_dataset_labels(id2label, dataset_features):
118
- id2label_mapping = {id2label[k]: None for k in id2label.keys()}
119
- dataset_labels = None
120
- for feature in dataset_features.values():
121
- if not isinstance(feature, datasets.ClassLabel):
122
- continue
123
- if len(feature.names) != len(id2label_mapping.keys()):
124
- continue
125
-
126
- dataset_labels = feature.names
127
- # Try to match labels
128
- for label in feature.names:
129
- if label in id2label_mapping.keys():
130
- model_label = label
131
- else:
132
- # Try to find case unsensative
133
- model_label, label = text_classificaiton_match_label_case_unsensative(
134
- id2label_mapping, label
135
- )
136
- if model_label is not None:
137
- id2label_mapping[model_label] = label
138
- else:
139
- print(f"Label {label} is not found in model labels")
140
-
141
- return id2label_mapping, dataset_labels
142
-
143
-
144
- """
145
- params:
146
- column_mapping: dict
147
- example: {
148
- "text": "sentences",
149
- "label": {
150
- "label0": "LABEL_0",
151
- "label1": "LABEL_1"
152
- }
153
- }
154
- ppl: pipeline
155
- """
156
-
157
-
158
- def check_column_mapping_keys_validity(column_mapping, ppl):
159
- # get the element in all the list elements
160
- column_mapping = json.loads(column_mapping)
161
- if "data" not in column_mapping.keys():
162
- return True
163
- user_labels = set([pair[0] for pair in column_mapping["data"]])
164
- model_labels = set([pair[1] for pair in column_mapping["data"]])
165
-
166
- id2label = ppl.model.config.id2label
167
- original_labels = set(id2label.values())
168
-
169
- return user_labels == model_labels == original_labels
170
-
171
-
172
- """
173
- params:
174
- column_mapping: dict
175
- dataset_features: dict
176
- example: {
177
- 'text': Value(dtype='string', id=None),
178
- 'label': ClassLabel(names=['negative', 'neutral', 'positive'], id=None)
179
- }
180
- """
181
-
182
-
183
- def infer_text_input_column(column_mapping, dataset_features):
184
- # Check whether we need to infer the text input column
185
- infer_text_input_column = True
186
- feature_map_df = None
187
-
188
- if "text" in column_mapping.keys():
189
- dataset_text_column = column_mapping["text"]
190
- if dataset_text_column in dataset_features.keys():
191
- infer_text_input_column = False
192
- else:
193
- logging.warning(f"Provided {dataset_text_column} is not in Dataset columns")
194
-
195
- if infer_text_input_column:
196
- # Try to retrieve one
197
- candidates = [
198
- f for f in dataset_features if dataset_features[f].dtype == "string"
199
- ]
200
- feature_map_df = pd.DataFrame(
201
- {"Dataset Features": [candidates[0]], "Model Input Features": ["text"]}
202
- )
203
- if len(candidates) > 0:
204
- logging.debug(f"Candidates are {candidates}")
205
- column_mapping["text"] = candidates[0]
206
-
207
- return column_mapping, feature_map_df
208
-
209
-
210
- """
211
- params:
212
- column_mapping: dict
213
- id2label_mapping: dict
214
- example:
215
- id2label_mapping: {
216
- 'negative': 'negative',
217
- 'neutral': 'neutral',
218
- 'positive': 'positive'
219
- }
220
- """
221
-
222
-
223
- def infer_output_label_column(
224
- column_mapping, id2label_mapping, id2label, dataset_labels
225
- ):
226
- # Check whether we need to infer the output label column
227
- if "data" in column_mapping.keys():
228
- if isinstance(column_mapping["data"], list):
229
- # Use the column mapping passed by user
230
- for user_label, model_label in column_mapping["data"]:
231
- id2label_mapping[model_label] = user_label
232
- elif None in id2label_mapping.values():
233
- column_mapping["label"] = {i: None for i in id2label.keys()}
234
- return column_mapping, None
235
-
236
- if "data" not in column_mapping.keys():
237
- # Column mapping should contain original model labels
238
- column_mapping["label"] = {
239
- str(i): id2label_mapping[label]
240
- for i, label in zip(id2label.keys(), dataset_labels)
241
- }
242
-
243
- id2label_df = pd.DataFrame(
244
- {
245
- "Dataset Labels": dataset_labels,
246
- "Model Prediction Labels": [
247
- id2label_mapping[label] for label in dataset_labels
248
- ],
249
- }
250
- )
251
-
252
- return column_mapping, id2label_df
253
-
254
-
255
  def check_dataset_features_validity(d_id, config, split):
256
  # We assume dataset is ok here
257
  ds = datasets.load_dataset(d_id, config, split=split, trust_remote_code=True)
@@ -335,48 +173,6 @@ def get_sample_prediction(ppl, df, column_mapping, id2label_mapping):
335
  return prediction_input, prediction_result
336
 
337
 
338
- def text_classification_fix_column_mapping(column_mapping, ppl, d_id, config, split):
339
- # load dataset as pd DataFrame
340
- # get features column from dataset
341
- df, dataset_features = check_dataset_features_validity(d_id, config, split)
342
-
343
- column_mapping, feature_map_df = infer_text_input_column(
344
- column_mapping, dataset_features
345
- )
346
- if feature_map_df is None:
347
- # dataset does not have any features
348
- return None, None, None, None, None
349
-
350
- # Retrieve all labels
351
- id2label = ppl.model.config.id2label
352
-
353
- # Infer labels
354
- id2label_mapping, dataset_labels = text_classification_map_model_and_dataset_labels(
355
- id2label, dataset_features
356
- )
357
- column_mapping, id2label_df = infer_output_label_column(
358
- column_mapping, id2label_mapping, id2label, dataset_labels
359
- )
360
- if id2label_df is None:
361
- # does not able to infer output label column
362
- return column_mapping, None, None, None, feature_map_df
363
-
364
- # Get a sample prediction
365
- prediction_input, prediction_result = get_sample_prediction(
366
- ppl, df, column_mapping, id2label_mapping
367
- )
368
- if prediction_result is None:
369
- # does not able to get a sample prediction
370
- return column_mapping, prediction_input, None, id2label_df, feature_map_df
371
-
372
- return (
373
- column_mapping,
374
- prediction_input,
375
- prediction_result,
376
- id2label_df,
377
- feature_map_df,
378
- )
379
-
380
  def strip_model_id_from_url(model_id):
381
  if model_id.startswith("https://huggingface.co/"):
382
  return "/".join(model_id.split("/")[-2])
@@ -387,9 +183,9 @@ def check_hf_token_validity(hf_token):
387
  return False
388
  if not isinstance(hf_token, str):
389
  return False
390
- # use inference api to check the token
391
- payload = {"inputs": "This is a test", "options": {"use_cache": True}}
392
- response = hf_inference_api("cardiffnlp/twitter-roberta-base-sentiment-latest", hf_token, payload)
393
- if "error" in response:
394
  return False
395
  return True
 
 
1
  import logging
2
 
3
  import datasets
4
  import huggingface_hub
 
 
5
  import requests
6
  import os
7
 
8
+ from app_env import HF_WRITE_TOKEN
 
9
 
10
+ logger = logging.getLogger(__name__)
11
+ AUTH_CHECK_URL = "https://huggingface.co/api/whoami-v2"
12
 
13
  class HuggingFaceInferenceAPIResponse:
14
  def __init__(self, message):
 
90
  hf_token = os.environ.get(HF_WRITE_TOKEN, default="")
91
  hf_inference_api(model_id, hf_token, payload)
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  def check_dataset_features_validity(d_id, config, split):
94
  # We assume dataset is ok here
95
  ds = datasets.load_dataset(d_id, config, split=split, trust_remote_code=True)
 
173
  return prediction_input, prediction_result
174
 
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  def strip_model_id_from_url(model_id):
177
  if model_id.startswith("https://huggingface.co/"):
178
  return "/".join(model_id.split("/")[-2])
 
183
  return False
184
  if not isinstance(hf_token, str):
185
  return False
186
+ # use huggingface api to check the token
187
+ headers = {"Authorization": f"Bearer {hf_token}"}
188
+ response = requests.get(AUTH_CHECK_URL, headers=headers)
189
+ if response.status_code != 200:
190
  return False
191
  return True
text_classification_ui_helpers.py → utils/ui_helpers.py RENAMED
@@ -7,18 +7,19 @@ import datasets
7
  import gradio as gr
8
  import pandas as pd
9
 
10
- import leaderboard
11
- from io_utils import read_column_mapping, write_column_mapping
12
- from run_jobs import save_job_to_pipe
13
- from text_classification import (
14
  strip_model_id_from_url,
15
  check_model_task,
16
  preload_hf_inference_api,
17
  get_example_prediction,
18
  get_labels_and_features_from_dataset,
 
19
  HuggingFaceInferenceAPIResponse,
20
  )
21
- from wordings import (
22
  CHECK_CONFIG_OR_SPLIT_RAW,
23
  CONFIRM_MAPPING_DETAILS_FAIL_RAW,
24
  MAPPING_STYLED_ERROR_WARNING,
@@ -26,6 +27,7 @@ from wordings import (
26
  UNMATCHED_MODEL_DATASET_STYLED_ERROR,
27
  CHECK_LOG_SECTION_RAW,
28
  get_styled_input,
 
29
  )
30
  import os
31
 
@@ -35,6 +37,9 @@ MAX_FEATURES = 20
35
  ds_dict = None
36
  ds_config = None
37
 
 
 
 
38
  def get_related_datasets_from_leaderboard(model_id):
39
  records = leaderboard.records
40
  model_id = strip_model_id_from_url(model_id)
@@ -46,18 +51,14 @@ def get_related_datasets_from_leaderboard(model_id):
46
 
47
  return gr.update(choices=datasets_unique, value="")
48
 
49
-
50
- logger = logging.getLogger(__file__)
51
-
52
-
53
  def check_dataset(dataset_id):
54
  logger.info(f"Loading {dataset_id}")
55
  try:
56
  configs = datasets.get_dataset_config_names(dataset_id, trust_remote_code=True)
57
  if len(configs) == 0:
58
  return (
59
- gr.update(),
60
- gr.update(),
61
  ""
62
  )
63
  splits = datasets.get_dataset_split_names(
@@ -70,13 +71,18 @@ def check_dataset(dataset_id):
70
  )
71
  except Exception as e:
72
  logger.warn(f"Check your dataset {dataset_id}: {e}")
 
 
 
 
73
  return (
74
- gr.update(),
75
- gr.update(),
76
  ""
77
  )
78
 
79
-
 
80
 
81
  def write_column_mapping_to_config(uid, *labels):
82
  # TODO: Substitute 'text' with more features for zero-shot
@@ -95,7 +101,6 @@ def write_column_mapping_to_config(uid, *labels):
95
 
96
  write_column_mapping(all_mappings, uid)
97
 
98
-
99
  def export_mappings(all_mappings, key, subkeys, values):
100
  if key not in all_mappings.keys():
101
  all_mappings[key] = dict()
@@ -111,7 +116,6 @@ def export_mappings(all_mappings, key, subkeys, values):
111
  all_mappings[key][subkey] = values[i % len(values)]
112
  return all_mappings
113
 
114
-
115
  def list_labels_and_features_from_dataset(ds_labels, ds_features, model_labels, uid):
116
  all_mappings = read_column_mapping(uid)
117
  # For flattened raw datasets with no labels
@@ -160,19 +164,20 @@ def list_labels_and_features_from_dataset(ds_labels, ds_features, model_labels,
160
 
161
  return lables + features
162
 
163
-
164
  def precheck_model_ds_enable_example_btn(
165
  model_id, dataset_id, dataset_config, dataset_split
166
  ):
 
 
167
  model_id = strip_model_id_from_url(model_id)
168
  model_task = check_model_task(model_id)
169
  preload_hf_inference_api(model_id)
170
  if model_task is None or model_task != "text-classification":
171
  gr.Warning(NOT_TEXT_CLASSIFICATION_MODEL_RAW)
172
- return (gr.update(), gr.update(),"")
173
-
174
  if dataset_config is None or dataset_split is None or len(dataset_config) == 0:
175
- return (gr.update(), gr.update(), "")
176
 
177
  try:
178
  ds = datasets.load_dataset(dataset_id, dataset_config, trust_remote_code=True)
@@ -304,12 +309,31 @@ def align_columns_and_show_prediction(
304
  def check_column_mapping_keys_validity(all_mappings):
305
  if all_mappings is None:
306
  gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
307
- return (gr.update(interactive=True), gr.update(visible=False))
308
 
309
  if "labels" not in all_mappings.keys():
310
  gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
311
- return (gr.update(interactive=True), gr.update(visible=False))
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  def construct_label_and_feature_mapping(all_mappings, ds_labels, ds_features):
315
  label_mapping = {}
@@ -328,9 +352,16 @@ def construct_label_and_feature_mapping(all_mappings, ds_labels, ds_features):
328
  feature_mapping = all_mappings["features"]
329
  return label_mapping, feature_mapping
330
 
 
 
 
 
 
 
331
  def try_submit(m_id, d_id, config, split, inference, inference_token, uid):
332
  all_mappings = read_column_mapping(uid)
333
- check_column_mapping_keys_validity(all_mappings)
 
334
 
335
  # get ds labels and features again for alignment
336
  ds = datasets.load_dataset(d_id, config, split=split, trust_remote_code=True)
 
7
  import gradio as gr
8
  import pandas as pd
9
 
10
+ import utils.leaderboard as leaderboard
11
+ from utils.io_utils import read_column_mapping, write_column_mapping
12
+ from utils.run_jobs import save_job_to_pipe
13
+ from utils.text_classification import (
14
  strip_model_id_from_url,
15
  check_model_task,
16
  preload_hf_inference_api,
17
  get_example_prediction,
18
  get_labels_and_features_from_dataset,
19
+ check_hf_token_validity,
20
  HuggingFaceInferenceAPIResponse,
21
  )
22
+ from utils.wordings import (
23
  CHECK_CONFIG_OR_SPLIT_RAW,
24
  CONFIRM_MAPPING_DETAILS_FAIL_RAW,
25
  MAPPING_STYLED_ERROR_WARNING,
 
27
  UNMATCHED_MODEL_DATASET_STYLED_ERROR,
28
  CHECK_LOG_SECTION_RAW,
29
  get_styled_input,
30
+ get_dataset_fetch_error_raw,
31
  )
32
  import os
33
 
 
37
  ds_dict = None
38
  ds_config = None
39
 
40
+ logger = logging.getLogger(__file__)
41
+
42
+
43
  def get_related_datasets_from_leaderboard(model_id):
44
  records = leaderboard.records
45
  model_id = strip_model_id_from_url(model_id)
 
51
 
52
  return gr.update(choices=datasets_unique, value="")
53
 
 
 
 
 
54
  def check_dataset(dataset_id):
55
  logger.info(f"Loading {dataset_id}")
56
  try:
57
  configs = datasets.get_dataset_config_names(dataset_id, trust_remote_code=True)
58
  if len(configs) == 0:
59
  return (
60
+ gr.update(visible=False),
61
+ gr.update(visible=False),
62
  ""
63
  )
64
  splits = datasets.get_dataset_split_names(
 
71
  )
72
  except Exception as e:
73
  logger.warn(f"Check your dataset {dataset_id}: {e}")
74
+ if "doesn't exist" in str(e):
75
+ gr.Warning(get_dataset_fetch_error_raw(e))
76
+ if "forbidden" in str(e).lower(): # GSK-2770
77
+ gr.Warning(get_dataset_fetch_error_raw(e))
78
  return (
79
+ gr.update(visible=False),
80
+ gr.update(visible=False),
81
  ""
82
  )
83
 
84
+ def empty_column_mapping(uid):
85
+ write_column_mapping(None, uid)
86
 
87
  def write_column_mapping_to_config(uid, *labels):
88
  # TODO: Substitute 'text' with more features for zero-shot
 
101
 
102
  write_column_mapping(all_mappings, uid)
103
 
 
104
  def export_mappings(all_mappings, key, subkeys, values):
105
  if key not in all_mappings.keys():
106
  all_mappings[key] = dict()
 
116
  all_mappings[key][subkey] = values[i % len(values)]
117
  return all_mappings
118
 
 
119
  def list_labels_and_features_from_dataset(ds_labels, ds_features, model_labels, uid):
120
  all_mappings = read_column_mapping(uid)
121
  # For flattened raw datasets with no labels
 
164
 
165
  return lables + features
166
 
 
167
  def precheck_model_ds_enable_example_btn(
168
  model_id, dataset_id, dataset_config, dataset_split
169
  ):
170
+ if model_id == "" or dataset_id == "":
171
+ return (gr.update(interactive=False), gr.update(visible=False), "")
172
  model_id = strip_model_id_from_url(model_id)
173
  model_task = check_model_task(model_id)
174
  preload_hf_inference_api(model_id)
175
  if model_task is None or model_task != "text-classification":
176
  gr.Warning(NOT_TEXT_CLASSIFICATION_MODEL_RAW)
177
+ return (gr.update(interactive=False), gr.update(visible=False), "")
178
+
179
  if dataset_config is None or dataset_split is None or len(dataset_config) == 0:
180
+ return (gr.update(interactive=False), gr.update(visible=False), "")
181
 
182
  try:
183
  ds = datasets.load_dataset(dataset_id, dataset_config, trust_remote_code=True)
 
309
  def check_column_mapping_keys_validity(all_mappings):
310
  if all_mappings is None:
311
  gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
312
+ return False
313
 
314
  if "labels" not in all_mappings.keys():
315
  gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
316
+ return False
317
 
318
+ return True
319
+
320
+ def enable_run_btn(uid, run_inference, inference_token, model_id, dataset_id, dataset_config, dataset_split):
321
+ if not run_inference or inference_token == "":
322
+ logger.warn("Inference API is not enabled")
323
+ return gr.update(interactive=False)
324
+ if model_id == "" or dataset_id == "" or dataset_config == "" or dataset_split == "":
325
+ logger.warn("Model id or dataset id is not selected")
326
+ return gr.update(interactive=False)
327
+
328
+ all_mappings = read_column_mapping(uid)
329
+ if not check_column_mapping_keys_validity(all_mappings):
330
+ logger.warn("Column mapping is not valid")
331
+ return gr.update(interactive=False)
332
+
333
+ if not check_hf_token_validity(inference_token):
334
+ logger.warn("HF token is not valid")
335
+ return gr.update(interactive=False)
336
+ return gr.update(interactive=True)
337
 
338
  def construct_label_and_feature_mapping(all_mappings, ds_labels, ds_features):
339
  label_mapping = {}
 
352
  feature_mapping = all_mappings["features"]
353
  return label_mapping, feature_mapping
354
 
355
+ def show_hf_token_info(token):
356
+ valid = check_hf_token_validity(token)
357
+ if not valid:
358
+ return gr.update(visible=True)
359
+ return gr.update(visible=False)
360
+
361
  def try_submit(m_id, d_id, config, split, inference, inference_token, uid):
362
  all_mappings = read_column_mapping(uid)
363
+ if not check_column_mapping_keys_validity(all_mappings):
364
+ return (gr.update(interactive=True), gr.update(visible=False))
365
 
366
  # get ds labels and features again for alignment
367
  ds = datasets.load_dataset(d_id, config, split=split, trust_remote_code=True)
wordings.py → utils/wordings.py RENAMED
@@ -1,28 +1,28 @@
1
  INTRODUCTION_MD = """
2
  <h1 style="text-align: center;">
3
- 🐢Giskard Evaluator
4
  </h1>
5
- Welcome to Giskard Evaluator Space! Get your report immediately by simply input your model id and dataset id below. Follow our leads and improve your model.
6
  """
7
  CONFIRM_MAPPING_DETAILS_MD = """
8
  <h1 style="text-align: center;">
9
  Confirm Pre-processing Details
10
  </h1>
11
- Please confirm the pre-processing details below. Align the column names of your model in the <b>dropdown</b> menu to your dataset's. If you are not sure, please double check your model and dataset.
12
  """
13
  CONFIRM_MAPPING_DETAILS_FAIL_MD = """
14
  <h1 style="text-align: center;">
15
  Confirm Pre-processing Details
16
  </h1>
17
- Sorry, we cannot align the input/output of your dataset with the model. <b>Pleaser double check your model and dataset.</b>
18
  """
19
 
20
  CONFIRM_MAPPING_DETAILS_FAIL_RAW = """
21
- Sorry, we cannot auto-align the input/output of your dataset with the model.
22
  """
23
 
24
  CHECK_CONFIG_OR_SPLIT_RAW = """
25
- Please check your dataset config or split.
26
  """
27
 
28
  CHECK_LOG_SECTION_RAW = """
@@ -33,18 +33,18 @@ PREDICTION_SAMPLE_MD = """
33
  <h1 style="text-align: center;">
34
  Model Prediction Sample
35
  </h1>
36
- Here is a sample prediction from your model based on your dataset.
37
  """
38
 
39
  MAPPING_STYLED_ERROR_WARNING = """
40
  <h3 style="text-align: center;color: orange; background-color: #fff0f3; border-radius: 8px; padding: 10px; ">
41
- Sorry, we cannot auto-align the labels/features of your dataset and model. Please double check.
42
  </h3>
43
  """
44
 
45
  UNMATCHED_MODEL_DATASET_STYLED_ERROR = """
46
  <h3 style="text-align: center;color: #fa5f5f; background-color: #fbe2e2; border-radius: 8px; padding: 10px; ">
47
- Your model and dataset have different numbers of labels. Please double check your model and dataset.
48
  </h3>
49
  """
50
 
@@ -53,30 +53,22 @@ NOT_TEXT_CLASSIFICATION_MODEL_RAW = """
53
  """
54
 
55
  USE_INFERENCE_API_TIP = """
56
- We recommend to use
57
  <a href="https://huggingface.co/docs/api-inference/detailed_parameters#text-classification-task">
58
  Hugging Face Inference API
59
  </a>
60
- for the evaluation,
61
- which requires your <a href="https://huggingface.co/settings/tokens">HF token</a>.
62
- <br/>
63
- Otherwise, an
64
- <a href="https://huggingface.co/docs/transformers/main_classes/pipelines#transformers.TextClassificationPipeline">
65
- HF pipeline
66
- </a>
67
- will be created and run in this Space. It takes more time to get the result.
68
- <br/>
69
- <b>
70
- Do not worry, your HF token is only used in this Space for your evaluation.
71
- </b>
72
  """
73
 
74
  HF_TOKEN_INVALID_STYLED= """
75
- <h3 style="text-align: center;color: #fa5f5f; background-color: #fbe2e2; border-radius: 8px; padding: 10px; ">
76
  Your Hugging Face token is invalid. Please double check your token.
77
- </h3>
78
  """
79
 
 
 
 
80
  def get_styled_input(input):
81
  return f"""<h3 style="text-align: center;color: #4ca154; background-color: #e2fbe8; border-radius: 8px; padding: 10px; ">
82
  Your model and dataset have been validated! <br /> Sample input: {input}
 
1
  INTRODUCTION_MD = """
2
  <h1 style="text-align: center;">
3
+ 🐢Giskard Evaluator - Text Classification
4
  </h1>
5
+ Welcome to the Giskard Evaluator Space! Get a model vulnerability report immediately by simply sharing your model and dataset id below.
6
  """
7
  CONFIRM_MAPPING_DETAILS_MD = """
8
  <h1 style="text-align: center;">
9
  Confirm Pre-processing Details
10
  </h1>
11
+ Make sure the output variable's labels and the input variable's name are accurately mapped across both the dataset and the model.
12
  """
13
  CONFIRM_MAPPING_DETAILS_FAIL_MD = """
14
  <h1 style="text-align: center;">
15
  Confirm Pre-processing Details
16
  </h1>
17
+ We're unable to automatically map the input variable's name and output variable's labels of your dataset with the model's. <b>Please manually check the mapping below.</b>
18
  """
19
 
20
  CONFIRM_MAPPING_DETAILS_FAIL_RAW = """
21
+ We're unable to automatically map the input variable's name and output variable's labels of your dataset with the model's. <b>Please manually check the mapping below.</b>
22
  """
23
 
24
  CHECK_CONFIG_OR_SPLIT_RAW = """
25
+ We're unanle to extract labels or features from your dataset. Please check your dataset config or split selection.
26
  """
27
 
28
  CHECK_LOG_SECTION_RAW = """
 
33
  <h1 style="text-align: center;">
34
  Model Prediction Sample
35
  </h1>
36
+ Here's a sample of your model's prediction on an example from the dataset.
37
  """
38
 
39
  MAPPING_STYLED_ERROR_WARNING = """
40
  <h3 style="text-align: center;color: orange; background-color: #fff0f3; border-radius: 8px; padding: 10px; ">
41
+ ⚠️ We're unable to automatically map the input variable's name and output variable's labels of your dataset with the model's. <b>Please manually check the mapping below.</b>
42
  </h3>
43
  """
44
 
45
  UNMATCHED_MODEL_DATASET_STYLED_ERROR = """
46
  <h3 style="text-align: center;color: #fa5f5f; background-color: #fbe2e2; border-radius: 8px; padding: 10px; ">
47
+ Your model and dataset have different numbers of labels. Please double check your model and dataset.
48
  </h3>
49
  """
50
 
 
53
  """
54
 
55
  USE_INFERENCE_API_TIP = """
56
+ To speed up the evaluation, we recommend using the
57
  <a href="https://huggingface.co/docs/api-inference/detailed_parameters#text-classification-task">
58
  Hugging Face Inference API
59
  </a>
60
+ . Please input your <a href="https://huggingface.co/settings/tokens">Hugging Face token</a> to do so.
 
 
 
 
 
 
 
 
 
 
 
61
  """
62
 
63
  HF_TOKEN_INVALID_STYLED= """
64
+ <p style="text-align: left;color: red; ">
65
  Your Hugging Face token is invalid. Please double check your token.
66
+ </p>
67
  """
68
 
69
+ def get_dataset_fetch_error_raw(error):
70
+ return f"""Sorry you cannot use this dataset because {error}. Contact HF team to support this dataset."""
71
+
72
  def get_styled_input(input):
73
  return f"""<h3 style="text-align: center;color: #4ca154; background-color: #e2fbe8; border-radius: 8px; padding: 10px; ">
74
  Your model and dataset have been validated! <br /> Sample input: {input}