Upload renderers.py with huggingface_hub
Browse files- renderers.py +24 -13
renderers.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
from abc import ABC
|
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,
|
8 |
-
from .random_utils import
|
9 |
from .templates import Template
|
10 |
|
11 |
|
@@ -21,7 +21,9 @@ class RenderTemplate(Renderer, StreamInstanceOperator):
|
|
21 |
random_reference: bool = False
|
22 |
skip_rendered_instance: bool = True
|
23 |
|
24 |
-
def process(
|
|
|
|
|
25 |
if self.skip_rendered_instance:
|
26 |
if (
|
27 |
"inputs" not in instance
|
@@ -32,16 +34,17 @@ class RenderTemplate(Renderer, StreamInstanceOperator):
|
|
32 |
):
|
33 |
return instance
|
34 |
|
35 |
-
inputs = instance
|
36 |
-
outputs = instance
|
37 |
|
38 |
source = self.template.process_inputs(inputs)
|
39 |
targets = self.template.process_outputs(outputs)
|
40 |
|
41 |
if self.template.is_multi_reference:
|
|
|
42 |
references = targets
|
43 |
if self.random_reference:
|
44 |
-
target =
|
45 |
else:
|
46 |
if len(references) == 0:
|
47 |
raise ValueError("No references found")
|
@@ -64,7 +67,9 @@ class RenderTemplate(Renderer, StreamInstanceOperator):
|
|
64 |
class RenderDemonstrations(RenderTemplate):
|
65 |
demos_field: str
|
66 |
|
67 |
-
def process(
|
|
|
|
|
68 |
demos = instance.get(self.demos_field, [])
|
69 |
|
70 |
processed_demos = []
|
@@ -80,7 +85,9 @@ class RenderDemonstrations(RenderTemplate):
|
|
80 |
class RenderInstruction(Renderer, StreamInstanceOperator):
|
81 |
instruction: Instruction
|
82 |
|
83 |
-
def process(
|
|
|
|
|
84 |
if self.instruction is not None:
|
85 |
instance["instruction"] = self.instruction()
|
86 |
else:
|
@@ -92,16 +99,20 @@ class RenderFormat(Renderer, StreamInstanceOperator):
|
|
92 |
format: Format
|
93 |
demos_field: str = None
|
94 |
|
95 |
-
def process(
|
|
|
|
|
96 |
demos_instances = instance.pop(self.demos_field, None)
|
97 |
if demos_instances is not None:
|
98 |
-
instance["source"] = self.format.format(
|
|
|
|
|
99 |
else:
|
100 |
instance["source"] = self.format.format(instance)
|
101 |
return instance
|
102 |
|
103 |
|
104 |
-
class StandardRenderer(Renderer,
|
105 |
template: Template
|
106 |
instruction: Instruction = None
|
107 |
demos_field: str = None
|
|
|
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 |
|
|
|
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
|
|
|
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")
|
|
|
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 = []
|
|
|
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:
|
|
|
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
|