AI21 Labs + Stable Diffusion Tutorial: Beginner friendly Tutorial – How to build your app with AI21 Labs, adding Stable Diffusion integration.

Introduction

Stable Diffusion, is a new generative model that can generate high-resolution images with a single forward pass. AI21 Studio is a platform that provides developers and businesses with top-tier natural language processing (NLP) solutions, powered by AI21 Labs’ state-of-the-art language models.

What we are going to do?

In this tutorial, we will build a simple and funny app, for creating engaging tweets, using AI21 Labs and cool cover images using Stable Diffusion. We will use Streamlit to build our app. Streamlit is an open-source Python library that makes it easy to build beautiful custom web-apps for machine learning and data science.

Prerequisites

Go to Visual Studio Code and choose that compatible with your operating system and download it, or feel free to use any other code editor like: IntelliJ IDEA, PyCharm, etc.

Visual Studio Code
Visual Studio Code
Visual Studio Code
Visual Studio Code

To use AI21 Labs Studio we need API Key. Go to AI21 Labs Studio, Sign up for an account. Go to Dashboard once you have created an account. Then, click to Profile picture on the top right corner.

AI21 Labs Studio
AI21 Labs Studio

Select API Key from the dropdown menu.

AI21 Labs Studio
AI21 Labs Studio

Click copy icon to copy your API Key. Don’t forget to save it somewhere safe.

AI21 Labs Studio
AI21 Labs Studio

To use Stable Diffusion we need API Key. Go to Dream Studio, Sign up for an account to be taken to your API Key. Go to Dashboard. Then, click + button to create new API Key.

See also  Stable Diffusion tutorial: how to create video with text prompts
Stable Diffusion
Stable Diffusion

Click copy icon to copy your API Key. Don’t forget to save it somewhere safe.

Stable Diffusion
Stable Diffusion

To deploy our app we need to create an account on Streamlit. Go to Streamlit and click Sign up button.

Stable Diffusion
Stable Diffusion

I recommend you to use GitHub to sign up. Later it will be helpful to depploy our app in a seconds. Click Sign up with GitHub button.

Getting Started

Create a brand new project

Open Visual Studio Code and create new folder named ai21-sd-tutorial:

Create a virtual environment and activate it

Next, we need to create a virtual environment and activate it. To do this, run the following command in your terminal:

Install all dependencies

Now, we need to install all dependencies. To do this, open and run the following command in your terminal:

Implementing the app

Firstly, let’s create a file named stable_diffusion.py and implement function to generate image from text using Stable Diffusion model. Read more about Text-to-image gRPC API python client.

Next, let’s create a file named ai21_studio.py and implement function which will be used to generate cool ideas for engaging tweets to promote a brand or product:

Perfect! Now we have all the functions we need to generate engaging ideas and cool cover images.

Finally, Create a file named app.py here we will implement our logic for Streamlit app. It allows us to create really fast web apps in pure Python and share it with everyone.

Stable Diffusion
Stable Diffusion

Let’s import all the necessary libraries and functions:

Basic configuration for the app:

Initialize the app states:

Work on the app layout:

Let’s create sidebar where we can put our API keys. Why not simple use .env? Because we want to make it available for everyone to use it without any hassle. Later, we can also deploy it to Streamlit Sharing Cloud.

See also  Stable Diffusion tutorial: Creating an animated avatar of yourself with Stable Diffusion

Quickly add a cool title and description for the app:

Now, let’s add a st.text_area() widget to get the prompt from the user and a st.button() widget to generate the ideas based on the prompt.

Now, let’s add another st.text_area() widget for image generation using Stable Diffusion. You may ask why we don’t instantly generate the image after clicking the Generate ideas button along with the ideas. The reason is that we want to make it more interactive and fun.

Output genearated ideas and image:

And last but not least, it’s time to implement main functions for generating ideas:

and image:

Perfect! Now, we can run our app and see the result.

Deploy to Streamlit Sharing Cloud

Checkout ElevenLabs Tutorial for detailed instructions on how to deploy your app to Streamlit Sharing Cloud.

Stable Diffusion
Stable Diffusion

Conclusion

We build something cool today! We learned how to use AI21 Studio API to generate ideas for tweets and Stable Diffusion API to generate images for tweets cover. We also learned how to deploy our app to Streamlit Sharing Cloud. Perfect! Don’t forget to join upcoming hackathons on lablab.ai and win cool prizes.

Thank you for following along with this tutorial.

If you have any questions, feel free to reach out to me on LinkedIn or Twitter. I’d love to hear from you!

made with 💜 by abdibrokhim for lablab.ai tutorials.