Let’s build with Stable Diffusion and GPT4ALL!
Need some inspiration for new product ideas? Want to create an AI app, but can’t find a problem to solve? We got you covered – welcome to the another outstanding tutorial in which you will learn more about how to create a Stable-Diffusion applictaions. In this tutorial, we will learn how to create an API that uses GPT-4-All alongside Stable Diffusion to generate new product ideas for free. For this purpose, we will create a Flask app that installs GPT-4-All locally. This will enable us to generate a name and description for the product. Additionally, we will utilize Stable Diffusion API to create a featured image to showcase the product. To enhance usability, we will also develop a simple UI that allows us to access the app through a browser. Here’s the repository with the final code
What is GPT4All?
GPT4All is an open source chatbot developed by Nomic AI and trained over a massive curated corpus of assistant interactions including word problems, multi-turn dialogue, code, poems, songs, and stories.
First, create a directory for your project:
You will need an API Key from Stable Diffusion. You can get one for free after you register at https://dreamstudio.ai/
Once you have your API Key, create a .env file and paste it there with the rest of the environment variables:
And install the required libraries:
Now we can create a file named app.py and start coding.
🤖 Let’s go!
First, we need to import all the necessary libraries and load environment variables:
Then, we can initialize a Flask app with CORS enabled
Now, we can define an endpoint that listens to any idea and transforms it into a product. We will start by utilizing GPT-4-All to generate a description and a name for the product. Please note that when running the app for the first time, you will likely need to download a model, which may take some time depending on your internet connection. In this tutorial, we will use the ‘gpt4all-j-v1.3-groovy’ model. Next, we will utilize the product name to invoke the Stable Diffusion API and generate an image for our new product.
/generate endpoint will accept any query string in the URL and respond with a JSON containing a name, a description, and the image path. For instance, when using the URL “http://localhost:8000/generate?prompt=Cooking%20app”, you will receive ideas for a “Cooking app”.
Next, we will define the “generate_image” function, which formats the prompt for Stable Diffusion and saves the generated image in the project’s root directory. It will then return the image path so that we can include it in the final JSON response.
Then we define check_and_create_filename(filename), an auxiliary function that creates a new name for the image file in case it already exists. Otherwise, we would overwrite the image everytime we make a call to the API.
Finally, we start an http server and expose the API endpoint.
🚀 Running the app
Now we can run our app:
We can test our app by sending a request to the endpoint. Open your browser and change <prompt> for any text you want.
Remember to use “%20” instead of spaces for the prompt. For example:
Congratulations! You have just finished building your API. You can now run it locally or deploy it and to get a json with a product idea that you can later use with your favorite frontend framework.
🎁 Bonus: create a frontend for the API
As a small bonus, we can create an html file named “index.html” that uses jquery and ajax with some styles to create a simple page with a text input and a loading button that calls our API.
You can access your page by going to /path_to_project_directory/index.html in your browser.
We’ve build with Stable Diffusion and GPT4ALL – 🤔 Final Thoughts
This app works as a starting point for generating cool ideas for new products and get some inspiration. But it can easily be improved adding more styles and refining the prompts. You can also try other models like Mosaic’s mpt and see which works best. I leave this up to you and hope that this tutorial will help you come up with some great ideas. You can check the final repository here
And practice what you;ve learn here during our amazing AI Hackathons! Join the AI revolution!