Spaces:
Sleeping
Sleeping
| # π 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! π | |