Upload validate.py with huggingface_hub
Browse files- validate.py +46 -0
validate.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .text_utils import split_words
|
2 |
+
from .stream import StreamInstanceOperator, InstanceOperatorWithGlobalAccess, Artifact
|
3 |
+
|
4 |
+
from datasets import Value, Features, Dataset, Sequence
|
5 |
+
|
6 |
+
from dataclasses import field
|
7 |
+
from typing import Dict, Any
|
8 |
+
from abc import ABC, abstractmethod
|
9 |
+
|
10 |
+
|
11 |
+
class Validator(ABC):
|
12 |
+
pass
|
13 |
+
|
14 |
+
|
15 |
+
class ValidateSchema(Validator, StreamInstanceOperator):
|
16 |
+
schema: Features = None
|
17 |
+
|
18 |
+
def verify(self):
|
19 |
+
assert isinstance(self.schema, Features), "Schema must be an instance of Features"
|
20 |
+
assert self.schema is not None, "Schema must be specified"
|
21 |
+
|
22 |
+
def verify_first_instance(self, instance):
|
23 |
+
for field in self.standart_fields:
|
24 |
+
assert field in instance, f'Field "{field}" is missing in the first instance'
|
25 |
+
|
26 |
+
def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
|
27 |
+
return instance
|
28 |
+
|
29 |
+
|
30 |
+
class StandardSchema(Features):
|
31 |
+
def __init__(self):
|
32 |
+
super().__init__(
|
33 |
+
{
|
34 |
+
"source": Value("string"),
|
35 |
+
"target": Value("string"),
|
36 |
+
"references": Sequence(Value("string")),
|
37 |
+
"metrics": Sequence(Value("string")),
|
38 |
+
"parser": Value("string"),
|
39 |
+
# 'group': Value('string'),
|
40 |
+
# 'guidance': Value('string'),
|
41 |
+
}
|
42 |
+
)
|
43 |
+
|
44 |
+
|
45 |
+
class ValidateStandartSchema:
|
46 |
+
schema: Features = field(default_factory=StandardSchema)
|