Spaces:
Sleeping
Sleeping
| # ποΈ 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) | |