├── Procfile ├── README.md ├── Spam SMS Classifier - Deployment.py ├── Spam SMS Collection ├── app.py ├── cv-transform.pkl ├── readme_resources ├── application-error-heroku.png └── spam-sms-web-app.gif ├── requirements.txt ├── spam-sms-mnb-model.pkl ├── static ├── not-spam.webp ├── spam-favicon.ico ├── spam.webp └── styles.css └── templates ├── home.html └── result.html /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn app:app -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spam SMS Classification - Deployment 2 | ![Kaggle](https://img.shields.io/badge/Dataset-Kaggle-blue.svg) ![Python 3.6](https://img.shields.io/badge/Python-3.6-brightgreen.svg) ![NLTK](https://img.shields.io/badge/Library-NLTK-orange.svg) 3 | 4 | • This repository consists of files required to deploy a ___Machine Learning Web App___ created with ___Flask___ on ___Heroku___ platform. 5 | 6 | • If you want to view the deployed model, click on the following link:
7 | Deployed at: _https://spam-sms-detector.herokuapp.com/_ 8 | 9 | • If you are searching for __Code, Algorithms used and Accuracy of the model__.. you won't find it here. Click the link mentioned below for the same:
10 | Link: _https://github.com/anujvyas/Natural-Language-Processing-Projects/tree/master/Spam%20SMS%20Classification_ 11 | 12 | • Please do ⭐ the repository, if it helped you in anyway. 13 | 14 | • A glimpse of the web app: 15 | 16 | ![GIF](readme_resources/spam-sms-web-app.gif) 17 | 18 | _**----- Important Note -----**_
19 | • If you encounter this webapp as shown in the picture given below, it is occuring just because **free dynos for this particular month provided by Heroku have been completely used.** _You can access the webpage on 1st of the next month._
20 | • Sorry for the inconvenience. 21 | 22 | ![Heroku-Error](readme_resources/application-error-heroku.png) 23 | -------------------------------------------------------------------------------- /Spam SMS Classifier - Deployment.py: -------------------------------------------------------------------------------- 1 | # Importing essential libraries 2 | import pandas as pd 3 | import pickle 4 | 5 | # Loading the dataset 6 | df = pd.read_csv('Spam SMS Collection', sep='\t', names=['label', 'message']) 7 | 8 | # Importing essential libraries for performing Natural Language Processing on 'SMS Spam Collection' dataset 9 | import nltk 10 | import re 11 | nltk.download('stopwords') 12 | from nltk.corpus import stopwords 13 | from nltk.stem.porter import PorterStemmer 14 | 15 | # Cleaning the messages 16 | corpus = [] 17 | ps = PorterStemmer() 18 | 19 | for i in range(0,df.shape[0]): 20 | 21 | # Cleaning special character from the message 22 | message = re.sub(pattern='[^a-zA-Z]', repl=' ', string=df.message[i]) 23 | 24 | # Converting the entire message into lower case 25 | message = message.lower() 26 | 27 | # Tokenizing the review by words 28 | words = message.split() 29 | 30 | # Removing the stop words 31 | words = [word for word in words if word not in set(stopwords.words('english'))] 32 | 33 | # Stemming the words 34 | words = [ps.stem(word) for word in words] 35 | 36 | # Joining the stemmed words 37 | message = ' '.join(words) 38 | 39 | # Building a corpus of messages 40 | corpus.append(message) 41 | 42 | # Creating the Bag of Words model 43 | from sklearn.feature_extraction.text import CountVectorizer 44 | cv = CountVectorizer(max_features=2500) 45 | X = cv.fit_transform(corpus).toarray() 46 | 47 | # Extracting dependent variable from the dataset 48 | y = pd.get_dummies(df['label']) 49 | y = y.iloc[:, 1].values 50 | 51 | # Creating a pickle file for the CountVectorizer 52 | pickle.dump(cv, open('cv-transform.pkl', 'wb')) 53 | 54 | # Model Building 55 | 56 | from sklearn.model_selection import train_test_split 57 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0) 58 | 59 | # Fitting Naive Bayes to the Training set 60 | from sklearn.naive_bayes import MultinomialNB 61 | classifier = MultinomialNB(alpha=0.3) 62 | classifier.fit(X_train, y_train) 63 | 64 | # Creating a pickle file for the Multinomial Naive Bayes model 65 | filename = 'spam-sms-mnb-model.pkl' 66 | pickle.dump(classifier, open(filename, 'wb')) -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # Importing essential libraries 2 | from flask import Flask, render_template, request 3 | import pickle 4 | 5 | # Load the Multinomial Naive Bayes model and CountVectorizer object from disk 6 | filename = 'spam-sms-mnb-model.pkl' 7 | classifier = pickle.load(open(filename, 'rb')) 8 | cv = pickle.load(open('cv-transform.pkl','rb')) 9 | app = Flask(__name__) 10 | 11 | @app.route('/') 12 | def home(): 13 | return render_template('home.html') 14 | 15 | @app.route('/predict',methods=['POST']) 16 | def predict(): 17 | if request.method == 'POST': 18 | message = request.form['message'] 19 | data = [message] 20 | vect = cv.transform(data).toarray() 21 | my_prediction = classifier.predict(vect) 22 | return render_template('result.html', prediction=my_prediction) 23 | 24 | if __name__ == '__main__': 25 | app.run(debug=True) -------------------------------------------------------------------------------- /cv-transform.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/cv-transform.pkl -------------------------------------------------------------------------------- /readme_resources/application-error-heroku.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/readme_resources/application-error-heroku.png -------------------------------------------------------------------------------- /readme_resources/spam-sms-web-app.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/readme_resources/spam-sms-web-app.gif -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==1.1.1 2 | gunicorn==19.9.0 3 | itsdangerous==1.1.0 4 | Jinja2==2.10.1 5 | MarkupSafe==1.1.1 6 | Werkzeug==0.15.5 7 | numpy>=1.9.2 8 | scipy>=0.15.1 9 | scikit-learn>=0.18 10 | matplotlib>=1.4.3 11 | pandas>=0.19 12 | -------------------------------------------------------------------------------- /spam-sms-mnb-model.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/spam-sms-mnb-model.pkl -------------------------------------------------------------------------------- /static/not-spam.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/static/not-spam.webp -------------------------------------------------------------------------------- /static/spam-favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/static/spam-favicon.ico -------------------------------------------------------------------------------- /static/spam.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anujvyas/Spam-SMS-Classifier-Deployment/07b1c1ccdb797cc04eb7e54fd0ebb7033f4da1b3/static/spam.webp -------------------------------------------------------------------------------- /static/styles.css: -------------------------------------------------------------------------------- 1 | html{ 2 | height: 100%; 3 | margin: 0; 4 | } 5 | 6 | body{ 7 | font-family: Arial, Helvetica,sans-serif; 8 | text-align: center; 9 | margin: 0; 10 | padding: 0; 11 | width: 100%; 12 | height: 100%; 13 | display: flex; 14 | flex-direction: column; 15 | } 16 | 17 | /* Website Title */ 18 | .container{ 19 | padding: 30px; 20 | position: relative; 21 | background: linear-gradient(45deg, #ffffff, #ffffff, #f9f9f9, #eeeeee, #e0e4e1, #d7e1ec); 22 | background-size: 500% 500%; 23 | animation: change-gradient 10s ease-in-out infinite; 24 | } 25 | @keyframes change-gradient { 26 | 0%{ 27 | background-position: 0 50%; 28 | } 29 | 50%{ 30 | background-position: 100% 50%; 31 | } 32 | 100%{ 33 | background-position: 0 50%; 34 | } 35 | } 36 | 37 | .container-heading{ 38 | margin: 0; 39 | } 40 | 41 | .container span{ 42 | color: #ff0000; 43 | } 44 | 45 | .description p{ 46 | font-style: italic; 47 | font-size: 14px; 48 | margin: 3px 0 0; 49 | } 50 | 51 | /* Text Area */ 52 | .ml-container{ 53 | margin: 30px 0; 54 | flex: 1 0 auto; 55 | } 56 | 57 | .message-box{ 58 | margin-bottom: 20px; 59 | } 60 | 61 | /* Predict Button */ 62 | .my-cta-button{ 63 | background: #f9f9f9; 64 | border: 2px solid #000000; 65 | border-radius: 1000px; 66 | box-shadow: 3px 3px #8c8c8c; 67 | padding: 10px 36px; 68 | color: #000000; 69 | display: inline-block; 70 | font: italic bold 20px/1 "Calibri", sans-serif; 71 | text-align: center; 72 | } 73 | 74 | .my-cta-button:hover{ 75 | color: #ff0000; 76 | border: 2px solid #ff0000; 77 | } 78 | 79 | .my-cta-button:active{ 80 | box-shadow: 0 0; 81 | } 82 | 83 | 84 | /* Footer */ 85 | .footer{ 86 | font-size: 14px; 87 | padding: 20px; 88 | flex-shrink: 0; 89 | position: relative; 90 | background: linear-gradient(45deg, #ffffff, #ffffff, #f9f9f9, #eeeeee, #e0e4e1, #d7e1ec); 91 | background-size: 500% 500%; 92 | animation: change-gradient 10s ease-in-out infinite; 93 | } 94 | 95 | .contact-icon{ 96 | color: #000000; 97 | padding: 7px; 98 | } 99 | 100 | 101 | .contact-icon:hover{ 102 | color: #8c8c8c; 103 | } 104 | 105 | .footer-description{ 106 | margin: 0; 107 | font-size: 12px; 108 | } 109 | 110 | /* Result */ 111 | .results{ 112 | padding: 30px 0 0; 113 | flex: 1 0 auto; 114 | } 115 | 116 | .danger{ 117 | color: #ff0000; 118 | } 119 | 120 | .safe{ 121 | color: green; 122 | } 123 | 124 | .gif{ 125 | width: 30%; 126 | } 127 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spam SMS Detector 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |

Spam Detector for Short Message Service (SMS)

17 |
18 |

A Machine Learning Web App, Built with Flask, Deployed using Heroku.

19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 | 27 |
28 |
29 | 30 | 31 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /templates/result.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spam SMS Detector 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |

Spam Detector for Short Message Service (SMS)

17 |
18 |

A Machine Learning Web App, Built with Flask, Deployed using Heroku.

19 |
20 |
21 | 22 | 23 |
24 | {% if prediction==1 %} 25 |

Prediction: Gotcha! This is a SPAM message.

26 | SPAM Image 27 | {% elif prediction==0 %} 28 |

Prediction: Great! This is NOT a spam message.

29 | Not a spam image 30 | {% endif %} 31 |
32 | 33 | 34 | 41 | 42 | 43 | 44 | --------------------------------------------------------------------------------