A machine learning-powered web application built with Streamlit that classifies emails and SMS messages as spam or legitimate (ham) using natural language processing techniques.
- Real-time Classification: Instantly classify messages as spam or not spam
- Text Preprocessing: Advanced NLP preprocessing including tokenization, stemming, and stopword removal
- Web Interface: User-friendly Streamlit interface for easy message input and classification
- Machine Learning: Uses pre-trained TF-IDF vectorizer and classification model
- Interactive UI: Clean and intuitive design with emoji indicators for results
-
Text Preprocessing:
- Converts text to lowercase
- Tokenizes the message into individual words
- Removes non-alphanumeric characters
- Filters out English stopwords and punctuation
- Applies Porter Stemming to reduce words to their root forms
-
Feature Extraction:
- Uses TF-IDF (Term Frequency-Inverse Document Frequency) vectorization
- Converts preprocessed text into numerical features
-
Classification:
- Applies pre-trained machine learning model
- Returns binary classification (Spam/Not Spam)
- Python 3.7+
- pip package manager
- Clone the repository:
git clone <repository-url>
cd spam-detector- Install dependencies:
pip install -r requirements.txt-
Ensure model files are present:
vectorizer.pkl- Pre-trained TF-IDF vectorizermodel.pkl- Pre-trained classification model
-
Run the application:
streamlit run app.py- Access the web interface:
- Open your browser and navigate to
http://localhost:8501
- Open your browser and navigate to
- Launch the application using the command above
- Enter your email or SMS message in the text input field
- Click the "Predict" button
- View the classification result:
- 🚫 Spam: Message classified as spam
- ✅ Not Spam: Message classified as legitimate
spam-detector/
├── app.py # Main Streamlit application
├── vectorizer.pkl # Pre-trained TF-IDF vectorizer
├── model.pkl # Pre-trained classification model
├── requirements.txt # Python dependencies
└── README.md # Project documentation
- streamlit: Web application framework
- nltk: Natural language processing toolkit
- scikit-learn: Machine learning library
- pandas: Data manipulation and analysis
- numpy: Numerical computing
- Porter Stemmer: Word stemming algorithm
- Stopwords: English stopword corpus
- Tokenization: Word tokenization tools
See requirements.txt for complete list of dependencies with versions.
def transform_text(text):
text = text.lower() # Lowercase conversion
text = word_tokenize(text) # Tokenization
y = [i for i in text if i.isalnum()] # Remove non-alphanumeric
y = [i for i in y if i not in stopwords.words('english')] # Remove stopwords
y = [ps.stem(i) for i in y] # Apply stemming
return " ".join(y)- Vectorization: TF-IDF (Term Frequency-Inverse Document Frequency)
- Classification: Binary classification (likely Naive Bayes, SVM, or similar)
- Output: Binary prediction (0 = Not Spam, 1 = Spam)
- NLTK Downloads: Automatic download of required NLTK corpora
- Model Loading: Models loaded once at startup for efficiency
- Preprocessing: Fast text preprocessing pipeline
- Memory Usage: Models kept in memory for quick predictions
- Language Support: Currently optimized for English text only
- Model Dependency: Requires pre-trained model files
- Internet Connection: NLTK may require internet for initial corpus downloads
- Text Length: Performance may vary with very long messages
- NLTK Download Errors:
python -c "import nltk; nltk.download('punkt'); nltk.download('stopwords')"-
Missing Model Files:
- Ensure
vectorizer.pklandmodel.pklare in the project directory - Verify files are not corrupted
- Ensure
-
Streamlit Port Issues:
streamlit run app.py --server.port 8502- File not found: Check if model pickle files exist
- Import errors: Verify all dependencies are installed
- NLTK errors: Ensure NLTK data is downloaded correctly
- Multi-language support
- Confidence score display
- Batch processing capability
- Model retraining interface
- Performance metrics dashboard
- API endpoint for integration
- Mobile-responsive design improvements
If you need to retrain the model:
- Prepare your dataset with labeled spam/ham messages
- Use the same preprocessing pipeline (
transform_textfunction) - Train TF-IDF vectorizer and classification model
- Save models using pickle for deployment
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is for educational and demonstration purposes. Please ensure compliance with relevant data privacy regulations when processing personal messages.
Note: This application is designed for educational purposes and should be thoroughly tested before use in production environments.