Basic APIs: Sentiment Analysis with Flask, Docker, and AWS
The repo for this project can be found here: https://github.com/jamesdeluk/data-science/tree/main/Projects/basic-apis/sentiment-analysis
The goal Send (POST) a word or phrase to an online API, and get a sentiment (positive or negative) and score as a response.
Step 1: Build the Python script First we need the script that can take a word or phrase as an input and return a sentiment.
import json from textblob import TextBlob def analyse(data): text = data.get('text', '') if not text: return json.dumps({'error': 'No text provided'}), 400 blob = TextBlob(text) sentiment_score = blob.sentiment.polarity return json.dumps({ 'input': text, 'sentiment_score': sentiment_score, 'sentiment': ( 'positive' if sentiment_score > 0 else 'negative' if sentiment_score < 0 else 'neutral' ) }) The API will be JSON-based, so this script is also. The function takes some text and uses TextBlob to give it a sentiment score between 1 (positive) and -1 (negative), which it returns, also in JSON form.