Build a Text-to-Image tool using opensource model from Huggingface

Share it with your senior IT friends and colleagues
Reading Time: 3 minutes

Namaste and Welcome to Build It Yourself.

In this tutorial, we will build a Text-to-Image tool using an opensource model from Huggingface

You will see how easy it is to access a model from Huggingface. You can also save money if you have subscribed to a “Text-to-Image” AI tool.

If you are a working IT professional looking to learn AI & Generative AI in a simple language, check out the courses and other details – https://www.aimletc.com/online-instructor-led-ai-llm-coaching-for-it-technical-professionals/

Why this article is needed?

In the previous article, we learnt how to use OpenAI’s models. We also learnt how to access an opensource LLM present on Huggingface

However, there are many other open-sourced models present on Huggingface which can also be used for a variety of purposes, for example – Image Generation, Text-to-video, Text-to-voice and many more.

We can access these models for free.

In this article, we will learn how to use an open-source model for text-to-image generation using a few lines of code.

We will use this Google Colab Notebook – https://github.com/tayaln/Image-Generation-using-opensource-LLM-from-Huggingface/blob/main/Image_Generation_Huggingface.ipynb

Let us dive in.

Pre-requirement

– An Open Mind to learn new things

– Huggingface account

Build a Text-to-Image tool using opensource model from Huggingface

Step 1 – Install Diffusers with torch and transformers

First, we will install the diffusers library and its dependencies, including PyTorch support. This is typically used for tasks like image generation and manipulation with diffusion models.\

Then we will install the transformers library, which is commonly used for tasks like text generation, translation, and sentiment analysis.

Step 2 – Let us create the text-to-image tool

So to start with we will import DiffusionPipeline and torch.

Diffusion Pipeline class will be used to download and load a pre-trained model. This method will grab all the necessary files for the specified model and configuration.

Step 3 – How to choose a model from Huggingface.

Go to Huggingface and go to the model section

In the model section, come to the Computer vision section and choose Text to Image

In the Text to Image section, we need to select the model we want to use.

To select the model, click on the model and copy its repo id

Go to Google Collab and paste the repo id

Step 4 – We will change the type to float16 to reduce the size of the model

Step 5 – pipeline.to(“cuda”) moves the Stable Diffusion pipeline to the CUDA device, which is your GPU.

This is done to leverage the GPU’s parallel processing capabilities, which can significantly speed up the image generation process, as these models are computationally intensive.

Step 6 – Add your prompt for the model to convert text into an image

As we are using the model on Google Collab, it will take some time for the model to create the image.

Step 7 – Show this tool to your friends and colleagues

We saw how easy it is to build a Text-to-Image tool using an open-source model with just a few lines of code.

Please share the images you create using this tool with me – https://www.linkedin.com/in/nikhileshtayal/

Dedicated AI and Gen AI Coaching for working IT Professionals

In case you are looking to learn AI + Gen AI in an instructor-led live class environment, check out these dedicated courses for senior IT professionals here

Pricing for AI courses for IT professionals – https://www.aimletc.com/ai-ml-etc-course-offerings-pricing/

Happy learning!

Share it with your senior IT friends and colleagues
Nikhilesh Tayal
Nikhilesh Tayal
Articles: 75