generated from tulane-cmps6730/sample-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
862bdc8
commit 692d073
Showing
2 changed files
with
14 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |