-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathpredictor.py
503 lines (401 loc) · 15.9 KB
/
predictor.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
import builtins
import enum
import importlib.util
import inspect
import os.path
import sys
import types
import uuid
from collections.abc import Iterable, Iterator
if sys.version_info >= (3, 10):
from types import NoneType
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Optional,
Type,
Union,
cast,
get_args,
get_origin,
)
from unittest.mock import patch
import pydantic
import structlog
from pydantic import BaseModel, Field, create_model
from pydantic.fields import FieldInfo
# Added in Python 3.9. Can be from typing if we drop support for <3.9
from typing_extensions import Annotated
from .base_input import BaseInput
from .base_predictor import BasePredictor
from .code_xforms import load_module_from_string, strip_model_source_code
from .types import (
PYDANTIC_V2,
Input,
Weights,
)
from .types import (
File as CogFile,
)
from .types import (
Path as CogPath,
)
from .types import Secret as CogSecret
log = structlog.get_logger("cog.server.predictor")
ALLOWED_INPUT_TYPES: List[Type[Any]] = [
str,
int,
float,
bool,
CogFile,
CogPath,
CogSecret,
NoneType,
]
def has_setup_weights(predictor: BasePredictor) -> bool:
weights_type = get_weights_type(predictor.setup)
return weights_type is not None
def extract_setup_weights(predictor: BasePredictor) -> Optional[Weights]:
weights_type = get_weights_type(predictor.setup)
assert weights_type
weights: Optional[Weights]
weights_url = os.environ.get("COG_WEIGHTS")
weights_path = "weights"
# TODO: Cog{File,Path}.validate(...) methods accept either "real"
# paths/files or URLs to those things. In future we can probably tidy this
# up a little bit.
# TODO: CogFile/CogPath should have subclasses for each of the subtypes
if weights_url:
if PYDANTIC_V2:
from pydantic import TypeAdapter
for t in [CogFile, CogPath]:
try:
weights = TypeAdapter(t).validate_python(weights_url)
break
except Exception: # pylint: disable=broad-except # noqa: S110
pass
else:
if weights_type is str:
weights = weights_url
else:
raise ValueError(
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File, Path and str are supported"
)
else:
if weights_type is CogFile:
weights = cast(CogFile, CogFile.validate(weights_url))
elif weights_type is CogPath:
# TODO: So this can be a url. evil!
weights = cast(CogPath, CogPath.validate(weights_url))
elif weights_type is str:
weights = weights_url
else:
raise ValueError(
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File, Path and str are supported"
)
elif os.path.exists(weights_path):
if weights_type == CogFile:
with open(weights_path, "rb") as f:
weights = cast(CogFile, f)
elif weights_type == CogPath:
weights = CogPath(weights_path)
else:
raise ValueError(
f"Predictor.setup() has an argument 'weights' of type {weights_type}, but only File, Path and str are supported"
)
else:
weights = None
return weights
def get_weights_type(setup_function: Callable[[Any], None]) -> Optional[Any]:
signature = inspect.signature(setup_function)
if "weights" not in signature.parameters:
return None
Type = signature.parameters["weights"].annotation # pylint: disable=invalid-name,redefined-outer-name
# Handle Optional. It is Union[Type, None]
if get_origin(Type) == Union:
args = get_args(Type)
if len(args) == 2 and args[1] is type(None):
Type = get_args(Type)[0] # pylint: disable=invalid-name
return Type
def load_full_predictor_from_file(
module_path: str, module_name: str
) -> types.ModuleType:
spec = importlib.util.spec_from_file_location(module_name, module_path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
# Remove any sys.argv while importing predictor to avoid conflicts when
# user code calls argparse.Parser.parse_args in production
with patch("sys.argv", sys.argv[:1]):
spec.loader.exec_module(module)
return module
def load_slim_predictor_from_file(
module_path: str, class_name: str, method_name: str
) -> Optional[types.ModuleType]:
with open(module_path, encoding="utf-8") as file:
source_code = file.read()
stripped_source = strip_model_source_code(source_code, [class_name], [method_name])
module = load_module_from_string(uuid.uuid4().hex, stripped_source)
return module
def get_predictor(module: types.ModuleType, class_name: str) -> Any:
predictor = getattr(module, class_name)
# It could be a class or a function
if inspect.isclass(predictor):
return predictor()
return predictor
def load_predictor_from_ref(ref: str) -> BasePredictor:
module_path, class_name = ref.split(":", 1)
module_name = os.path.basename(module_path).split(".py", 1)[0]
module = load_full_predictor_from_file(module_path, module_name)
predictor = get_predictor(module, class_name)
return predictor
def validate_input_type(
type: Type[Any], # pylint: disable=redefined-builtin
name: str,
) -> None:
if type is inspect.Signature.empty:
raise TypeError(
f"No input type provided for parameter `{name}`. Supported input types are: {readable_types_list(ALLOWED_INPUT_TYPES)}, or a Union or List of those types."
)
if type not in ALLOWED_INPUT_TYPES:
if get_origin(type) is Literal:
for t in get_args(type):
validate_input_type(builtins.type(t), name)
elif get_origin(type) in (Union, List, list) or (
hasattr(types, "UnionType") and get_origin(type) is types.UnionType
): # noqa: E721
args = get_args(type)
def is_optional() -> bool:
if len(args) != 2 or get_origin(type) is not Union:
return False
if sys.version_info >= (3, 10):
return args[1] is NoneType
return args[1] is None.__class__
if is_optional():
validate_input_type(args[0], name)
else:
for t in args:
validate_input_type(t, name)
else:
if PYDANTIC_V2:
# Cog types are exported as `Annotated[Type, ...]`, but `type` is the inner type
if hasattr(type, "__module__") and type.__module__ == "cog.types":
return
raise TypeError(
f"Unsupported input type {human_readable_type_name(type)} for parameter `{name}`. Supported input types are: {readable_types_list(ALLOWED_INPUT_TYPES)}, or a Union or List of those types."
)
def get_input_create_model_kwargs(signature: inspect.Signature) -> Dict[str, Any]:
create_model_kwargs = {}
order = 0
for name, parameter in signature.parameters.items():
InputType = parameter.annotation
validate_input_type(InputType, name)
# if no default is specified, create an empty, required input
if parameter.default is inspect.Signature.empty:
default = Input()
else:
if not isinstance(parameter.default, FieldInfo):
default = Input(default=parameter.default)
else:
default = parameter.default
if PYDANTIC_V2:
# https://github.com/pydantic/pydantic/blob/2.7/pydantic/json_schema.py#L1436-L1446
# json_schema_extra can be a callable, but we don't set that and users shouldn't set that
if not default.json_schema_extra: # type: ignore
default.json_schema_extra = {} # type: ignore
assert isinstance(default.json_schema_extra, dict) # type: ignore
extra = default.json_schema_extra # type: ignore
else:
extra = default.extra # type: ignore
extra["x-order"] = order
order += 1
# Choices!
choices = (
extra.pop("choices", None) # Pydantic v1
or extra.pop("enum", None) # Pydantic v2
)
# In either case, remove it as an extra field because it will be
# passed automatically as 'enum' in the schema
if choices:
if InputType == str and isinstance(choices, Iterable): # noqa: E721
class StringEnum(str, enum.Enum):
pass
InputType = StringEnum( # pylint: disable=invalid-name
name, [(value, value) for value in choices or []]
)
elif InputType == int: # noqa: E721
InputType = enum.IntEnum(name, {str(value): value for value in choices}) # type: ignore # pylint: disable=invalid-name
else:
raise TypeError(
f"The input {name} uses the option choices. Choices can only be used with str or int types."
)
create_model_kwargs[name] = (InputType, default)
return create_model_kwargs
def get_predict(predictor: Any) -> Callable[..., Any]:
if hasattr(predictor, "predict"):
return predictor.predict
return predictor
def get_input_type(predictor: BasePredictor) -> Type[BaseInput]:
"""
Creates a Pydantic Input model from the arguments of a Predictor's predict() method.
class Predictor(BasePredictor):
def predict(self, text: str):
...
programmatically creates a model like this:
class Input(BaseModel):
text: str
"""
predict = get_predict(predictor)
signature = inspect.signature(predict)
return create_model(
"Input",
__config__=None,
__base__=BaseInput,
__module__=__name__,
__validators__=None,
**get_input_create_model_kwargs(signature),
) # type: ignore
def get_output_type(predictor: BasePredictor) -> Type[BaseModel]:
"""
Creates a Pydantic Output model from the return type annotation of a Predictor's predict() method.
"""
predict = get_predict(predictor)
signature = inspect.signature(predict)
OutputType: Type[BaseModel]
if signature.return_annotation is inspect.Signature.empty:
raise TypeError(
"""You must set an output type. If your model can return multiple output types, you can explicitly set `Any` as the output type.
For example:
from typing import Any
def predict(
self,
image: Path = Input(description="Input image"),
) -> Any:
...
"""
)
else:
OutputType = signature.return_annotation
# The type that goes in the response is a list of the yielded type
if get_origin(OutputType) is Iterator:
# Annotated allows us to attach Field annotations to the list, which we use to mark that this is an iterator
# https://pydantic-docs.helpmanual.io/usage/schema/#typingannotated-fields
if PYDANTIC_V2:
field = Field(**{"json_schema_extra": {"x-cog-array-type": "iterator"}}) # type: ignore
else:
field = Field(**{"x-cog-array-type": "iterator"}) # type: ignore
OutputType: Type[BaseModel] = Annotated[List[get_args(OutputType)[0]], field] # type: ignore
name = OutputType.__name__ if hasattr(OutputType, "__name__") else ""
if name == "Output":
return OutputType
# We wrap the OutputType in an Output class to
# ensure consistent naming of the interface in the schema.
#
# NOTE: If the OutputType.__name__ is "TrainingOutput" then cannot use
# `__root__` here because this will create a reference for the Object.
# e.g.
# {'title': 'Output', '$ref': '#/definitions/TrainingOutput' ... }
#
# And this reference may conflict with other objects at which
# point the item will be namespaced and break our parsing. e.g.
# {'title': 'Output', '$ref': '#/definitions/predict_TrainingOutput' ... }
#
# So we work around this by inheriting from the original class rather
# than using "__root__".
if name == "TrainingOutput": # pylint: disable=no-else-return
class Output(OutputType): # type: ignore
pass
return Output
else:
if PYDANTIC_V2:
class Output(pydantic.RootModel[OutputType]): # type: ignore
pass
else:
class Output(BaseModel):
__root__: OutputType # type: ignore
return Output
def get_train(predictor: Any) -> Callable[..., Any]:
if hasattr(predictor, "train"):
return predictor.train
return predictor
def get_training_input_type(predictor: BasePredictor) -> Type[BaseInput]:
"""
Creates a Pydantic Input model from the arguments of a Predictor's train() method.
def train(self, text: str):
...
programmatically creates a model like this:
class TrainingInput(BaseModel):
text: str
"""
train = get_train(predictor)
signature = inspect.signature(train)
return create_model(
"TrainingInput",
__config__=None,
__base__=BaseInput,
__module__=__name__,
__validators__=None,
**get_input_create_model_kwargs(signature),
) # type: ignore
def get_training_output_type(predictor: BasePredictor) -> Type[BaseModel]:
"""
Creates a Pydantic Output model from the return type annotation of a train() method.
"""
train = get_train(predictor)
signature = inspect.signature(train)
if signature.return_annotation is inspect.Signature.empty:
raise TypeError(
"""You must set an output type. If your model can return multiple output types, you can explicitly set `Any` as the output type.
For example:
from typing import Any
def train(
self,
n: int
) -> Any:
...
"""
)
else:
TrainingOutputType = signature.return_annotation
name = (
TrainingOutputType.__name__ if hasattr(TrainingOutputType, "__name__") else ""
)
# We wrap the OutputType in a TrainingOutput class to
# ensure consistent naming of the interface in the schema
# See comment in get_output_type for more info.
if name == "TrainingOutput":
return TrainingOutputType
if name == "Output": # pylint: disable=no-else-return
class TrainingOutput(TrainingOutputType): # type: ignore
pass
return TrainingOutput
else:
if PYDANTIC_V2:
class TrainingOutput(pydantic.RootModel[TrainingOutputType]): # type: ignore
pass
return TrainingOutput
else:
class TrainingOutput(BaseModel):
__root__: TrainingOutputType # type: ignore
return TrainingOutput
def human_readable_type_name(t: Type[Union[Any, None]]) -> str:
"""
Generates a useful-for-humans label for a type. For builtin types, it's just the class name (eg "str" or "int"). For other types, it includes the module (eg "pathlib.Path" or "cog.File").
The special case for Cog modules is because the type lives in `cog.types` internally, but just `cog` when included as a dependency.
"""
if hasattr(t, "__module__"):
module = t.__module__
if module == "builtins":
return t.__qualname__
if module.split(".")[0] == "cog":
module = "cog"
try:
return f"{module}.{t.__qualname__}"
except AttributeError:
pass
return str(t)
def readable_types_list(type_list: List[Type[Any]]) -> str:
return ", ".join(human_readable_type_name(t) for t in type_list)