Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_name = "nmarinnn/bert-bregman"
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def predict(text):
|
| 10 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 11 |
+
with torch.no_grad():
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
|
| 14 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 15 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
| 16 |
+
|
| 17 |
+
class_labels = {0: "negativo", 1: "neutro", 2: "positivo"}
|
| 18 |
+
predicted_label = class_labels[predicted_class]
|
| 19 |
+
predicted_probability = probabilities[0][predicted_class].item()
|
| 20 |
+
|
| 21 |
+
result = f"Clase predicha: {predicted_label} (probabilidad = {predicted_probability:.2f})\n"
|
| 22 |
+
result += f"Probabilidades: Negativo: {probabilities[0][0]:.2f}, Neutro: {probabilities[0][1]:.2f}, Positivo: {probabilities[0][2]:.2f}"
|
| 23 |
+
|
| 24 |
+
return result
|
| 25 |
+
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=predict,
|
| 28 |
+
inputs=gr.Textbox(lines=2, placeholder="Ingrese el texto aquí..."),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Clasificador de Sentimientos",
|
| 31 |
+
description="Este modelo clasifica el sentimiento del texto como negativo, neutro o positivo."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
iface.launch()
|