ZeroCommand commited on
Commit
cbb886a
1 Parent(s): 9e4233f

add leaderboard ui and refactor code

Browse files
app.py CHANGED
@@ -5,11 +5,13 @@
5
  import gradio as gr
6
 
7
  from app_text_classification import get_demo as get_demo_text_classification
8
-
9
 
10
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
11
  with gr.Tab("Text Classification"):
12
  get_demo_text_classification()
13
- with gr.Tab("Leaderboard - Text Classification"):
14
- pass
15
- demo.launch()
 
 
 
5
  import gradio as gr
6
 
7
  from app_text_classification import get_demo as get_demo_text_classification
8
+ from app_leaderboard import get_demo as get_demo_leaderboard
9
 
10
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
11
  with gr.Tab("Text Classification"):
12
  get_demo_text_classification()
13
+ with gr.Tab("Leaderboard"):
14
+ get_demo_leaderboard()
15
+
16
+ demo.queue(max_size=100)
17
+ demo.launch(share=False)
app_leaderboard.py CHANGED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import datasets
3
+ import logging
4
+ from fetch_utils import check_dataset_and_get_config, check_dataset_and_get_split
5
+
6
+ def get_records_from_dataset_repo(dataset_id):
7
+ dataset_config = check_dataset_and_get_config(dataset_id)
8
+ logging.info(f"Dataset {dataset_id} has configs {dataset_config}")
9
+ dataset_split = check_dataset_and_get_split(dataset_id, dataset_config[0])
10
+ logging.info(f"Dataset {dataset_id} has splits {dataset_split}")
11
+ try:
12
+ ds = datasets.load_dataset(dataset_id, dataset_config[0])[dataset_split[0]]
13
+ df = ds.to_pandas()
14
+ return df
15
+ except Exception as e:
16
+ logging.warning(f"Failed to load dataset {dataset_id} with config {dataset_config}: {e}")
17
+ return None
18
+
19
+ def get_model_ids(ds):
20
+ logging.info(f"Dataset {ds} column names: {ds['model_id']}")
21
+ models = ds['model_id'].tolist()
22
+ # return unique elements in the list model_ids
23
+ model_ids = list(set(models))
24
+ return model_ids
25
+
26
+ def get_dataset_ids(ds):
27
+ logging.info(f"Dataset {ds} column names: {ds['dataset_id']}")
28
+ datasets = ds['dataset_id'].tolist()
29
+ dataset_ids = list(set(datasets))
30
+ return dataset_ids
31
+
32
+ def get_types(ds):
33
+ # set all types for each column
34
+ types = [str(t) for t in ds.dtypes.to_list()]
35
+ types = [t.replace('object', 'markdown') for t in types]
36
+ types = [t.replace('float64', 'number') for t in types]
37
+ types = [t.replace('int64', 'number') for t in types]
38
+ return types
39
+
40
+ def get_display_df(df):
41
+ # style all elements in the model_id column
42
+ display_df = df.copy()
43
+ if display_df['model_id'].any():
44
+ display_df['model_id'] = display_df['model_id'].apply(lambda x: f'<p href="https://huggingface.co/{x}" style="color:blue">🔗{x}</p>')
45
+ # style all elements in the dataset_id column
46
+ if display_df['dataset_id'].any():
47
+ display_df['dataset_id'] = display_df['dataset_id'].apply(lambda x: f'<p href="https://huggingface.co/datasets/{x}" style="color:blue">🔗{x}</p>')
48
+ # style all elements in the report_link column
49
+ if display_df['report_link'].any():
50
+ display_df['report_link'] = display_df['report_link'].apply(lambda x: f'<p href="{x}" style="color:blue">🔗{x}</p>')
51
+ return display_df
52
+
53
+ def get_demo():
54
+ records = get_records_from_dataset_repo('ZeroCommand/test-giskard-report')
55
+
56
+ model_ids = get_model_ids(records)
57
+ dataset_ids = get_dataset_ids(records)
58
+
59
+ column_names = records.columns.tolist()
60
+ default_columns = ['model_id', 'dataset_id', 'total_issue', 'report_link']
61
+ # set the default columns to show
62
+ default_df = records[default_columns]
63
+ types = get_types(default_df)
64
+ display_df = get_display_df(default_df)
65
+
66
+ with gr.Row():
67
+ task_select = gr.Dropdown(label='Task', choices=['text_classification', 'tabular'], value='text_classification', interactive=True)
68
+ model_select = gr.Dropdown(label='Model id', choices=model_ids, interactive=True)
69
+ dataset_select = gr.Dropdown(label='Dataset id', choices=dataset_ids, interactive=True)
70
+
71
+ with gr.Row():
72
+ columns_select = gr.CheckboxGroup(label='Show columns', choices=column_names, value=default_columns, interactive=True)
73
+
74
+ with gr.Row():
75
+ leaderboard_df = gr.DataFrame(display_df, datatype=types, interactive=False)
76
+
77
+ @gr.on(triggers=[model_select.change, dataset_select.change, columns_select.change, task_select.change],
78
+ inputs=[model_select, dataset_select, columns_select, task_select],
79
+ outputs=[leaderboard_df])
80
+ def filter_table(model_id, dataset_id, columns, task):
81
+ # filter the table based on task
82
+ df = records[(records['hf_pipeline_type'] == task)]
83
+ # filter the table based on the model_id and dataset_id
84
+ if model_id:
85
+ df = records[(records['model_id'] == model_id)]
86
+ if dataset_id:
87
+ df = records[(records['dataset_id'] == dataset_id)]
88
+
89
+ # filter the table based on the columns
90
+ df = df[columns]
91
+ types = get_types(df)
92
+ display_df = get_display_df(df)
93
+ return (
94
+ gr.update(value=display_df, datatype=types, interactive=False)
95
+ )
app_legacy.py CHANGED
@@ -11,7 +11,7 @@ import json
11
  from transformers.pipelines import TextClassificationPipeline
12
 
13
  from text_classification import check_column_mapping_keys_validity, text_classification_fix_column_mapping
14
- from utils import read_scanners, write_scanners, read_inference_type, write_inference_type, convert_column_mapping_to_json
15
  from wordings import CONFIRM_MAPPING_DETAILS_MD, CONFIRM_MAPPING_DETAILS_FAIL_MD
16
 
17
  HF_REPO_ID = 'HF_REPO_ID'
 
11
  from transformers.pipelines import TextClassificationPipeline
12
 
13
  from text_classification import check_column_mapping_keys_validity, text_classification_fix_column_mapping
14
+ from io_utils import read_scanners, write_scanners, read_inference_type, write_inference_type, convert_column_mapping_to_json
15
  from wordings import CONFIRM_MAPPING_DETAILS_MD, CONFIRM_MAPPING_DETAILS_FAIL_MD
16
 
17
  HF_REPO_ID = 'HF_REPO_ID'
app_text_classification.py CHANGED
@@ -9,9 +9,9 @@ import json
9
 
10
  from transformers.pipelines import TextClassificationPipeline
11
 
12
- from text_classification import get_labels_and_features_from_dataset, check_model, get_example_prediction, check_column_mapping_keys_validity, text_classification_fix_column_mapping
13
- from utils import read_scanners, write_scanners, read_inference_type, read_column_mapping, write_column_mapping, write_inference_type, convert_column_mapping_to_json
14
- from wordings import CONFIRM_MAPPING_DETAILS_MD, CONFIRM_MAPPING_DETAILS_FAIL_MD, CONFIRM_MAPPING_DETAILS_FAIL_RAW
15
 
16
  HF_REPO_ID = 'HF_REPO_ID'
17
  HF_SPACE_ID = 'SPACE_ID'
@@ -222,7 +222,11 @@ def get_demo():
222
  check_dataset_and_get_split,
223
  inputs=[dataset_id_input, dataset_config_input],
224
  outputs=[dataset_split_input])
225
-
 
 
 
 
226
  gr.on(
227
  triggers=[
228
  run_btn.click,
 
9
 
10
  from transformers.pipelines import TextClassificationPipeline
11
 
12
+ from text_classification import get_labels_and_features_from_dataset, check_model, get_example_prediction
13
+ from io_utils import read_scanners, write_scanners, read_inference_type, read_column_mapping, write_column_mapping, write_inference_type
14
+ from wordings import CONFIRM_MAPPING_DETAILS_MD, CONFIRM_MAPPING_DETAILS_FAIL_RAW
15
 
16
  HF_REPO_ID = 'HF_REPO_ID'
17
  HF_SPACE_ID = 'SPACE_ID'
 
222
  check_dataset_and_get_split,
223
  inputs=[dataset_id_input, dataset_config_input],
224
  outputs=[dataset_split_input])
225
+
226
+ run_inference.change(
227
+ write_inference_type,
228
+ inputs=[run_inference]
229
+ )
230
  gr.on(
231
  triggers=[
232
  run_btn.click,
fetch_utils.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import huggingface_hub
2
+ import datasets
3
+
4
+ def check_dataset_and_get_config(dataset_id):
5
+ try:
6
+ configs = datasets.get_dataset_config_names(dataset_id)
7
+ return configs
8
+ except Exception:
9
+ # Dataset may not exist
10
+ return None
11
+
12
+ def check_dataset_and_get_split(dataset_id, dataset_config):
13
+ try:
14
+ ds = datasets.load_dataset(dataset_id, dataset_config)
15
+ except Exception:
16
+ # Dataset may not exist
17
+ return None
18
+ try:
19
+ splits = list(ds.keys())
20
+ return splits
21
+ except Exception:
22
+ # Dataset has no splits
23
+ return None
utils.py → io_utils.py RENAMED
@@ -17,13 +17,13 @@ def read_scanners(path):
17
 
18
  # convert a list of scanners to yaml file
19
  def write_scanners(scanners):
20
- with open(YAML_PATH, "r") as f:
 
21
  config = yaml.load(f, Loader=yaml.FullLoader)
22
-
23
- config["detectors"] = scanners
24
- with open(YAML_PATH, "w") as f:
25
- # save scanners to detectors in yaml
26
- yaml.dump(config, f, Dumper=Dumper)
27
 
28
  # read model_type from yaml file
29
  def read_inference_type(path):
@@ -35,15 +35,14 @@ def read_inference_type(path):
35
 
36
  # write model_type to yaml file
37
  def write_inference_type(use_inference):
38
- with open(YAML_PATH, "r") as f:
39
  config = yaml.load(f, Loader=yaml.FullLoader)
40
  if use_inference:
41
  config["inference_type"] = 'hf_inference_api'
42
  else:
43
  config["inference_type"] = 'hf_pipeline'
44
- with open(YAML_PATH, "w") as f:
45
- # save inference_type to inference_type in yaml
46
- yaml.dump(config, f, Dumper=Dumper)
47
 
48
  # read column mapping from yaml file
49
  def read_column_mapping(path):
 
17
 
18
  # convert a list of scanners to yaml file
19
  def write_scanners(scanners):
20
+ print(scanners)
21
+ with open(YAML_PATH, "r+") as f:
22
  config = yaml.load(f, Loader=yaml.FullLoader)
23
+ if config:
24
+ config["detectors"] = scanners
25
+ # save scanners to detectors in yaml
26
+ yaml.dump(config, f, Dumper=Dumper)
 
27
 
28
  # read model_type from yaml file
29
  def read_inference_type(path):
 
35
 
36
  # write model_type to yaml file
37
  def write_inference_type(use_inference):
38
+ with open(YAML_PATH, "r+") as f:
39
  config = yaml.load(f, Loader=yaml.FullLoader)
40
  if use_inference:
41
  config["inference_type"] = 'hf_inference_api'
42
  else:
43
  config["inference_type"] = 'hf_pipeline'
44
+ # save inference_type to inference_type in yaml
45
+ yaml.dump(config, f, Dumper=Dumper)
 
46
 
47
  # read column mapping from yaml file
48
  def read_column_mapping(path):