As human beings, we are inherently imaginative and creative. But what if AI could also be imaginative and creative like human beings? That’s where generative AI comes into the picture. This will generate content exactly like human beings. Suppose you thought of drawing a picture and there is an idea of a picture in your mind. But you are not getting how to start and how to draw. Don’t worry. Image Alpha is for you. This creates realistic images using textual descriptions. In this article, we will learn about them. Let’s get started.
In this article, we will learn
This article was published as a part of the Data Science Blogathon.
Generative AI is a class of machine learning models that generate content. This content can be images, sounds, texts, and any other form of media. It generates content similar to the data that it was trained on. Like if the model is trained on a car dataset, then it is able to generate images similar to that car dataset. It uses very complex mathematical models to analyze training data and learns patterns from it. Also uses those patterns to generate similar patterns or structures as output. A type of creative intelligence as it doesn’t only analyze existing data, it will create new data.
It uses generative adversarial networks (GANs). These GANs consist of two neural networks. They are a generator and a discriminator. These two networks work together to generate new data. The generator creates new data based on input. And discriminator evaluates the generated data and decides whether it is real or fake. If it is fake then the discriminator gives feedback to the generator and this generator corrects it accordingly. This loop repeats until the generator produces the correct realistic output.
The advent of new technologies often brings with it a dual-edged impact on society. There are both positive and negative impacts.
Image Alpha is a deep learning model that is developed by OpenAI and uses artificial intelligence that generates images using textual prompts. This model is trained on a large dataset that has images and their description. AI models give output similar to the data used for training. It is capable of creating a wide range of images. I can create realistic photos and designs. It can also generate multiple variations of an image based on different textual descriptions provided.
This Image Alpha is particularly helpful for designers, content creators, and advertisers. By using it they can save time and effort. Though it has many advantages, it still has concerns. It may lead to the devaluation of human creativity. Because this AI is thinking and generating. Why do people use their creativity? This was the leading concern of it. The development of Image Alpha and other generative AI models could lead to advances in various fields like NLP, computer vision, and machine learning.
Image Alpha is a part of DALL E 2 which is also an AI system that creates realistic images using the descriptions provided. Here is the link for DALL E 2 in OpenAi. You can try it with your texts.
https://openai.com/product/dall-e-2
Now we will build a model that will generate a URL of the image using textual descriptions. For this, I used a pre-trained model Image Alpha.
First things first. We have to import the necessary libraries. Here we need openai, requests to send requests to OpenAI API and CaseInsensitiveDict to create headers for the HTTP request. Import CaseInsensitiveDict structures module.
import openai
import requests
from requests.structures import CaseInsensitiveDict
If you don’t have an account on OpenAI, then create one and generate an API key. We need an API key for authenticating when making API requests. Here you can create an API key. By clicking on Create new secret key, it will generate a key like “sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”. Load this using openai.
Link: https://platform.openai.com/account/api-keys
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Next, create a function that will generate an image using single text input. I created a generate_image function with an argument input_text. Now we will take a pre-trained model that takes textual descriptions and generated images. And load it into the variable model. The pre-trained model is image-alpha-001. Next, create a headers dictionary using CaseInsensitiveDict. Set the content type to JSON. This means the request data will be in JSON format. Set the authorization to openai API Key. So it uses the openai API key for authentication to access API.
Create JSON data and this will be sent to our API request. Firstly, the model is added and then the input parameter is passed which is used to generate images. Next, we specified the number of images we needed in the output, the size of the image, and the format of the response. Here I gave the URL. So it will generate a URL that contains the output image.
Now send an HTTP POST request with URL, headers, and data. Check the status of the code and raise a value error if it is unsuccessful. And finally, create the URL of the output image.
def generate_image(input_text):
model = "image-alpha-001"
url = "https://api.openai.com/v1/images/generations"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
headers["Authorization"] = f"Bearer {openai.api_key}"
data = """
{
"""
data += f'"model": "{model}",'
data += f'"prompt": "{input_text}",'
data += """
"num_images":1,
"size":"512x512",
"response_format":"url"
}
"""
resp = requests.post(url, headers=headers, data=data)
if resp.status_code != 200:
raise ValueError("Failed to generate image")
response_text = resp.json()
return response_text['data'][0]['url']
Example
Let’s try it with an example.
# Example usage
image_url = generate_image("Evening sunset with birds and mountains")
print(image_url)
Here are the links: first, second.
Overall, in this article, we learned about this amazing technology called Generative Ai and we have seen the power of one of its models, Image Alpha. Image Alpha is a powerful model that allows us to unlock our imagination and create visuals with just a few lines of input text. We can see amazing pictures generated by Image Alpha. Do you guys like it? Try implementing the code and generate images by feeding prompts to the model!
Some of the key takeaways are as follow:
Hope you found this article useful. Connect with me on LinkedIn.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.