Create AI-generated art via SMS with Replicate in Python
Tags: ai, replicate, twilio, stablediffusion
Published: 2023-09-21 21:22:37

---
title: Create AI-generated art via SMS with Replicate in Python
published: true
description: Generate art with AI and Replicate's stable diffusion model via SMS in Python
tags: ai, replicate, twilio, stablediffusion
cover_image: https://assets.cdn.prod.twilio.com/images/Q31hN1ZCXzAuMOB04P8QMhiZ9BXbavnO.width-1616.format-webp.webp
# Use a ratio of 100:42 for best results.
# published_at: 2023-09-21 21:09 +0000
---
This blog post was written for [Twilio](https://twilio.com) and [originally published on the Twilio blog](https://www.twilio.com/blog/ai-art-sms-replicate-python).

It's fun to get creative! Read on to learn how to create AI-generated images via SMS using [Replicate](http://replicate.com/) and [Twilio Programmable Messaging](https://www.twilio.com/docs/sms).

![Prompt via SMS is Pikachu playing tennis and the image returned is of Pikachu on a tennis court with a tennis ball](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fc2ccvqvngfpipo5cua.png)

Do you prefer learning via video more? Check out [this TikTok summarizing this tutorial](https://www.tiktok.com/@lizziepikachu/video/7281387478328610094).

### Prerequisites
1. A Twilio account - [sign up for a free Twilio account here](https://www.twilio.com/try-twilio)
2. A Twilio phone number with SMS capabilities - [learn how to buy a Twilio Phone Number here](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console)
3. Replicate account to use the Stable Diffusion text-to-image model – [make a Replicate account here](https://replicate.com/signin)
4. Python installed - [download Python here](https://www.python.org/downloads/)
5. [ngrok](https://ngrok.com/download), a handy utility to connect the development version of our Python application running on your machine to a public URL that Twilio can access.

:warning: **ngrok is needed for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet. You can also choose to automate ngrok as shown in this article.**

### Replicate
Replicate offers a cloud API and tooling so you can more easily run machine learning models, abstracting away some lower-level AI concepts and handling infrastructure so you can focus more on your own applications. You can run open-source models that others have published, or package and publish your own, either publicly or privately.

### Configuration
Since you will be installing some Python packages for this project, you need to make a new project directory and a [virtual environment](https://docs.python.org/3/tutorial/venv.html).

If you're using a Unix or macOS system, open a terminal and enter the following commands:
```bash
mkdir replicate-prompt-to-image-sms 
cd replicate-prompt-to-image-sms 
python3 -m venv venv 
source venv/bin/activate 
pip install replicate flask twilio
```
If you're following this tutorial on Windows, enter the following commands in a command prompt window:
```bash
mkdir replicate-prompt-to-image-sms  
cd replicate-prompt-to-image-sms  
python -m venv venv 
venv\Scripts\activate 
pip install replicate flask twilio
```
Grab your [default Replicate API Token or create a new one here](https://replicate.com/account/api-tokens). Run this command in the terminal in your current folder:
```bash
export REPLICATE_API_TOKEN={replace with your api token}
```
Now it's time to write some code to receive a prompt via SMS and return an AI-generated image!

### Turn a Prompt into an AI-generated image via SMS with Replicate

Make a file called <em>app.py</em> and place the following import statements at the top.

```python
import replicate
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
```
Next, make a Flask app so your app can receive the inbound text message to your Twilio phone number. That string is then passed to [Replicate 's stable diffusion model](https://replicate.com/stability-ai/stable-diffusion) to generate photo-realistic images given that prompt.

```python
app = Flask(__name__)
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    resp = MessagingResponse()
    inb_msg = request.form['Body'].lower().strip()
    output = replicate.run(
        "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
        input={"prompt": inb_msg}
        )
    res_prompt = "prompt was " + inb_msg
    msg = resp.message(res_prompt)
    img = output[0]
    msg.media(img)
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
```

On the command line, run `python app.py` to start the Flask app. Now it's time to set up a Twilio phone number so you can text it a prompt and get an AI-generated image back!

### Configure a Twilio Number for the SMS Chatbot
Now, your Flask app will need to be visible from the web so Twilio can send requests to it. ngrok lets you do this. With ngrok installed, run `ngrok http 5000` in a new terminal tab in the directory your code is in.

![ngrok terminal after running the command ngrok http 5000](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9ca99dfuftcf76hokcs.png)

You should see the screen above. Grab that ngrok <strong>Forwarding URL</strong> to configure your Twilio number: select your Twilio number under <strong>Active Numbers</strong> in [your Twilio console](https://www.twilio.com/console/phone-numbers/incoming), scroll to the Messaging section, and then modify the phone number’s routing by pasting the ngrok URL with the <em>/sms</em> path in the textbox corresponding to when <strong>A Message Comes In</strong> as shown below:

![Configure Twilio phone number with ngrok URL when a message comes in in the Twilio console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fk1ibibep57vql4jnf0b.png)

Click <strong>Save</strong> and now your Twilio phone number is configured so that it maps to your web application server running locally on your machine and your application can run. Text a prompt of a picture you'd like to generate to your Twilio number and you should get an AI-generated image of whatever you sent over SMS!

![Prompt via SMS is Pikachu playing tennis and the image returned is a different one than the first one at the start of this tutorial of Pikachu on a tennis court](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1dy9k71v16cwyxla1pa.png)

Instead of ngrok, you could [alternatively use another tunneling service like Tailscale](https://www.twilio.com/blog/expose-localhost-to-internet-with-tunnel)–my teammate Rishab reviewed this post, and kept getting a 403 error with ngrok. The solution was to switch port numbers with ngrok–it seems that Tailscale has more privileges to the network settings compared to ngrok. You can view the [complete code on GitHub here](https://github.com/elizabethsiegle/replicate-prompt-to-image-sms/tree/main).

### What's Next for Replicate and Twilio Programmable Messaging?
There is so much fun for developers to have around building with LLMs! Replicate offers so many models to play with like [sdxl, a text-to-image generative AI model](https://replicate.com/stability-ai/sdxl) that creates beautiful 1024x1024 images, or you can fine-tune your own. I can't wait to see what you build with AI–let me know online what you're working on!
- Twitter: [@lizziepika](https://twitter.com/lizziepika)  
- GitHub: [elizabethsiegle](https://github.com/elizabethsiegle)
- Email: lsiegle@twilio.com