-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (52 loc) · 1.62 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from flask import Flask
from flask import render_template
from flask import request
import pickle
import string
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
app = Flask(__name__)
# Ensure that the necessary NLTK resources are downloaded
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('punkt_tab')
# Load the pre-trained model and vectorizer
tfidf = pickle.load(open('./models/vectorizer.pkl', 'rb'))
model = pickle.load(open('./models/model.pkl', 'rb'))
ps = PorterStemmer()
# Preprocess the text
def transform_text(text):
text = text.lower()
text = nltk.word_tokenize(text)
y = []
for i in text:
if i.isalnum():
y.append(i)
text = y[:]
y.clear()
for i in text:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
text = y[:]
y.clear()
for i in text:
y.append(ps.stem(i))
return " ".join(y)
@app.route('/', methods=['GET', 'POST'])
def index():
result = None
input_sms = ""
if request.method == 'POST':
input_sms = request.form['message']
# 1. Preprocess the text
transformed_sms = transform_text(input_sms)
# 2. Vectorize the text
vector_input = tfidf.transform([transformed_sms])
# 3. Predict the result
result = model.predict(vector_input)[0]
# 4. Display the result
result = "Spam" if result == 1 else "Not Spam"
return render_template('index.html', result=result, input_sms=input_sms)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)