File size: 1,620 Bytes
5d9e296
 
 
 
e696513
5d9e296
 
 
93f096d
 
 
 
 
 
 
 
6a662e5
 
 
 
 
 
 
 
 
5d9e296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e31f266
5d9e296
 
 
 
 
 
93f096d
8755ab9
6a662e5
8755ab9
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Cargar el modelo y el tokenizador
model_name = "dagomem/modelo_tweets_2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Mapeo de etiquetas
label_map = {
    0: "enfado",
    1: "alegría",
    2: "optimismo",
    3: "tristeza"
}

# Ejemplos predefinidos
examples = [
    ["@user Interesting choice of words... Are you confirming that governments fund #terrorism? Bit of an open door, but still...", "enfado"],
    ["love to see them interacting 😸🙏 #itsbeensolong #laughing", "alegría"],
    ["Life is too short to hide your feelings. Don't be afraid to say what you feel.", "optimismo"],
    ["#GameOfThones how can you top that next week #heartbreaking", "tristeza"]
]


# Función de predicción
def predict_tags(text):
    # Tokenizar el texto de entrada
    encoded_input = tokenizer.encode_plus(
        text,
        padding="longest",
        truncation=True,
        return_tensors="pt"
    )

    # Realizar la predicción
    output = model(**encoded_input)

    # Obtener las etiquetas predichas
    predicted_label = output.logits.argmax(dim=1)

    return label_map[predicted_label.item()]

# Interfaz de usuario
gr.Interface(
    fn=predict_tags,
    inputs=gr.inputs.Textbox(lines=3, label="Texto"),
    outputs="text",
    title="Análisis de emociones de tweets (en inglés)",
    description="Escribe un tweet en inglés y el modelo te dirá qué emoción transmite.",
    examples=examples
    ).launch(share=False)