Spaces:
Running
Running
Commit
·
f25dac2
1
Parent(s):
02f1357
Use "giskard-bot/evaluator-leaderboard" and fix any None in cli
Browse files
text_classification_ui_helpers.py
CHANGED
|
@@ -7,16 +7,14 @@ import threading
|
|
| 7 |
import datasets
|
| 8 |
import gradio as gr
|
| 9 |
from transformers.pipelines import TextClassificationPipeline
|
| 10 |
-
from wordings import get_styled_input
|
| 11 |
|
| 12 |
from io_utils import (get_yaml_path, read_column_mapping, save_job_to_pipe,
|
| 13 |
-
write_column_mapping,
|
| 14 |
-
write_log_to_user_file)
|
| 15 |
from text_classification import (check_model, get_example_prediction,
|
| 16 |
get_labels_and_features_from_dataset)
|
| 17 |
from wordings import (CHECK_CONFIG_OR_SPLIT_RAW,
|
| 18 |
CONFIRM_MAPPING_DETAILS_FAIL_RAW,
|
| 19 |
-
MAPPING_STYLED_ERROR_WARNING)
|
| 20 |
|
| 21 |
MAX_LABELS = 40
|
| 22 |
MAX_FEATURES = 20
|
|
@@ -65,9 +63,7 @@ def deselect_run_inference(run_local):
|
|
| 65 |
return (gr.update(visible=True), gr.update(value=True))
|
| 66 |
|
| 67 |
|
| 68 |
-
def write_column_mapping_to_config(
|
| 69 |
-
uid, *labels
|
| 70 |
-
):
|
| 71 |
# TODO: Substitute 'text' with more features for zero-shot
|
| 72 |
# we are not using ds features because we only support "text" for now
|
| 73 |
all_mappings = read_column_mapping(uid)
|
|
@@ -75,10 +71,16 @@ def write_column_mapping_to_config(
|
|
| 75 |
if labels is None:
|
| 76 |
return
|
| 77 |
all_mappings = export_mappings(all_mappings, "labels", None, labels[:MAX_LABELS])
|
| 78 |
-
all_mappings = export_mappings(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
write_column_mapping(all_mappings, uid)
|
| 81 |
|
|
|
|
| 82 |
def export_mappings(all_mappings, key, subkeys, values):
|
| 83 |
if key not in all_mappings.keys():
|
| 84 |
all_mappings[key] = dict()
|
|
@@ -88,12 +90,13 @@ def export_mappings(all_mappings, key, subkeys, values):
|
|
| 88 |
if not subkeys:
|
| 89 |
logging.debug(f"subkeys is empty for {key}")
|
| 90 |
return all_mappings
|
| 91 |
-
|
| 92 |
for i, subkey in enumerate(subkeys):
|
| 93 |
if subkey:
|
| 94 |
all_mappings[key][subkey] = values[i % len(values)]
|
| 95 |
return all_mappings
|
| 96 |
|
|
|
|
| 97 |
def list_labels_and_features_from_dataset(ds_labels, ds_features, model_id2label, uid):
|
| 98 |
model_labels = list(model_id2label.values())
|
| 99 |
all_mappings = read_column_mapping(uid)
|
|
@@ -141,7 +144,10 @@ def list_labels_and_features_from_dataset(ds_labels, ds_features, model_id2label
|
|
| 141 |
|
| 142 |
return lables + features
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
| 145 |
ppl = check_model(model_id)
|
| 146 |
if ppl is None or not isinstance(ppl, TextClassificationPipeline):
|
| 147 |
gr.Warning("Please check your model.")
|
|
@@ -155,6 +161,7 @@ def precheck_model_ds_enable_example_btn(model_id, dataset_id, dataset_config, d
|
|
| 155 |
|
| 156 |
return gr.update(interactive=True)
|
| 157 |
|
|
|
|
| 158 |
def align_columns_and_show_prediction(
|
| 159 |
model_id, dataset_id, dataset_config, dataset_split, uid
|
| 160 |
):
|
|
@@ -230,6 +237,7 @@ def align_columns_and_show_prediction(
|
|
| 230 |
*column_mappings,
|
| 231 |
)
|
| 232 |
|
|
|
|
| 233 |
def check_column_mapping_keys_validity(all_mappings):
|
| 234 |
if all_mappings is None:
|
| 235 |
gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
|
|
@@ -239,6 +247,7 @@ def check_column_mapping_keys_validity(all_mappings):
|
|
| 239 |
gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
|
| 240 |
return (gr.update(interactive=True), gr.update(visible=False))
|
| 241 |
|
|
|
|
| 242 |
def construct_label_and_feature_mapping(all_mappings):
|
| 243 |
label_mapping = {}
|
| 244 |
for i, label in zip(
|
|
@@ -252,6 +261,7 @@ def construct_label_and_feature_mapping(all_mappings):
|
|
| 252 |
feature_mapping = all_mappings["features"]
|
| 253 |
return label_mapping, feature_mapping
|
| 254 |
|
|
|
|
| 255 |
def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid):
|
| 256 |
all_mappings = read_column_mapping(uid)
|
| 257 |
check_column_mapping_keys_validity(all_mappings)
|
|
@@ -259,8 +269,8 @@ def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid
|
|
| 259 |
|
| 260 |
leaderboard_dataset = None
|
| 261 |
if os.environ.get("SPACE_ID") == "giskardai/giskard-evaluator":
|
| 262 |
-
leaderboard_dataset = "
|
| 263 |
-
|
| 264 |
if local:
|
| 265 |
inference_type = "hf_pipeline"
|
| 266 |
if inference and inference_token:
|
|
@@ -279,10 +289,6 @@ def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid
|
|
| 279 |
config,
|
| 280 |
"--dataset_split",
|
| 281 |
split,
|
| 282 |
-
"--hf_token",
|
| 283 |
-
os.environ.get(HF_WRITE_TOKEN),
|
| 284 |
-
"--discussion_repo",
|
| 285 |
-
os.environ.get(HF_REPO_ID) or os.environ.get(HF_SPACE_ID),
|
| 286 |
"--output_format",
|
| 287 |
"markdown",
|
| 288 |
"--output_portal",
|
|
@@ -293,13 +299,28 @@ def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid
|
|
| 293 |
json.dumps(label_mapping),
|
| 294 |
"--scan_config",
|
| 295 |
get_yaml_path(uid),
|
| 296 |
-
"--leaderboard_dataset",
|
| 297 |
-
leaderboard_dataset,
|
| 298 |
"--inference_type",
|
| 299 |
inference_type,
|
| 300 |
"--inference_api_token",
|
| 301 |
inference_token,
|
| 302 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
if os.environ.get(HF_GSK_HUB_KEY):
|
| 304 |
command.append("--giskard_hub_api_key")
|
| 305 |
command.append(os.environ.get(HF_GSK_HUB_KEY))
|
|
@@ -327,11 +348,6 @@ def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid
|
|
| 327 |
gr.Info(f"Start local evaluation on {eval_str}")
|
| 328 |
|
| 329 |
return (
|
| 330 |
-
gr.update(interactive=False),
|
| 331 |
gr.update(lines=5, visible=True, interactive=False),
|
| 332 |
)
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
# TODO: Submit task to an endpoint")
|
| 336 |
-
|
| 337 |
-
# return (gr.update(interactive=True), gr.update(visible=False)) # Submit button
|
|
|
|
| 7 |
import datasets
|
| 8 |
import gradio as gr
|
| 9 |
from transformers.pipelines import TextClassificationPipeline
|
|
|
|
| 10 |
|
| 11 |
from io_utils import (get_yaml_path, read_column_mapping, save_job_to_pipe,
|
| 12 |
+
write_column_mapping, write_log_to_user_file)
|
|
|
|
| 13 |
from text_classification import (check_model, get_example_prediction,
|
| 14 |
get_labels_and_features_from_dataset)
|
| 15 |
from wordings import (CHECK_CONFIG_OR_SPLIT_RAW,
|
| 16 |
CONFIRM_MAPPING_DETAILS_FAIL_RAW,
|
| 17 |
+
MAPPING_STYLED_ERROR_WARNING, get_styled_input)
|
| 18 |
|
| 19 |
MAX_LABELS = 40
|
| 20 |
MAX_FEATURES = 20
|
|
|
|
| 63 |
return (gr.update(visible=True), gr.update(value=True))
|
| 64 |
|
| 65 |
|
| 66 |
+
def write_column_mapping_to_config(uid, *labels):
|
|
|
|
|
|
|
| 67 |
# TODO: Substitute 'text' with more features for zero-shot
|
| 68 |
# we are not using ds features because we only support "text" for now
|
| 69 |
all_mappings = read_column_mapping(uid)
|
|
|
|
| 71 |
if labels is None:
|
| 72 |
return
|
| 73 |
all_mappings = export_mappings(all_mappings, "labels", None, labels[:MAX_LABELS])
|
| 74 |
+
all_mappings = export_mappings(
|
| 75 |
+
all_mappings,
|
| 76 |
+
"features",
|
| 77 |
+
["text"],
|
| 78 |
+
labels[MAX_LABELS : (MAX_LABELS + MAX_FEATURES)],
|
| 79 |
+
)
|
| 80 |
|
| 81 |
write_column_mapping(all_mappings, uid)
|
| 82 |
|
| 83 |
+
|
| 84 |
def export_mappings(all_mappings, key, subkeys, values):
|
| 85 |
if key not in all_mappings.keys():
|
| 86 |
all_mappings[key] = dict()
|
|
|
|
| 90 |
if not subkeys:
|
| 91 |
logging.debug(f"subkeys is empty for {key}")
|
| 92 |
return all_mappings
|
| 93 |
+
|
| 94 |
for i, subkey in enumerate(subkeys):
|
| 95 |
if subkey:
|
| 96 |
all_mappings[key][subkey] = values[i % len(values)]
|
| 97 |
return all_mappings
|
| 98 |
|
| 99 |
+
|
| 100 |
def list_labels_and_features_from_dataset(ds_labels, ds_features, model_id2label, uid):
|
| 101 |
model_labels = list(model_id2label.values())
|
| 102 |
all_mappings = read_column_mapping(uid)
|
|
|
|
| 144 |
|
| 145 |
return lables + features
|
| 146 |
|
| 147 |
+
|
| 148 |
+
def precheck_model_ds_enable_example_btn(
|
| 149 |
+
model_id, dataset_id, dataset_config, dataset_split
|
| 150 |
+
):
|
| 151 |
ppl = check_model(model_id)
|
| 152 |
if ppl is None or not isinstance(ppl, TextClassificationPipeline):
|
| 153 |
gr.Warning("Please check your model.")
|
|
|
|
| 161 |
|
| 162 |
return gr.update(interactive=True)
|
| 163 |
|
| 164 |
+
|
| 165 |
def align_columns_and_show_prediction(
|
| 166 |
model_id, dataset_id, dataset_config, dataset_split, uid
|
| 167 |
):
|
|
|
|
| 237 |
*column_mappings,
|
| 238 |
)
|
| 239 |
|
| 240 |
+
|
| 241 |
def check_column_mapping_keys_validity(all_mappings):
|
| 242 |
if all_mappings is None:
|
| 243 |
gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
|
|
|
|
| 247 |
gr.Warning(CONFIRM_MAPPING_DETAILS_FAIL_RAW)
|
| 248 |
return (gr.update(interactive=True), gr.update(visible=False))
|
| 249 |
|
| 250 |
+
|
| 251 |
def construct_label_and_feature_mapping(all_mappings):
|
| 252 |
label_mapping = {}
|
| 253 |
for i, label in zip(
|
|
|
|
| 261 |
feature_mapping = all_mappings["features"]
|
| 262 |
return label_mapping, feature_mapping
|
| 263 |
|
| 264 |
+
|
| 265 |
def try_submit(m_id, d_id, config, split, local, inference, inference_token, uid):
|
| 266 |
all_mappings = read_column_mapping(uid)
|
| 267 |
check_column_mapping_keys_validity(all_mappings)
|
|
|
|
| 269 |
|
| 270 |
leaderboard_dataset = None
|
| 271 |
if os.environ.get("SPACE_ID") == "giskardai/giskard-evaluator":
|
| 272 |
+
leaderboard_dataset = "giskard-bot/evaluator-leaderboard"
|
| 273 |
+
|
| 274 |
if local:
|
| 275 |
inference_type = "hf_pipeline"
|
| 276 |
if inference and inference_token:
|
|
|
|
| 289 |
config,
|
| 290 |
"--dataset_split",
|
| 291 |
split,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
"--output_format",
|
| 293 |
"markdown",
|
| 294 |
"--output_portal",
|
|
|
|
| 299 |
json.dumps(label_mapping),
|
| 300 |
"--scan_config",
|
| 301 |
get_yaml_path(uid),
|
|
|
|
|
|
|
| 302 |
"--inference_type",
|
| 303 |
inference_type,
|
| 304 |
"--inference_api_token",
|
| 305 |
inference_token,
|
| 306 |
]
|
| 307 |
+
# The token to publish post
|
| 308 |
+
if os.environ.get(HF_WRITE_TOKEN):
|
| 309 |
+
command.append("--hf_token")
|
| 310 |
+
command.append(os.environ.get(HF_WRITE_TOKEN))
|
| 311 |
+
|
| 312 |
+
# The repo to publish post
|
| 313 |
+
if os.environ.get(HF_REPO_ID) or os.environ.get(HF_SPACE_ID):
|
| 314 |
+
command.append("--discussion_repo")
|
| 315 |
+
# TODO: Replace by the model id
|
| 316 |
+
command.append(os.environ.get(HF_REPO_ID) or os.environ.get(HF_SPACE_ID))
|
| 317 |
+
|
| 318 |
+
# The repo to publish for ranking
|
| 319 |
+
if leaderboard_dataset:
|
| 320 |
+
command.append("--leaderboard_dataset")
|
| 321 |
+
command.append(leaderboard_dataset)
|
| 322 |
+
|
| 323 |
+
# The info to upload to Giskard hub
|
| 324 |
if os.environ.get(HF_GSK_HUB_KEY):
|
| 325 |
command.append("--giskard_hub_api_key")
|
| 326 |
command.append(os.environ.get(HF_GSK_HUB_KEY))
|
|
|
|
| 348 |
gr.Info(f"Start local evaluation on {eval_str}")
|
| 349 |
|
| 350 |
return (
|
| 351 |
+
gr.update(interactive=False), # Submit button
|
| 352 |
gr.update(lines=5, visible=True, interactive=False),
|
| 353 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|