Upload task.py with huggingface_hub
Browse files
task.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import Any, Dict, List
|
| 2 |
|
| 3 |
from .operator import StreamInstanceOperator
|
| 4 |
|
|
@@ -11,22 +11,31 @@ class FormTask(Tasker, StreamInstanceOperator):
|
|
| 11 |
inputs: List[str]
|
| 12 |
outputs: List[str]
|
| 13 |
metrics: List[str]
|
|
|
|
| 14 |
|
| 15 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
inputs = {key: instance[key] for key in self.inputs}
|
| 18 |
except KeyError as e:
|
| 19 |
raise KeyError(
|
| 20 |
-
f"Unexpected input column names
|
| 21 |
-
f"
|
| 22 |
-
)
|
| 23 |
try:
|
| 24 |
outputs = {key: instance[key] for key in self.outputs}
|
| 25 |
except KeyError as e:
|
| 26 |
raise KeyError(
|
| 27 |
-
f"Unexpected output column names: {
|
| 28 |
f" \n available names:{list(instance.keys())}\n given output names:{self.outputs}"
|
| 29 |
-
)
|
| 30 |
|
| 31 |
return {
|
| 32 |
"inputs": inputs,
|
|
@@ -42,13 +51,15 @@ class MultipleChoiceTask(FormTask):
|
|
| 42 |
use_text_in_target: bool = False
|
| 43 |
alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
| 44 |
|
| 45 |
-
def process_single_choice(
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
processed_choice = f"{self.alphabet[index]}"
|
| 48 |
-
except IndexError:
|
| 49 |
raise ValueError(
|
| 50 |
f"Too many choices, the length of alphabet '{self.alphabet}': {len(self.alphabet)} is the limit"
|
| 51 |
-
)
|
| 52 |
if use_text:
|
| 53 |
processed_choice += f"{self.enumeration_suffix}{choice}"
|
| 54 |
return processed_choice
|
|
@@ -60,9 +71,13 @@ class MultipleChoiceTask(FormTask):
|
|
| 60 |
return self.choices_separator.join(processed_choices)
|
| 61 |
|
| 62 |
def process_target(self, choices, target_index):
|
| 63 |
-
return self.process_single_choice(
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
def process(
|
|
|
|
|
|
|
| 66 |
result = super().process(instance, stream_name)
|
| 67 |
target_key, target_value = next(iter(result["outputs"].items()))
|
| 68 |
choices = result["inputs"][self.choices_field]
|
|
|
|
| 1 |
+
from typing import Any, Dict, List, Optional
|
| 2 |
|
| 3 |
from .operator import StreamInstanceOperator
|
| 4 |
|
|
|
|
| 11 |
inputs: List[str]
|
| 12 |
outputs: List[str]
|
| 13 |
metrics: List[str]
|
| 14 |
+
augmentable_inputs: List[str] = []
|
| 15 |
|
| 16 |
+
def verify(self):
|
| 17 |
+
for augmentable_input in self.augmentable_inputs:
|
| 18 |
+
assert (
|
| 19 |
+
augmentable_input in self.inputs
|
| 20 |
+
), f"augmentable_input f{augmentable_input} is not part of {self.inputs}"
|
| 21 |
+
|
| 22 |
+
def process(
|
| 23 |
+
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 24 |
+
) -> Dict[str, Any]:
|
| 25 |
try:
|
| 26 |
inputs = {key: instance[key] for key in self.inputs}
|
| 27 |
except KeyError as e:
|
| 28 |
raise KeyError(
|
| 29 |
+
f"Unexpected FormTask input column names ({[key for key in self.inputs if key not in instance]})."
|
| 30 |
+
f"The available input names: {list(instance.keys())}"
|
| 31 |
+
) from e
|
| 32 |
try:
|
| 33 |
outputs = {key: instance[key] for key in self.outputs}
|
| 34 |
except KeyError as e:
|
| 35 |
raise KeyError(
|
| 36 |
+
f"Unexpected FormTask output column names: {[key for key in self.outputs if key not in instance]}"
|
| 37 |
f" \n available names:{list(instance.keys())}\n given output names:{self.outputs}"
|
| 38 |
+
) from e
|
| 39 |
|
| 40 |
return {
|
| 41 |
"inputs": inputs,
|
|
|
|
| 51 |
use_text_in_target: bool = False
|
| 52 |
alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
| 53 |
|
| 54 |
+
def process_single_choice(
|
| 55 |
+
self, choice: str, index: int, use_text: bool = True
|
| 56 |
+
) -> str:
|
| 57 |
try:
|
| 58 |
processed_choice = f"{self.alphabet[index]}"
|
| 59 |
+
except IndexError as e:
|
| 60 |
raise ValueError(
|
| 61 |
f"Too many choices, the length of alphabet '{self.alphabet}': {len(self.alphabet)} is the limit"
|
| 62 |
+
) from e
|
| 63 |
if use_text:
|
| 64 |
processed_choice += f"{self.enumeration_suffix}{choice}"
|
| 65 |
return processed_choice
|
|
|
|
| 71 |
return self.choices_separator.join(processed_choices)
|
| 72 |
|
| 73 |
def process_target(self, choices, target_index):
|
| 74 |
+
return self.process_single_choice(
|
| 75 |
+
choices[target_index], target_index, use_text=self.use_text_in_target
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
def process(
|
| 79 |
+
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 80 |
+
) -> Dict[str, Any]:
|
| 81 |
result = super().process(instance, stream_name)
|
| 82 |
target_key, target_value = next(iter(result["outputs"].items()))
|
| 83 |
choices = result["inputs"][self.choices_field]
|