Upload templates.py with huggingface_hub
Browse files- templates.py +37 -0
templates.py
CHANGED
|
@@ -181,6 +181,43 @@ class InputOutputTemplate(Template):
|
|
| 181 |
return self.postprocessors
|
| 182 |
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
class OutputQuantizingTemplate(InputOutputTemplate):
|
| 185 |
quantum: float = 0.1
|
| 186 |
|
|
|
|
| 181 |
return self.postprocessors
|
| 182 |
|
| 183 |
|
| 184 |
+
class KeyValTemplate(Template):
|
| 185 |
+
pairs_seperator: str = ", "
|
| 186 |
+
key_val_seperator: str = ": "
|
| 187 |
+
use_keys_for_inputs: bool = True
|
| 188 |
+
outputs_key_val_seperator: str = ": "
|
| 189 |
+
use_keys_for_outputs: bool = False
|
| 190 |
+
|
| 191 |
+
postprocessors: List[str] = field(default_factory=lambda: ["processors.to_string_stripped"])
|
| 192 |
+
|
| 193 |
+
def process_dict(self, dic: Dict[str, object], key_val_sep, pairs_sep, use_keys) -> str:
|
| 194 |
+
dic = {k: ", ".join(v) if isinstance(v, list) else v for k, v in dic.items()}
|
| 195 |
+
pairs = []
|
| 196 |
+
for key, val in dic.items():
|
| 197 |
+
key_val = [key, val] if use_keys else [val]
|
| 198 |
+
pairs.append(key_val_sep.join(key_val))
|
| 199 |
+
return pairs_sep.join(pairs)
|
| 200 |
+
|
| 201 |
+
def process_inputs(self, inputs: Dict[str, object]) -> str:
|
| 202 |
+
return self.process_dict(
|
| 203 |
+
inputs,
|
| 204 |
+
key_val_sep=self.key_val_seperator,
|
| 205 |
+
pairs_sep=self.pairs_seperator,
|
| 206 |
+
use_keys=self.use_keys_for_inputs,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
def process_outputs(self, outputs: Dict[str, object]) -> str:
|
| 210 |
+
return self.process_dict(
|
| 211 |
+
outputs,
|
| 212 |
+
key_val_sep=self.key_val_seperator,
|
| 213 |
+
pairs_sep=self.pairs_seperator,
|
| 214 |
+
use_keys=self.use_keys_for_outputs,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
def get_postprocessors(self) -> List[str]:
|
| 218 |
+
return self.postprocessors
|
| 219 |
+
|
| 220 |
+
|
| 221 |
class OutputQuantizingTemplate(InputOutputTemplate):
|
| 222 |
quantum: float = 0.1
|
| 223 |
|