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)