Upload templates.py with huggingface_hub
Browse files- templates.py +76 -16
templates.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
import random
|
2 |
from abc import ABC, abstractmethod
|
3 |
-
from typing import Any, Dict, List
|
4 |
|
5 |
from .artifact import Artifact
|
6 |
-
from .instructions import Instruction
|
7 |
from .operator import InstanceOperatorWithGlobalAccess, StreamInstanceOperator
|
|
|
8 |
from .text_utils import split_words
|
9 |
|
10 |
|
@@ -108,16 +108,9 @@ class RenderTemplatedICL(RenderAutoFormatTemplate):
|
|
108 |
size_limiter: Artifact = None
|
109 |
input_output_separator: str = "\n"
|
110 |
demo_separator: str = "\n\n"
|
111 |
-
demos_cache = None
|
112 |
-
|
113 |
-
def verify(self):
|
114 |
-
assert self.demos_cache is None
|
115 |
|
116 |
def render(self, instance: Dict[str, object]) -> Dict[str, object]:
|
117 |
-
|
118 |
-
self.demos_cache = instance.pop(self.demos_field, [])
|
119 |
-
else:
|
120 |
-
instance.pop(self.demos_field, None)
|
121 |
|
122 |
source = ""
|
123 |
|
@@ -128,7 +121,7 @@ class RenderTemplatedICL(RenderAutoFormatTemplate):
|
|
128 |
if self.instruction is not None:
|
129 |
source += self.instruction_prefix + self.instruction() + self.demo_separator
|
130 |
|
131 |
-
for demo_instance in
|
132 |
demo_example = super().render(demo_instance)
|
133 |
demo_str = (
|
134 |
self.input_prefix
|
@@ -157,16 +150,39 @@ class InputOutputTemplate(Template):
|
|
157 |
input_format: str = None
|
158 |
output_format: str = None
|
159 |
|
160 |
-
def
|
161 |
-
return
|
162 |
|
163 |
-
def
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
def get_postprocessors(self) -> List[str]:
|
167 |
return ["to_string"]
|
168 |
|
169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
class AutoInputOutputTemplate(InputOutputTemplate):
|
171 |
def infer_input_format(self, inputs):
|
172 |
input_format = ""
|
@@ -197,6 +213,50 @@ class TemplatesList(ListCollection):
|
|
197 |
assert isinstance(template, Template)
|
198 |
|
199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
class TemplatesDict(Dict):
|
201 |
def verify(self):
|
202 |
for key, template in self.items():
|
|
|
|
|
1 |
from abc import ABC, abstractmethod
|
2 |
+
from typing import Any, Dict, List, Union
|
3 |
|
4 |
from .artifact import Artifact
|
5 |
+
from .instructions import Instruction, TextualInstruction
|
6 |
from .operator import InstanceOperatorWithGlobalAccess, StreamInstanceOperator
|
7 |
+
from .random_utils import random
|
8 |
from .text_utils import split_words
|
9 |
|
10 |
|
|
|
108 |
size_limiter: Artifact = None
|
109 |
input_output_separator: str = "\n"
|
110 |
demo_separator: str = "\n\n"
|
|
|
|
|
|
|
|
|
111 |
|
112 |
def render(self, instance: Dict[str, object]) -> Dict[str, object]:
|
113 |
+
demos = instance.pop(self.demos_field, [])
|
|
|
|
|
|
|
114 |
|
115 |
source = ""
|
116 |
|
|
|
121 |
if self.instruction is not None:
|
122 |
source += self.instruction_prefix + self.instruction() + self.demo_separator
|
123 |
|
124 |
+
for demo_instance in demos:
|
125 |
demo_example = super().render(demo_instance)
|
126 |
demo_str = (
|
127 |
self.input_prefix
|
|
|
150 |
input_format: str = None
|
151 |
output_format: str = None
|
152 |
|
153 |
+
def process_template(self, template: str, data: Dict[str, object]) -> str:
|
154 |
+
return template.format(**data)
|
155 |
|
156 |
+
def process_inputs(self, inputs: Dict[str, object]) -> str:
|
157 |
+
try:
|
158 |
+
return self.process_template(self.input_format, inputs)
|
159 |
+
except KeyError as e:
|
160 |
+
raise KeyError(
|
161 |
+
f"Available inputs are {inputs.keys()} but input format requires a different one: {self.input_format}"
|
162 |
+
)
|
163 |
+
|
164 |
+
def process_outputs(self, outputs: Dict[str, object]) -> str:
|
165 |
+
try:
|
166 |
+
return self.process_template(self.output_format, outputs)
|
167 |
+
except KeyError as e:
|
168 |
+
raise KeyError(
|
169 |
+
f"Available inputs are {outputs.keys()} but output format requires a different one: {self.output_format}"
|
170 |
+
)
|
171 |
|
172 |
def get_postprocessors(self) -> List[str]:
|
173 |
return ["to_string"]
|
174 |
|
175 |
|
176 |
+
class OutputQuantizingTemplate(InputOutputTemplate):
|
177 |
+
quantum: float = 0.1
|
178 |
+
|
179 |
+
def process_outputs(self, outputs: Dict[str, object]) -> Dict[str, object]:
|
180 |
+
quantized_outputs = {
|
181 |
+
key: round(input_float / self.quantum) * self.quantum for key, input_float in outputs.items()
|
182 |
+
}
|
183 |
+
return super().process_outputs(quantized_outputs)
|
184 |
+
|
185 |
+
|
186 |
class AutoInputOutputTemplate(InputOutputTemplate):
|
187 |
def infer_input_format(self, inputs):
|
188 |
input_format = ""
|
|
|
213 |
assert isinstance(template, Template)
|
214 |
|
215 |
|
216 |
+
def outputs_inputs2templates(inputs: Union[str, List], outputs: Union[str, List]) -> TemplatesList:
|
217 |
+
"""
|
218 |
+
combines input and output formats into their dot product
|
219 |
+
:param inputs: list of input formats (or one)
|
220 |
+
:param outputs: list of output formats (or one)
|
221 |
+
:return: TemplatesList of InputOutputTemplate
|
222 |
+
"""
|
223 |
+
templates = []
|
224 |
+
if isinstance(inputs, str):
|
225 |
+
inputs = [inputs]
|
226 |
+
if isinstance(outputs, str):
|
227 |
+
outputs = [outputs]
|
228 |
+
for input in inputs:
|
229 |
+
for output in outputs:
|
230 |
+
templates.append(
|
231 |
+
InputOutputTemplate(
|
232 |
+
input_format=input.strip(),
|
233 |
+
output_format=output.strip(),
|
234 |
+
),
|
235 |
+
)
|
236 |
+
return TemplatesList(templates)
|
237 |
+
|
238 |
+
|
239 |
+
def instructions2templates(
|
240 |
+
instructions: List[TextualInstruction], templates: List[InputOutputTemplate]
|
241 |
+
) -> TemplatesList:
|
242 |
+
"""
|
243 |
+
Insert instructions into per demonstration templates
|
244 |
+
:param instructions:
|
245 |
+
:param templates: strings containing {instuction} where the instruction should be placed
|
246 |
+
:return:
|
247 |
+
"""
|
248 |
+
res_templates = []
|
249 |
+
for instruction in instructions:
|
250 |
+
for template in templates:
|
251 |
+
res_templates.append(
|
252 |
+
InputOutputTemplate(
|
253 |
+
input_format=template.input_format.replace("{instruction}", instruction.text),
|
254 |
+
output_format=template.output_format,
|
255 |
+
)
|
256 |
+
)
|
257 |
+
return TemplatesList(templates)
|
258 |
+
|
259 |
+
|
260 |
class TemplatesDict(Dict):
|
261 |
def verify(self):
|
262 |
for key, template in self.items():
|