GenAI Demos: Text and Text-to-Image Generation

In our previous post, we discussed how training models can be both costly and time-consuming. This task is typically handled by major companies such as OpenAI, Google, and Facebook. 

However, we can use LLM (Large Language Model) models to develop our own applications.

Experiment With OpenAI’s ChatGPT Application 

Click on ChatGPT and sign up to experiment with the built-in AI application. Write a prompt based on your requirements. Here is the example,

As we know LLM models are autoregressive–they produce one word at a time by predicting the next word based on the previous ones. Because they use randomness in their decision-making process, even with the same input, they might generate slightly different responses each time. If you use the same prompt as shown above, you will get a different story each time.

We can achieve the same with Python code. Below is an example using the GPT-3.5 turbo model developed by OpenAI.

First, create and copy your API key from the OpenAI API to use LLM. we’ll utilize this key to access and manipulate LLMs for content generation. Then, create a Python environment. Open your favorite editor and write the following code:

# chat.py
from openai import OpenAI
OPENAI_API_KEY="sk-......" # your API key
client = OpenAI(api_key=OPENAI_API_KEY)
# add your completion code
prompt = "Complete the following: Once upon a time there was a"
messages = [{"role": "user", "content": prompt}]
model="gpt-3.5-turbo"
# make completion
completion = client.chat.completions.create(model=model, messages=messages)
# print response
print(completion.choices[0].message.content)

Run the above python file,

python file_name.py

It will generate content for you and complete the story. Please refer to the OpenAI Documentation for more details on customizing applications to suit your needs.

Image Generation Application using Huggingface Model

Now, we are going to implement an image generation app using a stable diffusion model to create new images. This application has been developed on Hugging Face Spaces. Hugging face is a platform where the ML community collaborates on models, datasets, and applications.

You can experiment with it using our Text-to-Image Generation App.

  • Now, write a prompt of your choice.

It will take some time to generate an image after hitting the “Generate Image” Button. You will see a beautiful image generated by AI.

If you want to develop this application open Huggingface spaces and create new space with the streamlit framework and write the following code in the new app.py file. Use the “runwayml/stable-diffusion-v1-5” model through the stable diffusion pipeline to generate an image.

#app.py
import streamlit as st
from PIL import Image
import torch
from diffusers import StableDiffusionPipeline

def generate_image(input_text):
    # Load Diffusion pipeline
    pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
    # Generate image from text prompt
    prompt = input_text
    generated_image = pipe(prompt).images[0]
    return generated_image
   
st.title("Text to Image Generation App")# Set Streamlit app title
input_text = st.text_input("Enter your image description:", "")# Text input for prompt
if st.button("Generate Image"):# Button to generate image
    # Generate image based on the prompt
    img = generate_image(input_text)
    # Display the generated image
    st.image(img, caption="Generated Image")

Key elements of the above code

  • Streamlit app: User Interface.
  • Prompt: Image description (What you want to generate?)
  • Diffusion model: Generates pixels one by one to create a new image. 
  • st.image() function: Displays the image
  • Add a requirements.txt file.
torch
transformers
diffusers
streamlit
  • After committing these files it will take some time to build and run the application.

Explore HuggingFace to understand the pipeline and LLM models in detail. We will come up with tools like Streamlit and Huggingface in our future posts.

Summary

In this post, we explored two different examples using OpenAI and Huggingface models. Stay tuned for our upcoming posts to explore more on Generative AI.

Join Our Newsletter

Share this article:

Table of Contents