Skip to content

About

Deep learning system for fake review detection using RoBERTa, domain-adversarial training, linguistic features, and FastAPI.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

πŸ” Fake Review Detection Using Deep Learning

Python PyTorch Transformers FastAPI

🎯 Accuracy: 93.59% on Out-of-Domain Data

A deep learning system to detect fake product reviews using RoBERTa + Domain Adversarial Training

Features β€’ Installation β€’ Usage β€’ API β€’ Results β€’


πŸ“‹ Table of Contents


✨ Features

Feature Description
🧠 RoBERTa Base State-of-the-art transformer for text understanding
🎯 Domain Adversarial Training Generalizes to unseen product categories
πŸ“Š Linguistic Features 6 hand-crafted features for enhanced detection
🌐 REST API FastAPI-powered prediction service
πŸ’» Web Interface Beautiful, responsive UI for testing
πŸ“ˆ TensorBoard Real-time training visualization
πŸš€ Production Ready Easy to deploy and scale

πŸ“Š Results

Out-of-Domain Test Performance

Tested on Home & Kitchen category - never seen during training!

Metric Score
βœ… Accuracy 93.59%
🎯 Precision 94%
πŸ“ˆ Recall 94%
βš–οΈ F1-Score 93.86%

Classification Report

Class Precision Recall F1-Score Support
Real 0.98 0.89 0.93 609
Fake 0.90 0.98 0.94 608
Avg 0.94 0.94 0.94 1217

Confusion Matrix

              Predicted
            Real    Fake
Actual Real  543      66
       Fake   12     596

πŸ“ Dataset

Property Value
Source Kaggle - Fake Reviews Dataset
Total Reviews 40,526
Real Reviews ~20,000
Fake Reviews ~20,000
Categories Electronics, Home & Kitchen, etc.

🧠 Model Architecture

Input Review
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         RoBERTa Encoder             β”‚
β”‚         (768-dim output)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β–Ό                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 768-dim  β”‚            β”‚  6 Ling.    β”‚
β”‚ Features β”‚            β”‚  Features   β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
     β”‚                         β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  Combined   β”‚
         β”‚  774-dim    β”‚
         β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚   Label Classifier    β”‚
    β”‚      Real / Fake      β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Techniques

  1. Domain Adversarial Training (DANN) - Gradient Reversal Layer for domain-invariant features
  2. Linguistic Feature Extraction - 6 features: word count, unique ratio, exclamation ratio, caps ratio, avg word length, question ratio
  3. Text Preprocessing - URL removal, normalization, focus on content over style

πŸš€ Installation

Prerequisites

  • Python 3.8+
  • CUDA (optional, for GPU acceleration)

Setup

# Clone the repository
git clone https://github.com/yourusername/fake-review-detection.git
cd fake-review-detection

# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
.\venv\Scripts\activate  # Windows

# Install dependencies
pip install -r requirements.txt

Download Model Weights

The trained model weights (dann_fixed_best.pth, ~500MB) should be in the project root directory.


πŸ’» Usage

Running the API Server

python app.py

Server starts at: http://127.0.0.1:8000

Web Interface

Open your browser and navigate to:

http://127.0.0.1:8000/home

Python API

from app import predict_review

# Single prediction
result = predict_review("This product is amazing! Best ever!")

print(result)
# Output:
# {
#     'prediction': 'Fake',
#     'confidence': '95.32%',
#     'confidence_value': 95.32,
#     'features': {
#         'exclamation_ratio': 0.05,
#         'caps_ratio': 0.02,
#         'unique_ratio': 0.85
#     },
#     'probabilities': {
#         'real': 4.68,
#         'fake': 95.32
#     }
# }

🌐 API Endpoints

Health Check

GET /api/health

Response:

{
  "status": "healthy",
  "service": "Review Authenticity Detector",
  "version": "2.0",
  "model_loaded": true
}

Single Review Analysis

POST /reviews
Content-Type: application/json

{
    "review": "This product is amazing!"
}

Response:

{
    "review": "This product is amazing!",
    "prediction": "Fake",
    "confidence": "95.32%",
    "confidence_value": 95.32,
    "features": {...},
    "probabilities": {...},
    "timestamp": "2026-01-30T15:30:00"
}

Batch Analysis

POST /reviews/batch
Content-Type: application/json

{
    "reviews": [
        "Great product!",
        "Okay quality, nothing special",
        "BEST PURCHASE EVER!!!"
    ]
}

Response:

{
    "results": [...],
    "summary": {
        "total": 3,
        "fake_count": 2,
        "real_count": 1,
        "fake_percentage": 66.67,
        "average_confidence": 87.5
    }
}

πŸ“‚ Project Structure

πŸ“¦ fake-review-detection/
β”œβ”€β”€ πŸ““ Fake_Review_Detection.ipynb  # Training notebook
β”œβ”€β”€ 🌐 app.py                       # FastAPI server + Web UI
β”œβ”€β”€ πŸ—οΈ models.py                    # Model architecture
β”œβ”€β”€ πŸ”§ utils.py                     # Preprocessing utilities
β”œβ”€β”€ πŸ“‹ requirements.txt             # Python dependencies
β”œβ”€β”€ πŸ“„ project_report.pdf           # PDF report
β”œβ”€β”€ πŸ“Š presentation_slides.pptx     # PowerPoint presentation
β”œβ”€β”€ πŸ“– README.md                    # This file
β”œβ”€β”€ πŸ“ static/                      # CSS files
β”‚   └── style.css
└── πŸ“ .git/                        # Git repository

πŸ‹οΈ Training

Training on Kaggle

  1. Upload the notebook to Kaggle
  2. Enable GPU (Tesla P100)
  3. Run all cells
  4. Download dann_fixed_best.pth

Hyperparameters

Parameter Value
Max Length 256 tokens
Batch Size 16
Epochs 5
Learning Rate 2e-5
Optimizer AdamW
Scheduler Linear Warmup
Dropout 0.3

Training Progress

Epoch Task Loss Domain Loss Val Accuracy
1 0.4175 1.3891 92.06%
2 0.1899 1.6394 94.74%
3 0.2067 2.0495 89.36%
4 0.1741 2.2763 96.24%
5 0.1483 2.2861 95.16%

πŸ“ˆ TensorBoard

Monitor training in real-time:

tensorboard --logdir=./runs

Open: http://localhost:6006

Tracked Metrics:

  • πŸ“‰ Task Loss
  • πŸ“‰ Domain Loss
  • πŸ“ˆ Validation Accuracy
  • πŸ“Š Weight Distributions

πŸ› οΈ Technologies Used

Category Technologies
Deep Learning PyTorch, HuggingFace Transformers
Model RoBERTa-base (124M parameters)
API FastAPI, Uvicorn, Pydantic
Frontend HTML5, CSS3, JavaScript
Visualization TensorBoard, Matplotlib, Seaborn
Data Pandas, NumPy, Scikit-learn

πŸ™ Acknowledgments


πŸ“š References

  1. Liu, Y., et al. "RoBERTa: A Robustly Optimized BERT Pretraining Approach" (2019)
  2. Ganin, Y., et al. "Domain-Adversarial Training of Neural Networks" JMLR (2016)

⭐ Star this repo if you found it helpful!

Made with ❀️ for Deep Learning Course

About

Deep learning system for fake review detection using RoBERTa, domain-adversarial training, linguistic features, and FastAPI.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages