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.
- Fast, lightweight spam detector served via a single
/predictendpoint - 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
- Backend: Python, Flask, flask-cors, scikit-learn, pandas, nltk, joblib
- Frontend: HTML + TailwindCSS (CDN)
- Model:
email_fraud_detection_pipeline.pkl(trained offline in notebooks)
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
- 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>" }
You can call the same /predict endpoint from any site or app that can send an HTTP POST with JSON.
<!-- 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 -X POST \
-H "Content-Type: application/json" \
-d '{"email": "Your email content goes here"}' \
http://127.0.0.1:5000/predictimport requests
resp = requests.post(
"http://127.0.0.1:5000/predict",
json={"email": "Your email content goes here"}
)
print(resp.json())-
This API enables CORS in
app.pyviaCORS(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).
- 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.0or configure your process manager accordingly.
- Update your start command to listen on
- Example production servers:
- Windows: waitress (
pip install waitress, thenpython -m waitress --listen=0.0.0.0:5000 app:app) - Linux: gunicorn/uvicorn behind Nginx (reverse proxy TLS + rate limiting)
- Windows: waitress (
- If you change the public URL or port, update the
fetchURL in your frontend or in the JS example above.
- 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.
- Frontend (static HTML)
- With the backend running, open the file
EFD_System/Frontend/index.htmlin your browser (double‑click is fine). - Paste some email text and click “Analyze Email”.
That’s it—no Node.js or bundlers required.
- 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.
- CORS/Fetch errors: ensure the Flask server is running at
http://127.0.0.1:5000before opening theindex.html. - NLTK downloads behind a firewall: pre‑download data as shown in the setup, or set the
NLTK_DATAenvironment variable to a directory with the corpora. - Different port: if you change the Flask port, also update the
fetchURL inFrontend/index.html.
- Keep the API contract stable (
POST /predict). - If you update the pipeline or preprocessing, export a new
email_fraud_detection_pipeline.pkland verify end‑to‑end with the UI.

