Skip to content

sulavghimiree/Email_Fraud_Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Email Fraud/Spam Detection System

AI-powered email content analyzer with a Flask backend (ML pipeline) and a simple HTML/CSS (Tailwind) frontend. Paste any email text into the UI and get an instant spam vs. not-spam prediction.

Legitimate email result

Spam email result

Features

  • Fast, lightweight spam detector served via a single /predict endpoint
  • Scikit‑learn pipeline with custom text preprocessing (tokenization, stopword removal, lemmatization, stemming)
  • Frontend built with plain HTML + TailwindCSS CDN (no build step)
  • CORS enabled so the static frontend can call the local API

Tech Stack

  • Backend: Python, Flask, flask-cors, scikit-learn, pandas, nltk, joblib
  • Frontend: HTML + TailwindCSS (CDN)
  • Model: email_fraud_detection_pipeline.pkl (trained offline in notebooks)

Project Structure

EFD_System/
  EFD_api/
    app.py                        # Flask API exposing POST /predict
    model/
      email_fraud_detection_pipeline.pkl
  Frontend/
    index.html                    # Static UI that calls the API
  assets/
    ui-legitimate.png             # screenshot (add this file)
    ui-spam.png                   # screenshot (add this file)
Email_Fraud_Detection/
  classification.ipynb            # evaluation / exploration
  pipeline_creator.ipynb          # model training and pipeline export
  Dataset/
    fraud_email_.csv

API Contract

  • URL: POST http://127.0.0.1:5000/predict
  • Request body (JSON):
    { "email": "Paste the raw email content here" }
  • Response (JSON):
    { "prediction": "spam" } // or "not spam"
  • Error response:
    { "error": "<message>" }

Use this API on your own website

You can call the same /predict endpoint from any site or app that can send an HTTP POST with JSON.

Quick JS example (client-side)

<!-- In your page -->
<script>
    async function detectEmailSpam(emailText) {
      const API_URL = "https://your-domain.example.com/predict"; // or http://127.0.0.1:5000/predict during local dev
      const res = await fetch(API_URL, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ email: emailText })
      });
      const data = await res.json();
      if (data.error) throw new Error(data.error);
      // data.prediction is either "spam" or "not spam"
      return data.prediction;
    }
    // Example usage:
    // detectEmailSpam("Congratulations! You've won a prize...").then(console.log);
    // -> "spam"
    // -> or "not spam"
  }
</script>

cURL

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"email": "Your email content goes here"}' \
  http://127.0.0.1:5000/predict

Python (requests)

import requests

resp = requests.post(
    "http://127.0.0.1:5000/predict",
    json={"email": "Your email content goes here"}
)
print(resp.json())

CORS and security

  • This API enables CORS in app.py via CORS(app). For public deployments, restrict allowed origins:

    from flask_cors import CORS
    CORS(app, resources={r"/predict": {"origins": ["https://your-site.com", "https://www.your-site.com"]}})
  • Always validate and rate-limit in front of the API if you expose it publicly (reverse proxy, WAF, or API gateway).

Deploying for others to consume

  • Bind the server to all interfaces if you want it reachable on your LAN or via a reverse proxy:
    • Update your start command to listen on 0.0.0.0 or configure your process manager accordingly.
  • Example production servers:
    • Windows: waitress (pip install waitress, then python -m waitress --listen=0.0.0.0:5000 app:app)
    • Linux: gunicorn/uvicorn behind Nginx (reverse proxy TLS + rate limiting)
  • If you change the public URL or port, update the fetch URL in your frontend or in the JS example above.

Run Locally (Windows, PowerShell)

  1. Backend (Flask)
  • Open PowerShell in the repository root and run:
cd EFD_System/EFD_api
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python - <<'PY'
import nltk
for pkg in ["stopwords","wordnet","omw-1.4","punkt"]:
    nltk.download(pkg)
PY

python app.py
  • The API will listen on http://127.0.0.1:5000.
  1. Frontend (static HTML)
  • With the backend running, open the file EFD_System/Frontend/index.html in your browser (double‑click is fine).
  • Paste some email text and click “Analyze Email”.

That’s it—no Node.js or bundlers required.

Notes on the Model

  • The Flask app loads model/email_fraud_detection_pipeline.pkl (scikit‑learn pipeline).
  • Text preprocessing includes lowercasing, punctuation/digit stripping, stopword removal, lemmatization (WordNet), and stemming (Porter).
  • The “97.3% Accuracy” displayed in the UI reflects the prior evaluation from the training notebook; actual performance will depend on your dataset and evaluation split.

Troubleshooting

  • CORS/Fetch errors: ensure the Flask server is running at http://127.0.0.1:5000 before opening the index.html.
  • NLTK downloads behind a firewall: pre‑download data as shown in the setup, or set the NLTK_DATA environment variable to a directory with the corpora.
  • Different port: if you change the Flask port, also update the fetch URL in Frontend/index.html.

Contributing

  • Keep the API contract stable (POST /predict).
  • If you update the pipeline or preprocessing, export a new email_fraud_detection_pipeline.pkl and verify end‑to‑end with the UI.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published