Upload renderers.py with huggingface_hub
Browse files- renderers.py +107 -0
renderers.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from abc import ABC, abstractmethod
|
2 |
+
from typing import Any, Dict, List
|
3 |
+
|
4 |
+
from .dataclass import InternalField
|
5 |
+
from .formats import Format, ICLFormat
|
6 |
+
from .instructions import Instruction
|
7 |
+
from .operator import Operator, SequntialOperator, StreamInstanceOperator
|
8 |
+
from .random_utils import 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 |
+
|
23 |
+
def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
|
24 |
+
inputs = instance.pop("inputs")
|
25 |
+
outputs = instance.pop("outputs")
|
26 |
+
|
27 |
+
source = self.template.process_inputs(inputs)
|
28 |
+
targets = self.template.process_outputs(outputs)
|
29 |
+
|
30 |
+
if self.template.is_multi_reference:
|
31 |
+
references = targets
|
32 |
+
if self.random_reference:
|
33 |
+
target = random.choice(references)
|
34 |
+
else:
|
35 |
+
if len(references) == 0:
|
36 |
+
raise ValueError("No references found")
|
37 |
+
target = references[0]
|
38 |
+
else:
|
39 |
+
references = [targets]
|
40 |
+
target = targets
|
41 |
+
|
42 |
+
instance.update(
|
43 |
+
{
|
44 |
+
"source": source,
|
45 |
+
"target": target,
|
46 |
+
"references": references,
|
47 |
+
}
|
48 |
+
)
|
49 |
+
|
50 |
+
return instance
|
51 |
+
|
52 |
+
|
53 |
+
class RenderDemonstrations(RenderTemplate):
|
54 |
+
demos_field: str
|
55 |
+
|
56 |
+
def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
|
57 |
+
demos = instance.get(self.demos_field, [])
|
58 |
+
|
59 |
+
processed_demos = []
|
60 |
+
for demo_instance in demos:
|
61 |
+
processed_demo = super().process(demo_instance)
|
62 |
+
processed_demos.append(processed_demo)
|
63 |
+
|
64 |
+
instance[self.demos_field] = processed_demos
|
65 |
+
|
66 |
+
return instance
|
67 |
+
|
68 |
+
|
69 |
+
class RenderInstruction(Renderer, StreamInstanceOperator):
|
70 |
+
instruction: Instruction
|
71 |
+
|
72 |
+
def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
|
73 |
+
instance["instruction"] = self.instruction()
|
74 |
+
return instance
|
75 |
+
|
76 |
+
|
77 |
+
class RenderFormat(Renderer, StreamInstanceOperator):
|
78 |
+
format: Format
|
79 |
+
demos_field: str = None
|
80 |
+
|
81 |
+
def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
|
82 |
+
demos_instances = instance.pop(self.demos_field, None)
|
83 |
+
if demos_instances is not None:
|
84 |
+
instance["source"] = self.format.format(instance, demos_instances=demos_instances)
|
85 |
+
else:
|
86 |
+
instance["source"] = self.format.format(instance)
|
87 |
+
return instance
|
88 |
+
|
89 |
+
|
90 |
+
class StandardRenderer(Renderer, SequntialOperator):
|
91 |
+
template: Template
|
92 |
+
instruction: Instruction = None
|
93 |
+
demos_field: str = None
|
94 |
+
format: ICLFormat = None
|
95 |
+
|
96 |
+
steps: List[Operator] = InternalField(default_factory=list)
|
97 |
+
|
98 |
+
def prepare(self):
|
99 |
+
self.steps = [
|
100 |
+
RenderTemplate(template=self.template),
|
101 |
+
RenderDemonstrations(template=self.template, demos_field=self.demos_field),
|
102 |
+
RenderInstruction(instruction=self.instruction),
|
103 |
+
RenderFormat(format=self.format, demos_field=self.demos_field),
|
104 |
+
]
|
105 |
+
|
106 |
+
def get_postprocessors(self):
|
107 |
+
return self.template.get_postprocessors()
|