File size: 1,336 Bytes
d2c3827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

model_name = "nmarinnn/bert-bregman"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

def predict(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    predicted_class = torch.argmax(probabilities, dim=-1).item()
    
    class_labels = {0: "negativo", 1: "neutro", 2: "positivo"}
    predicted_label = class_labels[predicted_class]
    predicted_probability = probabilities[0][predicted_class].item()
    
    result = f"Clase predicha: {predicted_label} (probabilidad = {predicted_probability:.2f})\n"
    result += f"Probabilidades: Negativo: {probabilities[0][0]:.2f}, Neutro: {probabilities[0][1]:.2f}, Positivo: {probabilities[0][2]:.2f}"
    
    return result

iface = gr.Interface(
    fn=predict, 
    inputs=gr.Textbox(lines=2, placeholder="Ingrese el texto aquí..."),
    outputs="text",
    title="Clasificador de Sentimientos",
    description="Este modelo clasifica el sentimiento del texto como negativo, neutro o positivo."
)

iface.launch()