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.
- 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
pytestfor API endpoint validation
- 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
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 π¨"
}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
- Python 3.9+
- Docker
- Git
git clone https://github.com/yourusername/phishing-email-detector.git
cd phishing-email-detectorCreate a .env file:
API_TOKEN=your-secret-tokenpip install -r requirements.txtuvicorn app.main:app --reloadGo to http://127.0.0.1:8000/docs to explore the Swagger UI.
Run unit tests using:
pytest tests/docker build -t phishing-detector .docker run --name phishing-api -p 8000:8000 --env-file .env phishing-detectorVisit http://localhost:8000/docs to test it in Swagger.
Ensure your repo is pushed to GitHub.
- Go to Render
- Select "New Web Service"
- Connect to your GitHub repo
- Choose environment: Docker
- Set your environment variables (API_TOKEN)
Render will build and deploy your app. Use the provided Render URL to access Swagger.
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 are saved under /logs/app.log. Example entry:
2025-04-19 10:45:00 - INFO - Prediction: Phishing Email π¨ | From: 127.0.0.1
- β Demonstrates End-to-End ML Integration
- β Implements Authentication & Logging
- β Shows MLOps practices (Docker, testing, deployment)
- β Easy to demo with Swagger UI & Postman
Feel free to fork and enhance. PRs are welcome!
Deepak B
LinkedIn | deepakbalraj511@gmail.com