Upload task.py with huggingface_hub
Browse files
task.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
from typing import Any, Dict, List, Optional
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from .operator import StreamInstanceOperator
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class Tasker:
|
|
@@ -10,41 +13,88 @@ class Tasker:
|
|
| 10 |
class FormTask(Tasker, StreamInstanceOperator):
|
| 11 |
"""FormTask packs the different instance fields into dictionaries by their roles in the task.
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
The output instance contains three fields:
|
| 14 |
"inputs" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'inputs'.
|
| 15 |
"outputs" -- for the fields listed in Arg "outputs".
|
| 16 |
"metrics" -- to contain the value of Arg 'metrics'
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
|
| 20 |
-
inputs: List[str]
|
| 21 |
-
outputs: List[str]
|
| 22 |
metrics: List[str]
|
|
|
|
| 23 |
augmentable_inputs: List[str] = []
|
| 24 |
|
| 25 |
def verify(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
for augmentable_input in self.augmentable_inputs:
|
| 27 |
assert (
|
| 28 |
augmentable_input in self.inputs
|
| 29 |
), f"augmentable_input {augmentable_input} is not part of {self.inputs}"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def process(
|
| 32 |
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 33 |
) -> Dict[str, Any]:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
f"The available input names: {list(instance.keys())}"
|
| 40 |
-
) from e
|
| 41 |
-
try:
|
| 42 |
-
outputs = {key: instance[key] for key in self.outputs}
|
| 43 |
-
except KeyError as e:
|
| 44 |
-
raise KeyError(
|
| 45 |
-
f"Unexpected FormTask output column names: {[key for key in self.outputs if key not in instance]}"
|
| 46 |
-
f" \n available names:{list(instance.keys())}\n given output names:{self.outputs}"
|
| 47 |
-
) from e
|
| 48 |
|
| 49 |
return {
|
| 50 |
"inputs": inputs,
|
|
|
|
| 1 |
+
from typing import Any, Dict, List, Optional, Union
|
| 2 |
|
| 3 |
+
from .artifact import fetch_artifact
|
| 4 |
+
from .logging_utils import get_logger
|
| 5 |
from .operator import StreamInstanceOperator
|
| 6 |
+
from .type_utils import isoftype, parse_type_string, verify_required_schema
|
| 7 |
|
| 8 |
|
| 9 |
class Tasker:
|
|
|
|
| 13 |
class FormTask(Tasker, StreamInstanceOperator):
|
| 14 |
"""FormTask packs the different instance fields into dictionaries by their roles in the task.
|
| 15 |
|
| 16 |
+
Attributes:
|
| 17 |
+
inputs (Union[Dict[str, str], List[str]]):
|
| 18 |
+
Dictionary with string names of instance input fields and types of respective values.
|
| 19 |
+
In case a list is passed, each type will be assumed to be Any.
|
| 20 |
+
outputs (Union[Dict[str, str], List[str]]):
|
| 21 |
+
Dictionary with string names of instance output fields and types of respective values.
|
| 22 |
+
In case a list is passed, each type will be assumed to be Any.
|
| 23 |
+
metrics (List[str]): List of names of metrics to be used in the task.
|
| 24 |
+
prediction_type (Optional[str]):
|
| 25 |
+
Need to be consistent with all used metrics. Defaults to None, which means that it will
|
| 26 |
+
be set to Any.
|
| 27 |
+
|
| 28 |
The output instance contains three fields:
|
| 29 |
"inputs" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'inputs'.
|
| 30 |
"outputs" -- for the fields listed in Arg "outputs".
|
| 31 |
"metrics" -- to contain the value of Arg 'metrics'
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
inputs: Union[Dict[str, str], List[str]]
|
| 35 |
+
outputs: Union[Dict[str, str], List[str]]
|
| 36 |
metrics: List[str]
|
| 37 |
+
prediction_type: Optional[str] = None
|
| 38 |
augmentable_inputs: List[str] = []
|
| 39 |
|
| 40 |
def verify(self):
|
| 41 |
+
for io_type in ["inputs", "outputs"]:
|
| 42 |
+
data = self.inputs if io_type == "inputs" else self.outputs
|
| 43 |
+
if not isoftype(data, Dict[str, str]):
|
| 44 |
+
get_logger().warning(
|
| 45 |
+
f"'{io_type}' field of Task should be a dictionary of field names and their types. "
|
| 46 |
+
f"For example, {{'text': 'str', 'classes': 'List[str]'}}. Instead only '{data}' was "
|
| 47 |
+
f"passed. All types will be assumed to be 'Any'. In future version of unitxt this "
|
| 48 |
+
f"will raise an exception."
|
| 49 |
+
)
|
| 50 |
+
data = {key: "Any" for key in data}
|
| 51 |
+
if io_type == "inputs":
|
| 52 |
+
self.inputs = data
|
| 53 |
+
else:
|
| 54 |
+
self.outputs = data
|
| 55 |
+
|
| 56 |
+
if not self.prediction_type:
|
| 57 |
+
get_logger().warning(
|
| 58 |
+
"'prediction_type' was not set in Task. It is used to check the output of "
|
| 59 |
+
"template post processors is compatible with the expected input of the metrics. "
|
| 60 |
+
"Setting `prediction_type` to 'Any' (no checking is done). In future version "
|
| 61 |
+
"of unitxt this will raise an exception."
|
| 62 |
+
)
|
| 63 |
+
self.prediction_type = "Any"
|
| 64 |
+
|
| 65 |
+
self.check_metrics_type()
|
| 66 |
+
|
| 67 |
for augmentable_input in self.augmentable_inputs:
|
| 68 |
assert (
|
| 69 |
augmentable_input in self.inputs
|
| 70 |
), f"augmentable_input {augmentable_input} is not part of {self.inputs}"
|
| 71 |
|
| 72 |
+
def check_metrics_type(self) -> None:
|
| 73 |
+
prediction_type = parse_type_string(self.prediction_type)
|
| 74 |
+
for metric_name in self.metrics:
|
| 75 |
+
metric = fetch_artifact(metric_name)[0]
|
| 76 |
+
metric_prediction_type = metric.get_prediction_type()
|
| 77 |
+
|
| 78 |
+
if (
|
| 79 |
+
prediction_type == metric_prediction_type
|
| 80 |
+
or prediction_type == Any
|
| 81 |
+
or metric_prediction_type == Any
|
| 82 |
+
):
|
| 83 |
+
continue
|
| 84 |
+
|
| 85 |
+
raise ValueError(
|
| 86 |
+
f"The task's prediction type ({prediction_type}) and '{metric_name}' "
|
| 87 |
+
f"metric's prediction type ({metric_prediction_type}) are different."
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
def process(
|
| 91 |
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 92 |
) -> Dict[str, Any]:
|
| 93 |
+
verify_required_schema(self.inputs, instance)
|
| 94 |
+
verify_required_schema(self.outputs, instance)
|
| 95 |
+
|
| 96 |
+
inputs = {key: instance[key] for key in self.inputs.keys()}
|
| 97 |
+
outputs = {key: instance[key] for key in self.outputs.keys()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
return {
|
| 100 |
"inputs": inputs,
|