-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (41 loc) · 1.24 KB
/
Copy pathapp.py
File metadata and controls
59 lines (41 loc) · 1.24 KB
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
from flask import Flask, request, jsonify
import pickle
import nltk
import string
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
app = Flask(__name__)
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
nltk.download('punkt')
nltk.download('stopwords')
ps = PorterStemmer()
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.json['data']
preprocessed_data = transform_text(data)
vectorized_data = vectorizer.transform([preprocessed_data])
prediction = model.predict(vectorized_data)
return jsonify({'prediction': int(prediction[0])})
except Exception as e:
return jsonify({'error': str(e)})
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)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)