Skip to content

maledadams/sentiment-analysis-fast-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sentiment-analysis-fast-api

A REST API for real-time sentiment classification built with FastAPI.

The API accepts text input and returns predicted sentiment labels and confidence scores returns positive, negative, or neutral with a confidence score.. The project demonstrates how machine learning models can be deployed as scalable APIs.

Architecture

Client Request -> FastAPI Endpoint -> Sentiment Model -> Prediction Output -> JSON Response

Stack

  • FastAPI for the REST API
  • Hugging Face transformers for inference
  • PyTorch as the model backend
  • Pydantic for request and response validation

Model

The API loads cardiffnlp/twitter-roberta-base-sentiment-latest at startup. That model supports three sentiment classes, which makes it a better fit than the default binary sentiment pipeline.

You can override the model with the SENTIMENT_MODEL_NAME environment variable.

Setup

  1. Move into the project folder:
cd sentiment-analysis-fast-api
  1. Install dependencies:
pip install -r requirements.txt
  1. Start the API server:
python -m uvicorn app.main:app --reload
  1. Open the API in your browser:
http://127.0.0.1:8000/
http://127.0.0.1:8000/health
http://127.0.0.1:8000/docs

The first startup can take longer because the model may need to be downloaded from Hugging Face.

How to use the API

Option 1: Use the Swagger UI

Open:

http://127.0.0.1:8000/docs

Then:

  1. Expand POST /predict
  2. Click Try it out
  3. Paste a request body like this:
{
  "text": "I love this movie so much"
}
  1. Click Execute

Option 2: Use PowerShell

Invoke-RestMethod `
  -Method Post `
  -Uri "http://127.0.0.1:8000/predict" `
  -ContentType "application/json" `
  -Body '{"text":"This project is amazing"}'

Option 3: Use curl

curl -X POST "http://127.0.0.1:8000/predict" \
  -H "Content-Type: application/json" \
  -d "{\"text\":\"This project is amazing\"}"

Tests

Run the API tests with:

python -m pytest -q

Expected result:

5 passed

Endpoints

GET /

Returns a basic service message.

Example:

{
  "message": "Sentiment Analysis API is running"
}

GET /health

Returns the service health and whether the model is loaded.

Example response:

{
  "status": "ok",
  "model_loaded": true,
  "model_name": "cardiffnlp/twitter-roberta-base-sentiment-latest"
}

POST /predict

Request body:

{
  "text": "This movie was surprisingly good"
}

Example response:

{
  "text": "This movie was surprisingly good",
  "sentiment": "positive",
  "confidence": 0.997
}

Another example:

Request:

{
  "text": "I hate this class"
}

Possible response:

{
  "text": "I hate this class",
  "sentiment": "negative",
  "confidence": 0.98
}

Notes

  • The model is loaded once during application startup and reused for all requests.
  • The first startup may take longer because the model has to be downloaded from Hugging Face.

About

A machine learning project powered by FastAPI that analyzes and classifies text sentiment. Uses a trained ML model to predict positive, negative, or neutral sentiment with confidence scores.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages