Skip to content

πŸ“ˆ A lightweight FastAPI project for sentiment analysis. It exposes a REST API to analyze text input and categorize sentiments like positive, negative, neutral, and more β€” with confidence scores. Built with scalability and modularity in mind, this project follows best practices and includes error logging, tests, and clean structure. πŸš€

nexustech101/sentiment-analysis-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sentiment Analysis API

Project Overview

This project is a FastAPI-based sentiment analysis API. It provides endpoints for analyzing the sentiment of text inputs and is structured for scalability, modularity, and performance. The API leverages pre-labeled sentiment data and includes efficient caching, error logging, and a well-organized directory structure.

Features

  • FastAPI framework for high-performance API development
  • Modular architecture for models, routes, and utilities
  • Sentiment analysis using pre-labeled data
  • Efficient caching with @lru_cache
  • Structured error handling and logging
  • Pydantic models for request validation and response consistency

Project Structure

C:.
β”‚   .gitignore
β”‚   error.log                   # Error logging file
β”‚   main.py                     # Entry point for the FastAPI application
β”‚   README.md                   # Project documentation
β”‚   requirements.txt            # Python dependencies
β”‚   run.sh                      # Shell script to run the API
β”‚   sentiment_labels.json       # Sentiment data
β”‚
β”œβ”€β”€β”€docs
β”‚       docs.md                 # Additional project documentation
β”‚
β”œβ”€β”€β”€models
β”‚       models.py               # Pydantic models for API data validation
β”‚       __init__.py             # Package initializer
β”‚
β”œβ”€β”€β”€routers
β”‚       sentiment_route.py      # API route for sentiment analysis
β”‚       __init__.py             # Package initializer
β”‚
β”œβ”€β”€β”€test
β”‚       output.json             # Sample test output
β”‚       sentiment.test.py       # Test cases for sentiment analysis
β”‚
└───utils
        sentiment_utils.py      # Utility functions for sentiment analysis
        __init__.py             # Package initializer

API Architectural Diagram

flowchart TB
    subgraph Client
        Browser
        API_Consumer
    end

    subgraph Backend
        API_Gateway -->|Rate Limiting & Logging| Route_Handler
        Route_Handler -->|Caching Layer| Redis_Cache
        Route_Handler -->|Session Management| Session_Service
        Route_Handler -->|Database Access| Database_Service
        Database_Service --> SQLite3_DB
    end

    subgraph AI_Pipelines
        Route_Handler -->|Pipeline Selection| Pipeline_Manager
        Pipeline_Manager --> Audio_Classification
        Pipeline_Manager --> Speech_Recognition
        Pipeline_Manager --> Depth_Estimation
        Pipeline_Manager --> Text_Classification
        Pipeline_Manager --> Translation
        Pipeline_Manager --> Image_Classification
        Pipeline_Manager --> Object_Detection
    end

    subgraph Cloud_Infrastructure
        EC2_Instance
        S3_Bucket
    end

    Client -->|API Requests| API_Gateway
    Backend -->|Deployed on| EC2_Instance
    SQLite3_DB -->|Backups| S3_Bucket
Loading

Installation

  1. Clone the repository:
git clone https://github.com/your-repo/sentiment-analysis-api.git
  1. Navigate to the project directory:
cd sentiment-analysis
  1. Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate  # For Linux/Mac
venv\Scripts\activate     # For Windows
  1. Install dependencies:
pip install -r requirements.txt

Running the API

For Windows:

run.cmd

For Linux/Mac:

bash run.sh

Or run directly with Python:

uvicorn main:app --reload

API will be accessible at: http://127.0.0.1:8000

API Endpoints

POST /sentiment

Analyzes the sentiment of the provided text.

Request:

{
  "prompts": [
    "I'm going to teach a lesson on python best practices."
  ]
}

Response example:

{
  "sentiment": "positive",
  "confidence": 0.98
}
or
[
  {
    "sequence": "The new AI model has been making waves in the tech industry.",
    "sentiments": [
      { "label": "technology", "confidence": 0.831 },
      { "label": "surprise", "confidence": 0.048 },
      { "label": "statement", "confidence": 0.034 },
      { "label": "question", "confidence": 0.029 },
      { "label": "positive", "confidence": 0.014 },
      { "label": "command", "confidence": 0.011 },
      { "label": "business", "confidence": 0.009 },
      { "label": "joy", "confidence": 0.007 },
      { "label": "neutral", "confidence": 0.004 },
      { "label": "negative", "confidence": 0.003 },
      { "label": "fear", "confidence": 0.002 },
      { "label": "sports", "confidence": 0.002 },
      { "label": "anger", "confidence": 0.002 },
      { "label": "education", "confidence": 0.002 },
      { "label": "politics", "confidence": 0.001 },
      { "label": "sadness", "confidence": 0.001 }
    ]
  },
]

Testing

Run tests with:

pytest test/sentiment.test.py

Logging

Errors are logged to error.log.

Contributing

  1. Fork the repo.
  2. Create a new branch.
  3. Commit your changes.
  4. Submit a pull request.

License

This project is licensed under the MIT License.

About

πŸ“ˆ A lightweight FastAPI project for sentiment analysis. It exposes a REST API to analyze text input and categorize sentiments like positive, negative, neutral, and more β€” with confidence scores. Built with scalability and modularity in mind, this project follows best practices and includes error logging, tests, and clean structure. πŸš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages