Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app_title = "Portuguese Counter Hate Speech Detection (NFAA)"
|
6 |
|
@@ -11,32 +15,38 @@ Select a model from the dropdown menu and input your text to see the classificat
|
|
11 |
For more information about the kNOwHATE project and its initiatives, visit our website [here](https://knowhate.eu) and to explore and use these models visit our Hugging Face page [here](https://huggingface.co/knowhate).
|
12 |
"""
|
13 |
|
|
|
14 |
app_examples = [
|
15 |
-
["
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
]
|
18 |
|
19 |
model_list = [
|
20 |
-
"knowhate/twt-bertimbau"
|
21 |
-
"knowhate/twt-xlm-base",
|
22 |
]
|
23 |
|
24 |
def predict(text, target, chosen_model):
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
model_pipeline = pipeline("text-classification", model=chosen_model)
|
28 |
-
result = model_pipeline(text)
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
non_predicted_label = "Hate Speech" if predicted_label == "Non Hate Speech" else "Non Hate Speech"
|
35 |
-
non_predicted_score = 1 - predicted_score
|
36 |
-
|
37 |
scores_dict = {
|
38 |
-
|
39 |
-
|
|
|
40 |
}
|
41 |
|
42 |
return scores_dict
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from keras.models import load_model
|
4 |
+
from transformers import TFBertModel, TFXLMRobertaModel
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
+
from transformers import AutoTokenizer
|
8 |
|
9 |
app_title = "Portuguese Counter Hate Speech Detection (NFAA)"
|
10 |
|
|
|
15 |
For more information about the kNOwHATE project and its initiatives, visit our website [here](https://knowhate.eu) and to explore and use these models visit our Hugging Face page [here](https://huggingface.co/knowhate).
|
16 |
"""
|
17 |
|
18 |
+
# 1 0 2
|
19 |
app_examples = [
|
20 |
+
["Essa gente tem é de deixar de ser apaparicada pelo Estado e começar a cumprir os seus deveres como cidadãos",
|
21 |
+
"Nepia o que faz com que as pessoas generalizem é o ódio intrínseco que têm contra uma etnia, ng é responsável pela sua xenofobia",
|
22 |
+
"knowhate/twt-bertimbau/twt-bb-b16e5-avg767.keras"],
|
23 |
+
["Nem vou comentar o hate e misoginia que tenho visto aqui no tt em relação à Anitta",
|
24 |
+
"E xenofobia também. Tugas no seu melhor",
|
25 |
+
"knowhate/twt-bertimbau/twt-bb-b16e5-avg767.keras"],
|
26 |
+
["A Festa tá no Climax, chama o zuca pra Dançar.",
|
27 |
+
"Já reparaste no contador da luz? Vai trabalhar malandro",
|
28 |
+
"knowhate/twt-bertimbau/twt-bb-b16e5-avg767.keras"]
|
29 |
]
|
30 |
|
31 |
model_list = [
|
32 |
+
"knowhate/twt-bertimbau/twt-bb-b16e5-avg767.keras"
|
|
|
33 |
]
|
34 |
|
35 |
def predict(text, target, chosen_model):
|
36 |
+
model1 = load_model(chosen_model, custom_objects={"TFBertModel": TFBertModel})
|
37 |
+
checkpoint = "neuralmind/bert-base-portuguese-cased"
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint, use_fast=True)
|
39 |
+
tokpair = tokenizer(text, target, truncation=True, padding='max_length', return_tensors='np')
|
40 |
|
41 |
+
outp = model1(tokpair)
|
|
|
|
|
|
|
42 |
|
43 |
+
proto_tensor = tf.make_tensor_proto(outp)
|
44 |
+
allscores = tf.make_ndarray(proto_tensor)[0]
|
45 |
+
|
|
|
|
|
|
|
46 |
scores_dict = {
|
47 |
+
'Neutral': allscores[0],
|
48 |
+
'Counter Speech': allscores[1],
|
49 |
+
'Hate Speech': allscores[2]
|
50 |
}
|
51 |
|
52 |
return scores_dict
|