HalteroXHunter commited on
Commit
b3179c4
1 Parent(s): 762ead1

experiment3

Browse files
__pycache__/gradio_tst.cpython-39.pyc ADDED
Binary file (4.41 kB). View file
 
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import evaluate
2
- from evaluate.utils import launch_gradio_widget
3
 
4
  module = evaluate.load("absa_evaluator.py")
5
- launch_gradio_widget(module)
 
1
  import evaluate
2
+ from gradio_tst import launch_gradio_widget2
3
 
4
  module = evaluate.load("absa_evaluator.py")
5
+ launch_gradio_widget2(module)
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()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  evaluate
2
  datasets
3
  scikit-learn
4
- gradio==3.50
 
1
  evaluate
2
  datasets
3
  scikit-learn
4
+ gradio