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 3, 2024
1 parent b16c008 commit 0ad05bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nlp/app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
14 changes: 8 additions & 6 deletions nlp/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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)

0 comments on commit 0ad05bb

Please sign in to comment.