Upload task.py with huggingface_hub
Browse files1234563 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950from typing import Any, Dict, List, Optionalfrom .operator import StreamInstanceOperatorclass Tasker:3 unmodified linesclass FormTask(Tasker, StreamInstanceOperator):"""FormTask packs the different instance fields into dictionaries by their roles in the task.The output instance contains three fields:"inputs" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'inputs'."outputs" -- for the fields listed in Arg "outputs"."metrics" -- to contain the value of Arg 'metrics'"""inputs: List[str]outputs: List[str]metrics: List[str]augmentable_inputs: List[str] = []def verify(self):for augmentable_input in self.augmentable_inputs:assert (augmentable_input in self.inputs), f"augmentable_input {augmentable_input} is not part of {self.inputs}"def process(self, instance: Dict[str, Any], stream_name: Optional[str] = None) -> Dict[str, Any]:try:inputs = {key: instance[key] for key in self.inputs}except KeyError as e:raise KeyError(f"Unexpected FormTask input column names ({[key for key in self.inputs if key not in instance]})."f"The available input names: {list(instance.keys())}") from etry:outputs = {key: instance[key] for key in self.outputs}except KeyError as e:raise KeyError(f"Unexpected FormTask output column names: {[key for key in self.outputs if key not in instance]}"f" \n available names:{list(instance.keys())}\n given output names:{self.outputs}") from ereturn {"inputs": inputs,1234567893 unmodified lines131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100from typing import Any, Dict, List, Optional, Unionfrom .artifact import fetch_artifactfrom .logging_utils import get_loggerfrom .operator import StreamInstanceOperatorfrom .type_utils import isoftype, parse_type_string, verify_required_schemaclass Tasker:3 unmodified linesclass FormTask(Tasker, StreamInstanceOperator):"""FormTask packs the different instance fields into dictionaries by their roles in the task.Attributes:inputs (Union[Dict[str, str], List[str]]):Dictionary with string names of instance input fields and types of respective values.In case a list is passed, each type will be assumed to be Any.outputs (Union[Dict[str, str], List[str]]):Dictionary with string names of instance output fields and types of respective values.In case a list is passed, each type will be assumed to be Any.metrics (List[str]): List of names of metrics to be used in the task.prediction_type (Optional[str]):Need to be consistent with all used metrics. Defaults to None, which means that it willbe set to Any.The output instance contains three fields:"inputs" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'inputs'."outputs" -- for the fields listed in Arg "outputs"."metrics" -- to contain the value of Arg 'metrics'"""inputs: Union[Dict[str, str], List[str]]outputs: Union[Dict[str, str], List[str]]metrics: List[str]prediction_type: Optional[str] = Noneaugmentable_inputs: List[str] = []def verify(self):for io_type in ["inputs", "outputs"]:data = self.inputs if io_type == "inputs" else self.outputsif not isoftype(data, Dict[str, str]):get_logger().warning(f"'{io_type}' field of Task should be a dictionary of field names and their types. "f"For example, {{'text': 'str', 'classes': 'List[str]'}}. Instead only '{data}' was "f"passed. All types will be assumed to be 'Any'. In future version of unitxt this "f"will raise an exception.")data = {key: "Any" for key in data}if io_type == "inputs":self.inputs = dataelse:self.outputs = dataif not self.prediction_type:get_logger().warning("'prediction_type' was not set in Task. It is used to check the output of ""template post processors is compatible with the expected input of the metrics. ""Setting `prediction_type` to 'Any' (no checking is done). In future version ""of unitxt this will raise an exception.")self.prediction_type = "Any"self.check_metrics_type()for augmentable_input in self.augmentable_inputs:assert (augmentable_input in self.inputs), f"augmentable_input {augmentable_input} is not part of {self.inputs}"def check_metrics_type(self) -> None:prediction_type = parse_type_string(self.prediction_type)for metric_name in self.metrics:metric = fetch_artifact(metric_name)[0]metric_prediction_type = metric.get_prediction_type()if (prediction_type == metric_prediction_typeor prediction_type == Anyor metric_prediction_type == Any):continueraise ValueError(f"The task's prediction type ({prediction_type}) and '{metric_name}' "f"metric's prediction type ({metric_prediction_type}) are different.")def process(self, instance: Dict[str, Any], stream_name: Optional[str] = None) -> Dict[str, Any]:verify_required_schema(self.inputs, instance)verify_required_schema(self.outputs, instance)inputs = {key: instance[key] for key in self.inputs.keys()}outputs = {key: instance[key] for key in self.outputs.keys()}return {"inputs": inputs,
task.py+68-18