-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
91 lines (68 loc) · 2.06 KB
/
server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Import libraries
from flask import Flask, request, jsonify, render_template
#import get_data
import joblib
import spacy
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from spacy.lang.en.stop_words import STOP_WORDS
from spacy.symbols import punct
from sklearn.pipeline import Pipeline
#Tokenization
import string
punct = string.punctuation
print(punct)
nlp = spacy.load("en")
stopwords = list(STOP_WORDS)
def text_data_cleaning(sentence):
doc = nlp(sentence)
tokens = []
for token in doc:
if token.lemma_ != '-PRON-':
temp = token.lemma_.lower().strip()
else:
temp = token.lower_
tokens.append(temp)
cleaned_tokens = []
for token in tokens:
if token not in list(STOP_WORDS) and token not in punct:
cleaned_tokens.append(token)
return cleaned_tokens
def predictdata(text):
tfidf = TfidfVectorizer(tokenizer=text_data_cleaning)
joblib_LR_model = joblib.load('news_classifier.pkl')
print(joblib_LR_model)
pred = joblib_LR_model.predict([text])
print(pred)
return pred
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
# Load the model
#model = pickle.load(open('news_classifier.pkl','rb'))
@app.route('/api',methods=['POST'])
def predict():
# Get the data from the POST request.
data = request.get_json(force=True)
print(data)
if data['headline'] != "":
output = predictdata(data['headline'][0])
print("output = " + output[0])
return jsonify(output[0])
else:
output = 'Input cannot pe empty!!'
return jsonify(output)
@app.route('/form',methods=['POST'])
def form_predict():
# Get the data from the POST request.
data = request.form
print("data = ",data['headline'])
if data['headline'] != "":
output = predictdata(data['headline'][0])
return jsonify(output[0])
else:
output = 'Input cannot pe empty!!'
return jsonify(output)
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000, debug=True)