This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockwise.py
95 lines (78 loc) · 2.61 KB
/
lockwise.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
84
85
86
87
88
89
90
91
92
93
94
95
# coding = utf8
from flask import Flask, request, render_template, jsonify
from os import listdir
from os.path import isfile, join
from sklearn.preprocessing import Normalizer
from sklearn.externals import joblib
import numpy as np
import re
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
input_text = request.form.get('content')
if input_text == "":
message = "It can not process empty text."
return render_template('index.html', message = message)
result = predict(input_text)
if result:
message = "This may have some sensitive data."
else:
message = "It didn't detect any sensitive information here."
return render_template('index.html', message = message)
else:
return render_template('index.html')
@app.route('/check', methods=['POST'])
def check():
try:
data = request.get_json()
app.logger.debug(data)
# build_data
text_content = []
if 'subject' in data:
text_content.append("\nsubject\n")
text_content.append(data["subject"])
if 'body' in data:
text_content.append("body\n")
text_content.append(data["body"])
# convert to a string
input_text = ''.join(text_content)
response = { }
if input_text == "":
response["error"] = "Invalid Data!"
return jsonify(data)
# process the data
result = predict(input_text)
if result:
response["message"] = "This may have some sensitive data."
else:
response["message"] = "It didn't detect any sensitive information here."
response["status"] = 'ok'
return jsonify(response)
except:
response = {
'message' : 'Sorry, something went wrong.',
'status' : 'error'
}
return jsonify(message), 400
def predict(input_text):
testString = [input_text]
modesSaved=joblib.load('processed/modes.sav')
vectorizer = modesSaved[0]
lsa = modesSaved[1]
classifier = modesSaved[2]
testMatrix = vectorizer.transform(testString) # string into tfidf matrix
X_test = lsa.transform(testMatrix) # dimensionality reduction
X_test = Normalizer(copy=False).transform(X_test) # normalization
Y_predict = classifier.predict(X_test)
testMatrix = vectorizer.transform(testString) # string into tfidf matrix
X_test = lsa.transform(testMatrix) # dimensionality reduction
X_test = Normalizer(copy=False).transform(X_test) # normalization
Y_predict = classifier.predict(X_test) # predict with KNN method in our case
print(Y_predict[0])
if Y_predict[0] == 0.0:
return True
else:
return False
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')