Skip to content

inderpartap/ham-or-spam

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ham-or-Spam

Requirements Status
Build Status

An intelligent spam filtering system built using a custom Naive Bayes classifier

▶️ You can try it out here at https://ham-or-spam.herokuapp.com/


Table of contents


REST API usage

⬆️ Back to top

Yes, we do provide an API for our service!

using curl

General Syntax

$ curl -H "Content-Type: application/json" -X \
POST -d \
'{"email_text":"SAMPLE EMAIL TEXT"}' \
https://ham-or-spam.herokuapp.com/api/v1/classify/

Show me an example

You thought I was lying!

$ curl -H "Content-Type: application/json" \
-X POST -d \
'{"email_text":"Dear Inderpartap, I would like to immediately transfer 10000 thousand dollars to your account as my beloved husband has expired and I have nobody to ask for to transfer the money to your account. I come from the family of the royal prince of burkino fasa and I would be more than obliged to take your help on this matter. Would you care to share your bank account details with me in the next email conversation that we have? -regards -Liah herman"}' \
https://ham-or-spam.herokuapp.com/api/v1/classify/

JSON response

{
  "email_class": "spam", 
  "email_text": "Dear Inderpartap, I would like to immediately transfer 10000 thousand dollars to your account as my beloved husband has expired and I have nobody to ask for to transfer the money to your account. I come from the family of the royal prince of burkino fasa and I would be more than obliged to take your help on this matter. Would you care to share your bank account details with me in the next email conversation that we have? -regards -Liah herman", 
  "status": 200
}

using requests

⬆️ Back to top

>>> import requests
>>> import json
>>> import pprint
>>>
>>> api_url = "https://ham-or-spam.herokuapp.com/api/v1/classify/"
>>> payload = \
{
'email_text': 'Dear Inderpartap, I would like to immediately transfer 10000 '
               'thousand dollars to your account as my beloved husband has '
               'expired and I have nobody to ask for to transfer the money '
               'to your account. I come from the family of the royal prince '
               'of burkino fasa and I would be more than obliged to take '
               'your help on this matter. Would you care to share your bank '
               'account details with me in the next email conversation that '
               'we have? -regards -Liah herman'
}
>>>
>>> headers = {'content-type': 'application/json'}
>>> # query our API
>>> response = requests.post(api_url, data=json.dumps(payload), headers=headers)
>>> response.status_code
200
>>> pprint.pprint(response.json())
{
 'email_class': 'spam',
 'email_text': 'Dear Inderpartap, I would like to immediately transfer 10000 '
               'thousand dollars to your account as my beloved husband has '
               'expired and I have nobody to ask for to transfer the money '
               'to your account. I come from the family of the royal prince '
               'of burkino fasa and I would be more than obliged to take '
               'your help on this matter. Would you care to share your bank '
               'account details with me in the next email conversation that '
               'we have? -regards -Liah herman',
 'status': 200
 }
>>> 

Using standard python 3 library

⬆️ Back to top

requests module really makes our life easy and I use it all the time. But sigh, there should be an example using the standard library so here it is

>>> import urllib.request
>>> import json
>>> import pprint 
>>>
>>> url = "https://ham-or-spam.herokuapp.com/api/v1/classify/"
>>> req = urllib.request.Request(url)
>>> req.add_header(
       'Content-Type',
       'application/json; charset=utf-8'
   )
>>>
>>> body = \
{'email_text': 'Dear Inderpartap, I would like to immediately transfer 10000 '
               'thousand dollars to your account as my beloved husband has '
               'expired and I have nobody to ask for to transfer the money '
               'to your account. I come from the family of the royal prince '
               'of burkino fasa and I would be more than obliged to take '
               'your help on this matter. Would you care to share your bank '
               'account details with me in the next email conversation that '
               'we have? -regards -Liah herman'
}
>>> json_data = json.dumps(body).encode('utf-8')   # needs to be bytes
>>> req.add_header('Content-Length', len(json_data))
>>>
>>> with urllib.request.urlopen(req, json_data) as f:
...   print(f.read().decode('utf-8'))
... 
{
  "email_class": "spam", 
  "email_text": "Dear Inderpartap, I would like to immediately transfer 10000 thousand dollars to your account as my beloved husband has expired and I have nobody to ask for to transfer the money to your account. I come from the family of the royal prince of burkino fasa and I would be more than obliged to take your help on this matter. Would you care to share your bank account details with me in the next email conversation that we have? -regards -Liah herman", 
  "status": 200
}
>>> 

Technologies used

⬆️ Back to top

Built upon the giant shoulders of (in no particular order)

Backend

and some more

Front end


Testing

⬆️ Back to top

Installing it locally

$ virtualenv env              # Create virtual environment
$ source env/bin/activate     # Change default python to virtual one
(env)$ git clone https://github.com/inderpartap/ham-or-spam.git
(env)$ cd ham-or-spam
(env)$ pip install -r requirements.txt

Running it

$ make run

Contributers


Roadmap

⬆️ Back to top

  • Deploying to heroku
  • Creating a REST API
  • Improving the UI
  • Writing tests
  • Simple API authentication

About

email spam filter application in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published