Elron commited on
Commit
6b26378
1 Parent(s): 7f58297

Upload templates.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. templates.py +38 -26
templates.py CHANGED
@@ -3,6 +3,7 @@ from dataclasses import field
3
  from typing import Any, Dict, List, Union
4
 
5
  from .artifact import Artifact
 
6
  from .instructions import Instruction, TextualInstruction
7
  from .operator import InstanceOperatorWithGlobalAccess, StreamInstanceOperator
8
  from .random_utils import random
@@ -16,6 +17,9 @@ class Renderer(ABC):
16
 
17
 
18
  class Template(Artifact):
 
 
 
19
  @abstractmethod
20
  def process_inputs(self, inputs: Dict[str, object]) -> Dict[str, object]:
21
  pass
@@ -45,19 +49,19 @@ class RenderFormatTemplate(Renderer, StreamInstanceOperator):
45
  outputs = instance.pop("outputs")
46
 
47
  source = self.template.process_inputs(inputs)
48
-
49
- key, targets = next(iter(outputs.items()))
50
- if not isinstance(targets, list):
51
- targets = [targets]
52
-
53
- references = [self.template.process_outputs({key: target}) for target in targets]
54
-
55
- if self.random_reference:
56
- target = random.choice(references)
 
57
  else:
58
- if len(references) == 0:
59
- raise ValueError("No references found")
60
- target = references[0] # what
61
 
62
  return {
63
  **instance,
@@ -74,19 +78,13 @@ class RenderAutoFormatTemplate(RenderFormatTemplate):
74
  def prepare(self):
75
  if self.template is None:
76
  self.template = AutoInputOutputTemplate()
77
- elif isinstance(self.template, InputOutputTemplate):
78
- self.template = AutoInputOutputTemplate(
79
- input_format=self.template.input_format,
80
- output_format=self.template.output_format,
81
- )
82
- else:
83
- raise ValueError(
84
- f"Template must be an instance of InputOutputTemplate or AutoInputOutputTemplate, got {type(self.template)}"
85
- )
86
 
87
  def render(self, instance: Dict[str, object]) -> Dict[str, object]:
88
- if not self.template.is_complete():
89
- self.template.infer_missing(instance["inputs"], instance["outputs"])
 
 
 
90
 
91
  inputs = {key: value for key, value in instance["inputs"].items()}
92
 
@@ -185,11 +183,25 @@ class OutputQuantizingTemplate(InputOutputTemplate):
185
  return super().process_outputs(quantized_outputs)
186
 
187
 
188
- class SpanLabelingTemplate(InputOutputTemplate):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  spans_starts_field: str = "spans_starts"
190
  spans_ends_field: str = "spans_ends"
191
  text_field: str = "text"
192
- labels_field: str = "labels"
193
  span_label_format: str = "{span}: {label}"
194
  postprocessors = ["processors.to_span_label_pairs"]
195
 
@@ -213,7 +225,7 @@ class SpanLabelingTemplate(InputOutputTemplate):
213
  for span, label in zip(text_spans, labels):
214
  targets.append(self.span_label_format.format(span=span, label=label))
215
 
216
- return super().process_outputs({"spans_and_labels": targets})
217
 
218
 
219
  class AutoInputOutputTemplate(InputOutputTemplate):
 
3
  from typing import Any, Dict, List, Union
4
 
5
  from .artifact import Artifact
6
+ from .dataclass import NonPositionalField
7
  from .instructions import Instruction, TextualInstruction
8
  from .operator import InstanceOperatorWithGlobalAccess, StreamInstanceOperator
9
  from .random_utils import random
 
17
 
18
 
19
  class Template(Artifact):
20
+ is_multi_target: bool = NonPositionalField(default=False)
21
+ is_multi_reference: bool = NonPositionalField(default=False)
22
+
23
  @abstractmethod
24
  def process_inputs(self, inputs: Dict[str, object]) -> Dict[str, object]:
25
  pass
 
49
  outputs = instance.pop("outputs")
50
 
51
  source = self.template.process_inputs(inputs)
52
+ targets = self.template.process_outputs(outputs)
53
+
54
+ if self.template.is_multi_reference:
55
+ references = targets
56
+ if self.random_reference:
57
+ target = random.choice(references)
58
+ else:
59
+ if len(references) == 0:
60
+ raise ValueError("No references found")
61
+ target = references[0]
62
  else:
63
+ references = [targets]
64
+ target = targets
 
65
 
66
  return {
67
  **instance,
 
78
  def prepare(self):
79
  if self.template is None:
80
  self.template = AutoInputOutputTemplate()
 
 
 
 
 
 
 
 
 
81
 
82
  def render(self, instance: Dict[str, object]) -> Dict[str, object]:
83
+ try:
84
+ if not self.template.is_complete():
85
+ self.template.infer_missing(instance["inputs"], instance["outputs"])
86
+ except:
87
+ pass
88
 
89
  inputs = {key: value for key, value in instance["inputs"].items()}
90
 
 
183
  return super().process_outputs(quantized_outputs)
184
 
185
 
186
+ class MultiLabelTemplate(InputOutputTemplate):
187
+ labels_field: str = "labels"
188
+ labels_seprator: str = ", "
189
+ postprocessors = ["processors.to_list"]
190
+ output_format = "{labels}"
191
+ empty_label = "None"
192
+
193
+ def process_outputs(self, outputs: Dict[str, object]) -> Dict[str, object]:
194
+ labels = outputs[self.labels_field]
195
+ if len(labels) == 0:
196
+ labels = [self.empty_label]
197
+ labels_str = self.labels_seprator.join(labels)
198
+ return super().process_outputs({"labels": labels_str})
199
+
200
+
201
+ class SpanLabelingTemplate(MultiLabelTemplate):
202
  spans_starts_field: str = "spans_starts"
203
  spans_ends_field: str = "spans_ends"
204
  text_field: str = "text"
 
205
  span_label_format: str = "{span}: {label}"
206
  postprocessors = ["processors.to_span_label_pairs"]
207
 
 
225
  for span, label in zip(text_spans, labels):
226
  targets.append(self.span_label_format.format(span=span, label=label))
227
 
228
+ return super().process_outputs({"labels": targets})
229
 
230
 
231
  class AutoInputOutputTemplate(InputOutputTemplate):