-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (64 loc) · 2.12 KB
/
app.py
File metadata and controls
71 lines (64 loc) · 2.12 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
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, request, jsonify
from flask_cors import CORS
import pandas as pd
import re
import string
import bz2file as bz2
import joblib as jb
with bz2.BZ2File('wv.joblib.bz2', 'rb') as f:
td = jb.load(f)
with bz2.BZ2File('md.joblib.bz2', 'rb') as f:
mp = jb.load(f)
def clean_text(text):
text = text.lower()
text = re.sub(r"i'm", "i am", text)
text = re.sub(r"\r", "", text)
text = re.sub(r"he's", "he is", text)
text = re.sub(r"she's", "she is", text)
text = re.sub(r"it's", "it is", text)
text = re.sub(r"that's", "that is", text)
text = re.sub(r"what's", "that is", text)
text = re.sub(r"where's", "where is", text)
text = re.sub(r"how's", "how is", text)
text = re.sub(r"\'ll", " will", text)
text = re.sub(r"\'ve", " have", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"\'d", " would", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"won't", "will not", text)
text = re.sub(r"can't", "cannot", text)
text = re.sub(r"n't", " not", text)
text = re.sub(r"n'", "ng", text)
text = re.sub(r"'bout", "about", text)
text = re.sub(r"'til", "until", text)
text = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", text)
text = text.translate(str.maketrans('', '', string.punctuation))
text = re.sub("(\\W)"," ",text)
text = re.sub('\S*\d\S*\s*','', text)
return text
def make_test_predictions(df):
df.comment_text = df.comment_text.apply(clean_text)
X_test = df.comment_text
X_test_transformed = td.transform(X_test)
y_test_pred = mp.predict_proba(X_test_transformed)
result = sum(y_test_pred[0])
if result >=1 :
return 1
else :
return 0
app = Flask(__name__)
CORS(app)
@app.route("/media/text", methods=['POST'])
def sanitize():
val = request.get_json()
val = val['comment']
comment_text = val
comment ={'comment_text':[comment_text]}
comment = pd.DataFrame(comment)
result = make_test_predictions(comment)
if(result==0):
return(jsonify({"msg": 1}))
else:
return(jsonify({"msg": 0}))
if __name__ == '__main__':
app.run()