Join us at Uncanny Spaces, a series of talks about ML and creativity. 🚀

Run a model from Python

Learn how to run a model on Replicate from within your Python code. It could be an app, a notebook, an evaluation script, or anywhere else you want to use machine learning.

Sign up and add your credit card

To get started, you'll need to sign up and enter your billing info. There is no charge to sign up, and your predictions will be billed by the second.

Install the Python library

We maintain an open-source Python client for the API. Install it with pip:

pip install replicate

(Note: There's also a community-maintained Node.js/JavaScript library. See nicholascelestin/replicate-js)

Authenticate

Authenticate by setting your token in an environment variable:

export REPLICATE_API_TOKEN=<token>

Run predictions

You can run any public model on Replicate from your Python code:

$ python
>>> import replicate
>>> model = replicate.models.get("replicate/hello-world")
>>> model.predict(text="python")
"hello python"

To pass files as input, use a file handle or URL:

>>> model = replicate.models.get("replicate/resnet")
>>> model.predict(image=open("mystery.jpg", "rb"))
# or...
>>> model.predict(image="https://example.com/mystery.jpg")

URLs are more efficient if your file is already in the cloud somewhere, or it is a large file. Files are output as URLs.

Some models output progressively, as the model is running. They will return an iterator and you can iterate over that output:

>>> model = replicate.models.get("pixray/text2image")
>>> for image in model.predict(prompts="robots talking to robots"):
...    print(image)
https://...
https://...
https://...

Next steps

Read the full Python client documentation on GitHub.

You can also run models with the raw HTTP API. See the HTTP API reference for more details.

Replicate