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.
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 black-forest-labs/flux-schnell to generate an 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 as 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:
Some models generate files as output, such as images or audio. These are returned as FileOutput
objects, which you can easily save or process:
For more details on handling output files, see Output Files.