-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
534 lines (405 loc) · 15.9 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# generated by datamodel-codegen:
# filename: public-api.yaml
# timestamp: 2025-03-11T18:57:57+00:00
from __future__ import annotations
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional, Union
from pydantic import AnyUrl, BaseModel, Field, RootModel, confloat, conint, constr
class AnnotationsRequestedEnum(str, Enum):
"""
* `BINARY_CLASSIFICATION` - Binary Classification
* `BOUNDING_BOXES` - Bounding Boxes
"""
BINARY_CLASSIFICATION = "BINARY_CLASSIFICATION"
BOUNDING_BOXES = "BOUNDING_BOXES"
class BBoxGeometry(BaseModel):
"""
Mixin for serializers to handle data in the StrictBaseModel format
"""
left: float
top: float
right: float
bottom: float
x: float
y: float
class BBoxGeometryRequest(BaseModel):
"""
Mixin for serializers to handle data in the StrictBaseModel format
"""
left: float
top: float
right: float
bottom: float
class BlankEnum(Enum):
field_ = ""
class DetectorGroup(BaseModel):
id: str
name: constr(max_length=100)
class DetectorGroupRequest(BaseModel):
name: constr(min_length=1, max_length=100)
class DetectorTypeEnum(str, Enum):
detector = "detector"
class EdgeModelInfo(BaseModel):
"""
Information for the model running on edge, including temporary presigned urls to the model binaries
"""
model_binary_id: Optional[str] = None
model_binary_url: Optional[str] = None
oodd_model_binary_id: Optional[str] = None
oodd_model_binary_url: Optional[str] = None
pipeline_config: Optional[Any] = None
oodd_pipeline_config: Optional[Any] = None
predictor_metadata: Optional[Any] = None
class EscalationTypeEnum(str, Enum):
"""
* `STANDARD` - STANDARD
* `NO_HUMAN_LABELING` - NO_HUMAN_LABELING
"""
STANDARD = "STANDARD"
NO_HUMAN_LABELING = "NO_HUMAN_LABELING"
class ImageQueryTypeEnum(str, Enum):
image_query = "image_query"
class ModeEnum(str, Enum):
BINARY = "BINARY"
COUNT = "COUNT"
MULTI_CLASS = "MULTI_CLASS"
TEXT = "TEXT"
class Note(BaseModel):
detector_id: str
content: Optional[str] = Field(None, description="Text content of the note.")
is_pinned: Optional[bool] = None
class NoteRequest(BaseModel):
content: Optional[str] = Field(None, description="Text content of the note.")
is_pinned: Optional[bool] = None
image: Optional[bytes] = None
class PayloadTemplate(BaseModel):
template: str
headers: Optional[Dict[str, str]] = None
class PayloadTemplateRequest(BaseModel):
template: constr(min_length=1)
headers: Optional[Dict[str, constr(min_length=1)]] = None
class ROI(BaseModel):
"""
Mixin for serializers to handle data in the StrictBaseModel format
"""
label: str = Field(..., description="The label of the bounding box.")
score: float = Field(..., description="The confidence of the bounding box.")
geometry: BBoxGeometry
class ROIRequest(BaseModel):
"""
Mixin for serializers to handle data in the StrictBaseModel format
"""
label: constr(min_length=1) = Field(..., description="The label of the bounding box.")
geometry: BBoxGeometryRequest
class ResultTypeEnum(str, Enum):
binary_classification = "binary_classification"
counting = "counting"
multi_classification = "multi_classification"
text_recognition = "text_recognition"
class SnoozeTimeUnitEnum(str, Enum):
"""
* `DAYS` - DAYS
* `HOURS` - HOURS
* `MINUTES` - MINUTES
* `SECONDS` - SECONDS
"""
DAYS = "DAYS"
HOURS = "HOURS"
MINUTES = "MINUTES"
SECONDS = "SECONDS"
class SourceEnum(str, Enum):
"""
* `INITIAL_PLACEHOLDER` - InitialPlaceholder
* `CLOUD` - HumanCloud
* `CUST` - HumanCustomer
* `HUMAN_CLOUD_ENSEMBLE` - HumanCloudEnsemble
* `ALG` - Algorithm
* `ALG_REC` - AlgorithmReconciled
* `ALG_UNCLEAR` - AlgorithmUnclear
* `EDGE` - EDGE
"""
INITIAL_PLACEHOLDER = "INITIAL_PLACEHOLDER"
CLOUD = "CLOUD"
CUST = "CUST"
HUMAN_CLOUD_ENSEMBLE = "HUMAN_CLOUD_ENSEMBLE"
ALG = "ALG"
ALG_REC = "ALG_REC"
ALG_UNCLEAR = "ALG_UNCLEAR"
EDGE = "EDGE"
class StatusEnum(str, Enum):
"""
* `ON` - ON
* `OFF` - OFF
"""
ON = "ON"
OFF = "OFF"
class VerbEnum(str, Enum):
"""
* `ANSWERED_CONSECUTIVELY` - ANSWERED_CONSECUTIVELY
* `ANSWERED_WITHIN_TIME` - ANSWERED_WITHIN_TIME
* `CHANGED_TO` - CHANGED_TO
* `NO_CHANGE` - NO_CHANGE
* `NO_QUERIES` - NO_QUERIES
"""
ANSWERED_CONSECUTIVELY = "ANSWERED_CONSECUTIVELY"
ANSWERED_WITHIN_TIME = "ANSWERED_WITHIN_TIME"
CHANGED_TO = "CHANGED_TO"
NO_CHANGE = "NO_CHANGE"
NO_QUERIES = "NO_QUERIES"
class WebhookAction(BaseModel):
url: AnyUrl
include_image: Optional[bool] = None
payload_template: Optional[PayloadTemplate] = None
class WebhookActionRequest(BaseModel):
url: AnyUrl
include_image: Optional[bool] = None
payload_template: Optional[PayloadTemplateRequest] = None
class Source(str, Enum):
STILL_PROCESSING = "STILL_PROCESSING"
CLOUD = "CLOUD"
USER = "USER"
CLOUD_ENSEMBLE = "CLOUD_ENSEMBLE"
ALGORITHM = "ALGORITHM"
EDGE = "EDGE"
class ResultType(str, Enum):
binary_classification = "binary_classification"
class Label(str, Enum):
YES = "YES"
NO = "NO"
UNCLEAR = "UNCLEAR"
class BinaryClassificationResult(BaseModel):
confidence: Optional[confloat(ge=0.0, le=1.0)] = None
source: Optional[Source] = None
result_type: Optional[ResultType] = None
label: Label
class ResultType2(str, Enum):
counting = "counting"
class CountingResult(BaseModel):
confidence: Optional[confloat(ge=0.0, le=1.0)] = None
source: Optional[Source] = None
result_type: Optional[ResultType2] = None
count: Optional[conint(ge=0)] = Field(...)
greater_than_max: Optional[bool] = None
class ResultType3(str, Enum):
multi_classification = "multi_classification"
class MultiClassificationResult(BaseModel):
confidence: Optional[confloat(ge=0.0, le=1.0)] = None
source: Optional[Source] = None
result_type: Optional[ResultType3] = None
label: str
class ResultType4(str, Enum):
text_recognition = "text_recognition"
class TextRecognitionResult(BaseModel):
confidence: Optional[confloat(ge=0.0, le=1.0)] = None
source: Optional[Source] = None
result_type: Optional[ResultType4] = None
text: Optional[str] = Field(...)
truncated: bool
class CountModeConfiguration(BaseModel):
max_count: Optional[conint(ge=1, le=50)] = None
class_name: str
class MultiClassModeConfiguration(BaseModel):
class_names: List[str]
num_classes: Optional[int] = None
class TextModeConfiguration(BaseModel):
value_max_length: Optional[conint(ge=1, le=250)] = None
class ChannelEnum(str, Enum):
TEXT = "TEXT"
EMAIL = "EMAIL"
class Action(BaseModel):
channel: ChannelEnum
recipient: str
include_image: bool
class ActionList(RootModel[List[Action]]):
root: List[Action]
class AllNotes(BaseModel):
"""
Serializes all notes for a given detector, grouped by type as listed in UserProfile.NoteCategoryChoices
The fields must match whats in USERPROFILE.NoteCategoryChoices
"""
CUSTOMER: List[Note]
GL: List[Note]
class Condition(BaseModel):
verb: VerbEnum
parameters: Dict[str, Any]
class ConditionRequest(BaseModel):
verb: VerbEnum
parameters: Dict[str, Any]
class Detector(BaseModel):
"""
Groundlight Detectors provide answers to natural language questions about images.
Each detector can answer a single question, and multiple detectors can be strung together for
more complex logic. Detectors can be created through the create_detector method, or through the
create_[MODE]_detector methods for pro tier users
"""
id: str = Field(..., description="A unique ID for this object.")
type: DetectorTypeEnum = Field(..., description="The type of this object.")
created_at: datetime = Field(..., description="When this detector was created.")
name: constr(max_length=200) = Field(..., description="A short, descriptive name for the detector.")
query: str = Field(..., description="A question about the image.")
group_name: str = Field(..., description="Which group should this detector be part of?")
confidence_threshold: confloat(ge=0.0, le=1.0) = Field(
0.9,
description=(
"If the detector's prediction is below this confidence threshold, send the image query for human review."
),
)
patience_time: confloat(ge=0.0, le=3600.0) = Field(
30.0, description="How long Groundlight will attempt to generate a confident prediction"
)
metadata: Optional[Dict[str, Any]] = Field(..., description="Metadata about the detector.")
mode: ModeEnum
mode_configuration: Optional[Dict[str, Any]] = Field(...)
status: Optional[Union[StatusEnum, BlankEnum]] = None
escalation_type: Optional[EscalationTypeEnum] = Field(
None,
description=(
"Category that define internal proccess for labeling image queries\n\n* `STANDARD` - STANDARD\n*"
" `NO_HUMAN_LABELING` - NO_HUMAN_LABELING"
),
)
class DetectorCreationInputRequest(BaseModel):
"""
Helper serializer for validating POST /detectors input.
"""
name: constr(min_length=1, max_length=200) = Field(..., description="A short, descriptive name for the detector.")
query: constr(min_length=1, max_length=300) = Field(..., description="A question about the image.")
group_name: Optional[constr(min_length=1, max_length=100)] = Field(
None, description="Which group should this detector be part of?"
)
confidence_threshold: confloat(ge=0.0, le=1.0) = Field(
0.9,
description=(
"If the detector's prediction is below this confidence threshold, send the image query for human review."
),
)
patience_time: confloat(ge=0.0, le=3600.0) = Field(
30.0, description="How long Groundlight will attempt to generate a confident prediction"
)
pipeline_config: Optional[constr(max_length=100)] = Field(
None, description="(Advanced usage) Configuration needed to instantiate a prediction pipeline."
)
metadata: Optional[constr(min_length=1, max_length=1362)] = Field(
None,
description=(
"Base64-encoded metadata for the detector. This should be a JSON object with string keys. The size after"
" encoding should not exceed 1362 bytes, corresponding to 1KiB before encoding."
),
)
mode: ModeEnum = Field(
"BINARY",
description=(
"Mode in which this detector will work.\n\n* `BINARY` - BINARY\n* `COUNT` - COUNT\n* `MULTI_CLASS` -"
" MULTI_CLASS\n* `TEXT` - TEXT"
),
)
mode_configuration: Optional[Union[CountModeConfiguration, MultiClassModeConfiguration, TextModeConfiguration]] = (
None
)
class ImageQuery(BaseModel):
"""
ImageQuery objects are the answers to natural language questions about images created by detectors.
"""
metadata: Optional[Dict[str, Any]] = Field(..., description="Metadata about the image query.")
id: str = Field(..., description="A unique ID for this object.")
type: ImageQueryTypeEnum = Field(..., description="The type of this object.")
created_at: datetime = Field(..., description="When was this detector created?")
query: str = Field(..., description="A question about the image.")
detector_id: str = Field(..., description="Which detector was used on this image query?")
result_type: ResultTypeEnum = Field(..., description="What type of result are we returning?")
result: Optional[
Union[BinaryClassificationResult, CountingResult, MultiClassificationResult, TextRecognitionResult]
] = Field(...)
patience_time: float = Field(..., description="How long to wait for a confident response.")
confidence_threshold: float = Field(
..., description="Min confidence needed to accept the response of the image query."
)
rois: Optional[List[ROI]] = Field(
..., description="An array of regions of interest (bounding boxes) collected on image"
)
text: Optional[str] = Field(..., description="A text field on image query.")
done_processing: bool = Field(
False,
description="EDGE ONLY - Whether the image query has completed escalating and will receive no new results.",
)
class LabelValue(BaseModel):
confidence: Optional[float] = Field(...)
class_name: Optional[str] = Field(
..., description="Return a human-readable class name for this label (e.g. YES/NO)"
)
rois: Optional[List[ROI]] = None
annotations_requested: List[AnnotationsRequestedEnum]
created_at: datetime
detector_id: Optional[int] = Field(...)
source: SourceEnum
text: Optional[str] = Field(..., description="Text annotations")
class LabelValueRequest(BaseModel):
label: Optional[str] = Field(...)
image_query_id: constr(min_length=1)
rois: Optional[List[ROIRequest]] = None
class PaginatedDetectorList(BaseModel):
count: int = Field(..., example=123)
next: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=4")
previous: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=2")
results: List[Detector]
class PaginatedImageQueryList(BaseModel):
count: int = Field(..., example=123)
next: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=4")
previous: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=2")
results: List[ImageQuery]
class PatchedDetectorRequest(BaseModel):
"""
Groundlight Detectors provide answers to natural language questions about images.
Each detector can answer a single question, and multiple detectors can be strung together for
more complex logic. Detectors can be created through the create_detector method, or through the
create_[MODE]_detector methods for pro tier users
"""
name: Optional[constr(min_length=1, max_length=200)] = Field(
None, description="A short, descriptive name for the detector."
)
confidence_threshold: confloat(ge=0.0, le=1.0) = Field(
0.9,
description=(
"If the detector's prediction is below this confidence threshold, send the image query for human review."
),
)
patience_time: confloat(ge=0.0, le=3600.0) = Field(
30.0, description="How long Groundlight will attempt to generate a confident prediction"
)
status: Optional[Union[StatusEnum, BlankEnum]] = None
escalation_type: Optional[EscalationTypeEnum] = Field(
None,
description=(
"Category that define internal proccess for labeling image queries\n\n* `STANDARD` - STANDARD\n*"
" `NO_HUMAN_LABELING` - NO_HUMAN_LABELING"
),
)
class Rule(BaseModel):
id: int
detector_id: str
detector_name: str
name: constr(max_length=44)
enabled: bool = True
snooze_time_enabled: bool = False
snooze_time_value: conint(ge=0) = 0
snooze_time_unit: SnoozeTimeUnitEnum = "DAYS"
human_review_required: bool = False
condition: Condition
action: Optional[Union[Action, ActionList]] = None
webhook_action: Optional[List[WebhookAction]] = None
class RuleRequest(BaseModel):
name: constr(min_length=1, max_length=44)
enabled: bool = True
snooze_time_enabled: bool = False
snooze_time_value: conint(ge=0) = 0
snooze_time_unit: SnoozeTimeUnitEnum = "DAYS"
human_review_required: bool = False
condition: ConditionRequest
action: Optional[Union[Action, ActionList]] = None
webhook_action: Optional[List[WebhookActionRequest]] = None
class PaginatedRuleList(BaseModel):
count: int = Field(..., example=123)
next: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=4")
previous: Optional[AnyUrl] = Field(None, example="http://api.example.org/accounts/?page=2")
results: List[Rule]