Skip to content

Support predictions.create with model, version, or deployment parameters #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 128 additions & 2 deletions replicate/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
List,
Literal,
Optional,
Tuple,
Union,
overload,
)

from typing_extensions import NotRequired, TypedDict, Unpack
Expand All @@ -31,6 +33,8 @@

if TYPE_CHECKING:
from replicate.client import Client
from replicate.deployment import Deployment
from replicate.model import Model
from replicate.stream import ServerSentEvent


Expand Down Expand Up @@ -380,21 +384,82 @@ class CreatePredictionParams(TypedDict):
stream: NotRequired[bool]
"""Enable streaming of prediction output."""

@overload
def create(
self,
version: Union[Version, str],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

@overload
def create(
self,
*,
model: Union[str, Tuple[str, str], "Model"],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

@overload
def create(
self,
*,
deployment: Union[str, Tuple[str, str], "Deployment"],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

def create( # type: ignore
self,
*args,
model: Optional[Union[str, Tuple[str, str], "Model"]] = None,
version: Optional[Union[Version, str, "Version"]] = None,
deployment: Optional[Union[str, Tuple[str, str], "Deployment"]] = None,
input: Optional[Dict[str, Any]] = None,
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction:
"""
Create a new prediction for the specified model version.
Create a new prediction for the specified model, version, or deployment.
"""

if args:
version = args[0] if len(args) > 0 else None
input = args[1] if len(args) > 1 else input

if sum(bool(x) for x in [model, version, deployment]) != 1:
raise ValueError(
"Exactly one of 'model', 'version', or 'deployment' must be specified."
)

if model is not None:
from replicate.model import ( # pylint: disable=import-outside-toplevel
Models,
)

return Models(self._client).predictions.create(
model=model,
input=input or {},
**params,
)

if deployment is not None:
from replicate.deployment import ( # pylint: disable=import-outside-toplevel
Deployments,
)

return Deployments(self._client).predictions.create(
deployment=deployment,
input=input or {},
**params,
)

body = _create_prediction_body(
version,
input,
**params,
)

resp = self._client._request(
"POST",
"/v1/predictions",
Expand All @@ -403,21 +468,82 @@ def create(

return _json_to_prediction(self._client, resp.json())

@overload
async def async_create(
self,
version: Union[Version, str],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

@overload
async def async_create(
self,
*,
model: Union[str, Tuple[str, str], "Model"],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

@overload
async def async_create(
self,
*,
deployment: Union[str, Tuple[str, str], "Deployment"],
input: Optional[Dict[str, Any]],
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction: ...

async def async_create( # type: ignore
self,
*args,
model: Optional[Union[str, Tuple[str, str], "Model"]] = None,
version: Optional[Union[Version, str, "Version"]] = None,
deployment: Optional[Union[str, Tuple[str, str], "Deployment"]] = None,
input: Optional[Dict[str, Any]] = None,
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Prediction:
"""
Create a new prediction for the specified model version.
Create a new prediction for the specified model, version, or deployment.
"""

if args:
version = args[0] if len(args) > 0 else None
input = args[1] if len(args) > 1 else input

if sum(bool(x) for x in [model, version, deployment]) != 1:
raise ValueError(
"Exactly one of 'model', 'version', or 'deployment' must be specified."
)

if model is not None:
from replicate.model import ( # pylint: disable=import-outside-toplevel
Models,
)

return await Models(self._client).predictions.async_create(
model=model,
input=input or {},
**params,
)

if deployment is not None:
from replicate.deployment import ( # pylint: disable=import-outside-toplevel
Deployments,
)

return await Deployments(self._client).predictions.async_create(
deployment=deployment,
input=input or {},
**params,
)

body = _create_prediction_body(
version,
input,
**params,
)

resp = await self._client._async_request(
"POST",
"/v1/predictions",
Expand Down
Loading
Loading