Upload 6 files
Browse files- main.py +43 -0
- max_len.pkl +3 -0
- spam.csv +0 -0
- spam_alert.pkl +3 -0
- spam_detection_model.h5 +3 -0
- tokenizer.pkl +3 -0
main.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.metrics import confusion_matrix
|
| 4 |
+
from flask import Flask, request, render_template
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
+
from tensorflow.keras.models import Sequential
|
| 9 |
+
from tensorflow.keras.layers import Embedding, Flatten, Dense
|
| 10 |
+
import pickle
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Load the pre-trained model and artifacts
|
| 15 |
+
loaded_model = keras.models.load_model("spam_detection_model.h5")
|
| 16 |
+
with open("tokenizer.pkl", "rb") as f:
|
| 17 |
+
loaded_tokenizer = pickle.load(f)
|
| 18 |
+
with open("max_len.pkl", "rb") as f:
|
| 19 |
+
loaded_max_len = pickle.load(f)
|
| 20 |
+
|
| 21 |
+
# Function to predict ham or spam using the pre-trained model
|
| 22 |
+
def predict_message(message, model, tokenizer, max_len):
|
| 23 |
+
# Preprocess the message
|
| 24 |
+
sequence = tokenizer.texts_to_sequences([message])
|
| 25 |
+
padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post')
|
| 26 |
+
# Predict probabilities
|
| 27 |
+
probabilities = model.predict(padded_sequence)
|
| 28 |
+
# Convert probabilities to class labels
|
| 29 |
+
prediction = "Ham" if probabilities[0] < 0.5 else "Spam"
|
| 30 |
+
return prediction
|
| 31 |
+
|
| 32 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 33 |
+
def home():
|
| 34 |
+
if request.method == 'POST':
|
| 35 |
+
# Get the message from the form input
|
| 36 |
+
message = request.form['message']
|
| 37 |
+
# Predict using the pre-trained model
|
| 38 |
+
prediction = predict_message(message, loaded_model, loaded_tokenizer, loaded_max_len)
|
| 39 |
+
return render_template('index.html', prediction=prediction)
|
| 40 |
+
return render_template('index.html')
|
| 41 |
+
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
app.run(debug=True)
|
max_len.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:41da2fb4c152221cdaeec24a77d948d12cd6bd54cdd6433fc565bc5c2877ba77
|
| 3 |
+
size 5
|
spam.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
spam_alert.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c87ab31f58ef5b221d95c6f3d037857307fc7fb971d1d72a2876a140e6dae24f
|
| 3 |
+
size 16163741
|
spam_detection_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d8364f593d378f8a4b90b703ce3f5318fc81310bdae1d1c120464dad12b95096
|
| 3 |
+
size 16169896
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bd6500f295b774cfb0e8848dc6a3b0fe968344237ae1f532b3ea6f9637cee658
|
| 3 |
+
size 347210
|