-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
59 lines (51 loc) · 2.03 KB
/
main.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import joblib
import numpy as np
from flask import Flask, abort, redirect, render_template, request
app = Flask(__name__)
l = [{'title':'ankhiq yg', 't2': 'resd'},{'title':'ankhiq pjhhh', 't2': 'res1'},{'title':'ankit k na', 't2': 'res2'},]
class Sonar(object):
def __init__(self):
BASE_DIR = os.path.join(os.path.dirname(__file__), './data')
model_file = os.path.join(BASE_DIR, 'model.joblib')
preprocessor_file = os.path.join(BASE_DIR, 'preprocess.joblib')
self.estimator = joblib.load(model_file)
self.preprocessor = joblib.load(preprocessor_file)
def ping(self, text):
assert isinstance(text, str)
vector = self.preprocessor.transform([text])
proba = self.estimator.predict_proba(vector)[0]
mapping = {0: True, 1: True, 2: False}
res = {
'text': text,
'state': mapping[np.argmax(proba)],
}
return res
def get_weights(self, text):
def get_class_idx():
res = self.ping(text)
for i, class_ in enumerate(res['classes']):
if class_['class_name'] == res['top_class']:
return i
class_idx = get_class_idx()
features = self.preprocessor.get_feature_names()
weights = self.estimator.coef_[class_idx]
word2weight = {f: w for f, w in zip(features, weights)}
tokenize = self.preprocessor.build_analyzer()
words = tokenize(text)
return {w: word2weight.get(w, 0) for w in words}
Profanity_check_object = Sonar()
@app.route('/')
def home():
# print(Profanity_check_object.ping(request.args.get('word')))
print(Profanity_check_object.ping('fuck'))
return render_template('home.html', posts = l)
@app.route('/WordCheck', methods = ['POST', 'GET'])
def check():
string = request.form.get('text')
print(string)
return {'response':Profanity_check_object.ping(string)}
app.run(debug=True)