Replicate's API lets you run machine learning models on Replicate in the cloud from your own code, without having to set up any servers. You can run models from apps, notebooks, evaluation scripts, or anywhere else you want to use machine learning.
Only certain models have opted-in to use the API. Here are the models you can run:
We're working with model authors to get more added.
Before using the API, you'll need to sign up to Replicate and subscribe for $10/mo to cover the cost of the GPUs.
First, install the Python library from pip:
pip install replicate
Authenticate by setting your token in an environment variable:
Then, you can run models 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://...
For more details, see the full documentation on GitHub.
You can also run models with the raw HTTP API. See the HTTP API reference for more details.