Exploring text to image models

📰 Replicate Blog

Explore text to image models using Replicate's Python client and API

intermediate Published 18 Jul 2022
Action Steps
  1. Install Replicate's Python client using pip
  2. Login to Replicate using an API access token
  3. Import the replicate module and generate an image from text using a pre-trained model
Who Needs to Know This

Data scientists and AI engineers can benefit from this tutorial to generate images from text using pre-trained models

Key Insight

💡 Replicate provides a platform to easily explore and use pre-trained text to image models

Share This
🔍 Generate images from text using Replicate's API and Python client! #texttoimage #AI

Key Takeaways

Explore text to image models using Replicate's Python client and API

Full Article

# Exploring text to image models – Replicate blog

[Replicate has joined Cloudflare](https://replicate.com/blog/replicate-cloudflare)

[](https://replicate.com/)

cmd+k

[Explore](https://replicate.com/explore)[Pricing](https://replicate.com/pricing)[Enterprise](https://replicate.com/enterprise)[Docs](https://replicate.com/docs)[Blog](https://replicate.com/blog)[Sign in](https://replicate.com/signin)[Try for free](https://replicate.com/signin)

Menu

[Explore](https://replicate.com/explore)[Pricing](https://replicate.com/pricing)[Enterprise](https://replicate.com/enterprise)[Docs](https://replicate.com/docs)[Blog](https://replicate.com/blog)[Sign in](https://replicate.com/signin)[Try for free](https://replicate.com/signin)

* * *

[Compare models in the Playground](https://replicate.com/playground)

* Replicate
* [Blog](https://replicate.com/blog)

# Exploring text to image models

Posted July 18, 2022 by
* [afiaka87](https://replicate.com/afiaka87)
* [rossjillian](https://replicate.com/rossjillian)

I’m Clay, a member of the team at Replicate. In this post, I’ll show you how Replicate allows you to easily explore many open [text to image models](https://replicate.com/collections/text-to-image).

You can follow along by downloading the accompanying Jupyter notebook [here](https://replicate.delivery/pbxt/blog/exploring-text-to-image-models/replicate_api_notebook.ipynb)

If you’re running the notebook in Colab, it’s recommended to use Firefox/Chrome.

## [](https://replicate.com/blog/exploring-text-to-image-models#install)Install

It’s wise to use a virtual environment to keep your global Python installation clean. `venv` or `conda` will work fine:

Copy

```
python3 -m venv replicate_venv
source replicate_venv/bin/activate
```

or

Copy

```
conda create --name replicate_venv
conda activate replicate_venv
```

In whatever environment you choose, install Replicate’s [Python client](https://pypi.org/project/replicate/).

Copy

`(replicate_venv) % pip install replicate`

## [](https://replicate.com/blog/exploring-text-to-image-models#login)Login

To use the API, you’ll need an API access token. You can get a token by [subscribing to Replicate](https://replicate.com/account). Then, you’ll be able to log in to Replicate using your API token each time you need to run Python.

You should never store your API key directly in a Python file or notebook - this would enable others to gain unauthorized access to your account. Instead, it is recommended to set the `REPLICATE_API_TOKEN` variable in your shell prior to running Python:

Copy

`(replicate_venv) % REPLICATE_API_TOKEN="..." python run_text2image_model.py`

If you’re in a Jupyter notebook, you can use [getpass](https://docs.python.org/3/library/getpass.html#module-getpass) to receive user input beneath a cell without displaying it.

Assuming everything worked, you should be able to import the replicate module now. In our accompanying notebook, we also include `pathlib.Path`, which is needed for some inputs.

Copy

```
import replicate
from pathlib import Path
```

## [](https://replicate.com/blog/exploring-text-to-image-models#generate-an-image-from-text)Generate an image from text

Using a few lines of Python, you can programmatically generate an image via text.

Replicate allows us to look up models by `f"{username}/{model_name}` with `replicate.models.get`.

For the example in our notebook, we’ll use [“afiaka87/glid-3-xl”](https://replicate.com/afiaka87/glid-3-xl), a great model for generating photorealistic images. For fun, let’s generate an image of an avocado lightbulb!

Copy

```
model = replicate.models.get("afiaka87/glid-3-xl")
version = model.versions.get("d74db2a276065cf0d42fe9e2917219112ddf8c698f5d9acbe1cc353b58097dab")
```

Models on Replicate are run using the `.predict` method. Let’s take a quick look at the named/keyword arguments for `.predict`.

The named/keyword arguments for each model will vary. `glid-3-xl` requires one input `prompt` - a scene descri
Read full article → ← Back to Reads