An open API service indexing awesome lists of open source software.

https://github.com/nexustech101/sentiment-analysis-api

πŸ“ˆ 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. πŸš€
https://github.com/nexustech101/sentiment-analysis-api

api data-pipeline fastapi machine-learning production sentiment-analysis sentiment-classification

Last synced: about 1 month ago
JSON representation

πŸ“ˆ 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. πŸš€

Awesome Lists containing this project

README

          

# 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

```mermaid
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
```

## Installation
1. Clone the repository:
```bash
git clone https://github.com/your-repo/sentiment-analysis-api.git
```

2. Navigate to the project directory:
```bash
cd sentiment-analysis
```

3. Create a virtual environment and activate it:
```bash
python -m venv venv
source venv/bin/activate # For Linux/Mac
venv\Scripts\activate # For Windows
```

4. Install dependencies:
```bash
pip install -r requirements.txt
```

## Running the API
For Windows:
```bash
run.cmd
```

For Linux/Mac:
```bash
bash run.sh
```

Or run directly with Python:
```bash
uvicorn main:app --reload
```

API will be accessible at: [http://127.0.0.1:8000](http://127.0.0.1:8000)

## API Endpoints
### `POST /sentiment`
Analyzes the sentiment of the provided text.

**Request:**

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

**Response example:**

```json
{
"sentiment": "positive",
"confidence": 0.98
}
```
##### or

```json
[
{
"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:
```bash
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.