Upload instructions.py with huggingface_hub
Browse files- instructions.py +28 -7
instructions.py
CHANGED
@@ -1,24 +1,45 @@
|
|
1 |
from abc import abstractmethod
|
2 |
-
from typing import Dict
|
3 |
|
4 |
-
from .artifact import Artifact
|
5 |
from .collections import ListCollection
|
|
|
|
|
6 |
|
7 |
|
8 |
-
class Instruction(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@abstractmethod
|
10 |
-
def
|
11 |
pass
|
12 |
|
13 |
|
14 |
class TextualInstruction(Instruction):
|
15 |
text: str
|
16 |
|
17 |
-
def
|
18 |
return self.text
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
|
23 |
|
24 |
class InstructionsList(ListCollection):
|
|
|
1 |
from abc import abstractmethod
|
2 |
+
from typing import Any, Dict, Optional
|
3 |
|
|
|
4 |
from .collections import ListCollection
|
5 |
+
from .dataclass import NonPositionalField
|
6 |
+
from .operator import StreamInstanceOperator
|
7 |
|
8 |
|
9 |
+
class Instruction(StreamInstanceOperator):
|
10 |
+
"""The role of instruction is to add instruction to every instance.
|
11 |
+
|
12 |
+
Meaning the instruction is taking the instance and generating instruction field for it.
|
13 |
+
"""
|
14 |
+
|
15 |
+
skip_rendered_instance: bool = NonPositionalField(default=True)
|
16 |
+
|
17 |
+
def process(
|
18 |
+
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
19 |
+
) -> Dict[str, Any]:
|
20 |
+
if self.skip_rendered_instance:
|
21 |
+
if "instruction" in instance:
|
22 |
+
return instance
|
23 |
+
|
24 |
+
instance["instruction"] = self.get_instruction(instance)
|
25 |
+
|
26 |
+
return instance
|
27 |
+
|
28 |
@abstractmethod
|
29 |
+
def get_instruction(self, instance: Dict[str, object]) -> str:
|
30 |
pass
|
31 |
|
32 |
|
33 |
class TextualInstruction(Instruction):
|
34 |
text: str
|
35 |
|
36 |
+
def get_instruction(self, instance: Dict[str, object]) -> str:
|
37 |
return self.text
|
38 |
|
39 |
+
|
40 |
+
class EmptyInstruction(Instruction):
|
41 |
+
def get_instruction(self, instance: Dict[str, object]) -> str:
|
42 |
+
return ""
|
43 |
|
44 |
|
45 |
class InstructionsList(ListCollection):
|