Upload templates.py with huggingface_hub
Browse files1233457 unmodified lines1314151617181920212223242526272829303132339 unmodified lines43444538464748491 unmodified line5152535455565758495960616219 unmodified lines8283848586757677787980818788899091929394959697987 unmodified lines10610710810911011111211311411511611711811912012112212312412512612712812913051 unmodified lines18218318415218518618718818919019119219315916016116216316419419519619719819920020120220353 unmodified lines25725825922422522626022822923023123223323423523623726126226326426526626726826927027127227327427527625 unmodified lines3023033042692702713053063077 unmodified lines3153163172852862872883183193207 unmodified lines3283293303023033313323333343353363373383393403414 unmodified lines346347348319320321349350351import jsonfrom abc import abstractmethodfrom dataclasses import fieldfrom typing import Any, Dict, List, Optional, Tuple
from .collections import ListCollection7 unmodified lines """The role of template is to take the fields of every instance and verbalize it.
Meaning the template is taking the instance and generating source, target and references.
Args: skip_rendered_instance (bool): if "source", "target", and "references" are already defined fields in the instance, skip its processing postprocessors: a list of strings being artifact names of text processors, to be applied on the model output instruction: a formatting string that yields an instruction with potential participation of values from the "inputs" part of the instance target_prefix: a string to be used to format the prompt. Not a formatting string.
"""
skip_rendered_instance: bool = NonPositionalField(default=True) postprocessors: List[str] = NonPositionalField( default_factory=lambda: ["processors.to_string_stripped"] ) instruction: str = NonPositionalField(default_factory=lambda: "") target_prefix: str = NonPositionalField(default_factory=lambda: "")
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None9 unmodified lines inputs = instance.get("inputs") outputs = instance.get("outputs")
source = self.inputs_to_source(inputs) source, instruction = self.inputs_to_source(inputs) target, references = self.outputs_to_target_and_references(outputs)
return {1 unmodified line "source": source, "target": target, "references": references, "instruction": instruction, "target_prefix": self.target_prefix.format(**inputs), }
@abstractmethod def inputs_to_source(self, inputs: Dict[str, object]) -> str: def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]: pass
@abstractmethod19 unmodified lines data = {k: ", ".join(v) if isinstance(v, list) else v for k, v in data.items()} return template.format(**data)
def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]: formatted = [] def inputs_to_source(self, inputs: Dict[str, object]) -> str: try: return self.process_template(self.input_format, inputs) except KeyError as e: raise KeyError( f"Available inputs are {list(inputs.keys())} but input format requires a different ones: '{self.input_format}'" ) from e for formatting in [self.input_format, self.instruction]: try: formatted.append(self.process_template(formatting, inputs)) except KeyError as e: raise KeyError( f"Available inputs are {list(inputs.keys())} but input format requires a different ones: '{formatting}'" ) from e
return tuple(formatted)
def outputs_to_target_and_references(self, outputs: Dict[str, object]) -> str: try:7 unmodified lines return target, references
class InputOutputReferenceTemplate(InputOutputTemplate): reference: str
def outputs_to_target_and_references(self, outputs: Dict[str, object]) -> str: output_fields = {} for name, val in [ ("target", self.output_format), ("reference", self.reference), ]: try: result = self.process_template(val, outputs) output_fields[name] = result except KeyError as e: raise KeyError( f"Available outputs are {outputs.keys()} but {name} requires a different one: {val}" ) from e return output_fields["target"], [output_fields["reference"]]
class MultipleChoiceTemplate(Template): """Formats the input (that specifies the question), the multiple choices to select the answer from, and specifies the field with the correct answer."""
51 unmodified lines ) return enumrated_choices
def inputs_to_source(self, inputs: Dict[str, object]) -> str: def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]: choices = self.get_choices(inputs, self.source_choice_format) inputs = { "numerals": ",".join(self.get_choices(inputs, "{choice_numeral}")), **inputs, self.choices_field: self.choices_seperator.join(choices), } formatted = [] for formatting in [self.input_format, self.instruction]: try: return self.input_format.format(**inputs) except KeyError as e: raise KeyError( f"Available inputs are {inputs.keys()} but input format requires a different one: {self.input_format}" ) from e try: formatted.append(formatting.format(**inputs)) except KeyError as e: raise KeyError( f"Available inputs are {inputs.keys()} but input format requires a different one: {formatting}" ) from e return tuple(formatted)
def outputs_to_target_and_references(self, outputs: Dict[str, object]) -> str: target = outputs[self.target_field]53 unmodified lines label_field: str = None yes_answer: str = "Yes" no_answer: str = "No" postprocessors: List[str] = field( default_factory=lambda: ["processors.to_string_stripped"] )
def inputs_to_source(self, inputs: Dict[str, object]) -> str: try: data = { k: ", ".join(v) if isinstance(v, list) else v for k, v in inputs.items() } return self.input_format.format(**data) except KeyError as e: raise RuntimeError( f"Available inputs are {list(inputs.keys())} but input format requires a different one: {self.input_format}" ) from e def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]: data = { k: ", ".join(v) if isinstance(v, list) else v for k, v in inputs.items() } formatted = [] for formatting in [self.input_format, self.instruction]: try: formatted.append(formatting.format(**data)) except KeyError as e: raise RuntimeError( f"Available inputs are {list(inputs.keys())} but input format requires a different one: {formatting}" ) from e return tuple(formatted)
def outputs_to_target_and_references(self, outputs: Dict[str, object]) -> str: try:25 unmodified lines return self.yes_answer, [self.yes_answer] return self.no_answer, [self.no_answer]
def get_postprocessors(self) -> List[str]: return self.postprocessors
class KeyValTemplate(Template): """Generate field 'source' from fields designated as input, and fields 'target' and 'references' from fields designated as output, of the processed instance.7 unmodified lines outputs_key_val_seperator: str = ": " use_keys_for_outputs: bool = False
postprocessors: List[str] = field( default_factory=lambda: ["processors.to_string_stripped"] )
def process_dict( self, dic: Dict[str, object], key_val_sep, pairs_sep, use_keys ) -> str:7 unmodified lines pairs.append(key_val_sep.join(key_val)) return pairs_sep.join(pairs)
def inputs_to_source(self, inputs: Dict[str, object]) -> str: return self.process_dict( def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]: ret = self.process_dict( inputs, key_val_sep=self.key_val_seperator, pairs_sep=self.pairs_seperator, use_keys=self.use_keys_for_inputs, ) return (ret, ret)
def outputs_to_target_and_references(self, outputs: Dict[str, object]) -> str: target = self.process_dict(4 unmodified lines ) return target, [target]
def get_postprocessors(self) -> List[str]: return self.postprocessors
class OutputQuantizingTemplate(InputOutputTemplate): quantum: float = 0.1templates.py+69-42