Skip to content

Latest commit

 

History

History
397 lines (294 loc) · 10.5 KB

README.md

File metadata and controls

397 lines (294 loc) · 10.5 KB

🔍 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