HalteroXHunter commited on
Commit
a63e912
1 Parent(s): 1d5d2b3
Files changed (4) hide show
  1. app.py +5 -0
  2. classification_evaluator.py +59 -0
  3. gradio_tst.py +130 -0
  4. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import evaluate
2
+ from gradio_tst import launch_gradio_widget2
3
+
4
+ module = evaluate.load("classification_evaluator.py")
5
+ launch_gradio_widget2(module)
classification_evaluator.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import evaluate
2
+ from datasets import Features, Value
3
+ from sklearn.metrics import accuracy_score
4
+
5
+ _CITATION = """
6
+ @article{scikit-learn,
7
+ title={Scikit-learn: Machine Learning in {P}ython},
8
+ author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
9
+ and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
10
+ and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
11
+ Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
12
+ journal={Journal of Machine Learning Research},
13
+ volume={12},
14
+ pages={2825--2830},
15
+ year={2011}
16
+ }
17
+ """
18
+
19
+ _DESCRIPTION = """
20
+ Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
21
+ Accuracy = (TP + TN) / (TP + TN + FP + FN)
22
+ Where:
23
+ TP: True positive
24
+ TN: True negative
25
+ FP: False positive
26
+ FN: False negative
27
+ """
28
+
29
+ _KWARGS_DESCRIPTION = """
30
+ Args:
31
+ predictions (`list` of `str`): Predicted labels.
32
+ references (`list` of `str`): Ground truth labels.
33
+
34
+ Returns:
35
+ accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.
36
+
37
+ """
38
+
39
+
40
+ class ClassificationEvaluatorTest(evaluate.Metric):
41
+ def _info(self):
42
+ return evaluate.MetricInfo(
43
+ description=_DESCRIPTION,
44
+ citation=_CITATION,
45
+ inputs_description=_KWARGS_DESCRIPTION,
46
+ features=Features(
47
+ {"predictions": Value("string"), "references": Value("string")}
48
+ ),
49
+ )
50
+
51
+ def _compute(self, predictions, references):
52
+
53
+ return {
54
+ "accuracy": float(
55
+ accuracy_score(
56
+ references, predictions, normalize=True, sample_weight=None
57
+ )
58
+ )
59
+ }
gradio_tst.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import re
4
+ import sys
5
+ from pathlib import Path
6
+
7
+ import numpy as np
8
+ from datasets import Value
9
+
10
+ import logging
11
+
12
+
13
+
14
+ REGEX_YAML_BLOCK = re.compile(r"---[\n\r]+([\S\s]*?)[\n\r]+---[\n\r]")
15
+
16
+
17
+ def infer_gradio_input_types(feature_types):
18
+ """
19
+ Maps metric feature types to input types for gradio Dataframes:
20
+ - float/int -> numbers
21
+ - string -> strings
22
+ - any other -> json
23
+ Note that json is not a native gradio type but will be treated as string that
24
+ is then parsed as a json.
25
+ """
26
+ input_types = []
27
+ for feature_type in feature_types:
28
+ input_type = "json"
29
+ if isinstance(feature_type, Value):
30
+ if feature_type.dtype.startswith("int") or feature_type.dtype.startswith("float"):
31
+ input_type = "number"
32
+ elif feature_type.dtype == "string":
33
+ input_type = "str"
34
+ input_types.append(input_type)
35
+ return input_types
36
+
37
+
38
+ def json_to_string_type(input_types):
39
+ """Maps json input type to str."""
40
+ return ["str" if i == "json" else i for i in input_types]
41
+
42
+
43
+ def parse_readme(filepath):
44
+ """Parses a repositories README and removes"""
45
+ if not os.path.exists(filepath):
46
+ return "No README.md found."
47
+ with open(filepath, "r") as f:
48
+ text = f.read()
49
+ match = REGEX_YAML_BLOCK.search(text)
50
+ if match:
51
+ text = text[match.end() :]
52
+ return text
53
+
54
+
55
+ def parse_gradio_data(data, input_types):
56
+ """Parses data from gradio Dataframe for use in metric."""
57
+ metric_inputs = {}
58
+ data.replace("", np.nan, inplace=True)
59
+ data.dropna(inplace=True)
60
+ for feature_name, input_type in zip(data, input_types):
61
+ if input_type == "json":
62
+ metric_inputs[feature_name] = [json.loads(d) for d in data[feature_name].to_list()]
63
+ elif input_type == "str":
64
+ metric_inputs[feature_name] = [d.strip('"') for d in data[feature_name].to_list()]
65
+ else:
66
+ metric_inputs[feature_name] = data[feature_name]
67
+ return metric_inputs
68
+
69
+
70
+ def parse_test_cases(test_cases, feature_names, input_types):
71
+ """
72
+ Parses test cases to be used in gradio Dataframe. Note that an apostrophe is added
73
+ to strings to follow the format in json.
74
+ """
75
+ if len(test_cases) == 0:
76
+ return None
77
+ examples = []
78
+ for test_case in test_cases:
79
+ parsed_cases = []
80
+ for feat, input_type in zip(feature_names, input_types):
81
+ if input_type == "json":
82
+ parsed_cases.append([str(element) for element in test_case[feat]])
83
+ elif input_type == "str":
84
+ parsed_cases.append(['"' + element + '"' for element in test_case[feat]])
85
+ else:
86
+ parsed_cases.append(test_case[feat])
87
+ examples.append([list(i) for i in zip(*parsed_cases)])
88
+ return examples
89
+
90
+
91
+ def launch_gradio_widget2(metric):
92
+ """Launches `metric` widget with Gradio."""
93
+
94
+ try:
95
+ import gradio as gr
96
+ except ImportError as error:
97
+ logging.error("To create a metric widget with Gradio make sure gradio is installed.")
98
+ raise error
99
+
100
+ local_path = Path(sys.path[0])
101
+ # if there are several input types, use first as default.
102
+ if isinstance(metric.features, list):
103
+ (feature_names, feature_types) = zip(*metric.features[0].items())
104
+ else:
105
+ (feature_names, feature_types) = zip(*metric.features.items())
106
+ gradio_input_types = infer_gradio_input_types(feature_types)
107
+
108
+ def compute(data):
109
+ return metric.compute(**parse_gradio_data(data, gradio_input_types))
110
+
111
+ iface = gr.Interface(
112
+ fn=compute,
113
+ inputs=gr.Dataframe(
114
+ headers=feature_names,
115
+ col_count=len(feature_names),
116
+ row_count=1,
117
+ datatype=json_to_string_type(gradio_input_types),
118
+ ),
119
+ outputs=gr.Textbox(label=metric.name),
120
+ description=(
121
+ metric.info.description + "\nIf this is a text-based metric, make sure to wrap you input in double quotes."
122
+ " Alternatively you can use a JSON-formatted list as input."
123
+ ),
124
+ title=f"Metric: {metric.name}",
125
+ article=parse_readme(local_path / "README.md"),
126
+ # TODO: load test cases and use them to populate examples
127
+ # examples=[parse_test_cases(test_cases, feature_names, gradio_input_types)]
128
+ )
129
+
130
+ iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ evaluate
2
+ datasets
3
+ scikit-learn
4
+ gradio