forked from replicate/replicate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
118 lines (86 loc) · 2.96 KB
/
Copy pathversion.py
File metadata and controls
118 lines (86 loc) · 2.96 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
import datetime
from typing import TYPE_CHECKING, Any, Dict, Tuple, Union
if TYPE_CHECKING:
from replicate.client import Client
from replicate.model import Model
from replicate.pagination import Page
from replicate.resource import Namespace, Resource
class Version(Resource):
"""
A version of a model.
"""
id: str
"""The unique ID of the version."""
created_at: datetime.datetime
"""When the version was created."""
cog_version: str
"""The version of the Cog used to create the version."""
openapi_schema: dict
"""An OpenAPI description of the model inputs and outputs."""
class Versions(Namespace):
"""
Namespace for operations related to model versions.
"""
model: Tuple[str, str]
def __init__(
self, client: "Client", model: Union[str, Tuple[str, str], "Model"]
) -> None:
super().__init__(client=client)
from replicate.model import Model # pylint: disable=import-outside-toplevel
if isinstance(model, Model):
self.model = (model.owner, model.name)
elif isinstance(model, str):
owner, name = model.split("/", 1)
self.model = (owner, name)
else:
self.model = model
def get(self, id: str) -> Version:
"""
Get a specific model version.
Args:
id: The version ID.
Returns:
The model version.
"""
resp = self._client._request(
"GET", f"/v1/models/{self.model[0]}/{self.model[1]}/versions/{id}"
)
return _json_to_version(resp.json())
async def async_get(self, id: str) -> Version:
"""
Get a specific model version.
Args:
id: The version ID.
Returns:
The model version.
"""
resp = await self._client._async_request(
"GET", f"/v1/models/{self.model[0]}/{self.model[1]}/versions/{id}"
)
return _json_to_version(resp.json())
def list(self) -> Page[Version]:
"""
Return a list of all versions for a model.
Returns:
List[Version]: A list of version objects.
"""
resp = self._client._request(
"GET", f"/v1/models/{self.model[0]}/{self.model[1]}/versions"
)
obj = resp.json()
obj["results"] = [_json_to_version(result) for result in obj["results"]]
return Page[Version](**obj)
async def async_list(self) -> Page[Version]:
"""
Return a list of all versions for a model.
Returns:
List[Version]: A list of version objects.
"""
resp = await self._client._async_request(
"GET", f"/v1/models/{self.model[0]}/{self.model[1]}/versions"
)
obj = resp.json()
obj["results"] = [_json_to_version(result) for result in obj["results"]]
return Page[Version](**obj)
def _json_to_version(json: Dict[str, Any]) -> Version:
return Version(**json)