Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CappucciNOPE authored May 2, 2024
1 parent 862bdc8 commit 692d073
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion nlp/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess' # for CSRF

from . import routes
from . import routes

21 changes: 12 additions & 9 deletions nlp/app/routes.py
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)

0 comments on commit 692d073

Please sign in to comment.