-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
77 lines (60 loc) · 1.9 KB
/
request.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
import pickle
import re
import string
from flask import Flask, jsonify, request
from flask import flash, redirect, render_template, request, session, abort
from flask_cors import CORS, cross_origin
punctuation_list = ["।", "”", "“", "’"]
for p in string.punctuation.lstrip():
punctuation_list.append(p)
def prediction(txt):
infile = open("D:/01 Research/Fake News/API/tfidf_char_pkl", 'rb')
tfidf_char = pickle.load(infile)
infile.close()
x = tfidf_char.transform([txt])
# print(x.shape)
infile = open("D:/01 Research/Fake News/API/model", 'rb')
clf = pickle.load(infile)
infile.close()
y_pred = clf.predict(x)
print(y_pred)
return y_pred[0]
def clean(doc):
for p in punctuation_list:
doc = doc.replace(p, "")
doc = re.sub(r'[\u09E6-\u09EF]', "", doc, re.DEBUG) # replace digits
# doc = doc.replace("\n", "")
return doc
# with open("input.txt", 'r') as infile:
# doc = ""
# for line in infile:
# doc = doc + line.replace("\n", "")
# txt = clean(doc)
# prediction(txt)
app = Flask(__name__)
CORS(app)
@app.route("/", methods=['POST'])
def predict():
doc = request.json['data']
#doc = request.form["news"]
print(doc)
txt = clean(doc)
infile = open("D:/01 Research/Fake News/API/tfidf_char_pkl", 'rb')
tfidf_char = pickle.load(infile)
infile.close()
x = tfidf_char.transform([txt])
# print(x.shape)
infile = open("D:/01 Research/Fake News/API/model", 'rb')
clf = pickle.load(infile)
infile.close()
y_pred = clf.predict(x)
output = "True News"
if y_pred == 0:
output = "Fake News"
print(y_pred)
ret = '{"prediction":' + output + '}'
#return render_template('form.html',value= output)
return jsonify(result=output)
# running REST interface, port=5000 for direct test
if __name__ == "__main__":
app.run(debug=False, host='127.0.0.1', port=4000)