top of page

Build a Sentiment Analysis app with AI and Python - cool starter project for Middle/High School

Updated: Apr 11, 2022


How to build a Sentiment Analysis app with Artificial Intelligence AI and Python, starter project for middle school and high school students

Want to get started building AI apps? At AIClub we have helped thousands of kids who have gone on to build many thousands of apps with AI, Python, and more. In this blog, we show you a simple app that can get you started with AI and Python - a sentiment detector.


What is Sentiment Analysis?


Sentiment Analysis is a type of AI that can detect how a user is feeling (their mood) from text, images, sounds or video. It is used by many businesses to assess customer sentiment. For example, if a customer sends an email, this type of AI can be used to detect whether the customer is angry. Based on the conclusion, the business can respond differently, for example via email to neutral customers, and via phone calls to angry ones.


What are we going to build?


We are going to build a python app that can send sentences to an AI and have the AI predict the mood. We will start by building an AI to learn patterns of mood from sentences, and then we will create a python program to connect with this AI.


To build the AI - you will need a (free) AIClub account. If you need to sign up - see the instructions in the video below. If you already have an account - go straight to Building the AI.




Building the AI

You can follow the instructions in the video below to build your AI.



Once you have your AI built, go ahead and try it out!


Things to try:

  • Try different sentences and record the ones that you think the AI predicted incorrectly

  • Try trick sentences like "I am so happy I am crying", or "I am crying with joy". Does the AI get confused by the word "crying"?

  • Try neutral sentences like "It is 4 pm in the afternoon". Does the AI know that this is neither happy nor sad? Can it give a third answer?


If you find that you do not like your AI's answers, you can improve it by retraining with more data and corrections. The video below shows how to retrain your AI.




Connecting your AI to a Python Program


Once you have the AI responding as you would like - you can access it directly from a python program. The code snippet below shows how to access the AI. The URL is the address of the AI on the internet. The video below shows how to find your own URL from your AI. Once you have that, you can replace the URL in the code snippet with your own and then the code will be accessing your AI.



import requests
import json

def get_prediction(data={"sentence":"I am sad."}):
  url = 'https://k3hn7n41xi.execute-api.us-east-1.amazonaws.com/Predict/d9a90693-debe-4911-8714-6fbe9a3936fa'
  r = requests.post(url, data=json.dumps(data))
  response = getattr(r,'_content').decode("utf-8")
 return response

test_data = {"sentence": "I am happy"}
prediction = json.loads(json.loads(get_prediction(test_data))['body'])['predicted_label']
print('prediction: ', prediction)

The video below shows how to get the URL for your AI



Once you have connected your AI to your code, you can modify your code to create whatever kind of sentiment analysis app you would like! You can analyze emails, twitter messages and more!


The code listed above is also at a Google Colab notebook https://colab.research.google.com/drive/1g8b7hMst4NTsOYBXGrhGvwtuLPwrVwO5?usp=sharing#scrollTo=A6K-k-166gCT


Enjoy..

365 views0 comments
bottom of page