Replicate Tutorial: Run AI Models and Create Images

Welcome to this tutorial on Replicate! 🚀

Replicate is a platform that allows you to run AI models in the cloud with a simple API. It's especially powerful for hosting and running image and video models, making it the go-to platform for creators who want to generate stunning visual content without dealing with the technical complexity of GPU management and model deployment.

In this notebook, we'll cover the essential concepts of Replicate and walk you through running your first image generation model. You'll see how easy it is to create professional-quality images from text descriptions using state-of-the-art AI models.

How Does Replicate Work?

Replicate works on a simple principle: you send an API request to run a specific model with your inputs, and Replicate takes care of the rest. Here's a high-level overview of the process:

  1. Choose a Model: Replicate hosts a vast library of public models created by the community. You can browse and select models for image generation, video creation, style transfer, image enhancement, and much more.

  2. API Request: You use a client library (like the Python client we'll use here) to send a request to the Replicate API. This request includes the model you want to run and the inputs it requires.

  3. Prediction: Replicate runs the model with your inputs and creates a "prediction." For long-running models (common in image and video generation), you can poll the API to check the status of your prediction.

  4. Get the Output: Once the prediction is complete, you can retrieve the output, which could be an image, video, text, or any other file. Replicate excels at handling large media files and providing reliable URLs for your generated content.

Setup

First, let's install the necessary Python libraries. We'll use replicate to interact with the Replicate API and python-dotenv to manage our API key.

# %pip install replicate python-dotenv -q

API Key

To use the Replicate API, you need an API key. You can get one from your Replicate account settings.

For security, it's a best practice to store your API key in a .env file. Create a file named .env in the same directory as this notebook and add the following line:

REPLICATE_API_TOKEN="your_api_key_here"

Now, we can load the API key from the .env file and set it as an environment variable.

import os
from dotenv import load_dotenv

load_dotenv()

# The replicate library automatically looks for the REPLICATE_API_TOKEN environment variable.
# So, you don't need to explicitly pass it to the client.
True

Running a Model

Now that we're set up, let's run a model! We'll use the Stable Diffusion model to generate an image from a text prompt.

import replicate

# Run the Stable Diffusion model to generate an image
print("Generating image with Stable Diffusion...")
output = replicate.run(
    "stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
    input={"prompt": "a photograph of an astronaut riding a horse"}
)
# Convert the output to a list and extract the URL string
output_list = list(output)
if output_list and len(output_list) > 0:
    # Convert FileOutput to string URL
    image_url = str(output_list[0])
    print(f"Generated image URL: {image_url}")
else:
    print("No image was generated.")
    image_url = None
Generated image URL: https://replicate.delivery/yhqm/w7g9AEduCgZZI1kWKic6GvcF0Upd0eZz2vTI4Dzh0HWuLjkKA/out-0.png

The replicate.run() function returns the output of the model. In this case, it's a URL to the generated image. Let's display the image.

# Display image with custom size
from IPython.display import Image, display

if image_url:
    print("Displaying generated image with custom size:")
    display(Image(url=image_url, width=400, height=300))
else:
    print("No image available to display. Please run the previous cell first.")
Displaying generated image with custom size:

Getting Predictions for Long-Running Models

Some models can take a while to run. For these, you can use replicate.predictions.create() and prediction.wait() to run a model and wait for the result.

prediction = replicate.predictions.create(
    version="db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
    input={"prompt": "a 19th-century portrait of a robot in a suit"}
)

# Wait for the prediction to complete
prediction.wait()

# Print the output
print(prediction.output)
['https://replicate.delivery/yhqm/MfxCBc67zJXtIyHjfb3DGaguYNrpbpH3PYMGumhAxRXjcGJVA/out-0.png']
from IPython.display import Image

# Display the prediction result
if prediction.output and len(prediction.output) > 0:
    # Convert FileOutput to string URL
    prediction_image_url = str(prediction.output[0])
    print(f"Prediction image URL: {prediction_image_url}")
    display(Image(url=prediction_image_url, width=400, height=300))
else:
    print("No prediction output available.")
Prediction image URL: https://replicate.delivery/yhqm/MfxCBc67zJXtIyHjfb3DGaguYNrpbpH3PYMGumhAxRXjcGJVA/out-0.png

Conclusion

Congratulations! You've learned the basics of using Replicate to run AI models. You now know how to:

  • Set up your environment and manage your API key securely.
  • Run a model and get the output.
  • Handle long-running predictions.

From here, you can explore the vast library of models on the Replicate website and integrate them into your own projects.