Introduction
Welcome to a detailed explanation of code for building GenAI (Generative Artificial Intelligence) Applications using Amazon Web Services (AWS).
AWS is a comprehensive cloud computing platform provided by Amazon. It offers various tools and services to individuals and companies, such as Amazon Bedrock, AWS DevOps services, and AWS cloud services.
This blog will guide you through the process of setting up GenAI applications: Banner Design that leverages Amazon Bedrock. By the end of this blog, you will be able to develop a functional application capable of generating high-quality images based on the input values.
Let us begin by understanding the basic components for the development of GenAI-based applications
What Is Generative AI?
Generative AI is a branch of AI that mainly focuses on multimodal foundation models that can generate data based on trained or existing data. It is the subset of deep learning models capable of generating text and images. Various examples of Generative AI include:
- OpenAI – ChatGPT (Chatbot based on GPT-3 model)
- Bard (Chatbot developed by Google)
Generative AI has a wide range of applications in Healthcare, HR and Legal, Customer Service, and content for marketing campaigning. It has various applications such as Content Generation, Image Generation, Text Summarization, and Chatbots.
How Does GenAI Applications Work?
The key components of building GenAI applications are as follows :
Prompt, Completion, and Inference are used for input, output, and inference for trained models.
What is Amazon Bedrock?
Amazon offers a comprehensive suite of tools and resources designed to streamline the development, training, and deployment of advanced GenAI applications
Powered by the robust infrastructure of Amazon Web Services (AWS), Bedrock serves as a crucial solution, empowering developers and data scientists to harness the full potential of modern generative AI technology.
Let’s take a deep dive into the service managed by AWS.
Amazon Bedrock is a fully managed, serverless service from AWS. It gives access to base foundation models from Amazon and other providers such as Cohere, Stability AI, AI21 Labs, and Meta.
In Amazon bedrock, It accepts the input as prompt as text and generates the output depending on foundation model parameters such as:
- Amazon: Titan
- AI21 Labs: Jurassic 2
- Anthropic: Claude
- Stability.AI: Stable Diffusion
Architecture of GenAI Applications
GenAI Applications
Figure: The architecture of the application
Setting Up Your Environment
Step one to start with Amazon Bedrock for building your GenAI applications is putting it in your environment. Here’s what you need to do:
- Create an AWS Account
- Access Amazon Bedrock
- Set Up Permissions
- Install Required Tools
- Configure Settings
Now, let’s walk through the step-by-step process of building GenAI applications using Amazon Bedrock.
Some Prerequisites include Model Access and Boto3 Version > 1.28.63.
Step 1: Import Libraries
The first step to be completed is importing all the required libraries. This library gives us access to make connections to set up and invoke the Amazon Bedrock service.
Import json
Import boto3
Import base64
Import datetime
Step 2: Create AWS Bedrock Client Connection
With the libraries in place, the next step is to create connections for Amazon Bedrock to access foundation models (FM) and s3 bucket to store and retrieve input and output information.
client_bedrock = boto3.client('bedrock-runtime')
client_s3 = boto3.client('s3')
Step 3: Store the input prompt in a variable
input_prompt=event['input']
print(input_prompt)
Step 4: Request to invoke and access the Amazon Bedrock Service and set default parameters
Now, it’s time to invoke Amazon Bedrock, which interacts with the input prompt and the model. Amazon Bedrock accepts the model name and some config parameters.
response_bedrock = client_bedrock.invoke_model(contentType='application/json', accept='application/json',modelId='stability.stable-diffusion-xl-v0',
body=json.dumps({"text_prompts": [{"text": input_prompt}],"cfg_scale": 10,"steps": 30,"seed": 0}))
Step 5: Convert Output from Amazon bedrock into readable format.
response_bedrock_byte=json.loads(response_bedrock['body'].read())
Step 6: Convert Binary machine-readable format into image format.
response_bedrock_base64 = response_bedrock_byte['artifacts'][0]['base64']
response_bedrock_outputimage = base64.b64decode(response_bedrock_base64)
Step 7: Store the resultant image in bucket s3.
banner_name = 'bannerName'+ datetime.datetime.today().strftime('%Y-%M-%D-%M-%S')
response_s3=client_s3.put_object(Bucket='bannergeneration',Body=response_bedrock_outputimage,Key=banner_name)
Step 8: Generate API URL
generate_presigned_url = client_s3.generate_presigned_url('get_object', Params={'Bucket':'bannergeneration','Key':banner_name}, ExpiresIn=3600)
Step 9: Test API using the Postman Tool
We can simply access the API using the GET method in the Postman tool and provide prompts as input.
Step 10: Output of the API
Prompt 1: spider man riding a bike
Output
spider man riding a bike
Prompt 2: Spiderman in a snow valley
Output
Spiderman in a snow valley
Conclusion
Congratulations! You have successfully built GenAI applications using Amazon Bedrock.
This walkthrough highlights the seamless integration of Amazon Bedrock, S3 bucket, Lambda function, and API to create a powerful application capable of generating high-quality images.
As you explore the possibilities of GenAI applications, always remember to manage AWS resources to optimize costs and maintain a secure environment. Happy Coding!
FAQs
Amazon Bedrock is a fully managed service that offers a variety of foundation models to fulfill the needs to build an enormous number of applications. It is serverless, so a user does not have to manage any infrastructure.
Amazon Bedrock provides various foundation models, including those from Amazon, Meta, Cohere, Anthropic, and embedding models.
It leverages multiple services, including an AWS lambda function for triggering the function, an S3 bucket for storing the data, and Cloudwatch for logging the input and output logs.
Excerpt - Discover and learn the complete procedure of building GenAI applications using Amazon Bedrock: Banner Design.