diff --git a/nlp/app/__init__.py b/nlp/app/__init__.py index 8c02e85..b212875 100644 --- a/nlp/app/__init__.py +++ b/nlp/app/__init__.py @@ -4,4 +4,5 @@ app = Flask(__name__) app.config['SECRET_KEY'] = 'you-will-never-guess' # for CSRF -from . import routes \ No newline at end of file +from . import routes + diff --git a/nlp/app/routes.py b/nlp/app/routes.py index ec4db3f..9d54e5a 100644 --- a/nlp/app/routes.py +++ b/nlp/app/routes.py @@ -1,28 +1,31 @@ from flask import render_template, flash, redirect, session +from sentence_transformers import SentenceTransformer from . import app from .forms import MyForm from .. import clf_path - +import torch import pickle import sys -clf, vec = pickle.load(open(clf_path, 'rb')) +clf = pickle.load(open(clf_path, 'rb')) print('read clf %s' % str(clf)) -print('read vec %s' % str(vec)) -labels = ['liberal', 'conservative'] +#print('read vec %s' % str(vec)) +labels = ['not_hate', 'implicit_hate','explicit_hate'] +sbert384 = SentenceTransformer("all-MiniLM-L6-v2") +##@app.route('/index', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST']) -@app.route('/index', methods=['GET', 'POST']) def index(): form = MyForm() result = None if form.validate_on_submit(): input_field = form.input_field.data - X = vec.transform([input_field]) - pred = clf.predict(X)[0] - proba = clf.predict_proba(X)[0].max() + X = sbert384.encode(input_field) + proba = clf.forward(X) + print("Probability Not Hate: "+proba[0]+"\nProbability Implicit Hate: "+proba[1]+"\nProbability Explicit Hate: "+proba[2]) + proba = torch.argmin(proba) # flash(input_field) return render_template('myform.html', title='', form=form, - prediction=labels[pred], confidence='%.2f' % proba) + prediction="Most Likely: "+labels[pred], confidence='%.2f' % proba) #return redirect('/index') return render_template('myform.html', title='', form=form, prediction=None, confidence=None)