275 | );
276 | }
277 |
278 | export default withAuthenticator(App, {
279 | includeGreetings: true,
280 | signUpConfig: {
281 | hiddenDefaults: ["phone_number"]
282 | }
283 | });
284 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This lab is provided as part of **[AWS Innovate Data Edition](https://aws.amazon.com/events/aws-innovate/data/)**
2 | Click [here](https://github.com/phonghuule/aws-innovate-data) to explore the full list of hands-on labs.
3 | :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.
4 |
5 |
6 | # Dev Labs: Build a Sentiment Analysis App in minutes using Amplify Framework
7 |
8 | In this workshop we will demonstrate how to add the AI and ML cloud service feature to your web application with [React](https://reactjs.org/) and the [Amplify Framework](https://aws-amplify.github.io/). We will learn to integrate following 3 AWS Services in your web application in few minutes
9 |
10 | 1. **Amazon Cognito**- Fully managed User Management
11 | 2. **Amazon Transcribe** - Adds speech-to-text capability
12 | 3. **Amazon Comprehend** - Uses ML to find insights and relationships in text.
13 |
14 | 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.
15 |
16 | # Setting Up the Lab Environment
17 |
18 | 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.
19 |
20 |
21 |
22 | 1. Login to your AWS Account.
23 |
24 | 2. From the Services menu, select Cloud9.
25 |
26 | If you are prompted for a region, select **us-east-1**.
27 |
28 | You will now create a Cloud9 environment.
29 |
30 | 3. Click Create environment.
31 |
32 | 4. For Name, enter: amplify-sentimentAnalysis-lab
33 |
34 | 5. Click Next and choose following configuration
35 |
36 | Environment type : **Create a new EC2 instance for environment (direct access)**
37 |
38 | Instance type : **Other instance type -> t3.medium**
39 |
40 | Platform : **Amazon Linux 2 (recommended)**
41 |
42 | For rest of config use default values and click Next Step.
43 |
44 | 6. Finally select Create Environment button.
45 | Cloud9 will take a few minutes to launch the environment. Once it is ready, continue to the next step.
46 |
47 | 7. In the bash terminal at the bottom of the screen (showing ~/environment $), run the following commands:
48 |
49 | ```
50 | region=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')
51 |
52 | cat < ~/.aws/config
53 | [default]
54 | region=$region
55 | END
56 |
57 | git clone https://github.com/rahulbaisla/sentimentAnalysisLab.git
58 |
59 | ```
60 |
61 |
62 | 8. Switch to projects root directory
63 |
64 | ```
65 | cd sentimentAnalysisLab
66 |
67 | npm install
68 | ```
69 |
70 | 9. You will now install the AWS Amplify CLI.
71 |
72 | `
73 | npm install -g @aws-amplify/cli
74 | `
75 |
76 | **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.**
77 |
78 | [Resize an Amazon EBS volume used by an environment ](https://docs.aws.amazon.com/cloud9/latest/user-guide/move-environment.html#move-environment-resize)
79 |
80 | ## Initialize Amplify
81 |
82 |
83 | Inside root directory of project run following command and provide the provided values to set up Amplify project.
84 |
85 | `
86 | amplify init
87 | `
88 |
89 | `Enter a name for the project:` **sentimentAnalysisLab**
90 |
91 | `Enter a name for the environment:` **dev**
92 |
93 | `Choose your default editor:` **Visual Studio Code**
94 |
95 | `Choose the type of app that you're building:` **javascript**
96 |
97 | `What javascript framework are you using:` **react**
98 |
99 | `Source Directory Path:` **src**
100 |
101 | `Distribution Directory Path:` **build**
102 |
103 | `Build Command:` **npm run-script build**
104 |
105 | `Start Command:` **npm run-script start**
106 |
107 | `Do you want to use an AWS profile?` **Yes**
108 |
109 | `Please choose the profile you want to use` **default**
110 |
111 |
112 | 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.
113 |
114 | ## Add Authentication to the Web Application
115 |
116 | Amplify CLI provisions [Amazon Cognito](https://aws.amazon.com/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
117 |
118 | `amplify add auth`
119 |
120 | `Do you want to use the default authentication and security configuration?` **Default configuration**
121 |
122 | `Warning: you will not be able to edit these selections.
123 | How do you want users to be able to sign in?` **Username**
124 |
125 | `Do you want to configure advanced settings?` **No, I am done.**
126 |
127 | ## Add functionality to Transcribe text from audio
128 |
129 | Amplify CLI provisions [Amazon Transcribe](https://aws.amazon.com/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.
130 |
131 | `amplify add Predictions`
132 |
133 | `Please select from one of the categories below:` **Convert**
134 |
135 | `What would you like to convert?` **Transcribe text from audio**
136 |
137 | `Provide a friendly name for your resource:` **transcription**
138 |
139 | `What is the source language?` **US English**
140 |
141 | `Who should have access?` **Auth users only**
142 |
143 |
144 |
145 | ## Add functionality to Interpret the text
146 |
147 | Amplify CLI provisions [Amazon Comprehend](https://aws.amazon.com/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.
148 |
149 | `amplify add Predictions`
150 |
151 | `Please select from one of the categories below:` **Interpret**
152 |
153 | `What would you like to interpret Interpret:` **Text**
154 |
155 | `Provide a friendly name for your resource:` **interpret**
156 |
157 | `What kind of interpretation would you like?` **All**
158 |
159 | `Who should have access? ` **Auth users only**
160 |
161 |
162 | ## Push the Backend to AWS Cloud
163 |
164 | amplify push
165 | ```
166 | ✔ Successfully pulled backend environment dev from the cloud.
167 |
168 | Current Environment: dev
169 |
170 | | Category | Resource name | Operation | Provider plugin |
171 | | ----------- | ---------------------------- | --------- | ----------------- |
172 | | Auth | sentimentanalysislab | Create | awscloudformation |
173 | | Predictions | transcription | Create | awscloudformation |
174 | | Predictions | interpret | Create | awscloudformation |
175 |
176 | Are you sure you want to continue? Yes
177 | ```
178 |
179 | **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.
180 |
181 | ## Test the Application
182 |
183 | 1. In your project directory run following command
184 | ```
185 | npm run start
186 | ```
187 |
188 | You should see something like this:
189 |
190 | ```
191 | You can now view sentimentAnalysisLab in the browser.
192 |
193 | Local: http://localhost:8080/
194 | On Your Network: http://172.31.40.28:8080/
195 | ```
196 |
197 | 2. In the Cloud9 **Preview** menu, click **Preview Running Application**.
198 |
199 | 3. Click the 'popout' icon to open it in a new browser tab, as shown below:
200 | 
201 |
202 | 4. Application launched in new tab in browser
203 | 
204 |
205 | 5. Create an user account
206 | 
207 |
208 | 6. Sign-in to the Application
209 |
210 | 
211 |
212 | 7. Start Recording and speak some text. Once finished stop the recording.
213 |
214 | 
215 |
216 | 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.
217 |
218 |
219 | ## Understanding the code
220 |
221 | Importing Amplify into your Front-end application.
222 |
223 | **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.
224 |
225 | ```
226 | import Amplify, { Predictions } from 'aws-amplify';
227 | import { AmazonAIPredictionsProvider } from '@aws-amplify/predictions';
228 | import { withAuthenticator } from "@aws-amplify/ui-react";
229 | import awsconfig from './aws-exports'; //aws-exports
230 | ```
231 |
232 |
233 | 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.
234 |
235 | ```
236 | Amplify.configure(awsconfig);
237 |
238 | Amplify.addPluggable(new AmazonAIPredictionsProvider());
239 | ```
240 |
241 | Alternatively, You can also manually specify your existing Amazon AI and ML resources in your app using [Manual Setup](https://aws-amplify.github.io/docs/js/predictions#manual-setup)
242 |
243 | Convert recorder audio to text Predictions.convert
244 | ```
245 | function convertFromBuffer(bytes) {
246 | setResponse('Performing Sentiment Analysis...');
247 |
248 | Predictions.convert({
249 | transcription: {
250 | source: {
251 | bytes
252 | },
253 | language: "en-US", // other options are "en-GB", "fr-FR", "fr-CA", "es-US"
254 | },
255 | }).then(({ transcription: { fullText } }) => {interpretFromPredictions(JSON.stringify(fullText, null, 2))})
256 | .catch(err => console.log(JSON.stringify(err, null, 2)))
257 | }
258 | ```
259 |
260 | Performing sentiment analysis on text using Predictions.interpret
261 | ```
262 | function interpretFromPredictions(textToInterpret) {
263 | console.log("inside interpretFromPredictions")
264 | Predictions.interpret({
265 | text: {
266 | source: {
267 | text: textToInterpret,
268 | },
269 | type: "ALL"
270 | }
271 | }).then(result => {setResponse(JSON.stringify(result, null, 2));setGauge(result);})
272 | .catch(err => setResponse(JSON.stringify(err, null, 2)))
273 | }
274 | ```
275 |
276 | ## Host your web application using the AWS Amplify Console
277 |
278 | AWS also provides a CI/CD solution named [Amplify Console](https://aws.amazon.com/amplify/console/getting-started/) for single page web applications that follows a git-based workflow to deploy and host fullstack serverless web applications which can include frontend and backend both. Using Amplify Console to host your app can accelerate the release cycle of your product by providing a simple workflow for deploying full-stack serverless applications. Here are few [Fullstack serverless example projects](https://aws.amazon.com/amplify/console/getting-started/) to start with.
279 |
280 |
281 | ## Recap
282 |
283 | In a few minutes you were able to create a Sentiment Analysis application from scratch with:
284 |
285 | A scalable serverless back-end:
286 |
287 | Amazon Cognito - Fully managed authN/authZ service
288 | Amazon Transcribe - Functionality to convert speech-to-text
289 | Amazon Comprehend - Natural language processing (NLP) service that uses machine learning to find insights and relationships in text.
290 |
291 | A browser-based React front-end:
292 |
293 | Use case-centric open source libraries that require minimal code to use for invoking the APIs and connect to backend resources.
294 |
295 | Overall, Developers can focus on the business logic and use AWS Amplify to provision and manage the backend services for your app.
296 |
297 | ## Clean Up
298 |
299 | Please follow these instructions to clean-up your account so that there will be no on-going charges for any services used.
300 |
301 | Run this command to remove resources that were created during the lab (Press Ctrl+C first to stop the running app):
302 |
303 | amplify delete
304 |
305 | Return to the Cloud9 console by clicking AWS Cloud9 in the top left, then click Go To Your Dashboard.
306 |
307 | Delete the amplify-sentimentAnalysis-lab.
308 |
309 | You will need to enter Delete to delete the environment.
310 |
311 | ## Further reading on AWS Amplify
312 |
313 | AWS Amplify framework: https://aws-amplify.github.io/
314 | AWS Amplify product page: https://aws.amazon.com/amplify/
315 | AWS Amplify GitHub: https://github.com/aws-amplify
316 | AWS Amplify Community: https://amplify.aws/community/posts
317 | AWS Amplify Lobby: https://gitter.im/AWS-Amplify/Lobby
318 |
319 | ## SurveySurvey:
320 | Please help us to provide your feedback [here](https://amazonmr.au1.qualtrics.com/jfe/form/SV_3a6rNirgLrWYRW6?Session=HOL02). Participants who complete the surveys from AWS Innovate Online Conference - Data Edition will receive a gift code for USD25 in AWS credits. AWS credits will be sent via email by 30 September, 2021.
321 |
--------------------------------------------------------------------------------