Upload folder using huggingface_hub
Browse files- _version.py +0 -1
- common.py +0 -104
- load.py +0 -15
- renderers.py +0 -132
- serializers.py +0 -130
_version.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
def get_current_version(): return '1.0.31'
|
|
|
|
|
|
common.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
from typing import Union
|
| 2 |
-
|
| 3 |
-
from .card import TaskCard
|
| 4 |
-
from .collections import ItemPicker, RandomPicker
|
| 5 |
-
from .dataclass import OptionalField
|
| 6 |
-
from .operator import SourceOperator
|
| 7 |
-
from .recipe import Recipe, SequentialRecipe
|
| 8 |
-
from .schema import ToUnitxtGroup
|
| 9 |
-
from .splitters import RandomSampler, Sampler, SeparateSplit, SliceSplit, SpreadSplit
|
| 10 |
-
from .stream import MultiStream
|
| 11 |
-
from .templates import RenderTemplatedICL
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
class CommonRecipe(Recipe, SourceOperator):
|
| 15 |
-
card: TaskCard
|
| 16 |
-
demos_pool_name: str = "demos_pool"
|
| 17 |
-
demos_taken_from: str = "train"
|
| 18 |
-
demos_pool_size: int = None
|
| 19 |
-
demos_field: str = "demos"
|
| 20 |
-
num_demos: int = None
|
| 21 |
-
sampler: Sampler = None
|
| 22 |
-
instruction_item: Union[str, int] = None
|
| 23 |
-
template_item: Union[str, int] = None
|
| 24 |
-
system_prompt: str = None
|
| 25 |
-
|
| 26 |
-
def verify(self):
|
| 27 |
-
super().verify()
|
| 28 |
-
|
| 29 |
-
def prepare(self):
|
| 30 |
-
steps = [
|
| 31 |
-
self.card.loader,
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
-
if self.card.preprocess_steps is not None:
|
| 35 |
-
steps.extend(self.card.preprocess_steps)
|
| 36 |
-
|
| 37 |
-
steps.append(self.card.task)
|
| 38 |
-
|
| 39 |
-
if self.demos_pool_size is not None:
|
| 40 |
-
steps.append(
|
| 41 |
-
SeparateSplit(
|
| 42 |
-
from_split=self.demos_taken_from,
|
| 43 |
-
to_split_names=[self.demos_pool_name, self.demos_taken_from],
|
| 44 |
-
to_split_sizes=[int(self.demos_pool_size)],
|
| 45 |
-
)
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
if self.num_demos is not None:
|
| 49 |
-
sampler = self.card.sampler
|
| 50 |
-
|
| 51 |
-
if self.sampler is not None:
|
| 52 |
-
sampler = self.sampler
|
| 53 |
-
|
| 54 |
-
sampler.set_size(self.num_demos)
|
| 55 |
-
|
| 56 |
-
steps.append(
|
| 57 |
-
SpreadSplit(
|
| 58 |
-
source_stream=self.demos_pool_name,
|
| 59 |
-
target_field=self.demos_field,
|
| 60 |
-
sampler=sampler,
|
| 61 |
-
)
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
if self.card.instructions is not None:
|
| 65 |
-
if not self.instruction_item is None:
|
| 66 |
-
picker = ItemPicker(int(self.instruction_item))
|
| 67 |
-
else:
|
| 68 |
-
picker = RandomPicker()
|
| 69 |
-
instruction = picker(self.card.instructions)
|
| 70 |
-
else:
|
| 71 |
-
instruction = None
|
| 72 |
-
|
| 73 |
-
if self.card.templates is not None:
|
| 74 |
-
if self.template_item is None:
|
| 75 |
-
picker = RandomPicker()
|
| 76 |
-
else:
|
| 77 |
-
picker = ItemPicker(self.template_item)
|
| 78 |
-
template = picker(self.card.templates)
|
| 79 |
-
else:
|
| 80 |
-
template = None
|
| 81 |
-
|
| 82 |
-
render = RenderTemplatedICL(
|
| 83 |
-
instruction=instruction,
|
| 84 |
-
template=template,
|
| 85 |
-
demos_field=self.demos_field,
|
| 86 |
-
system_prompt=self.system_prompt,
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
steps.append(render)
|
| 90 |
-
|
| 91 |
-
postprocessors = render.get_postprocessors()
|
| 92 |
-
|
| 93 |
-
steps.append(
|
| 94 |
-
ToUnitxtGroup(
|
| 95 |
-
group="unitxt",
|
| 96 |
-
metrics=self.card.task.metrics,
|
| 97 |
-
postprocessors=postprocessors,
|
| 98 |
-
)
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
self.recipe = SequentialRecipe(steps)
|
| 102 |
-
|
| 103 |
-
def process(self) -> MultiStream:
|
| 104 |
-
return self.recipe()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
load.py
DELETED
|
@@ -1,15 +0,0 @@
|
|
| 1 |
-
from typing import Union
|
| 2 |
-
|
| 3 |
-
from datasets import DatasetDict
|
| 4 |
-
|
| 5 |
-
from .artifact import fetch_artifact
|
| 6 |
-
from .operator import StreamSource
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def load_dataset(source: Union[StreamSource, str]) -> DatasetDict:
|
| 10 |
-
assert isinstance(
|
| 11 |
-
source, (StreamSource, str)
|
| 12 |
-
), "source must be a StreamSource or a string"
|
| 13 |
-
if isinstance(source, str):
|
| 14 |
-
source, _ = fetch_artifact(source)
|
| 15 |
-
return source().to_dataset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderers.py
DELETED
|
@@ -1,132 +0,0 @@
|
|
| 1 |
-
from abc import ABC
|
| 2 |
-
from typing import Any, Dict, List, Optional
|
| 3 |
-
|
| 4 |
-
from .dataclass import InternalField
|
| 5 |
-
from .formats import Format, ICLFormat
|
| 6 |
-
from .instructions import Instruction
|
| 7 |
-
from .operator import Operator, SequentialOperator, StreamInstanceOperator
|
| 8 |
-
from .random_utils import get_random
|
| 9 |
-
from .templates import Template
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
class Renderer(ABC):
|
| 13 |
-
pass
|
| 14 |
-
# @abstractmethod
|
| 15 |
-
# def get_postprocessors(self) -> List[str]:
|
| 16 |
-
# pass
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class RenderTemplate(Renderer, StreamInstanceOperator):
|
| 20 |
-
template: Template
|
| 21 |
-
random_reference: bool = False
|
| 22 |
-
skip_rendered_instance: bool = True
|
| 23 |
-
|
| 24 |
-
def process(
|
| 25 |
-
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 26 |
-
) -> Dict[str, Any]:
|
| 27 |
-
if self.skip_rendered_instance:
|
| 28 |
-
if (
|
| 29 |
-
"inputs" not in instance
|
| 30 |
-
and "outputs" not in instance
|
| 31 |
-
and "source" in instance
|
| 32 |
-
and "target" in instance
|
| 33 |
-
and "references" in instance
|
| 34 |
-
):
|
| 35 |
-
return instance
|
| 36 |
-
|
| 37 |
-
inputs = instance["inputs"]
|
| 38 |
-
outputs = instance["outputs"]
|
| 39 |
-
|
| 40 |
-
source = self.template.process_inputs(inputs)
|
| 41 |
-
targets = self.template.process_outputs(outputs)
|
| 42 |
-
|
| 43 |
-
if self.template.is_multi_reference:
|
| 44 |
-
assert isinstance(targets, list), f"{targets} must be a list"
|
| 45 |
-
references = targets
|
| 46 |
-
if self.random_reference:
|
| 47 |
-
target = get_random().choice(references)
|
| 48 |
-
else:
|
| 49 |
-
if len(references) == 0:
|
| 50 |
-
raise ValueError("No references found")
|
| 51 |
-
target = references[0]
|
| 52 |
-
else:
|
| 53 |
-
references = [targets]
|
| 54 |
-
target = targets
|
| 55 |
-
|
| 56 |
-
instance.update(
|
| 57 |
-
{
|
| 58 |
-
"source": source,
|
| 59 |
-
"target": target,
|
| 60 |
-
"references": references,
|
| 61 |
-
}
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
return instance
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
class RenderDemonstrations(RenderTemplate):
|
| 68 |
-
demos_field: str
|
| 69 |
-
|
| 70 |
-
def process(
|
| 71 |
-
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 72 |
-
) -> Dict[str, Any]:
|
| 73 |
-
demos = instance.get(self.demos_field, [])
|
| 74 |
-
|
| 75 |
-
processed_demos = []
|
| 76 |
-
for demo_instance in demos:
|
| 77 |
-
demo_instance = super().process(demo_instance)
|
| 78 |
-
processed_demos.append(demo_instance)
|
| 79 |
-
|
| 80 |
-
instance[self.demos_field] = processed_demos
|
| 81 |
-
|
| 82 |
-
return instance
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class RenderInstruction(Renderer, StreamInstanceOperator):
|
| 86 |
-
instruction: Instruction
|
| 87 |
-
|
| 88 |
-
def process(
|
| 89 |
-
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 90 |
-
) -> Dict[str, Any]:
|
| 91 |
-
if self.instruction is not None:
|
| 92 |
-
instance["instruction"] = self.instruction()
|
| 93 |
-
else:
|
| 94 |
-
instance["instruction"] = ""
|
| 95 |
-
return instance
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
class RenderFormat(Renderer, StreamInstanceOperator):
|
| 99 |
-
format: Format
|
| 100 |
-
demos_field: str = None
|
| 101 |
-
|
| 102 |
-
def process(
|
| 103 |
-
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 104 |
-
) -> Dict[str, Any]:
|
| 105 |
-
demos_instances = instance.pop(self.demos_field, None)
|
| 106 |
-
if demos_instances is not None:
|
| 107 |
-
instance["source"] = self.format.format(
|
| 108 |
-
instance, demos_instances=demos_instances
|
| 109 |
-
)
|
| 110 |
-
else:
|
| 111 |
-
instance["source"] = self.format.format(instance)
|
| 112 |
-
return instance
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
class StandardRenderer(Renderer, SequentialOperator):
|
| 116 |
-
template: Template
|
| 117 |
-
instruction: Instruction = None
|
| 118 |
-
demos_field: str = None
|
| 119 |
-
format: ICLFormat = None
|
| 120 |
-
|
| 121 |
-
steps: List[Operator] = InternalField(default_factory=list)
|
| 122 |
-
|
| 123 |
-
def prepare(self):
|
| 124 |
-
self.steps = [
|
| 125 |
-
RenderTemplate(template=self.template),
|
| 126 |
-
RenderDemonstrations(template=self.template, demos_field=self.demos_field),
|
| 127 |
-
RenderInstruction(instruction=self.instruction),
|
| 128 |
-
RenderFormat(format=self.format, demos_field=self.demos_field),
|
| 129 |
-
]
|
| 130 |
-
|
| 131 |
-
def get_postprocessors(self):
|
| 132 |
-
return self.template.get_postprocessors()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
serializers.py
DELETED
|
@@ -1,130 +0,0 @@
|
|
| 1 |
-
from abc import ABC, abstractmethod
|
| 2 |
-
from copy import deepcopy
|
| 3 |
-
from typing import (
|
| 4 |
-
Any,
|
| 5 |
-
Dict,
|
| 6 |
-
List,
|
| 7 |
-
)
|
| 8 |
-
|
| 9 |
-
from .operators import FieldOperator
|
| 10 |
-
|
| 11 |
-
"""
|
| 12 |
-
TableSerializer converts a given table into a flat sequence with special symbols.
|
| 13 |
-
Input table format must be:
|
| 14 |
-
{"header": ["col1", "col2"], "rows": [["row11", "row12"], ["row21", "row22"], ["row31", "row32"]]}
|
| 15 |
-
Output format varies depending on the chosen serializer. Abstract class at the top defines structure of a typical table serializer that any concrete implementation should follow.
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class TableSerializer(ABC, FieldOperator):
|
| 20 |
-
# main method to serialize a table
|
| 21 |
-
@abstractmethod
|
| 22 |
-
def serialize_table(self, table_content: Dict) -> str:
|
| 23 |
-
pass
|
| 24 |
-
|
| 25 |
-
# method to process table header
|
| 26 |
-
@abstractmethod
|
| 27 |
-
def process_header(self, header: List):
|
| 28 |
-
pass
|
| 29 |
-
|
| 30 |
-
# method to process a table row
|
| 31 |
-
@abstractmethod
|
| 32 |
-
def process_row(self, row: List, row_index: int):
|
| 33 |
-
pass
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Concrete classes implementing table serializers follow..
|
| 37 |
-
"""
|
| 38 |
-
Indexed Row Major Table Serializer.
|
| 39 |
-
Commonly used row major serialization format.
|
| 40 |
-
Format: col : col1 | col2 | col 3 row 1 : val1 | val2 | val3 | val4 row 2 : val1 | ...
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
class IndexedRowMajorTableSerializer(TableSerializer):
|
| 45 |
-
def process_value(self, table: Any) -> Any:
|
| 46 |
-
table_input = deepcopy(table)
|
| 47 |
-
return self.serialize_table(table_content=table_input)
|
| 48 |
-
|
| 49 |
-
# main method that processes a table
|
| 50 |
-
# table_content must be in the presribed input format
|
| 51 |
-
def serialize_table(self, table_content: Dict) -> str:
|
| 52 |
-
# Extract headers and rows from the dictionary
|
| 53 |
-
header = table_content.get("header", [])
|
| 54 |
-
rows = table_content.get("rows", [])
|
| 55 |
-
|
| 56 |
-
assert header and rows, "Incorrect input table format"
|
| 57 |
-
|
| 58 |
-
# Process table header first
|
| 59 |
-
serialized_tbl_str = self.process_header(header) + " "
|
| 60 |
-
|
| 61 |
-
# Process rows sequentially starting from row 1
|
| 62 |
-
for i, row in enumerate(rows, start=1):
|
| 63 |
-
serialized_tbl_str += self.process_row(row, row_index=i) + " "
|
| 64 |
-
|
| 65 |
-
# return serialized table as a string
|
| 66 |
-
return serialized_tbl_str.strip()
|
| 67 |
-
|
| 68 |
-
# serialize header into a string containing the list of column names separated by '|' symbol
|
| 69 |
-
def process_header(self, header: List):
|
| 70 |
-
return "col : " + " | ".join(header)
|
| 71 |
-
|
| 72 |
-
# serialize a table row into a string containing the list of cell values separated by '|'
|
| 73 |
-
def process_row(self, row: List, row_index: int):
|
| 74 |
-
serialized_row_str = ""
|
| 75 |
-
row_cell_values = [
|
| 76 |
-
str(value) if isinstance(value, (int, float)) else value for value in row
|
| 77 |
-
]
|
| 78 |
-
|
| 79 |
-
serialized_row_str += " | ".join(row_cell_values)
|
| 80 |
-
|
| 81 |
-
return f"row {row_index} : {serialized_row_str}"
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"""
|
| 85 |
-
Markdown Table Serializer.
|
| 86 |
-
Markdown table format is used in GitHub code primarily.
|
| 87 |
-
Format:
|
| 88 |
-
|col1|col2|col3|
|
| 89 |
-
|---|---|---|
|
| 90 |
-
|A|4|1|
|
| 91 |
-
|I|2|1|
|
| 92 |
-
...
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
class MarkdownTableSerializer(TableSerializer):
|
| 97 |
-
def process_value(self, table: Any) -> Any:
|
| 98 |
-
table_input = deepcopy(table)
|
| 99 |
-
return self.serialize_table(table_content=table_input)
|
| 100 |
-
|
| 101 |
-
# main method that serializes a table.
|
| 102 |
-
# table_content must be in the presribed input format.
|
| 103 |
-
def serialize_table(self, table_content: Dict) -> str:
|
| 104 |
-
# Extract headers and rows from the dictionary
|
| 105 |
-
header = table_content.get("header", [])
|
| 106 |
-
rows = table_content.get("rows", [])
|
| 107 |
-
|
| 108 |
-
assert header and rows, "Incorrect input table format"
|
| 109 |
-
|
| 110 |
-
# Process table header first
|
| 111 |
-
serialized_tbl_str = self.process_header(header)
|
| 112 |
-
|
| 113 |
-
# Process rows sequentially starting from row 1
|
| 114 |
-
for i, row in enumerate(rows, start=1):
|
| 115 |
-
serialized_tbl_str += self.process_row(row, row_index=i)
|
| 116 |
-
|
| 117 |
-
# return serialized table as a string
|
| 118 |
-
return serialized_tbl_str.strip()
|
| 119 |
-
|
| 120 |
-
# serialize header into a string containing the list of column names
|
| 121 |
-
def process_header(self, header: List):
|
| 122 |
-
header_str = "|{}|\n".format("|".join(header))
|
| 123 |
-
header_str += "|{}|\n".format("|".join(["---"] * len(header)))
|
| 124 |
-
return header_str
|
| 125 |
-
|
| 126 |
-
# serialize a table row into a string containing the list of cell values
|
| 127 |
-
def process_row(self, row: List, row_index: int):
|
| 128 |
-
row_str = ""
|
| 129 |
-
row_str += "|{}|\n".format("|".join(str(cell) for cell in row))
|
| 130 |
-
return row_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|