From 0ad05bb8246722f7a28666538d7037bc26fddf90 Mon Sep 17 00:00:00 2001 From: Merrilee Montgomery <109876041+CappucciNOPE@users.noreply.github.com> Date: Fri, 3 May 2024 01:54:33 -0500 Subject: [PATCH] Add files via upload --- nlp/app/forms.py | 2 +- nlp/app/routes.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/nlp/app/forms.py b/nlp/app/forms.py index 5395597..d24e357 100644 --- a/nlp/app/forms.py +++ b/nlp/app/forms.py @@ -6,7 +6,7 @@ class MyForm(FlaskForm): class Meta: # Ignoring CSRF security feature. csrf = False - input_field = StringField(label='input headline:', id='input_field', + input_field = StringField(label='input text:', id='input_field', validators=[DataRequired()], render_kw={'style': 'width:50%'}) submit = SubmitField('Submit') \ No newline at end of file diff --git a/nlp/app/routes.py b/nlp/app/routes.py index 9d54e5a..6dffe8a 100644 --- a/nlp/app/routes.py +++ b/nlp/app/routes.py @@ -3,14 +3,16 @@ from . import app from .forms import MyForm from .. import clf_path +from ..cli import clf import torch import pickle import sys +import torch -clf = pickle.load(open(clf_path, 'rb')) +clf.load_state_dict(torch.load(clf_path)) print('read clf %s' % str(clf)) #print('read vec %s' % str(vec)) -labels = ['not_hate', 'implicit_hate','explicit_hate'] +labels = ['implicit_hate','not_hate','explicit_hate'] sbert384 = SentenceTransformer("all-MiniLM-L6-v2") ##@app.route('/index', methods=['GET', 'POST']) @@ -20,12 +22,12 @@ def index(): result = None if form.validate_on_submit(): input_field = form.input_field.data - X = sbert384.encode(input_field) + X = torch.tensor(sbert384.encode(input_field),dtype=torch.float64) proba = clf.forward(X) - print("Probability Not Hate: "+proba[0]+"\nProbability Implicit Hate: "+proba[1]+"\nProbability Explicit Hate: "+proba[2]) - proba = torch.argmin(proba) + print("Probability Not Hate: "+str(proba[0].item())+"\nProbability Implicit Hate: "+str(proba[1].item())+"\nProbability Explicit Hate: "+str(proba[2].item())) + pred = torch.argmax(proba) # flash(input_field) return render_template('myform.html', title='', form=form, - prediction="Most Likely: "+labels[pred], confidence='%.2f' % proba) + prediction=labels[pred], confidence='%.2f' % proba[pred]) #return redirect('/index') return render_template('myform.html', title='', form=form, prediction=None, confidence=None)