Skip to content

This project is a FastAPI-based ML application that detects phishing emails using a trained Random Forest model and TF-IDF vectorization. It includes secure token-based authentication, containerized deployment with Docker, and live deployment on Render.

License

deepak5-ux/phishing-email-detector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“§ Phishing Email Detection API

An AI-powered REST API built with FastAPI that detects whether an email is a phishing attempt or safe using a trained Machine Learning model. This project is containerized using Docker and ready for deployment on platforms like Render or AWS.


πŸš€ Features

  • FastAPI-powered REST API with Swagger (OpenAPI) documentation
  • Text Preprocessing Pipeline with NLTK for cleaning email content
  • ML Model Integration: Random Forest trained on TF-IDF features
  • Secure API using Bearer Token authentication
  • Detailed Logging with timestamp, IP tracking, and prediction logs
  • Dockerized for consistent local and production environments
  • Fully Tested using pytest for API endpoint validation

πŸ› οΈ Tech Stack

  • FastAPI for building the REST API
  • scikit-learn and joblib for the ML model
  • NLTK for text preprocessing
  • Docker for containerization
  • Render (or AWS) for deployment
  • pytest for automated testing

πŸ§ͺ Input Example

Send a POST request to /predict with a JSON body:

{
  "text": "Congratulations! You've won a free iPhone. Click here."
}

If the token is valid, you'll get a prediction:

{
  "prediction": "Phishing Email 🚨"
}

πŸ“‚ Project Structure

phishing_email_detector/
β”œβ”€β”€ app/
β”‚   └── main.py               # FastAPI app
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ text_cleaning.py     # Preprocessing
β”‚   └── logger.py            # Logger setup
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ phishing_rf_model.pkl
β”‚   └── tfidf_vectorizer.pkl
β”œβ”€β”€ tests/
β”‚   └── test_main.py         # Pytest-based tests
β”œβ”€β”€ logs/
β”‚   └── app.log              # Auto-generated logs
β”œβ”€β”€ .env                     # Environment variables
β”œβ”€β”€ requirements.txt         # Dependencies
β”œβ”€β”€ Dockerfile               # Docker setup
└── README.md

βš™οΈ Installation

πŸ“Œ Prerequisites

  • Python 3.9+
  • Docker
  • Git

1. Clone the Repository

git clone https://github.com/yourusername/phishing-email-detector.git
cd phishing-email-detector

2. Set Up Environment

Create a .env file:

API_TOKEN=your-secret-token

3. Install Dependencies

pip install -r requirements.txt

4. Run the App Locally

uvicorn app.main:app --reload

Go to http://127.0.0.1:8000/docs to explore the Swagger UI.


πŸ§ͺ Testing

Run unit tests using:

pytest tests/

🐳 Docker Usage

Build the Docker Image

docker build -t phishing-detector .

Run the Container

docker run --name phishing-api -p 8000:8000 --env-file .env phishing-detector

Visit http://localhost:8000/docs to test it in Swagger.


☁️ Deploy to Render

1. Push Code to GitHub

Ensure your repo is pushed to GitHub.

2. Create a New Render Web Service

  • Go to Render
  • Select "New Web Service"
  • Connect to your GitHub repo
  • Choose environment: Docker
  • Set your environment variables (API_TOKEN)

3. Deploy

Render will build and deploy your app. Use the provided Render URL to access Swagger.


πŸ” API Authentication

Use the Authorize πŸ”’ button in Swagger to test:

Bearer your-secret-token

You can also test using Postman with the Authorization header:

Key: Authorization
Value: Bearer your-secret-token

πŸ“ Logs

Logs are saved under /logs/app.log. Example entry:

2025-04-19 10:45:00 - INFO - Prediction: Phishing Email 🚨 | From: 127.0.0.1

🧠 Skills Demonstrated Through This Project

  • βœ… Demonstrates End-to-End ML Integration
  • βœ… Implements Authentication & Logging
  • βœ… Shows MLOps practices (Docker, testing, deployment)
  • βœ… Easy to demo with Swagger UI & Postman

🀝 Contributing

Feel free to fork and enhance. PRs are welcome!


πŸ“§ Contact

Deepak B
LinkedIn | deepakbalraj511@gmail.com

About

This project is a FastAPI-based ML application that detects phishing emails using a trained Random Forest model and TF-IDF vectorization. It includes secure token-based authentication, containerized deployment with Docker, and live deployment on Render.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published