File size: 2,699 Bytes
fa9917c
c7baec5
fa9917c
 
c500f94
df18eaf
 
c500f94
 
 
 
 
 
 
df18eaf
 
17ee3d5
7b4da08
7fcd0f7
d91019d
 
 
 
17ee3d5
 
b4b2d15
 
 
 
 
 
 
816ad81
e31ea1f
77e17ee
918502d
2d548f2
72ea02e
69eadf1
 
 
 
 
 
 
 
 
 
 
 
45d0f71
 
7c47179
be4ba0f
45d0f71
 
 
ef34b99
45d0f71
 
 
 
5d127b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
import torch
from transformers import pipeline

app_title = "Portuguese Hate Speech Detection"

app_description = """
This app is the culmination of the kNOwHATE consortium project, which aimed to tackle Online Hate Speech in the Portuguese comunity. It serves as an user-friendly interface to classify text and identify instances of Hate Speech.

This app leverages state-of-the-art Natural Language Processing (NLP) models developed in the scope of this project to classify harmful text.

Select a model from the dropdown menu and input your text to see the classification results. Explore the examples of Hate Speech and Non-Hate Speech offered, and join us in fostering a safer and more respectful online community.

For more information about the kNOwHATE project and its initiatives, visit our website [here](https://knowhate.eu).
"""

app_examples = [
    ["As pessoas tem que perceber que ser 'panasca' não é deixar de ser homem, é deixar de ser humano 😂😂", "knowhate/HateBERTimbau-youtube"],
    ["Vamo-nos unir para criar um mundo mais inclusivo e tolerante.", "knowhate/HateBERTimbau-twitter"],
    ["Isso pulhiticos merdosos, continuem a importar lixo, até Portugal deixar de ser Portugal.", "knowhate/HateBERTimbau-yt-tt"],
    ["Eu admiro muito a coragem e a determinação da minha colega de trabalho.", "knowhate/HateBERTimbau"],
    ["Vai pá puta que te pariu seu paneleiro do caralho, virgem ofendida", "knowhate/HateBERTimbau-youtube"],
    ["O tempo está ensolarado hoje, perfeito para um passeio no parque.", "knowhate/HateBERTimbau-twitter"]
]

model_list = [
    "knowhate/HateBERTimbau",
    "knowhate/HateBERTimbau-youtube",
    "knowhate/HateBERTimbau-twitter",
    "knowhate/HateBERTimbau-yt-tt",
]

def predict(text, chosen_model):    
    
    # Initialize the pipeline with the chosen model
    model_pipeline = pipeline("text-classification", model=chosen_model)
    result = model_pipeline(text)
    
    predicted_label = result[0]['label']
    predicted_score = result[0]['score']

    non_predicted_label = "Hate Speech" if predicted_label == "Non Hate Speech" else "Non Hate Speech"
    non_predicted_score = 1 - predicted_score

    scores_dict = {
        predicted_label: predicted_score,
        non_predicted_label: non_predicted_score
    }
    
    return scores_dict#, predicted_label

inputs = [
    gr.Textbox(label="Text", value= app_examples[0][0]),
    gr.Dropdown(label="Model", choices=model_list, value=model_list[2])
]

outputs = [
    gr.Label(label="Result"),
]


gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title, 
             description=app_description, examples=app_examples).launch()