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.
- 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
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
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
- Clone the repository:
git clone https://github.com/your-repo/sentiment-analysis-api.git- Navigate to the project directory:
cd sentiment-analysis- Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # For Linux/Mac
venv\Scripts\activate # For Windows- Install dependencies:
pip install -r requirements.txtFor Windows:
run.cmdFor Linux/Mac:
bash run.shOr run directly with Python:
uvicorn main:app --reloadAPI will be accessible at: http://127.0.0.1:8000
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
}[
{
"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 }
]
},
]Run tests with:
pytest test/sentiment.test.pyErrors are logged to error.log.
- Fork the repo.
- Create a new branch.
- Commit your changes.
- Submit a pull request.
This project is licensed under the MIT License.