In the past, Generative AI has captured the market, and as a result, we now have various models with different applications. The evaluation of Gen AI began with the Transformer architecture, and this strategy has since been adopted in other fields. Let’s take an example. As we know, we are currently using the VIT model in the field of stable diffusion. When you explore the model further, you will see that two types of services are available: paid services and open-source models that are free to use. The user who wants to access the extra services can use paid services like OpenAI, and for the open-source model, we have a Hugging Face.
You can access the model and according to your task, you can download the respective model from the services. Also, note that charges may be applied for token models according to the respective service in the paid version. Similarly, AWS is also providing services like AWS Bedrock, which allows access to LLM models through API. Toward the end of this blog post, let’s discuss pricing for services.
This article was published as a part of the Data Science Blogathon.
Generative AI is a subset of artificial intelligence(AI) that is developed to create new content based on user requests, such as images, text, or code. These models are highly trained on large amounts of data, which makes the production of content or response to user requests much more accurate and less complex in terms of time. Generative AI has a lot of applications in different domains, such as creative arts, content generation, data augmentation, and problem-solving.
You can refer to some of my blogs created with LLM models, such as chatbot (Gemini Pro) and Automated Fine-Tuning of LLaMA 2 Models on Gradient AI Cloud. I also created the Hugging Face BLOOM model by Meta to develop the chatbot.
AWS Bedrock is a platform provided by Amazon Web Services (AWS). AWS provides a variety of services, so they recently added the Generative AI service Bedrock, which added a variety of large language models (LLMs). These models are built for specific tasks in different domains. We have various models like the text generation model and the image model that can be integrated seamlessly into software like VSCode by data scientists. We can use LLMs to train and deploy for different NLP tasks such as text generation, summarization, translation, and more.
Setting up AWS Bedrock is simple yet powerful. This framework, based on Amazon Web Services (AWS), provides a reliable foundation for your applications. Let’s walk through the straightforward steps to get started.
Step 1: Firstly, navigate to the AWS Management Console. And change the region. I marked in red box us-east-1.
Step 2: Next, search for “Bedrock” in the AWS Management Console and click on it. Then, click on the “Get Started” button. This will take you to the Bedrock dashboard, where you can access the user interface.
Step 3: Within the dashboard, you’ll notice a yellow rectangle containing various foundation models such as LLaMA 2, Claude, etc. Click on the red rectangle to view examples and demonstrations of these models.
Step 4: Upon clicking the example, you’ll be directed to a page where you’ll find a red rectangle. Click on any one of these options for playground purposes.
Stable Diffusion is a GenAI model that generates images based on user(text) input. Users provide text prompts, and Stable Diffusion produces corresponding images, as demonstrated in the practical part. It was launched in 2022 and utilizes diffusion technology and latent space to create high-quality images.
After the inception of transformer architecture in natural language processing (NLP), significant progress was made. In computer vision, models like the Vision Transformer (ViT) became prevalent. While traditional architectures like the encoder-decoder model were common, Stable Diffusion adopts an encoder-decoder architecture using U-Net. This architectural choice contributes to its effectiveness in generating high-quality images.
Stable Diffusion operates by progressively adding Gaussian noise to an image until only random noise remains—a process known as forward diffusion. Subsequently, this noise is reversed to recreate the original image using a noise predictor.
Overall, Stable Diffusion represents a notable advancement in generative AI, offering efficient and high-quality image generation capabilities.
Some of the Images that are created by using the stable diffusion model
To build Stable Diffusion, you’ll need to follow several steps, including setting up your development environment, accessing the model, and invoking it with the appropriate parameters.
Step 1. Environment Preparation
conda create -p ./venv python=3.10 -y
conda activate ./venv
Step 2. Installing Requirements Packages
!pip install boto3
!pip install awscli
Step 3: Setting up the AWS CLI
aws configure
Step 4: Importing the necessary libraries
import boto3
import json
import base64
import os
Step 5: Create an AWS Bedrock Client
bedrock = boto3.client(service_name="bedrock-runtime")
Step 6: Define Payload Parameters
# DEFINE THE USER QUERY
USER_QUERY="provide me an 4k hd image of a beach, also use a blue sky rainy season and
cinematic display"
payload_params = {
"text_prompts": [{"text": USER_QUERY, "weight": 1}],
"cfg_scale": 10,
"seed": 0,
"steps": 50,
"width": 512,
"height": 512
}
Step 7: Define the Payload Object
model_id = "stability.stable-diffusion-xl-v0"
response = bedrock.invoke_model(
body= json.dumps(payload_params),
modelId=model_id,
accept="application/json",
contentType="application/json",
)
Step 8: Send a Request to the AWS Bedrock API and Get the Response Body
response_body = json.loads(response.get("body").read())
Step 9: Extract Image Data from the Response
artifact = response_body.get("artifacts")[0]
image_encoded = artifact.get("base64").encode("utf-8")
image_bytes = base64.b64decode(image_encoded)
Step 10: Save the Image to a File
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
file_name = f"{output_dir}/generated-img.png"
with open(file_name, "wb") as f:
f.write(image_bytes)
Step 11: Create a Streamlit app
pip install streamlit
import streamlit as st
import boto3
import json
import base64
import os
def generate_image(prompt_text):
prompt_template = [{"text": prompt_text, "weight": 1}]
bedrock = boto3.client(service_name="bedrock-runtime")
payload = {
"text_prompts": prompt_template,
"cfg_scale": 10,
"seed": 0,
"steps": 50,
"width": 512,
"height": 512
}
body = json.dumps(payload)
model_id = "stability.stable-diffusion-xl-v0"
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept="application/json",
contentType="application/json",
)
response_body = json.loads(response.get("body").read())
artifact = response_body.get("artifacts")[0]
image_encoded = artifact.get("base64").encode("utf-8")
image_bytes = base64.b64decode(image_encoded)
# Save image to a file in the output directory.
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
file_name = f"{output_dir}/generated-img.png"
with open(file_name, "wb") as f:
f.write(image_bytes)
return file_name
def main():
st.title("Generated Image")
st.write("This Streamlit app generates an image based on the provided text prompt.")
# Text input field for user prompt
prompt_text = st.text_input("Enter your text prompt here:")
if st.button("Generate Image") and prompt_text:
image_file = generate_image(prompt_text)
st.image(image_file, caption="Generated Image", use_column_width=True)
elif st.button("Generate Image") and not prompt_text:
st.error("Please enter a text prompt.")
if __name__ == "__main__":
main()
streamlit run app.py
LLaMA 2, or the Large Language Model of Many Applications, belongs to the category of Large Language Models (LLM). Facebook (Meta) developed this model to explore a broad spectrum of natural language processing (NLP) applications. In the earlier series, the ‘LAMA’ model was the starting face of development, but it utilized outdated methods.
To build LLaMA 2, you’ll need to follow several steps, including setting up your development environment, accessing the model, and invoking it with the appropriate parameters.
Step 1: Import Libraries
import boto3
import json
Step 2: Define Prompt and AWS Bedrock Client
prompt_data = """
Act as a Shakespeare and write a poem on Generative AI
"""
bedrock = boto3.client(service_name="bedrock-runtime")
Step 3: Define Payload and Invoke Model
payload = {
"prompt": "[INST]" + prompt_data + "[/INST]",
"max_gen_len": 512,
"temperature": 0.5,
"top_p": 0.9
}
body = json.dumps(payload)
model_id = "meta.llama2-70b-chat-v1"
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get("body").read())
response_text = response_body['generation']
print(response_text)
Step 4: Run the Notebook
Step 5: Create a Streamlit app
import streamlit as st
import boto3
import json
# Define AWS Bedrock client
bedrock = boto3.client(service_name="bedrock-runtime")
# Streamlit app layout
st.title('LLama2 Model App')
# Text input for user prompt
user_prompt = st.text_area('Enter your text prompt here:', '')
# Button to trigger model invocation
if st.button('Generate Output'):
payload = {
"prompt": user_prompt,
"max_gen_len": 512,
"temperature": 0.5,
"top_p": 0.9
}
body = json.dumps(payload)
model_id = "meta.llama2-70b-chat-v1"
response = bedrock.invoke_model(
body=body,
modelId=model_id,
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get("body").read())
generation = response_body['generation']
st.text('Generated Output:')
st.write(generation)
streamlit run llama2_app.py
The pricing of AWS Bedrock depends on various factors and the services you use, such as model hosting, inference requests, data storage, and data transfer. AWS typically charges based on usage, meaning you only pay for what you use. I recommend checking the official pricing page as AWS may change their pricing structure. I can provide you with the current charges, but it’s best to verify the information on the official page for the most accurate details.
Meta LlaMA 2
Stability AI
This blog delved into the realm of generative AI, focusing specifically on two powerful LLM models: Stable Diffusion and LLamV2. We also explored AWS Bedrock as a platform for creating LLM model APIs. Using these APIs, we demonstrated how to write code to interact with the models. Additionally, we utilized the AWS Bedrock playground to practice and assess the capabilities of the models.
At the outset, we highlighted the importance of selecting the correct region within AWS Bedrock, as these models may not be available in all regions. Moving forward, we provided a practical exploration of each LLM model, starting with the creation of Jupyter notebooks and then transitioning to the development of Streamlit applications.
Finally, we discussed AWS Bedrock’s pricing structure, underscoring the necessity of understanding the associated costs and referring to the official pricing page for accurate information.
A. Generative AI is a subset of artificial intelligence focused on creating new content, such as images, text, or code, rather than just analyzing existing data.
A. Stable Diffusion is a generative AI model that produces photorealistic images from text and image prompts using diffusion technology and latent space.
A. AWS Bedrock provides APIs for managing, training, and deploying models, allowing users to access large language models like LLAMv2 for various applications.
A. You can access LLM models on AWS Bedrock using the provided APIs, such as invoking the model with specific parameters and receiving the generated output.
A. Stable Diffusion can generate high-quality images from text prompts, operates efficiently using latent space, and is accessible to a wide range of users.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.