Skip to content

Flask app exposing a REST endpoint to determine sentiments of text inputs.

License

Notifications You must be signed in to change notification settings

apexDev37/sentiment-api

 
 

Repository files navigation

Sentiment Analysis API

Flask API exposing an endpoint for determining the sentiment of text.

How to run locally. (Terminal)

The following commands require execution in the project root.

  1. Create a virtual environment.
python3 -m venv .venv
  1. Activate the virtual environment.
. .venv/bin/activate

The following commands assume you have an active virtual environment.

  1. Install dependencies. (Using the Makefile)
make install

⚠ The above make target will install the latest, unpinned requirements from the file: requirements.in. If encounter any dependency-related issue during installation or runtime, fall back to use tested, pinned, and stable requirements defined in the file: requirements.txt

  • [Optional] Fall back to stable requirements.
pip install -r requirements.txt
  1. Run the application.
python app.py
  1. Query the api.

Query: I don't like this game

curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"input": "I don't like this game"}' \
"http://127.0.0.1:10010/api/v1/sentiment"

Output:

{
  "prediction": {
    "confidence": 0.9779077172279358,
    "label": "Negative"
  }
}

How to run locally. (Docker)

  1. Prerequisite: Make sure you have docker installed. Link

  2. Build the docker image. This uses the Docker file in the repository's root.

docker build --build-arg VERSION=SENTIMENT-v1 -t sentiment-api:dev .
  1. Run a container using the image as its base.
docker run --rm -p 10010:10010 --name sentiment-api sentiment-api:dev
  1. Querying works like the previous one. You can also use Postman.

  2. More Examples:

Query: I like this game

curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"input": "I like this game"}' \
"http://127.0.0.1:10010/api/v1/sentiment"
{
  "prediction": {
    "confidence": 0.9681828022003174,
    "label": "Positive"
  }
}

Query: I have no opinion

curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"input": "I have no opinion"}' \
"http://127.0.0.1:10010/api/v1/sentiment"
{
  "prediction": {
    "confidence": 0.49172118306159973,
    "label": "Neutral"
  }
}

How to run on cloud. (AWS)

  1. Code available in branches deploy for AWS Lightsail and ebs for AWS Elasticbeanstalk.
  2. Documentations are available via this link, Pages 8 & 9.

Built with ❤️ by martinoywa.

Releases

No releases published

Packages

No packages published

Languages

  • Dockerfile 50.6%
  • Python 38.9%
  • Makefile 10.5%