Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,253 +1,12 @@
|
|
| 1 |
-
# Vietnamese Product Rating Prediction System
|
| 2 |
-
|
| 3 |
-
## π― Project Overview
|
| 4 |
-
A full-stack web application that predicts sentiment ratings (1-5 stars) for Vietnamese product reviews using Machine Learning.
|
| 5 |
-
|
| 6 |
-
**Built for:** Introduction to Machine Learning - University Project
|
| 7 |
-
**Tech Stack:** FastAPI + Jinja2 + TailwindCSS + SQLite + Chart.js
|
| 8 |
-
|
| 9 |
---
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
β βββ routers/ # API route handlers
|
| 18 |
-
β β βββ auth.py # Authentication endpoints
|
| 19 |
-
β β βββ prediction.py # Prediction endpoints
|
| 20 |
-
β β βββ dashboard.py # Frontend routes
|
| 21 |
-
β βββ services/ # Business logic
|
| 22 |
-
β β βββ auth_service.py # JWT & password handling
|
| 23 |
-
β β βββ ml_service.py # ML prediction (DUMMY - replace with your model)
|
| 24 |
-
β β βββ visualization_service.py # WordCloud & charts
|
| 25 |
-
β βββ static/ # Static files (CSS, JS, uploads)
|
| 26 |
-
β β βββ uploads/
|
| 27 |
-
β β βββ wordclouds/ # Generated word cloud images
|
| 28 |
-
β βββ templates/ # Jinja2 HTML templates
|
| 29 |
-
β β βββ base.html # Base layout
|
| 30 |
-
β β βββ login.html # Login page
|
| 31 |
-
β β βββ register.html # Registration page
|
| 32 |
-
β β βββ dashboard.html # Main prediction interface
|
| 33 |
-
β βββ config.py # Configuration settings
|
| 34 |
-
β βββ database.py # Database connection
|
| 35 |
-
β βββ models.py # SQLAlchemy models (User, PredictionHistory)
|
| 36 |
-
β βββ schemas.py # Pydantic validation schemas
|
| 37 |
-
βββ main.py # FastAPI application entry point
|
| 38 |
-
βββ requirements.txt # Python dependencies
|
| 39 |
-
```
|
| 40 |
-
|
| 41 |
-
---
|
| 42 |
-
|
| 43 |
-
## π Setup Instructions
|
| 44 |
-
|
| 45 |
-
### 1. Install Dependencies
|
| 46 |
-
|
| 47 |
-
```bash
|
| 48 |
-
pip install -r requirements.txt
|
| 49 |
-
```
|
| 50 |
-
|
| 51 |
-
### 2. Run the Application
|
| 52 |
-
|
| 53 |
-
```bash
|
| 54 |
-
python main.py
|
| 55 |
-
```
|
| 56 |
-
|
| 57 |
-
The server will start at: **http://localhost:8000**
|
| 58 |
-
|
| 59 |
-
### 3. Access the Application
|
| 60 |
-
|
| 61 |
-
- **Frontend Dashboard:** http://localhost:8000/dashboard
|
| 62 |
-
- **API Documentation (Swagger UI):** http://localhost:8000/docs β **SHOW THIS TO YOUR TEACHER**
|
| 63 |
-
- **Alternative API Docs (ReDoc):** http://localhost:8000/redoc
|
| 64 |
-
|
| 65 |
---
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
FastAPI automatically generates **interactive API documentation** at `/docs`.
|
| 70 |
-
|
| 71 |
-
### How to Access:
|
| 72 |
-
1. Run the application
|
| 73 |
-
2. Open browser: **http://localhost:8000/docs**
|
| 74 |
-
3. You'll see all API endpoints with:
|
| 75 |
-
- Request/response schemas
|
| 76 |
-
- Try it out functionality
|
| 77 |
-
- Authentication support
|
| 78 |
-
|
| 79 |
-
### Key API Endpoints:
|
| 80 |
-
|
| 81 |
-
#### Authentication
|
| 82 |
-
- `POST /api/auth/register` - Register new user
|
| 83 |
-
- `POST /api/auth/login` - Login (get JWT token)
|
| 84 |
-
- `GET /api/auth/me` - Get current user info
|
| 85 |
-
|
| 86 |
-
#### Predictions
|
| 87 |
-
- `POST /api/predict/single` - Predict single comment
|
| 88 |
-
- `POST /api/predict/batch` - Predict batch from CSV
|
| 89 |
-
- `GET /api/predict/history` - Get prediction history
|
| 90 |
-
|
| 91 |
-
---
|
| 92 |
-
|
| 93 |
-
## π How to Use (User Journey)
|
| 94 |
-
|
| 95 |
-
### Step 1: Register/Login
|
| 96 |
-
1. Go to http://localhost:8000/login
|
| 97 |
-
2. Register a new account or login
|
| 98 |
-
3. You'll be redirected to the dashboard
|
| 99 |
-
|
| 100 |
-
### Step 2: Select Product
|
| 101 |
-
- Choose a target product from the dropdown list
|
| 102 |
-
|
| 103 |
-
### Step 3A: Single Comment Prediction
|
| 104 |
-
1. Click "Single Comment" tab
|
| 105 |
-
2. Enter a Vietnamese product review
|
| 106 |
-
3. Click "Predict Rating"
|
| 107 |
-
4. See the predicted rating (1-5 stars) with confidence score
|
| 108 |
-
|
| 109 |
-
### Step 3B: Batch CSV Prediction
|
| 110 |
-
1. Click "Upload CSV" tab
|
| 111 |
-
2. Upload a CSV file with a `Comment` column
|
| 112 |
-
3. Click "Predict Batch"
|
| 113 |
-
4. View results:
|
| 114 |
-
- **Bar Chart:** Rating distribution (how many 1β, 2β, etc.)
|
| 115 |
-
- **Word Cloud:** Most frequent words in comments
|
| 116 |
-
- **Table:** All predictions with confidence scores
|
| 117 |
-
- **Download:** Export results as CSV with `Predicted_Rating` column
|
| 118 |
-
|
| 119 |
-
---
|
| 120 |
-
|
| 121 |
-
## π§ Replace Dummy ML Model
|
| 122 |
-
|
| 123 |
-
The current `ml_service.py` uses a **DUMMY** prediction function. Replace it with your real model:
|
| 124 |
-
|
| 125 |
-
### File: `app/services/ml_service.py`
|
| 126 |
-
|
| 127 |
-
```python
|
| 128 |
-
class MLPredictionService:
|
| 129 |
-
def __init__(self):
|
| 130 |
-
# TODO: Load your trained model
|
| 131 |
-
self.model = load_model('path/to/your/model.h5') # Example
|
| 132 |
-
self.tokenizer = load_tokenizer('path/to/tokenizer.pkl')
|
| 133 |
-
|
| 134 |
-
def predict_single(self, text: str) -> Dict[str, any]:
|
| 135 |
-
# TODO: Implement your preprocessing
|
| 136 |
-
preprocessed = self.preprocess(text)
|
| 137 |
-
|
| 138 |
-
# TODO: Make prediction with your model
|
| 139 |
-
prediction = self.model.predict(preprocessed)
|
| 140 |
-
rating = self.postprocess(prediction) # Convert to 1-5
|
| 141 |
-
|
| 142 |
-
return {
|
| 143 |
-
'rating': rating,
|
| 144 |
-
'confidence': prediction.max()
|
| 145 |
-
}
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
---
|
| 149 |
-
|
| 150 |
-
## π Database Schema
|
| 151 |
-
|
| 152 |
-
### Users Table
|
| 153 |
-
- `id`: Primary key
|
| 154 |
-
- `username`: Unique username
|
| 155 |
-
- `email`: Unique email
|
| 156 |
-
- `hashed_password`: Bcrypt hashed password
|
| 157 |
-
- `created_at`: Registration timestamp
|
| 158 |
-
|
| 159 |
-
### Prediction History Table
|
| 160 |
-
- `id`: Primary key
|
| 161 |
-
- `user_id`: Foreign key to Users
|
| 162 |
-
- `product_name`: Product name
|
| 163 |
-
- `comment`: Original comment
|
| 164 |
-
- `predicted_rating`: Predicted rating (1-5)
|
| 165 |
-
- `confidence_score`: Confidence (0-1)
|
| 166 |
-
- `prediction_type`: 'single' or 'batch'
|
| 167 |
-
- `created_at`: Prediction timestamp
|
| 168 |
-
|
| 169 |
-
---
|
| 170 |
-
|
| 171 |
-
## π¨ Features
|
| 172 |
-
|
| 173 |
-
β
**Authentication:** JWT-based secure login/registration
|
| 174 |
-
β
**Single Prediction:** Predict one comment at a time
|
| 175 |
-
β
**Batch Prediction:** Upload CSV and predict multiple comments
|
| 176 |
-
β
**Visualization:**
|
| 177 |
-
- Bar chart for rating distribution
|
| 178 |
-
- Word cloud for frequent words
|
| 179 |
-
β
**History Tracking:** All predictions saved to database
|
| 180 |
-
β
**CSV Export:** Download results with predicted ratings
|
| 181 |
-
β
**Responsive UI:** TailwindCSS mobile-friendly design
|
| 182 |
-
β
**API Documentation:** Auto-generated Swagger UI
|
| 183 |
-
|
| 184 |
-
---
|
| 185 |
-
|
| 186 |
-
## π Bonus Points for Teacher Demo
|
| 187 |
-
|
| 188 |
-
1. **Show Swagger UI** at `/docs` - Automatic API documentation β
|
| 189 |
-
2. **Demonstrate:**
|
| 190 |
-
- User registration/login flow
|
| 191 |
-
- Single comment prediction
|
| 192 |
-
- CSV batch upload with visualizations
|
| 193 |
-
- Download CSV results
|
| 194 |
-
3. **Explain:**
|
| 195 |
-
- Clean separation of concerns (routers, services, models)
|
| 196 |
-
- RESTful API design
|
| 197 |
-
- JWT authentication
|
| 198 |
-
- Database relationships
|
| 199 |
-
|
| 200 |
-
---
|
| 201 |
-
|
| 202 |
-
## π CSV File Format
|
| 203 |
-
|
| 204 |
-
Your CSV file should have at least a `Comment` column:
|
| 205 |
-
|
| 206 |
-
```csv
|
| 207 |
-
Comment
|
| 208 |
-
"SαΊ£n phαΊ©m rαΊ₯t tα»t, ΔΓ³ng gΓ³i cαΊ©n thαΊn"
|
| 209 |
-
"ChαΊ₯t lượng kΓ©m, khΓ΄ng nhΖ° mΓ΄ tαΊ£"
|
| 210 |
-
"Giao hΓ ng nhanh, sαΊ£n phαΊ©m α»n"
|
| 211 |
-
```
|
| 212 |
-
|
| 213 |
-
After prediction, you'll get:
|
| 214 |
-
|
| 215 |
-
```csv
|
| 216 |
-
Comment,Predicted_Rating,Confidence
|
| 217 |
-
"SαΊ£n phαΊ©m rαΊ₯t tα»t, ΔΓ³ng gΓ³i cαΊ©n thαΊn",5,0.95
|
| 218 |
-
"ChαΊ₯t lượng kΓ©m, khΓ΄ng nhΖ° mΓ΄ tαΊ£",1,0.88
|
| 219 |
-
"Giao hΓ ng nhanh, sαΊ£n phαΊ©m α»n",4,0.92
|
| 220 |
-
```
|
| 221 |
-
|
| 222 |
-
---
|
| 223 |
-
|
| 224 |
-
## π Security Notes
|
| 225 |
-
|
| 226 |
-
- Change `SECRET_KEY` in `app/config.py` before deployment
|
| 227 |
-
- Passwords are hashed using bcrypt
|
| 228 |
-
- JWT tokens expire after 24 hours
|
| 229 |
-
- CORS is enabled for development (configure for production)
|
| 230 |
-
|
| 231 |
-
---
|
| 232 |
-
|
| 233 |
-
## π Troubleshooting
|
| 234 |
-
|
| 235 |
-
### Issue: "Import errors" when running
|
| 236 |
-
**Solution:** Make sure all dependencies are installed:
|
| 237 |
-
```bash
|
| 238 |
-
pip install -r requirements.txt
|
| 239 |
-
```
|
| 240 |
-
|
| 241 |
-
### Issue: "Database errors"
|
| 242 |
-
**Solution:** Delete `app/database/rating_prediction.db` and restart the app to recreate tables
|
| 243 |
-
|
| 244 |
-
### Issue: "Word cloud doesn't display"
|
| 245 |
-
**Solution:** Check that `app/static/uploads/wordclouds/` directory exists
|
| 246 |
-
|
| 247 |
-
---
|
| 248 |
-
|
| 249 |
-
## π§ Support
|
| 250 |
-
|
| 251 |
-
For questions about the project structure or implementation, refer to the code comments or consult your instructor.
|
| 252 |
-
|
| 253 |
-
**Good luck with your project presentation! π**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Predict Rating
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Predict Rating App
|
| 12 |
+
This is a FastAPI application deployed on Hugging Face Spaces using Docker.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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