SkillAgentSearch skills...

SentimentAnalysisLab

This lab is about how to add the AI and ML cloud service feature to your web application with React and the Amplify Framework.

Install / Use

/learn @rahulbaisla/SentimentAnalysisLab
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

This lab is provided as part of AWS Innovate Data Edition Click here to explore the full list of hands-on labs. :information_source: You will run this lab in your own AWS account in us-east-1. Please follow directions at the end of the lab to remove resources to avoid future costs.

Dev Labs: Build a Sentiment Analysis App in minutes using Amplify Framework

In this workshop we will demonstrate how to add the AI and ML cloud service feature to your web application with React and the Amplify Framework. We will learn to integrate following 3 AWS Services in your web application in few minutes

  1. Amazon Cognito- Fully managed User Management
  2. Amazon Transcribe - Adds speech-to-text capability
  3. Amazon Comprehend - Uses ML to find insights and relationships in text.

The above mentioned are a subset of services that can be added using AWS Amplify. Apart from these, You can provision, attach and use AWS AppSync(GraphQL API), API Gateway (REST API), Amazon S3(Storage), AWS Lambda (Functions), Amazon Pinpoint (Analytics), Amazon Lex (Interactions/Chatbots) etc to your application using AWS Amplify.

Setting Up the Lab Environment

To run this lab, you will require an AWS account. You will be using a Cloud9, which is a web-based development environment that provides a terminal program running on a virtual machine that has the AWS CLI pre-installed and configured.

  1. Login to your AWS Account.

  2. From the Services menu, select Cloud9.

    If you are prompted for a region, select us-east-1.

    You will now create a Cloud9 environment.

  3. Click Create environment.

  4. For Name, enter: amplify-sentimentAnalysis-lab

  5. Click Next and choose following configuration

Environment type : Create a new EC2 instance for environment (direct access)

Instance type : Other instance type -> t3.medium

Platform : Amazon Linux 2 (recommended)

For rest of config use default values and click Next Step.

  1. Finally select Create Environment button. Cloud9 will take a few minutes to launch the environment. Once it is ready, continue to the next step.

  2. In the bash terminal at the bottom of the screen (showing ~/environment $), run the following commands:

    region=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')
    
    cat <<END > ~/.aws/config
    [default]
    region=$region
    END
    
    git clone https://github.com/rahulbaisla/sentimentAnalysisLab.git
    
    
  3. Switch to projects root directory

    cd sentimentAnalysisLab
    
    npm install
    
  4. You will now install the AWS Amplify CLI.

    npm install -g @aws-amplify/cli

IMPORTANT NOTE : If during any part of lab you may face issues related to no space left on device. Follow the instructions in below AWS Documentation to modify the storage volume.

Resize an Amazon EBS volume used by an environment

Initialize Amplify

Inside root directory of project run following command and provide the provided values to set up Amplify project.

amplify init

Enter a name for the project: sentimentAnalysisLab

Enter a name for the environment: dev

Choose your default editor: Visual Studio Code

Choose the type of app that you're building: javascript

What javascript framework are you using: react

Source Directory Path: src

Distribution Directory Path: build

Build Command: npm run-script build

Start Command: npm run-script start

Do you want to use an AWS profile? Yes

Please choose the profile you want to use default

The AWS Amplify CLI will initialize a new project inside your React project & you will see a new folder: amplify. The files in this folder hold your project configuration.

Add Authentication to the Web Application

Amplify CLI provisions Amazon Cognito as backend to provide authN/authZ support for your application. Below command and selected options will create a cloudformation template to provision the Amazon Cognito resource locally under PROJECT_ROOT_DIR/amplify/auth/NAME_OF_COGNITO_RESOURCE folder

amplify add auth

Do you want to use the default authentication and security configuration? Default configuration

Warning: you will not be able to edit these selections. How do you want users to be able to sign in? Username

Do you want to configure advanced settings? No, I am done.

Add functionality to Transcribe text from audio

Amplify CLI provisions Amazon Transcribe as backend to add speech-to-text functionality to your application. Below command and options will create a cloudformation template to provision the the Amazon Transcribe resource locally in your project under PROJECT_ROOT_DIR/amplify/predictions/NAME_OF_CONVERT_RESOURCE folder.

amplify add Predictions

Please select from one of the categories below: Convert

What would you like to convert? Transcribe text from audio

Provide a friendly name for your resource: transcription

What is the source language? US English

Who should have access? Auth users only

Add functionality to Interpret the text

Amplify CLI provisions Amazon Comprehend a natural language processing (NLP) service as backend to provide ability to interpret text and perform sentiment analysis. No machine learning experience is required for this feature. Below command and options will create a cloudformation template to provision the Amazon Comprehend resource locally in your project under PROJECT_ROOT_DIR/amplify/predictions/NAME_OF_INTERPRET_RESOURCE folder.

amplify add Predictions

Please select from one of the categories below: Interpret

What would you like to interpret Interpret: Text

Provide a friendly name for your resource: interpret

What kind of interpretation would you like? All

Who should have access? Auth users only

Push the Backend to AWS Cloud

amplify push

✔ Successfully pulled backend environment dev from the cloud.

Current Environment: dev

| Category    | Resource name                | Operation | Provider plugin   |
| ----------- | ---------------------------- | --------- | ----------------- |
| Auth        | sentimentanalysislab         | Create    | awscloudformation |
| Predictions | transcription                | Create    | awscloudformation |
| Predictions | interpret                    | Create    | awscloudformation |

Are you sure you want to continue? Yes

Note: Amplify CLI uses AWS CloudFormation as default provider to manage the backend attached to the application. Sometimes it can take additional time to create the AWS resources. In order to confirm the status of the Cloud Formation stack and resources being created, login to AWS Console -> CloudFormation and locate the stack being deployed for your app.

Test the Application

  1. In your project directory run following command
	npm run start

You should see something like this:

	You can now view sentimentAnalysisLab in the browser.
	
	Local:            http://localhost:8080/
	On Your Network:  http://172.31.40.28:8080/
  1. In the Cloud9 Preview menu, click Preview Running Application.

  2. Click the 'popout' icon to open it in a new browser tab, as shown below:

  3. Application launched in new tab in browser

  4. Create an user account

  5. Sign-in to the Application

  6. Start Recording and speak some text. Once finished stop the recording.

The audio will be converted into text using Amazon Transcribe Service and the converted text will be interpreted to perform sentiment analysis using Amazon Comprehend Service.

Understanding the code

Importing Amplify into your Front-end application.

How it Works: Amplify supports configuration of your connected AWS resources through a centralized file called aws-exports.js which defines all the regions and service endpoints to communicate. Whenever you run amplify push, this file is automatically created allowing you to focus on your application code. The Amplify CLI will place this file in the appropriate source directory configured with amplify init.

import Amplify, { Predictions } from 'aws-amplify';
import { AmazonAIPredictionsProvider } from '@aws-amplify/predictions';
import { withAuthenticator } from "@aws-amplify/ui-react";
import awsconfig from './aws-exports'; //aws-exports

It’s recommended to add the Amplify configuration step to your app’s root entry point. In case of React it would be App.js.

Amplify.configure(awsconfig);

Amplify.addPluggable(new AmazonAIPredictionsProvider());

Alternatively, You can also manually specify your existing Amazon AI and ML resources in your app using Manual Setup

Convert recorder audio to text Predictions.convert

function convertFromBuffer(bytes) {
    setResponse('Performing Sentiment Analysis...');

    Predictions.convert({
        transcription: {
          source: {
            bytes
          },
           language: "en-US", // other options are "en-GB", "fr-FR", "fr-CA", "es-US"
        },
      }).then(({ transcription: { fullText } }) => {interpretFromPredictions(JSON.stringify(fullText, null, 2))})
        .catch(err => console.log(JSON.stringify(err, null, 2)))   
  }

Performing sentiment analysis on text using Predictions.interpret

  function interpretFromPredictions(textToInt
View on GitHub
GitHub Stars84
CategoryDevelopment
Updated1y ago
Forks66

Languages

JavaScript

Security Score

70/100

Audited on Mar 2, 2025

No findings