-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathschemas.py
More file actions
353 lines (276 loc) · 11.1 KB
/
schemas.py
File metadata and controls
353 lines (276 loc) · 11.1 KB
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
import datetime
import logging
import typing
import pydantic
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class BoundingBox(pydantic.BaseModel):
x1: float
y1: float
x2: float
y2: float
@classmethod
def from_coords(cls, coords: typing.Any, raise_on_error: bool = True) -> "BoundingBox | None":
"""
Create BoundingBox from coordinate list [x1, y1, x2, y2].
Args:
coords: List of 4 float coordinates
raise_on_error: If True (default), raises ValueError on invalid input.
If False, returns None on invalid input.
"""
if not isinstance(coords, list) or len(coords) != 4:
if raise_on_error:
if not isinstance(coords, list):
raise ValueError(f"BoundingBox coords must be a list, got {type(coords).__name__}")
raise ValueError(f"BoundingBox coords must have 4 elements, got {len(coords)}")
return None
try:
return cls(x1=coords[0], y1=coords[1], x2=coords[2], y2=coords[3])
except (TypeError, pydantic.ValidationError) as e:
if raise_on_error:
raise ValueError(f"Invalid BoundingBox coordinates: {e}") from e
return None
@property
def width(self) -> float:
return abs(self.x2 - self.x1)
@property
def height(self) -> float:
return abs(self.y2 - self.y1)
def to_string(self) -> str:
return f"{self.x1},{self.y1},{self.x2},{self.y2}"
def to_path(self) -> str:
return "-".join([str(int(x)) for x in [self.x1, self.y1, self.x2, self.y2]])
def to_tuple(self):
return (self.x1, self.y1, self.x2, self.y2)
class AlgorithmReference(pydantic.BaseModel):
name: str
key: str
class AlgorithmCategoryMapResponse(pydantic.BaseModel):
data: list[dict] = pydantic.Field(
default_factory=dict,
description="Complete data for each label, such as id, gbif_key, explicit index, source, etc.",
examples=[
[
{"label": "Moth", "index": 0, "gbif_key": 1234},
{"label": "Not a moth", "index": 1, "gbif_key": 5678},
]
],
)
labels: list[str] = pydantic.Field(
default_factory=list,
description="A simple list of string labels, in the correct index order used by the model.",
examples=[["Moth", "Not a moth"]],
)
version: str | None = pydantic.Field(
default=None,
description="The version of the category map. Can be a descriptive string or a version number.",
examples=["LepNet2021-with-2023-mods"],
)
description: str | None = pydantic.Field(
default=None,
description="A description of the category map used to train. e.g. source, purpose and modifications.",
examples=["LepNet2021 with Schmidt 2023 corrections. Limited to species with > 1000 observations."],
)
uri: str | None = pydantic.Field(
default=None,
description="A URI to the category map file, could be a public web URL or object store path.",
)
class AlgorithmConfigResponse(pydantic.BaseModel):
name: str
key: str = pydantic.Field(
description=("A unique key for an algorithm to lookup the category map (class list) and other metadata."),
)
description: str | None = None
task_type: str | None = pydantic.Field(
default=None,
description="The type of task the model is trained for. e.g. 'detection', 'classification', 'embedding', etc.",
examples=["detection", "classification", "segmentation", "embedding"],
)
version: int = pydantic.Field(
default=1,
description="A sortable version number for the model. Increment this number when the model is updated.",
)
version_name: str | None = pydantic.Field(
default=None,
description="A complete version name e.g. '2021-01-01', 'LepNet2021'.",
)
uri: str | None = pydantic.Field(
default=None,
description="A URI to the weight or model details, could be a public web URL or object store path.",
)
category_map: AlgorithmCategoryMapResponse | None = None
class Config:
extra = "ignore"
class ClassificationResponse(pydantic.BaseModel):
classification: str
labels: list[str] | None = pydantic.Field(
default=None,
description=(
"A list of all possible labels for the model, in the correct order. "
"Omitted if the model has too many labels to include for each classification in the response. "
"Use the category map from the algorithm to get the full list of labels and metadata."
),
)
scores: list[float] = []
logits: list[float] | None = None
inference_time: float | None = None
algorithm: AlgorithmReference
terminal: bool = True
timestamp: datetime.datetime
class SourceImageRequest(pydantic.BaseModel):
# @TODO bring over new SourceImage & b64 validation from the lepsAI repo
id: str
url: str
# b64: str | None = None
class SourceImageResponse(pydantic.BaseModel):
id: str
url: str
class Config:
extra = "ignore"
KnownPipelineChoices = typing.Literal[
"panama_moths_2023",
"quebec_vermont_moths_2023",
"uk_denmark_moths_2023",
]
class DetectionRequest(pydantic.BaseModel):
source_image: SourceImageRequest # the 'original' image
bbox: BoundingBox | None = None
crop_image_url: str | None = None
algorithm: AlgorithmReference
class DetectionResponse(pydantic.BaseModel):
source_image_id: str
bbox: BoundingBox | None = None
inference_time: float | None = None
algorithm: AlgorithmReference
timestamp: datetime.datetime
crop_image_url: str | None = None
classifications: list[ClassificationResponse] = []
class PipelineRequestConfigParameters(dict):
"""Parameters used to configure a pipeline request.
Accepts any serializable key-value pair.
Example: {"force_reprocess": True, "auth_token": "abc123"}
Supported parameters are defined by the pipeline in the processing service
and should be published in the Pipeline's info response.
Parameters that are used by Antenna before sending the request to the Processing Service
should be prefixed with "request_".
Example: {"request_source_image_batch_size": 8}
Such parameters need to be ignored by the schema in the Processing Service, or
removed before sending the request to the Processing Service.
"""
pass
class PipelineRequest(pydantic.BaseModel):
pipeline: str
source_images: list[SourceImageRequest]
detections: list[DetectionRequest] | None = None
config: PipelineRequestConfigParameters | dict | None = None
def summary(self) -> str:
"""
Return a human-friendly summary string of the request key details.
(number of images, pipeline name, number of detections, etc.)
e.g. "pipeline request with 10 images and 25 detections to 'panama_moths_2023'"
Returns:
str: A summary string.
"""
num_images = len(self.source_images)
num_detections = len(self.detections) if self.detections else 0
return (
f"pipeline request with {num_images} image{'s' if num_images != 1 else ''} "
f"and {num_detections} detection{'s' if num_detections != 1 else ''} "
f"to pipeline '{self.pipeline}'"
)
class PipelineResultsResponse(pydantic.BaseModel):
# pipeline: PipelineChoice
pipeline: str
algorithms: dict[str, AlgorithmConfigResponse] = pydantic.Field(
default_factory=dict,
description=(
"A dictionary of all algorithms used in the pipeline, including their class list and other "
"metadata, keyed by the algorithm key. "
"DEPRECATED: Algorithms should only be provided in the ProcessingServiceInfoResponse."
),
depreciated=True,
)
total_time: float
source_images: list[SourceImageResponse]
detections: list[DetectionResponse]
errors: list | str | None = None
class PipelineResultsError(pydantic.BaseModel):
"""Error result when pipeline processing fails for an image."""
error: str
image_id: str | None = None
class PipelineProcessingTask(pydantic.BaseModel):
"""
A task representing a single image or detection to be processed in an async pipeline.
"""
id: str
image_id: str
image_url: str
reply_subject: str | None = None # The NATS subject to send the result to
# TODO: Do we need these?
# detections: list[DetectionRequest] | None = None
# config: PipelineRequestConfigParameters | dict | None = None
class PipelineTaskResult(pydantic.BaseModel):
"""
The result from processing a single PipelineProcessingTask.
Note: this schema is called `AntennaTaskResult` in the ADC worker processing service.
"""
reply_subject: str # The reply_subject from the PipelineProcessingTask
result: PipelineResultsResponse | PipelineResultsError
class PipelineStageParam(pydantic.BaseModel):
"""A configurable parameter of a stage of a pipeline."""
name: str
key: str
category: str = "default"
class PipelineStage(pydantic.BaseModel):
"""A configurable stage of a pipeline."""
key: str
name: str
params: list[PipelineStageParam] = []
description: str | None = None
class PipelineConfigResponse(pydantic.BaseModel):
"""
Details of a pipeline available in the processing service.
Includes the algorithm (model) definitions used in the pipeline, and
their category maps (class lists).
This must be retrieved from the processing service API and saved in Antenna
before images are submitted for processing.
"""
name: str
slug: str
version: int
description: str | None = None
algorithms: list[AlgorithmConfigResponse] = []
stages: list[PipelineStage] = []
class ProcessingServiceInfoResponse(pydantic.BaseModel):
"""
Information about the processing service returned from the Processing Service backend.
"""
name: str
description: str | None = None
pipelines: list[PipelineConfigResponse] = []
algorithms: list[AlgorithmConfigResponse] = []
class ProcessingServiceStatusResponse(pydantic.BaseModel):
"""
Status response returned by the Antenna API about the Processing Service.
"""
timestamp: datetime.datetime
request_successful: bool
pipeline_configs: list[PipelineConfigResponse] = []
error: str | None = None
server_live: bool | None = None
pipelines_online: list[str] = []
endpoint_url: str | None = None
latency: float
class PipelineRegistrationResponse(pydantic.BaseModel):
timestamp: datetime.datetime
success: bool
error: str | None = None
pipelines: list[PipelineConfigResponse] = []
pipelines_created: list[str] = []
algorithms_created: list[str] = []
class AsyncPipelineRegistrationRequest(pydantic.BaseModel):
"""
Request to register pipelines from an async processing service
"""
processing_service_name: str
pipelines: list[PipelineConfigResponse] = []