Spaces:
Sleeping
Sleeping
File size: 3,569 Bytes
116e578 bf8eab1 116e578 bf8eab1 116e578 5fb7b67 116e578 bf8eab1 116e578 5fb7b67 116e578 bf8eab1 116e578 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 5fb7b67 bf8eab1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# Sentiment Analysis API
A production-ready REST API for analyzing text sentiment using transformer models, containerized with Docker.



## Features
- **Fast sentiment analysis** using DistilBERT transformer model
- **RESTful API** with automatic interactive documentation
- **Docker containerized** for consistent deployment
- **Health check endpoints** for production monitoring
- **Input validation** with Pydantic models
- **Sub-100ms inference** after model warm-up
## Tech Stack
- **Backend:** FastAPI (async Python web framework)
- **ML Model:** DistilBERT via HuggingFace Transformers
- **Containerization:** Docker
- **API Documentation:** Auto-generated with Swagger UI
## Quick Start
### Prerequisites
- Docker Desktop installed
- 4GB RAM minimum
### Run with Docker
```bash
# Build image
docker build -t sentiment-api:v1.0 .
# Run container
docker run -d -p 8000:8000 --name sentiment-api sentiment-api:v1.0
# View logs
docker logs sentiment-api
```
### Access API
- **Interactive Docs:** http://localhost:8000/docs
- **Health Check:** http://localhost:8000/health
## π API Usage
### Example Request
```bash
curl -X POST "http://localhost:8000/analyze" \
-H "Content-Type: application/json" \
-d '{"text": "I love this product!"}'
```
### Example Response
```json
{
"text": "I love this product!",
"sentiment": "POSITIVE",
"confidence": 0.9998,
"processing_time_ms": 45
}
```
## API Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/` | Health check with version info |
| POST | `/analyze` | Analyze text sentiment |
| GET | `/health` | Kubernetes-style health endpoint |
## Project Structure
```
sentiment-api/
βββ src/
β βββ main.py # FastAPI application
βββ tests/ # Unit tests
βββ Dockerfile # Container definition
βββ .dockerignore # Docker build exclusions
βββ requirements.txt # Python dependencies
βββ README.md
```
## Development
### Local Setup (without Docker)
```bash
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run locally
uvicorn src.main:app --reload
```
### Run Tests
```bash
pytest tests/ -v
```
## Docker Details
- **Base Image:** `python:3.11-slim`
- **Image Size:** ~1.2GB (includes PyTorch + transformers)
- **Health Check:** Configured for production monitoring
- **Multi-stage optimized** for faster rebuilds
## Performance
- **First request:** 30-60s (model download)
- **Subsequent requests:** < 100ms
- **Memory usage:** ~500MB (model in RAM)
- **Concurrent requests:** 10-20 (CPU-bound)
## Future Enhancements
- [ ] GPU support for faster inference
- [ ] Model caching layer (Redis)
- [ ] Rate limiting
- [ ] Authentication
- [ ] Batch inference endpoint
- [ ] Multiple model support
- [ ] Kubernetes deployment configs
## License
MIT License - feel free to use for your projects!
## Author
**Your Name**
- GitHub: [@simplyarfan](https://github.com/simplyarfan)
- LinkedIn: [Syed Arfan Hussain](https://linkedin.com/in/syedarfan)
## Acknowledgments
- [HuggingFace](https://huggingface.co/) for the DistilBERT model
- [FastAPI](https://fastapi.tiangolo.com/) for the excellent framework |