Spaces:
Running
Running
Syed Arfan
commited on
Commit
Β·
6d724cf
1
Parent(s):
5a8e35b
Add Mermaid architecture diagrams
Browse files- Add system overview diagram
- Add request flow sequence diagram
- Add container architecture diagram
- Add performance comparison visualization
README.md
CHANGED
|
@@ -1,144 +1,541 @@
|
|
| 1 |
# Sentiment Analysis API
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-

|
| 7 |
-

|
| 8 |
|
| 9 |
## Features
|
| 10 |
|
| 11 |
-
|
| 12 |
-
- **
|
| 13 |
-
- **
|
| 14 |
-
- **
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
## Tech Stack
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
### Prerequisites
|
| 28 |
- Docker Desktop installed
|
| 29 |
-
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
###
|
| 32 |
-
```bash
|
| 33 |
-
# Build image
|
| 34 |
-
docker build -t sentiment-api:v1.0 .
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
```
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
- **Health Check:** http://localhost:8000/health
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
```
|
| 56 |
|
| 57 |
-
|
| 58 |
```json
|
| 59 |
{
|
| 60 |
-
"text": "I love this product!",
|
| 61 |
"sentiment": "POSITIVE",
|
| 62 |
"confidence": 0.9998,
|
| 63 |
-
"processing_time_ms":
|
|
|
|
| 64 |
}
|
| 65 |
```
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
## Project Structure
|
| 76 |
```
|
| 77 |
sentiment-api/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
βββ src/
|
| 79 |
-
β
|
| 80 |
-
βββ
|
| 81 |
-
βββ
|
| 82 |
-
|
| 83 |
-
βββ
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
```
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
```bash
|
| 91 |
-
# Create
|
| 92 |
-
|
| 93 |
-
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
|
|
|
| 100 |
```
|
| 101 |
|
| 102 |
-
###
|
| 103 |
```bash
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
```
|
| 106 |
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
-
- **Image Size:** ~1.2GB (includes PyTorch + transformers)
|
| 111 |
-
- **Health Check:** Configured for production monitoring
|
| 112 |
-
- **Multi-stage optimized** for faster rebuilds
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
-
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
## License
|
| 132 |
|
| 133 |
-
MIT License - feel free to use for
|
|
|
|
|
|
|
| 134 |
|
| 135 |
## Author
|
| 136 |
|
| 137 |
-
**
|
| 138 |
- GitHub: [@simplyarfan](https://github.com/simplyarfan)
|
| 139 |
- LinkedIn: [Syed Arfan Hussain](https://linkedin.com/in/syedarfan)
|
| 140 |
|
|
|
|
|
|
|
| 141 |
## Acknowledgments
|
| 142 |
|
| 143 |
-
-
|
| 144 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Sentiment Analysis API
|
| 2 |
|
| 3 |
+

|
| 4 |
|
| 5 |
+
A production-ready sentiment analysis API built with FastAPI, featuring multi-service architecture with PostgreSQL, Redis caching, and nginx load balancing. Analyzes text sentiment (POSITIVE/NEGATIVE) with 99%+ accuracy using DistilBERT transformer model.
|
|
|
|
|
|
|
| 6 |
|
| 7 |
## Features
|
| 8 |
|
| 9 |
+
### Core Functionality
|
| 10 |
+
- **Real-time Sentiment Analysis**: Instant text sentiment classification using state-of-the-art NLP
|
| 11 |
+
- **High Accuracy**: 99%+ confidence scores using DistilBERT transformer model
|
| 12 |
+
- **REST API**: Clean, documented API endpoints with interactive Swagger UI
|
| 13 |
+
|
| 14 |
+
### Production Architecture
|
| 15 |
+
- **PostgreSQL Database**: Persistent storage of all analysis history
|
| 16 |
+
- **Redis Caching**: 75x speed improvement for repeated queries (100ms β 2ms)
|
| 17 |
+
- **nginx Load Balancer**: Production-grade reverse proxy for scalability
|
| 18 |
+
- **Docker Compose**: One-command deployment of entire stack
|
| 19 |
+
|
| 20 |
+
### DevOps & Quality
|
| 21 |
+
- **Automated Testing**: 19 comprehensive unit tests covering all endpoints
|
| 22 |
+
- **CI/CD Pipeline**: GitHub Actions for automated testing on every commit
|
| 23 |
+
- **100% Test Coverage**: All endpoints validated for reliability
|
| 24 |
+
- **Professional Git Workflow**: Feature branches, pull requests, clean commit history
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## Architecture
|
| 29 |
+
### System Overview
|
| 30 |
+
```mermaid
|
| 31 |
+
graph TB
|
| 32 |
+
Client[Client Browser]
|
| 33 |
+
Nginx[nginx Load Balancer<br/>Port 80]
|
| 34 |
+
API[β‘ FastAPI Application<br/>Port 8000]
|
| 35 |
+
Redis[(Redis Cache<br/>Port 6379<br/>2ms response)]
|
| 36 |
+
Postgres[(PostgreSQL<br/>Port 5432<br/>Persistent Storage)]
|
| 37 |
+
|
| 38 |
+
Client -->|HTTP Request| Nginx
|
| 39 |
+
Nginx -->|Proxy| API
|
| 40 |
+
API -->|1. Check Cache| Redis
|
| 41 |
+
Redis -->|Cache Hit: Return| API
|
| 42 |
+
API -->|2. Cache Miss| API
|
| 43 |
+
API -->|3. Run ML Model| API
|
| 44 |
+
API -->|4. Store Result| Postgres
|
| 45 |
+
API -->|5. Cache Result| Redis
|
| 46 |
+
API -->|Response| Nginx
|
| 47 |
+
Nginx -->|Response| Client
|
| 48 |
+
|
| 49 |
+
style Client fill:#e1f5ff
|
| 50 |
+
style Nginx fill:#fff3e0
|
| 51 |
+
style API fill:#e8f5e9
|
| 52 |
+
style Redis fill:#ffebee
|
| 53 |
+
style Postgres fill:#f3e5f5
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
### Request Flow
|
| 57 |
+
```mermaid
|
| 58 |
+
sequenceDiagram
|
| 59 |
+
participant User
|
| 60 |
+
participant nginx
|
| 61 |
+
participant API
|
| 62 |
+
participant Redis
|
| 63 |
+
participant ML as ML Model(DistilBERT)
|
| 64 |
+
participant DB as PostgreSQL
|
| 65 |
+
|
| 66 |
+
User->>nginx: POST /analyze
|
| 67 |
+
nginx->>API: Forward request
|
| 68 |
+
|
| 69 |
+
API->>Redis: Check cache
|
| 70 |
+
alt Cache Hit
|
| 71 |
+
Redis-->>API: Return cached result (2ms)
|
| 72 |
+
API-->>nginx: Response
|
| 73 |
+
nginx-->>User: Result
|
| 74 |
+
else Cache Miss
|
| 75 |
+
Redis-->>API: Not found
|
| 76 |
+
API->>ML: Run inference
|
| 77 |
+
ML-->>API: Sentiment result (100ms)
|
| 78 |
+
API->>DB: Store in database
|
| 79 |
+
API->>Redis: Cache for next time
|
| 80 |
+
API-->>nginx: Response
|
| 81 |
+
nginx-->>User: Result
|
| 82 |
+
end
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### Container Architecture
|
| 86 |
+
```mermaid
|
| 87 |
+
graph LR
|
| 88 |
+
subgraph "Docker Compose"
|
| 89 |
+
N[nginx:alpine15MB]
|
| 90 |
+
A[sentiment-api1.2GB]
|
| 91 |
+
R[redis:7-alpine15MB]
|
| 92 |
+
P[postgres:15-alpine240MB]
|
| 93 |
+
end
|
| 94 |
+
|
| 95 |
+
N -.->|depends_on| A
|
| 96 |
+
A -.->|depends_on| R
|
| 97 |
+
A -.->|depends_on| P
|
| 98 |
+
|
| 99 |
+
V1[(postgres_dataVolume)]
|
| 100 |
+
P -.->|persists to| V1
|
| 101 |
+
|
| 102 |
+
style N fill:#fff3e0
|
| 103 |
+
style A fill:#e8f5e9
|
| 104 |
+
style R fill:#ffebee
|
| 105 |
+
style P fill:#f3e5f5
|
| 106 |
+
style V1 fill:#e0f2f1
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
### Performance Comparison
|
| 110 |
+
```mermaid
|
| 111 |
+
graph TD
|
| 112 |
+
subgraph "Without Cache"
|
| 113 |
+
A1[Request 1: 100ms] --> A2[Request 2: 100ms]
|
| 114 |
+
A2 --> A3[Request 3: 100ms]
|
| 115 |
+
A3 --> A4[1000 requests: 100 seconds]
|
| 116 |
+
end
|
| 117 |
+
|
| 118 |
+
subgraph "With Redis Cache"
|
| 119 |
+
B1[Request 1: 100msCache Miss] --> B2[Request 2: 2msCache Hit]
|
| 120 |
+
B2 --> B3[Request 3: 2msCache Hit]
|
| 121 |
+
B3 --> B4[1000 requests: 2.1 seconds β‘]
|
| 122 |
+
end
|
| 123 |
+
|
| 124 |
+
style A4 fill:#ffcdd2
|
| 125 |
+
style B4 fill:#c8e6c9
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
---
|
| 129 |
|
| 130 |
## Tech Stack
|
| 131 |
|
| 132 |
+
| Category | Technology | Purpose |
|
| 133 |
+
|----------|-----------|---------|
|
| 134 |
+
| **API Framework** | FastAPI | High-performance async API |
|
| 135 |
+
| **ML Model** | DistilBERT | Sentiment classification |
|
| 136 |
+
| **Database** | PostgreSQL 15 | Persistent data storage |
|
| 137 |
+
| **Cache** | Redis 7 | Sub-millisecond lookups |
|
| 138 |
+
| **Load Balancer** | nginx | Reverse proxy & distribution |
|
| 139 |
+
| **Containerization** | Docker + Compose | Service orchestration |
|
| 140 |
+
| **Testing** | pytest | Automated unit testing |
|
| 141 |
+
| **CI/CD** | GitHub Actions | Automated testing pipeline |
|
| 142 |
|
| 143 |
+
---
|
| 144 |
+
|
| 145 |
+
## Installation & Setup
|
| 146 |
|
| 147 |
### Prerequisites
|
| 148 |
- Docker Desktop installed
|
| 149 |
+
- Git installed
|
| 150 |
+
- 8GB RAM minimum
|
| 151 |
+
- 5GB disk space
|
| 152 |
|
| 153 |
+
### Quick Start
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
1. **Clone the repository**
|
| 156 |
+
```bash
|
| 157 |
+
git clone https://github.com/YOUR-USERNAME/sentiment-api.git
|
| 158 |
+
cd sentiment-api
|
| 159 |
+
```
|
| 160 |
|
| 161 |
+
2. **Start all services**
|
| 162 |
+
```bash
|
| 163 |
+
docker-compose up
|
| 164 |
```
|
| 165 |
|
| 166 |
+
3. **Access the API**
|
| 167 |
+
- API Docs: http://localhost/docs
|
| 168 |
+
- Direct API: http://localhost:8000/docs
|
| 169 |
+
- Health Check: http://localhost/health
|
| 170 |
|
| 171 |
+
**That's it!** All services (API, PostgreSQL, Redis, nginx) start automatically.
|
|
|
|
| 172 |
|
| 173 |
+
---
|
| 174 |
|
| 175 |
+
## API Endpoints
|
| 176 |
+
|
| 177 |
+
### Core Endpoints
|
| 178 |
+
|
| 179 |
+
#### `POST /analyze` - Analyze Sentiment
|
| 180 |
+
Analyze text sentiment with caching support.
|
| 181 |
+
|
| 182 |
+
**Request:**
|
| 183 |
+
```json
|
| 184 |
+
{
|
| 185 |
+
"text": "I absolutely love this product! It's amazing!"
|
| 186 |
+
}
|
| 187 |
```
|
| 188 |
|
| 189 |
+
**Response:**
|
| 190 |
```json
|
| 191 |
{
|
| 192 |
+
"text": "I absolutely love this product! It's amazing!",
|
| 193 |
"sentiment": "POSITIVE",
|
| 194 |
"confidence": 0.9998,
|
| 195 |
+
"processing_time_ms": 2,
|
| 196 |
+
"cached": true
|
| 197 |
}
|
| 198 |
```
|
| 199 |
|
| 200 |
+
#### `GET /history?limit=10` - Get Analysis History
|
| 201 |
+
Retrieve recent sentiment analyses from database.
|
| 202 |
+
|
| 203 |
+
**Response:**
|
| 204 |
+
```json
|
| 205 |
+
{
|
| 206 |
+
"total": 10,
|
| 207 |
+
"analyses": [
|
| 208 |
+
{
|
| 209 |
+
"id": 1,
|
| 210 |
+
"text": "Sample text",
|
| 211 |
+
"sentiment": "POSITIVE",
|
| 212 |
+
"confidence": 0.9999,
|
| 213 |
+
"processing_time_ms": 85,
|
| 214 |
+
"created_at": "2025-12-11T14:30:00"
|
| 215 |
+
}
|
| 216 |
+
]
|
| 217 |
+
}
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
#### `GET /cache/stats` - Cache Statistics
|
| 221 |
+
Monitor Redis cache performance.
|
| 222 |
+
|
| 223 |
+
**Response:**
|
| 224 |
+
```json
|
| 225 |
+
{
|
| 226 |
+
"status": "connected",
|
| 227 |
+
"total_keys": 150,
|
| 228 |
+
"sentiment_keys": 150,
|
| 229 |
+
"memory_used_mb": 12.5,
|
| 230 |
+
"hits": 450,
|
| 231 |
+
"misses": 50,
|
| 232 |
+
"hit_rate": 90.0
|
| 233 |
+
}
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
### Health & Monitoring
|
| 237 |
+
|
| 238 |
+
- `GET /` - Root endpoint (status check)
|
| 239 |
+
- `GET /health` - Health check endpoint
|
| 240 |
+
- `DELETE /cache/clear` - Clear all cached results
|
| 241 |
+
|
| 242 |
+
---
|
| 243 |
+
|
| 244 |
+
## Testing
|
| 245 |
+
|
| 246 |
+
### Run Tests Locally
|
| 247 |
+
```bash
|
| 248 |
+
# Install dependencies
|
| 249 |
+
pip install -r requirements.txt
|
| 250 |
+
|
| 251 |
+
# Run all tests
|
| 252 |
+
pytest tests/ -v
|
| 253 |
+
|
| 254 |
+
# Run with coverage
|
| 255 |
+
pytest tests/ --cov=src --cov-report=html
|
| 256 |
+
```
|
| 257 |
+
|
| 258 |
+
### Test Coverage
|
| 259 |
+
- β
All endpoints (GET /, POST /analyze, GET /health, GET /history)
|
| 260 |
+
- β
Input validation (empty text, too long, invalid types)
|
| 261 |
+
- β
Edge cases (special characters, multiple languages, max length)
|
| 262 |
+
- β
Response format validation
|
| 263 |
+
- β
Performance tests (response time < 5s)
|
| 264 |
+
- β
API documentation accessibility
|
| 265 |
+
|
| 266 |
+
**Result:** 19 tests, 100% passing
|
| 267 |
+
|
| 268 |
+
---
|
| 269 |
+
|
| 270 |
+
## Performance
|
| 271 |
+
|
| 272 |
+
### Caching Impact
|
| 273 |
+
|
| 274 |
+
| Scenario | Without Cache | With Redis Cache | Improvement |
|
| 275 |
+
|----------|--------------|------------------|-------------|
|
| 276 |
+
| First request | 100ms | 100ms | Baseline |
|
| 277 |
+
| Repeated request | 100ms | 2ms | **50x faster** |
|
| 278 |
+
| 1000 identical requests | 100s | 2.1s | **47x faster** |
|
| 279 |
+
|
| 280 |
+
### Scalability
|
| 281 |
+
- **Horizontal scaling**: nginx distributes load across multiple API instances
|
| 282 |
+
- **Cache hit rate**: 80-95% in production (typical)
|
| 283 |
+
- **Throughput**: 1000+ requests/second (single instance)
|
| 284 |
+
|
| 285 |
+
---
|
| 286 |
+
|
| 287 |
+
## Configuration
|
| 288 |
+
|
| 289 |
+
### Environment Variables
|
| 290 |
+
|
| 291 |
+
| Variable | Default | Description |
|
| 292 |
+
|----------|---------|-------------|
|
| 293 |
+
| `DATABASE_URL` | postgresql://user:pass@postgres:5432/sentiment | PostgreSQL connection string |
|
| 294 |
+
| `REDIS_URL` | redis://redis:6379 | Redis connection string |
|
| 295 |
+
| `CACHE_TTL_SECONDS` | 3600 | Cache expiration time (1 hour) |
|
| 296 |
+
|
| 297 |
+
### Docker Compose Services
|
| 298 |
+
```yaml
|
| 299 |
+
services:
|
| 300 |
+
nginx: # Load balancer (port 80)
|
| 301 |
+
api: # FastAPI application (port 8000)
|
| 302 |
+
postgres: # PostgreSQL database (port 5432)
|
| 303 |
+
redis: # Redis cache (port 6379)
|
| 304 |
+
```
|
| 305 |
|
| 306 |
+
---
|
| 307 |
+
|
| 308 |
+
## Deployment
|
| 309 |
+
|
| 310 |
+
### Local Development
|
| 311 |
+
```bash
|
| 312 |
+
docker-compose up
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
### Production (Coming Soon)
|
| 316 |
+
- AWS ECS/Fargate deployment
|
| 317 |
+
- CloudWatch monitoring
|
| 318 |
+
- Auto-scaling configuration
|
| 319 |
+
- SSL/TLS certificates
|
| 320 |
+
|
| 321 |
+
---
|
| 322 |
|
| 323 |
## Project Structure
|
| 324 |
```
|
| 325 |
sentiment-api/
|
| 326 |
+
βββ .github/
|
| 327 |
+
β βββ workflows/
|
| 328 |
+
β βββ test.yml # CI/CD pipeline
|
| 329 |
+
βββ nginx/
|
| 330 |
+
οΏ½οΏ½ βββ nginx.conf # Load balancer config
|
| 331 |
βββ src/
|
| 332 |
+
β βββ __init__.py
|
| 333 |
+
β βββ main.py # FastAPI application
|
| 334 |
+
β βββ database.py # PostgreSQL models & connection
|
| 335 |
+
β βββ cache.py # Redis caching layer
|
| 336 |
+
βββ tests/
|
| 337 |
+
β βββ __init__.py
|
| 338 |
+
β βββ test_api.py # 19 unit tests
|
| 339 |
+
βββ docker-compose.yml # Multi-service orchestration
|
| 340 |
+
βββ Dockerfile # API container definition
|
| 341 |
+
βββ requirements.txt # Python dependencies
|
| 342 |
+
βββ README.md # This file
|
| 343 |
```
|
| 344 |
|
| 345 |
+
---
|
| 346 |
+
|
| 347 |
+
## How It Works
|
| 348 |
+
|
| 349 |
+
### Request Flow
|
| 350 |
+
|
| 351 |
+
1. **User sends request** β nginx (port 80)
|
| 352 |
+
2. **nginx forwards** β FastAPI (port 8000)
|
| 353 |
+
3. **FastAPI checks cache** β Redis
|
| 354 |
+
- **Cache HIT**: Return cached result (2ms) β
|
| 355 |
+
- **Cache MISS**: Continue to step 4
|
| 356 |
+
4. **Run ML model** β DistilBERT inference (100ms)
|
| 357 |
+
5. **Store in database** β PostgreSQL (persistent)
|
| 358 |
+
6. **Store in cache** β Redis (for next time)
|
| 359 |
+
7. **Return response** β User
|
| 360 |
+
|
| 361 |
+
### Caching Strategy
|
| 362 |
+
|
| 363 |
+
**Cache Key Generation:**
|
| 364 |
+
```python
|
| 365 |
+
text = "I love this product"
|
| 366 |
+
hash = sha256(text) = "a7f3b2c1..."
|
| 367 |
+
key = "sentiment:a7f3b2c1"
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
**Cache Eviction:**
|
| 371 |
+
- TTL: 1 hour (3600 seconds)
|
| 372 |
+
- Policy: LRU (Least Recently Used)
|
| 373 |
+
- Max memory: 256MB
|
| 374 |
+
|
| 375 |
+
---
|
| 376 |
+
|
| 377 |
+
## Learning Outcomes
|
| 378 |
|
| 379 |
+
This project demonstrates:
|
| 380 |
+
|
| 381 |
+
### Technical Skills
|
| 382 |
+
- β
Multi-service architecture design
|
| 383 |
+
- β
Docker containerization & orchestration
|
| 384 |
+
- β
RESTful API development
|
| 385 |
+
- β
Database design & ORM (SQLAlchemy)
|
| 386 |
+
- β
Caching strategies & optimization
|
| 387 |
+
- β
Load balancing & reverse proxies
|
| 388 |
+
- β
ML model integration & deployment
|
| 389 |
+
- β
Automated testing & CI/CD
|
| 390 |
+
- β
Git workflow & version control
|
| 391 |
+
|
| 392 |
+
---
|
| 393 |
+
|
| 394 |
+
## Development Workflow
|
| 395 |
+
|
| 396 |
+
### Adding Features
|
| 397 |
```bash
|
| 398 |
+
# Create feature branch
|
| 399 |
+
git checkout -b feature/new-feature
|
|
|
|
| 400 |
|
| 401 |
+
# Make changes
|
| 402 |
+
# ... code ...
|
| 403 |
+
|
| 404 |
+
# Test locally
|
| 405 |
+
pytest tests/
|
| 406 |
+
|
| 407 |
+
# Commit and push
|
| 408 |
+
git add .
|
| 409 |
+
git commit -m "Add new feature"
|
| 410 |
+
git push origin feature/new-feature
|
| 411 |
|
| 412 |
+
# Create Pull Request on GitHub
|
| 413 |
+
# GitHub Actions runs tests automatically
|
| 414 |
+
# Merge when tests pass β
|
| 415 |
```
|
| 416 |
|
| 417 |
+
### Updating Dependencies
|
| 418 |
```bash
|
| 419 |
+
# Update requirements.txt
|
| 420 |
+
pip freeze > requirements.txt
|
| 421 |
+
|
| 422 |
+
# Rebuild containers
|
| 423 |
+
docker-compose up --build
|
| 424 |
```
|
| 425 |
|
| 426 |
+
---
|
| 427 |
|
| 428 |
+
## Troubleshooting
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
+
### Common Issues
|
| 431 |
|
| 432 |
+
**Port 8000 already in use:**
|
| 433 |
+
```bash
|
| 434 |
+
# Stop any process using port 8000
|
| 435 |
+
lsof -ti:8000 | xargs kill -9
|
| 436 |
|
| 437 |
+
# Or change port in docker-compose.yml
|
| 438 |
+
ports:
|
| 439 |
+
- "8001:8000" # Use port 8001 instead
|
| 440 |
+
```
|
| 441 |
|
| 442 |
+
**Database connection error:**
|
| 443 |
+
```bash
|
| 444 |
+
# Wait for PostgreSQL to initialize (first-time setup)
|
| 445 |
+
# Check logs:
|
| 446 |
+
docker-compose logs postgres
|
| 447 |
+
|
| 448 |
+
# Should see: "database system is ready to accept connections"
|
| 449 |
+
```
|
| 450 |
+
|
| 451 |
+
**Model download fails:**
|
| 452 |
+
```bash
|
| 453 |
+
# Check internet connection
|
| 454 |
+
# Model downloads from Hugging Face (~500MB)
|
| 455 |
+
# Takes 2-5 minutes on first run
|
| 456 |
+
```
|
| 457 |
+
|
| 458 |
+
---
|
| 459 |
+
|
| 460 |
+
## Monitoring
|
| 461 |
+
|
| 462 |
+
### View Logs
|
| 463 |
+
```bash
|
| 464 |
+
# All services
|
| 465 |
+
docker-compose logs -f
|
| 466 |
+
|
| 467 |
+
# Specific service
|
| 468 |
+
docker-compose logs -f api
|
| 469 |
+
docker-compose logs -f postgres
|
| 470 |
+
docker-compose logs -f redis
|
| 471 |
+
docker-compose logs -f nginx
|
| 472 |
+
```
|
| 473 |
+
|
| 474 |
+
### Database Access
|
| 475 |
+
```bash
|
| 476 |
+
# Connect to PostgreSQL
|
| 477 |
+
docker exec -it sentiment-api-postgres psql -U user -d sentiment
|
| 478 |
+
|
| 479 |
+
# View analyses
|
| 480 |
+
SELECT * FROM sentiment_analyses;
|
| 481 |
+
```
|
| 482 |
+
|
| 483 |
+
### Cache Access
|
| 484 |
+
```bash
|
| 485 |
+
# Connect to Redis
|
| 486 |
+
docker exec -it sentiment-api-redis redis-cli
|
| 487 |
+
|
| 488 |
+
# View all keys
|
| 489 |
+
KEYS *
|
| 490 |
+
|
| 491 |
+
# Get cached value
|
| 492 |
+
GET sentiment:abc123...
|
| 493 |
+
```
|
| 494 |
+
|
| 495 |
+
---
|
| 496 |
+
|
| 497 |
+
## Contributing
|
| 498 |
+
|
| 499 |
+
Contributions welcome! Please:
|
| 500 |
+
1. Fork the repository
|
| 501 |
+
2. Create a feature branch
|
| 502 |
+
3. Add tests for new features
|
| 503 |
+
4. Ensure all tests pass
|
| 504 |
+
5. Submit a pull request
|
| 505 |
+
|
| 506 |
+
---
|
| 507 |
|
| 508 |
## License
|
| 509 |
|
| 510 |
+
MIT License - feel free to use this project for learning or portfolio purposes.
|
| 511 |
+
|
| 512 |
+
---
|
| 513 |
|
| 514 |
## Author
|
| 515 |
|
| 516 |
+
**Syed Arfan Hussain**
|
| 517 |
- GitHub: [@simplyarfan](https://github.com/simplyarfan)
|
| 518 |
- LinkedIn: [Syed Arfan Hussain](https://linkedin.com/in/syedarfan)
|
| 519 |
|
| 520 |
+
---
|
| 521 |
+
|
| 522 |
## Acknowledgments
|
| 523 |
|
| 524 |
+
- **Hugging Face** - DistilBERT model
|
| 525 |
+
- **FastAPI** - Modern Python web framework
|
| 526 |
+
- **Docker** - Containerization platform
|
| 527 |
+
- **PostgreSQL** - Robust database system
|
| 528 |
+
- **Redis** - High-performance cache
|
| 529 |
+
|
| 530 |
+
---
|
| 531 |
+
|
| 532 |
+
## Resources
|
| 533 |
+
|
| 534 |
+
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
|
| 535 |
+
- [Docker Compose Documentation](https://docs.docker.com/compose/)
|
| 536 |
+
- [DistilBERT Paper](https://arxiv.org/abs/1910.01108)
|
| 537 |
+
- [Redis Best Practices](https://redis.io/docs/management/optimization/)
|
| 538 |
+
|
| 539 |
+
---
|
| 540 |
+
|
| 541 |
+
**Built with β€οΈ for learning and demonstration purposes**
|