File size: 8,847 Bytes
c09e844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# πŸ“‹ Project Summary - Vietnamese Product Rating Prediction System

## βœ… What Has Been Built

### πŸ—οΈ Complete Project Structure
```

PredictRating/

β”œβ”€β”€ main.py                    # FastAPI application entry

β”œβ”€β”€ requirements.txt           # All dependencies

β”œβ”€β”€ README.md                  # Full documentation

β”œβ”€β”€ QUICKSTART.md             # Quick setup guide

β”œβ”€β”€ sample_comments.csv       # Test data

β”œβ”€β”€ .gitignore                # Git ignore rules

β”‚

└── app/

    β”œβ”€β”€ config.py             # Configuration settings

    β”œβ”€β”€ database.py           # Database connection

    β”œβ”€β”€ models.py             # SQLAlchemy models (User, PredictionHistory)

    β”œβ”€β”€ schemas.py            # Pydantic validation schemas

    β”‚

    β”œβ”€β”€ routers/              # API endpoints

    β”‚   β”œβ”€β”€ auth.py           # Login/Register endpoints

    β”‚   β”œβ”€β”€ prediction.py     # Single/Batch prediction

    β”‚   └── dashboard.py      # Frontend routes

    β”‚

    β”œβ”€β”€ services/             # Business logic

    β”‚   β”œβ”€β”€ auth_service.py   # JWT authentication & password hashing

    β”‚   β”œβ”€β”€ ml_service.py     # ML prediction (DUMMY - replace with your model)

    β”‚   └── visualization_service.py  # WordCloud & chart data

    β”‚

    β”œβ”€β”€ templates/            # Jinja2 HTML templates

    β”‚   β”œβ”€β”€ base.html         # Base layout with TailwindCSS

    β”‚   β”œβ”€β”€ login.html        # Login page

    β”‚   β”œβ”€β”€ register.html     # Registration page

    β”‚   └── dashboard.html    # Main prediction interface

    β”‚

    β”œβ”€β”€ static/               # Static files

    β”‚   β”œβ”€β”€ css/

    β”‚   β”œβ”€β”€ js/

    β”‚   └── uploads/

    β”‚       └── wordclouds/   # Generated word cloud images

    β”‚

    └── database/             # SQLite database location

```

---

## 🎯 Features Implemented

### 1. Authentication System βœ…
- **User Registration** with email validation
- **JWT-based Login** (secure token authentication)
- **Password Hashing** using bcrypt
- **Protected Routes** requiring authentication

### 2. Single Comment Prediction βœ…
- Select target product
- Input Vietnamese comment
- Get predicted rating (1-5 stars)
- Display confidence score
- Save to prediction history

### 3. Batch CSV Prediction βœ…
- Upload CSV file with comments
- Bulk prediction processing
- **Visualizations:**
  - Bar chart showing rating distributionStart command
  - Word cloud of frequent words
  - Results table with all predictions
- **Export:** Download CSV with predicted ratings

### 4. Data Visualization βœ…
- **Chart.js** for interactive bar charts
- **WordCloud** library for generating word cloud images
- Responsive charts that update dynamically

### 5. API Documentation βœ…
- **Swagger UI** at `/docs` (automatic generation)
- **ReDoc** at `/redoc` (alternative documentation)
- Interactive API testing interface
- Complete request/response schemas

### 6. Database Integration βœ…
- **SQLite** database
- **User table** (username, email, hashed password)
- **PredictionHistory table** (tracks all predictions)
- Automatic table creation on startup

### 7. Frontend UI βœ…
- **TailwindCSS** for modern, responsive design
- **Jinja2** server-side rendering
- Tab-based interface (Single/Batch)
- Real-time form validation
- Loading states and error handling

---

## πŸš€ How to Run

### Step 1: Install Dependencies
```bash

pip install -r requirements.txt

```

### Step 2: Start Server
```bash

python main.py

```

### Step 3: Access Application
- **Dashboard:** http://localhost:8000/dashboard
- **Swagger API Docs:** http://localhost:8000/docs ⭐

---

## πŸ“Š API Endpoints

### Authentication
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/auth/register` | Register new user |
| POST | `/api/auth/login` | Login (returns JWT token) |
| GET | `/api/auth/me` | Get current user info |

### Predictions
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/predict/single` | Predict single comment |
| POST | `/api/predict/batch` | Predict batch from CSV |
| GET | `/api/predict/history` | Get prediction history |

### Frontend
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/login` | Login page |
| GET | `/register` | Registration page |
| GET | `/dashboard` | Main dashboard |

---

## πŸ”§ Replace Dummy ML Model

The file `app/services/ml_service.py` contains a **DUMMY prediction function** that returns random ratings.

### To integrate your real model:

1. **Load your model in `__init__`:**

```python

def __init__(self):

    self.model = load_model('path/to/your/model.h5')

    self.tokenizer = load_tokenizer('path/to/tokenizer.pkl')

```



2. **Update `predict_single` method:**
```python

def predict_single(self, text: str) -> Dict[str, any]:

    # Preprocess Vietnamese text

    preprocessed = self.preprocess(text)

    

    # Tokenize

    tokens = self.tokenizer.encode(preprocessed)

    

    # Predict

    prediction = self.model.predict([tokens])

    rating = int(prediction.argmax()) + 1  # 1-5 scale

    confidence = float(prediction.max())

    

    return {

        'rating': rating,

        'confidence': confidence

    }

```

3. **Implement preprocessing:**
```python

def preprocess(self, text: str) -> str:

    # Your Vietnamese text preprocessing

    text = text.lower()

    text = remove_special_characters(text)

    text = normalize_vietnamese(text)

    return text

```

---

## πŸŽ“ Demo for Teacher

### Show Swagger UI (Bonus Points!)
1. Open http://localhost:8000/docs
2. Demonstrate:
   - All API endpoints organized by tags
   - Request/response schemas
   - "Try it out" functionality
   - Authentication with JWT Bearer token

### User Flow Demo
1. **Register** a new account
2. **Login** and show JWT token storage
3. **Single Prediction:**
   - Select product
   - Enter Vietnamese comment
   - Show predicted rating + confidence
4. **Batch Prediction:**
   - Upload `sample_comments.csv`
   - Show bar chart of rating distribution
   - Show word cloud visualization
   - Download CSV with predictions

### Technical Highlights
- βœ… FastAPI automatic Swagger generation
- βœ… JWT authentication security
- βœ… RESTful API design
- βœ… Separation of concerns (routers, services, models)
- βœ… Database relationships (User ↔ PredictionHistory)
- βœ… Responsive frontend with TailwindCSS
- βœ… Data visualization with Chart.js + WordCloud

---

## πŸ“¦ Dependencies Installed

```

fastapi              # Web framework

uvicorn              # ASGI server

sqlalchemy           # ORM for database

python-jose          # JWT tokens

passlib              # Password hashing

pydantic             # Data validation

jinja2               # Template engine

wordcloud            # Word cloud generation

matplotlib           # Image rendering

python-multipart     # File uploads

```

---

## 🎯 What You Need to Do Next

1. **Test the application:**
   - Register an account
   - Try single prediction
   - Upload the `sample_comments.csv` file
   - Test batch prediction

2. **Replace the dummy ML model:**
   - Edit `app/services/ml_service.py`
   - Load your fine-tuned model
   - Implement proper preprocessing
   - Update prediction logic

3. **Customize (optional):**
   - Add more products in `app/config.py`
   - Adjust styling in templates
   - Add more Vietnamese stopwords in visualization service

4. **Prepare for demo:**
   - Practice showing Swagger UI
   - Prepare sample comments in Vietnamese
   - Explain the architecture and tech stack

---

## πŸ“ž Quick Reference

| What | Where |
|------|-------|
| Start server | `python main.py` |
| Swagger UI | http://localhost:8000/docs |
| Dashboard | http://localhost:8000/dashboard |
| Replace model | `app/services/ml_service.py` |
| Add products | `app/config.py` β†’ PRODUCTS list |
| Database file | `app/database/rating_prediction.db` |
| Uploads folder | `app/static/uploads/` |
| Test CSV | `sample_comments.csv` |

---

## ✨ Success Criteria Met

βœ… FastAPI backend with Swagger UI  
βœ… Jinja2 templates + TailwindCSS  
βœ… SQLite database (Users + History)  
βœ… JWT authentication  
βœ… Single comment prediction  
βœ… Batch CSV prediction  
βœ… Data visualization (charts + word cloud)  
βœ… CSV export with predictions  
βœ… Professional project structure  
βœ… Complete documentation  

**Your ML prediction web app is ready! πŸŽ‰**

Good luck with your presentation! πŸŽ“