AI Powered Spam Detection Web App Deployed on AWS EC2 with Flask & Gunicorn
Introduction
Spam emails continue to plague inboxes worldwide, affecting productivity and exposing users to phishing and malware. To address this problem, I developed an AI-based Spam Detection Web Application using Python and Flask. The app utilizes natural language processing (NLP) and machine learning to classify messages as spam or not spam with high accuracy.
In this project, I not only built the core spam classifier but also deployed the full stack application on an AWS EC2 instance, making it publicly accessible via the web. I also implemented a production-ready environment using Gunicorn, a WSGI HTTP server for Python applications.
This article outlines the development process, deployment architecture, tools used, and the steps I followed to bring the app live.
Project Goals
✅ Build an AI model capable of detecting spam emails
✅ Create a web-based UI using Flask
✅ Deploy the application on a cloud server (AWS EC2)
✅ Make the server production ready using Gunicorn
✅ Demonstrate understanding of cloud infrastructure, Python backend, and deployment best practices
How the Spam Detection Works
At the heart of the application is a machine learning model trained on labeled email data. The model uses techniques such as:
Text preprocessing: Removing noise, stop words, tokenization
TF-IDF Vectorization: Converting text into numerical form
Multinomial Naive Bayes Classifier: A popular algorithm for text classification
When users input a message into the web form, the model predicts whether the message is spam or ham (not spam) in real-time.
Tech Stack
- Layer Technology
- Frontend HTML/CSS
- Backend Python, Flask
- ML/NLP Scikit-learn, Pandas, NLTK
- Deployment AWS EC2 (Ubuntu 20.04), Gunicorn
- Package Manager pip
- Git Repository GitHub
App Features
- Simple, clean UI for user input
- Real-time spam classification
- RESTful architecture
- Lightweight and responsive
- Publicly accessible via EC2 public IP
The app is hosted on an Ubuntu EC2 instance with Flask handling the app logic and Gunicorn ensuring the app can handle production traffic efficiently.
Step by Step Deployment Process
- Launching EC2 Instance
- Selected Ubuntu 20.04 AMI
- t2.micro instance (free tier)
- Set up key pair (.pem) and security group
Allowed ports: 22 (SSH), 80 (HTTP), 5000 (Flask Dev)
- Installing System Dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip -y
- Cloning the Flask App
git clone <your-github-repo-url>
cd spamemail
- Fixing Windows-style line endings (if needed)
sudo apt install dos2unix -y
dos2unix requirements.txt
- Installing Python Dependencies
pip3 install -r requirements.txt
- Running the App in Development
python3 app.py
Access at: http://your-ec2-ip:5000
- Running with Gunicorn for Production
pip3 install gunicorn
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
Note: Replace app with your filename: flask app object if your file or Flask object has a different name.
Security Considerations
- For development, port 5000 was opened to the public.
- For production, the app can be routed through Nginx on port 80 with HTTPS using Let's Encrypt.
- SSH access was secured with a private key.
Testing & Validation
- Tested on multiple test cases and known spam messages
- Verified model accuracy and false positive rates
- Load tested the Gunicorn server for concurrent requests
Lessons Learned
- Handling OS level issues like CRLF line endings on Linux
- Setting up and securing cloud infrastructure on AWS
- Working with WSGI servers like Gunicorn for Python deployment
- Enhancing app performance and fault tolerance Repository
GitHub: your repo link
Next Steps
- Integrate with email clients to automatically flag spam
- Add user authentication and database support
- Deploy behind an Nginx reverse proxy for better performance
- Add HTTPS support for secure connections
- Dockerize the application for portability
Final Thoughts
This project helped me bridge the gap between machine learning development and real world deployment. From building a robust spam classifier to deploying it in the cloud, I’ve gained hands-on experience in both AI and DevOps fundamentals. It’s a great addition to my portfolio and a stepping stone toward more scalable AI solutions.
Preview of the Application
The application interface is simple and intuitive. Users can paste email content into a text box and receive immediate feedback indicating whether the content is spam or not.
check out the screenshot of what the app looks like
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vml4qhcy2ibh00lsduh.png)
(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihwwkaai2bfqafw18axp.png)
Top comments (0)