forked from replicate/replicate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.py
More file actions
154 lines (115 loc) · 3.77 KB
/
Copy pathdeployment.py
File metadata and controls
154 lines (115 loc) · 3.77 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
from typing import TYPE_CHECKING, Any, Dict
from typing_extensions import Unpack, deprecated
from replicate.prediction import (
Prediction,
_create_prediction_body,
_json_to_prediction,
)
from replicate.resource import Namespace, Resource
try:
from pydantic import v1 as pydantic # type: ignore
except ImportError:
import pydantic # type: ignore
if TYPE_CHECKING:
from replicate.client import Client
from replicate.prediction import Predictions
class Deployment(Resource):
"""
A deployment of a model hosted on Replicate.
"""
_client: "Client" = pydantic.PrivateAttr()
owner: str
"""
The name of the user or organization that owns the deployment.
"""
name: str
"""
The name of the deployment.
"""
@property
@deprecated("Use `deployment.owner` instead.")
def username(self) -> str:
"""
The name of the user or organization that owns the deployment.
This attribute is deprecated and will be removed in future versions.
"""
return self.owner
@property
def id(self) -> str:
"""
Return the qualified deployment name, in the format `owner/name`.
"""
return f"{self.owner}/{self.name}"
@property
def predictions(self) -> "DeploymentPredictions":
"""
Get the predictions for this deployment.
"""
return DeploymentPredictions(client=self._client, deployment=self)
class Deployments(Namespace):
"""
Namespace for operations related to deployments.
"""
_client: "Client"
def get(self, name: str) -> Deployment:
"""
Get a deployment by name.
Args:
name: The name of the deployment, in the format `owner/model-name`.
Returns:
The model.
"""
owner, name = name.split("/", 1)
deployment = Deployment(owner=owner, name=name)
deployment._client = self._client
return deployment
async def async_get(self, name: str) -> Deployment:
"""
Get a deployment by name.
Args:
name: The name of the deployment, in the format `owner/model-name`.
Returns:
The model.
"""
owner, name = name.split("/", 1)
deployment = Deployment(owner=owner, name=name)
deployment._client = self._client
return deployment
class DeploymentPredictions(Namespace):
"""
Namespace for operations related to predictions in a deployment.
"""
_deployment: Deployment
def __init__(self, client: "Client", deployment: Deployment) -> None:
super().__init__(client=client)
self._deployment = deployment
def create(
self,
input: Dict[str, Any],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction:
"""
Create a new prediction with the deployment.
"""
body = _create_prediction_body(version=None, input=input, **params)
resp = self._client._request(
"POST",
f"/v1/deployments/{self._deployment.owner}/{self._deployment.name}/predictions",
json=body,
)
return _json_to_prediction(self._client, resp.json())
async def async_create(
self,
input: Dict[str, Any],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction:
"""
Create a new prediction with the deployment.
"""
body = _create_prediction_body(version=None, input=input, **params)
resp = await self._client._async_request(
"POST",
f"/v1/deployments/{self._deployment.owner}/{self._deployment.name}/predictions",
json=body,
)
return _json_to_prediction(self._client, resp.json())