bert-bregman / app.py
nmarinnn's picture
Create app.py
9e5cede verified
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()