Spaces:
Runtime error
Runtime error
File size: 14,009 Bytes
92db116 |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# ποΈ System Architecture
## High-Level Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FRONTEND β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Login/ β β Dashboard β β Register β β
β β Register β β (Jinja2) β β Page β β
β β (Jinja2) β β + TailwindCSSβ β (Jinja2) β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β β
β ββββββββββββββββββββ΄βββββββββββββββββββ β
β β β
β JavaScript (Fetch API) β
β + Chart.js for viz β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FASTAPI BACKEND β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β API ROUTERS β β
β β ββββββββββββ ββββββββββββ ββββββββββββ β β
β β β Auth β βPredictionβ βDashboard β β β
β β β Router β β Router β β Router β β β
β β β /api/authβ β/api/pred β β /pages β β β
β β ββββββββββββ ββββββββββββ ββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β SERVICES β β
β β ββββββββββββββββ ββββββββββββββββ β β
β β β Auth β β ML β β β
β β β Service β β Service β β β
β β β(JWT, bcrypt) β β (Model) β β β
β β ββββββββββββββββ ββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββ β β
β β β Visualization Service β β β
β β β (WordCloud, Charts) β β β
β β ββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β DATA LAYER β β
β β ββββββββββββ ββββββββββββ β β
β β β SQLAlchemyβ β Pydantic β β β
β β β Models β β Schemas β β β
β β β(ORM Layer)β β(Validation) β β
β β ββββββββββββ ββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DATABASE β
β ββββββββββββββββββββββββ ββββββββββββββββββββββββ β
β β Users Table β β PredictionHistory β β
β β - id (PK) β β - id (PK) β β
β β - username β β - user_id (FK) β β
β β - email β β - product_name β β
β β - hashed_password β β - comment β β
β β - created_at β β - predicted_rating β β
β β β β - confidence_score β β
β β β β - created_at β β
β ββββββββββββββββββββββββ ββββββββββββββββββββββββ β
β SQLite Database β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## Request Flow Examples
### 1οΈβ£ User Login Flow
```
User enters credentials
β
βΌ
[Login.html]
β
βΌ
POST /api/auth/login
β
βΌ
[Auth Router]
β
βΌ
[Auth Service] βββΊ Verify password (bcrypt)
β Generate JWT token
βΌ
[Database] βββΊ Query User table
β
βΌ
Return JWT token to frontend
β
βΌ
Store token in localStorage
β
βΌ
Redirect to /dashboard
```
### 2οΈβ£ Single Prediction Flow
```
User enters comment
β
βΌ
[Dashboard.html]
β
βΌ
POST /api/predict/single
(with JWT token in header)
β
βΌ
[Prediction Router]
β
βΌ
[Auth Service] βββΊ Verify JWT token
β
βΌ
[ML Service] βββΊ predict_single(comment)
β (DUMMY: return random rating)
βΌ
[Database] βββΊ Save to PredictionHistory
β
βΌ
Return {rating, confidence}
β
βΌ
Display result in UI
```
### 3οΈβ£ Batch CSV Prediction Flow
```
User uploads CSV file
β
βΌ
[Dashboard.html]
β
βΌ
POST /api/predict/batch
(multipart/form-data)
β
βΌ
[Prediction Router]
β
βΌ
Parse CSV βββΊ Extract comments
β
βΌ
[ML Service] βββΊ predict_batch(comments)
β For each comment:
β predict_single()
βΌ
[Visualization Service]
β
ββββΊ generate_wordcloud()
β Save PNG to /static/uploads/
β
ββββΊ calculate_rating_distribution()
Count 1β, 2β, 3β, 4β, 5β
β
βΌ
[Database] βββΊ Save all predictions
β
βΌ
Return:
- wordcloud_url
- rating_distribution
- results array
β
βΌ
[Dashboard.html]
β
ββββΊ Render Chart.js bar chart
ββββΊ Display word cloud image
ββββΊ Populate results table
ββββΊ Enable CSV download
```
---
## Technology Stack Details
### Backend
```
FastAPI (0.104.1)
βββ Auto-generates Swagger UI (/docs)
βββ Automatic data validation (Pydantic)
βββ Async support
βββ Built-in dependency injection
SQLAlchemy (2.0.23)
βββ ORM for database operations
βββ Models: User, PredictionHistory
βββ Automatic table creation
JWT Authentication
βββ python-jose for token generation
βββ passlib[bcrypt] for password hashing
βββ OAuth2PasswordBearer for token validation
```
### Frontend
```
Jinja2 Templates
βββ Server-side rendering
βββ Template inheritance (base.html)
βββ Context variables from backend
TailwindCSS (CDN)
βββ Utility-first CSS framework
βββ Responsive design
βββ Custom animations
Chart.js (CDN)
βββ Interactive bar charts
βββ Rating distribution visualization
JavaScript (Vanilla)
βββ Fetch API for HTTP requests
βββ LocalStorage for JWT token
βββ Dynamic DOM manipulation
```
### Visualization
```
WordCloud (1.9.3)
βββ Generate word cloud images
βββ Vietnamese stopwords support
βββ Save to PNG files
Matplotlib (3.8.2)
βββ Render word cloud to image
βββ Non-GUI backend (Agg)
```
---
## File Responsibilities
### Backend Files
| File | Purpose |
|------|---------|
| `main.py` | FastAPI app initialization, router inclusion |
| `config.py` | Configuration (SECRET_KEY, products list) |
| `database.py` | SQLAlchemy engine, session management |
| `models.py` | Database table definitions (User, PredictionHistory) |
| `schemas.py` | Pydantic models for request/response validation |
### Router Files
| File | Purpose |
|------|---------|
| `routers/auth.py` | Register, login, get current user |
| `routers/prediction.py` | Single/batch prediction, history |
| `routers/dashboard.py` | Serve HTML pages (login, register, dashboard) |
### Service Files
| File | Purpose |
|------|---------|
| `services/auth_service.py` | JWT generation, password hashing, token validation |
| `services/ml_service.py` | ML model wrapper, prediction logic (DUMMY) |
| `services/visualization_service.py` | WordCloud generation, chart data |
### Frontend Files
| File | Purpose |
|------|---------|
| `templates/base.html` | Base layout with navigation, CDN imports |
| `templates/login.html` | Login form with JWT handling |
| `templates/register.html` | Registration form |
| `templates/dashboard.html` | Main interface (product select, predictions, viz) |
---
## Security Features
1. **Password Hashing:** bcrypt with salt
2. **JWT Tokens:** Signed with SECRET_KEY (HS256)
3. **Token Expiration:** 24 hours
4. **Protected Routes:** Dependency injection (`get_current_user`)
5. **CORS:** Configured for security
6. **Input Validation:** Pydantic schemas
---
## Database Schema
```sql
-- Users Table
CREATE TABLE users (
id INTEGER PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- PredictionHistory Table
CREATE TABLE prediction_history (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
product_name VARCHAR(200) NOT NULL,
comment TEXT NOT NULL,
predicted_rating INTEGER NOT NULL,
confidence_score FLOAT,
prediction_type VARCHAR(20) DEFAULT 'single',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
```
---
## API Response Examples
### POST /api/auth/login
```json
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}
```
### POST /api/predict/single
```json
{
"predicted_rating": 5,
"confidence_score": 0.92,
"comment": "SαΊ£n phαΊ©m rαΊ₯t tα»t..."
}
```
### POST /api/predict/batch
```json
{
"total_predictions": 20,
"rating_distribution": {
"1": 2,
"2": 3,
"3": 5,
"4": 6,
"5": 4
},
"wordcloud_url": "/static/uploads/wordclouds/wordcloud_20241125_143022.png",
"results": [
{
"Comment": "SαΊ£n phαΊ©m tα»t",
"Predicted_Rating": 5,
"Confidence": 0.95
}
],
"csv_download_url": "/api/predict/download/1/1700924622.123"
}
```
---
## Deployment Checklist
Before production:
- [ ] Change `SECRET_KEY` in config.py
- [ ] Set `reload=False` in uvicorn
- [ ] Configure CORS properly
- [ ] Use PostgreSQL instead of SQLite
- [ ] Add environment variables (.env file)
- [ ] Set up HTTPS
- [ ] Add rate limiting
- [ ] Configure logging
- [ ] Add error monitoring
- [ ] Set up backup strategy
---
This architecture provides:
β
**Separation of Concerns**
β
**Scalability** (easy to add features)
β
**Maintainability** (clear file structure)
β
**Security** (JWT, password hashing)
β
**Documentation** (auto-generated Swagger)
β
**Testing** (clear API endpoints)
|