A robust sentiment analysis API powered by state-of-the-art transformer models. This API can analyze text sentiment with high accuracy, handling both short and long texts through intelligent chunking.
- π Fast and Accurate: Powered by pre-trained transformer models
- π Long Text Support: Automatically chunks and analyzes texts longer than model capacity
- π― Detailed Analysis: Returns confidence scores, sentiment distribution, and chunk-level analysis
- π§ RESTful API: Easy-to-use endpoints with comprehensive documentation
- π Token Counting: Check text length before analysis
- π³ Docker Support: Ready-to-deploy Docker configuration
- β Well-Tested: Comprehensive test suite with unit and integration tests
sentiment-analysis/
βββ sentiment_analysis/ # Main package
β βββ __init__.py
β βββ app.py # FastAPI application setup
β β βββ __init__.py
β β βββ routes.py # Route definitions
β βββ core/ # Core business logic
β β βββ __init__.py
β β βββ analyzer.py # Sentiment analysis logic
β βββ models/ # Data models
β β βββ __init__.py
β β βββ schemas.py # Pydantic schemas
β βββ tests/ # Test suite
β βββ __init__.py
β βββ test_analyzer.py # Unit tests
β βββ test_integration.py # Integration tests
βββ .github/ # GitHub Actions workflows
β βββ workflows/
β βββ ci.yml
βββ main.py # Application entry point
βββ requirements.txt # Production dependencies
βββ requirements-dev.txt # Development dependencies
βββ setup.py # Package setup
βββ Dockerfile # Docker configuration
βββ LICENSE # MIT License
βββ CONTRIBUTING.md # Contribution guidelines
βββ README.md # This file
# Clone the repository
git clone https://github.com/yourusername/sentiment-analysis.git
cd sentiment-analysis
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install the package
pip install -e .
# For development
pip install -e ".[dev]"# Build the image
docker build -t sentiment-analysis .
# Run the container
docker run -p 8000:8000 sentiment-analysis-
Start the API server:
python main.py
-
Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
-
Make your first request:
curl -X POST "http://localhost:8000/api/v1/analyze" \ -H "Content-Type: application/json" \ -d '{"text": "I love this API! It works great."}'
Endpoint: POST /api/v1/analyze
Analyzes the sentiment of provided text.
Request:
{
"text": "Your text to analyze"
}Response:
{
"label": "POSITIVE",
"score": 0.9876,
"raw_scores": [0.001, 0.002, 0.009, 0.9876, 0.0004],
"numerical_sentiment": 1.0,
"sentiment_distribution": {"POSITIVE": 100.0},
"confidence_level": "high",
"num_chunks": 1,
"chunk_votes": {"POSITIVE": 1}
}Endpoint: POST /api/v1/count-tokens
Counts tokens in the provided text.
Request:
{
"text": "Your text to count tokens"
}Response:
{
"token_count": 8,
"max_tokens": 512,
"percentage": 1.56,
"truncated": false
}Endpoint: GET /api/v1/model-info/capacity
Returns the maximum token capacity of the model.
Response:
{
"max_tokens": 512
}Endpoint: GET /api/v1/health
Check service health status.
Response:
{
"status": "healthy",
"service": "sentiment-analysis-api"
}The API automatically handles long texts that exceed the model's token limit (512 tokens) by:
- Chunking: Dividing text into overlapping segments
- Individual Analysis: Analyzing each chunk separately
- Aggregation: Combining results using:
- Voting: Most common sentiment wins
- Weighted Scoring: Confidence-weighted averaging
- Distribution Analysis: Percentage breakdown by sentiment
- Confidence Assessment: Based on agreement between chunks
# Run all tests
pytest
# Run with coverage
pytest --cov=sentiment_analysis --cov-report=html
# Run specific test file
pytest sentiment_analysis/tests/test_analyzer.py -v# Format code
black sentiment_analysis/
# Lint code
flake8 sentiment_analysis/
# Type checking
mypy sentiment_analysis/# Install pre-commit
pip install pre-commit
# Set up hooks
pre-commit installThe API uses the following model by default:
- Model:
tabularisai/robust-sentiment-analysis - Max tokens: 512
- Classes: 5 (Very Negative, Negative, Neutral, Positive, Very Positive)
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with FastAPI
- Powered by Hugging Face Transformers
- Model: tabularisai/robust-sentiment-analysis
- π« Email: support@example.com
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
Made with β€οΈ by the Sentiment Analysis API Team