CoolFace
Apppublic

Anutri03/email_spam_api

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿ“ง Email Spam Detection API

A powerful AI-powered email spam detection system that not only classifies emails as spam or legitimate but also provides detailed explanations for its decisions.

๐Ÿš€ Features

  • โ€”High Accuracy: Uses DistilBERT transformer model with 98.86% F1-Score
  • โ€”Multiple Models: Supports both transformer and traditional ML models
  • โ€”Detailed Explanations: Provides reasons why an email was classified as spam or ham
  • โ€”Batch Processing: Can process multiple emails at once
  • โ€”RESTful API: Clean, well-documented API endpoints
  • โ€”Fast Performance: Optimized for production use

๐Ÿ—๏ธ Architecture

Models Available:

  1. 1.DistilBERT (Best Performance) - 98.86% F1-Score
  2. 2.Random Forest - 97.69% F1-Score
  3. 3.Logistic Regression - 95.47% F1-Score
  4. 4.Naive Bayes - 91.38% F1-Score

Spam Detection Features:

  • โ€”Text Pattern Analysis: Detects urgency words, money references, action words
  • โ€”Formatting Analysis: Identifies excessive caps, multiple exclamation marks
  • โ€”URL Detection: Counts and analyzes suspicious links
  • โ€”Feature Importance: Shows which words/patterns influenced the decision

๐Ÿ“ก API Endpoints

Base URL

https://anutri03-email-spam-api.hf.space

1. Health Check

http
GET /health

Response:

json
{
  "status": "healthy",
  "models_loaded": 4,
  "best_model": "DistilBERT"
}

2. Single Email Prediction

http
POST /predict
Content-Type: application/json

{
  "text": "URGENT: Your account has been compromised. Click here to update your password!"
}

Response:

json
{
  "prediction": "spam",
  "confidence": 0.95,
  "explanation": "๐Ÿšจ **SPAM DETECTED**\n\n**Spam indicators found:**\nโ€ข Contains 'urgent'\nโ€ข Contains 'click here'\nโ€ข Multiple exclamation marks (1)",
  "model_used": "DistilBERT"
}

3. Batch Email Prediction

http
POST /predict_batch
Content-Type: application/json

{
  "emails": [
    "Hi team, meeting at 2 PM today",
    "FREE MONEY!!! Click here to claim your prize!"
  ]
}

4. Model Information

http
GET /model_info

๐Ÿ”ง Usage Examples

Python

python
import requests

# Single prediction
response = requests.post(
    "https://anutri03-email-spam-api.hf.space/predict",
    json={"text": "Your email text here"}
)
result = response.json()
print(f"Prediction: {result['prediction']}")
print(f"Explanation: {result['explanation']}")

# Batch prediction
emails = ["Email 1", "Email 2", "Email 3"]
response = requests.post(
    "https://anutri03-email-spam-api.hf.space/predict_batch", 
    json={"emails": emails}
)
results = response.json()

cURL

bash
# Single prediction
curl -X POST "https://anutri03-email-spam-api.hf.space/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": "URGENT: Click here to claim your prize!"}'

# Health check
curl "https://anutri03-email-spam-api.hf.space/health"

๐Ÿšจ Spam Indicators Detected

The system analyzes emails for various spam indicators:

Keywords

  • โ€”Urgency: "urgent", "immediate", "asap", "emergency", "act now"
  • โ€”Money: "free", "win", "prize", "money", "cash", "million", "dollar"
  • โ€”Actions: "click here", "buy now", "order now", "call now"

Patterns

  • โ€”Suspicious Patterns: $d+, % off, limited time, act now
  • โ€”Excessive Formatting: Multiple caps, exclamation marks
  • โ€”URLs: Suspicious links and multiple email addresses

๐Ÿ“Š Performance Metrics

ModelAccuracyF1-ScorePrecisionRecall
DistilBERT99.04%98.86%99.09%98.64%
Random Forest98.05%97.69%97.87%97.52%
Logistic Regression96.11%95.47%94.08%96.90%
Naive Bayes92.80%91.38%92.50%90.29%

๐Ÿ“ API Documentation

Once deployed, visit /docs for interactive API documentation:

  • โ€”Swagger UI: https://anutri03-email-spam-api.hf.space/docs
  • โ€”ReDoc: https://anutri03-email-spam-api.hf.space/redoc

๐Ÿ”’ Rate Limits

  • โ€”Single Predictions: No limit
  • โ€”Batch Predictions: Maximum 100 emails per request
  • โ€”Text Length: Maximum 10,000 characters per email

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments

  • โ€”Hugging Face for the DistilBERT model
  • โ€”Scikit-learn for traditional ML algorithms
  • โ€”FastAPI for the web framework
  • โ€”Transformers library for NLP capabilities

Made with โค๏ธ for email security