/
model.py
358 lines (276 loc) · 9.79 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# coding=utf-8
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Common model for task definitions
"""
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
from uuid import uuid4
from fastapi import Query
from pydantic import BaseModel, Field, validator
from pydantic.generics import GenericModel
from rubrix._constants import MAX_KEYWORD_LENGTH
from rubrix.server.commons.helpers import flatten_dict, limit_value_length
from rubrix.server.commons.settings import settings
from rubrix.server.tasks.commons.api.errors import MetadataLimitExceededError
class EsRecordDataFieldNames(str, Enum):
"""Common elasticsearch field names"""
predicted_as = "predicted_as"
annotated_as = "annotated_as"
annotated_by = "annotated_by"
predicted_by = "predicted_by"
status = "status"
predicted = "predicted"
score = "score"
words = "words"
event_timestamp = "event_timestamp"
last_updated = "last_updated"
def __str__(self):
return self.value
class SortOrder(str, Enum):
asc = "asc"
desc = "desc"
class SortableField(BaseModel):
"""Sortable field structure"""
id: str
order: SortOrder = SortOrder.asc
class BulkResponse(BaseModel):
"""
Data info for bulk results
Attributes
----------
dataset:
The dataset name
processed:
Number of records in bulk
failed:
Number of failed records
"""
dataset: str
processed: int
failed: int = 0
@dataclass
class PaginationParams:
"""Query pagination params"""
limit: int = Query(50, gte=0, le=1000, description="Response records limit")
from_: int = Query(
0, ge=0, le=10000, alias="from", description="Record sequence from"
)
class BaseAnnotation(BaseModel):
"""
Annotation class base
Attributes:
-----------
agent:
Which agent or component makes the annotation. We should find model annotations, user annotations,
or some other human-supervised automatic process.
"""
agent: str = Field(max_length=64)
class TaskType(str, Enum):
"""
The available task types:
**text_classification**, for text classification tasks
**token_classification**, for token classification tasks
"""
text_classification = "TextClassification"
token_classification = "TokenClassification"
text2text = "Text2Text"
multi_task_text_token_classification = "MultitaskTextTokenClassification"
class TaskStatus(str, Enum):
"""
Task data status:
**Default**, default status, for no provided status records.
**Edited**, normally used when original annotation was modified but not yet validated (confirmed).
**Discarded**, for records that will be excluded for analysis.
**Validated**, when annotation was confirmed as ok.
"""
default = "Default"
edited = "Edited" # TODO: DEPRECATE
discarded = "Discarded"
validated = "Validated"
class PredictionStatus(str, Enum):
"""
The prediction status:
**OK**, for record containing a success prediction
**KO**, for record containing a wrong prediction
"""
OK = "ok"
KO = "ko"
Annotation = TypeVar("Annotation", bound=BaseAnnotation)
class BaseRecord(GenericModel, Generic[Annotation]):
"""
Minimal dataset record information
Attributes:
-----------
id:
The record id
metadata:
The metadata related to record
event_timestamp:
The timestamp when record event was triggered
"""
id: Optional[Union[int, str]] = Field(None)
metadata: Dict[str, Any] = Field(default=None)
event_timestamp: Optional[datetime] = None
status: Optional[TaskStatus] = None
prediction: Optional[Annotation] = None
annotation: Optional[Annotation] = None
metrics: Dict[str, Any] = Field(default_factory=dict)
search_keywords: Optional[List[str]] = None
@validator("search_keywords")
def remove_duplicated_keywords(cls, value) -> List[str]:
"""Remove duplicated keywords"""
if value:
return list(set(value))
@validator("id", always=True)
def default_id_if_none_provided(cls, id: Optional[str]) -> str:
"""Validates id info and sets a random uuid if not provided"""
if id is None:
return str(uuid4())
return id
@validator("metadata", pre=True)
def flatten_metadata(cls, metadata: Dict[str, Any]):
"""
A fastapi validator for flatten metadata dictionary
Parameters
----------
metadata:
The metadata dictionary
Returns
-------
A flatten version of metadata dictionary
"""
if metadata:
metadata = flatten_dict(metadata, drop_empty=True)
metadata = limit_value_length(metadata, max_length=MAX_KEYWORD_LENGTH)
return metadata
@validator("status", always=True)
def fill_default_value(cls, status: TaskStatus):
"""Fastapi validator for set default task status"""
return TaskStatus.default if status is None else status
@classmethod
def task(cls) -> TaskType:
"""The task type related to this task info"""
raise NotImplementedError
@property
def predicted(self) -> Optional[PredictionStatus]:
"""The task record prediction status (if any)"""
return None
@property
def predicted_as(self) -> Optional[List[str]]:
"""Predictions strings representation"""
return None
@property
def annotated_as(self) -> Optional[List[str]]:
"""Annotations strings representation"""
return None
@property
def scores(self) -> Optional[List[float]]:
"""Prediction scores"""
return None
def all_text(self) -> str:
"""All textual information related to record"""
raise NotImplementedError
@property
def predicted_by(self) -> List[str]:
"""The prediction agents"""
if self.prediction:
return [self.prediction.agent]
return []
@property
def annotated_by(self) -> List[str]:
"""The annotation agents"""
if self.annotation:
return [self.annotation.agent]
return []
def extended_fields(self) -> Dict[str, Any]:
"""
Used for extends fields to store in db. Tasks that would include extra
properties than commons (predicted, annotated_as,....) could implement
this method.
"""
return {
EsRecordDataFieldNames.predicted: self.predicted,
EsRecordDataFieldNames.annotated_as: self.annotated_as,
EsRecordDataFieldNames.predicted_as: self.predicted_as,
EsRecordDataFieldNames.annotated_by: self.annotated_by,
EsRecordDataFieldNames.predicted_by: self.predicted_by,
EsRecordDataFieldNames.score: self.scores,
}
def dict(self, *args, **kwargs) -> "DictStrAny":
"""
Extends base component dict extending object properties
and user defined extended fields
"""
return {
**super().dict(*args, **kwargs),
**self.extended_fields(),
}
class BaseSearchResultsAggregations(BaseModel):
"""
API for result aggregations
Attributes:
-----------
predicted_as: Dict[str, int]
Occurrence info about more relevant predicted terms
annotated_as: Dict[str, int]
Occurrence info about more relevant annotated terms
annotated_by: Dict[str, int]
Occurrence info about more relevant annotation agent terms
predicted_by: Dict[str, int]
Occurrence info about more relevant prediction agent terms
status: Dict[str, int]
Occurrence info about task status
predicted: Dict[str, int]
Occurrence info about task prediction status
words: Dict[str, int]
The word cloud aggregations
metadata: Dict[str, Dict[str, Any]]
The metadata fields aggregations
"""
predicted_as: Dict[str, int] = Field(default_factory=dict)
annotated_as: Dict[str, int] = Field(default_factory=dict)
annotated_by: Dict[str, int] = Field(default_factory=dict)
predicted_by: Dict[str, int] = Field(default_factory=dict)
status: Dict[str, int] = Field(default_factory=dict)
predicted: Dict[str, int] = Field(default_factory=dict)
score: Dict[str, int] = Field(default_factory=dict)
words: Dict[str, int] = Field(default_factory=dict)
metadata: Dict[str, Dict[str, Any]] = Field(default_factory=dict)
Record = TypeVar("Record", bound=BaseRecord)
Aggregations = TypeVar("Aggregations", bound=BaseSearchResultsAggregations)
class BaseSearchResults(GenericModel, Generic[Record, Aggregations]):
"""
API search results
Attributes:
-----------
total:
The total number of records
records:
The selected records to return
aggregations:
Requested aggregations
"""
total: int = 0
records: List[Record] = Field(default_factory=list)
aggregations: Aggregations = None
class ScoreRange(BaseModel):
"""Score range filter"""
range_from: float = Field(default=0.0, alias="from")
range_to: float = Field(default=None, alias="to")
class Config:
allow_population_by_field_name = True