Over 10 years we help companies reach their financial and branding goals. Engitech is a values-driven technology agency dedicated.

Gallery

Contacts

Bhubaneswar, India

info@krescitus.com

+1 -800-456-478-23

 

Prerequisites

Before getting started, there are a few prerequisites that you need to have in place:

  • An AWS account: To use AWS services like Bedrock ,Lambda and API Gateway, you will need an AWS account. If you don’t have one already, you can sign up for a free tier account that is free for the first year.
  • Angular project: You should have an existing Angular project from where you will put texts or files for summerization. If you don’t have one, you can create a new project using the Angular CLI.

Now that you have the prerequisites in place, let’s dive into the step-by-step process of how to summerize documents and articles with generative AI

 

Step 1: Set Up Lambda Functions

Login Into your AWS Management Console.

Navigate to Lambda and click “Create Function”

Provide a unique function name , then choose the language to use

to write your function

After

creating the function, in the configuration tab click on the Role Name and then

click on Add Permissions , then Attach Policies and add Bedrock Full Acess to

the lambda function.

Like this create two functions for text-summerization and file-summerization.

After functions are created change the code as follows-

Code for text-summerization

import json

import boto3

import botocore

import textwrap

import os

 

os.environ[‘AWS_LAMBDA_INITIALIZATION_TYPE’] = ‘always’

 

# Bedrock Runtime client used to invoke and question the models

bedrock_runtime = boto3.client(

service_name=’bedrock-runtime’,

region_name=’us-east-1′

)

 

def lambda_handler(event, context):

# Extract input text from the request

input_text = event.get(‘text’, ”)

 

# Log the input text for debugging

print(f”Input Text: {input_text}”)

 

# Use the input_text in your model generation logic

# Replace the hardcoded prompt with the dynamic input_text

prompt = textwrap.dedent(f”””

Please provide different summary for different texts of the following text. Do not add any information that is not mentioned in the text below.

 

{input_text}

“””)

 

body = json.dumps({

“inputText”: prompt,

“textGenerationConfig”: {

“maxTokenCount”: 4096,

“stopSequences”: [],

“temperature”: 0.3,

“topP”: 1

},

})

 

model_id = ‘amazon.titan-tg1-large’  # change this to use a different version from the model provider

 

accept = ‘application/json’

content_type = ‘application/json’

 

try:

response = bedrock_runtime.invoke_model(modelId=model_id, body=body, contentType=content_type)

response_body = json.loads(response[‘body’].read())

return response_body

 

except botocore.exceptions.ClientError as error:

return {‘error’: str(error)}

Code for File-Summerization

import json

import boto3

import botocore

import textwrap

import os

 

os.environ[‘AWS_LAMBDA_INITIALIZATION_TYPE’] = ‘always’

 

# Bedrock Runtime client used to invoke and question the models

bedrock_runtime = boto3.client(

service_name=’bedrock-runtime’,

region_name=’us-east-1′

)

 

def extract_text_from_file(file_content):

# Perform any necessary text extraction logic here based on your file format

# For simplicity, assuming a plain text file

return file_content

 

def summarize_text(input_text):

prompt = textwrap.dedent(f”””

Please provide a summary of the following text. Do not add any information that is not mentioned in the text below.

 

{input_text}

“””)

 

body = json.dumps({

“inputText”: prompt,

“textGenerationConfig”: {

“maxTokenCount”: 4096,

“stopSequences”: [],

“temperature”: 0.,

“topP”: 1

},

})

 

model_id = ‘amazon.titan-tg1-large’  # change this to use a different version from the model provider

 

accept = ‘application/json’

content_type = ‘application/json’

 

try:

response = bedrock_runtime.invoke_model(modelId=model_id, body=body, contentType=content_type)

response_body = json.loads(response[‘body’].read())

return response_body

 

except botocore.exceptions.ClientError as error:

return {‘error’: str(error)}

 

def lambda_handler(event, context):

try:

# Extract file content from the event

file_content = event.get(‘fileContent’, ”)

 

# Extract text from the file

input_text = extract_text_from_file(file_content)

 

# Log the input text for debugging

print(f”Extracted Text: {input_text}”)

 

# Use the input_text in your model generation logic

response = summarize_text(input_text)

 

return response

 

except Exception as error:

return {‘error’: str(error)}

 

Step 2: Set Up API Gateway for Lambda Functions

Login Into your AWS Management Console.

Navigate to API Gateway and click “Create API”

Choose HTTP API and click on Build

  •         Click on Add Integration , there choose Lmabda, then choose your lambda function Give an API name and click Next
  •         Give method type, click Next and again click click Next and then Create API

Like this create APIs for both the functions

In your Angular service file invoke those lambda function using their respective API invoke URLs.

You will find those URls by clicking Stages under Deploy tab in the left panel.

 

 

Conclusion

In this

step-by-step guide, we have walked you through the process of summerizig texts.

By leveraging AWS services like Bedrock , Lambda, API Gateway, you can generate

summerized texts. We covered the prerequisites, setting up Lambda and API Gateway.

By following this guide, you are now equipped with the knowledge and tools to

successfully summerize long texts and files on AWS.