Table of contents
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.
🐍 Check out an interactive notebook version of this tutorial on Google Colab.
We maintain an open-source Python client for the API. Install it with pip:
Generate an API token at replicate.com/account/api-tokens, copy the token, then set it as an environment variable in your shell:
You can run any public model on Replicate from your Python code. Here's an example that runs stability-ai/sdxl to generate an image, then returns the URL of the image:
Some models take files as inputs. You can use a local file on your machine as input, or you can provide an HTTPS URL to a file on the public internet.
Here's an example that uses a local file as input to the LLaVA vision model, which takes an image and a text prompt and input and responds with text:
URLs are more efficient if your file is already in the cloud somewhere, or it is a large file.
Here's an example that uses an HTTPS URL of an image on the internet as input to a model:
Some models stream output as the model is running. They will return an iterator, and you can iterate over that output: