File size: 1,483 Bytes
649e10c
 
 
 
 
 
bff7a05
649e10c
 
 
 
 
 
 
 
 
 
 
bff7a05
649e10c
 
 
 
 
 
 
 
 
 
 
 
 
 
4479a0e
 
 
 
 
 
 
 
 
 
649e10c
796398a
649e10c
 
 
 
 
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
"""
Streamlit for classification using transformers
"""
# Import streamlit
import streamlit as st
import torch
from pysentimiento.preprocessing import preprocess_tweet
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_name = "piubamas/beto-contextualized-hate-speech"
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Horrible pero efectivo
id2label = [model.config.id2label[k] for k in range(len(model.config.id2label))]

def predict(*args):
    args = [preprocess_tweet(arg) for arg in args]
    encoding = tokenizer.encode_plus(*args)

    inputs = {
        k:torch.LongTensor(encoding[k]).reshape(1, -1) for k in {"input_ids", "attention_mask", "token_type_ids"}
    }

    output = model.forward(
        **inputs
    )

    chars = list(zip(id2label, list(output.logits[0].detach().cpu().numpy() > 0)))

    return [char for char, pred in chars if pred]



# Create a title
st.title("Detección de discurso de odio en medios")

# Create an input box for context
context = st.text_input("Contexto", "China prohíbe la cría de perros para consumo humano")
# Create an input box for text
text = st.text_input("Comentario", "Chinos hdrmp hay que matarlos a todos")

# Create a button to classify
if st.button("Predict"):
    # Classify the text
    prediction = predict(text, context)
    # Print the classification
    st.write(prediction)