|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModel |
|
import torch |
|
from torch import nn |
|
import torch.nn.functional as F |
|
|
|
|
|
|
|
class RobertaModel(nn.Module): |
|
|
|
def __init__(self, n_classes: int = 2): |
|
super().__init__() |
|
self.roberta = AutoModel.from_pretrained("xlm-roberta-base") |
|
self.dropout = nn.Dropout(p=0.3) |
|
self.linear = nn.Linear(self.roberta.config.hidden_size, self.roberta.config.hidden_size) |
|
self.classification = nn.Linear(self.roberta.config.hidden_size, n_classes) |
|
|
|
def forward(self, input_ids, attention_mask): |
|
|
|
cls_output = self.roberta(input_ids=input_ids, attention_mask=attention_mask) |
|
pooled_output = torch.mean(cls_output.last_hidden_state, 1) |
|
|
|
|
|
|
|
pooled_output = self.linear(pooled_output) |
|
pooled_output = F.relu(pooled_output) |
|
pooled_output = self.dropout(pooled_output) |
|
output = self.classification(pooled_output) |
|
|
|
return output |
|
|
|
model = RobertaModel() |
|
|
|
|
|
|
|
model.load_state_dict( |
|
torch.load( |
|
f="Modelo_Amazon_review.pt", |
|
map_location=torch.device("cpu") |
|
) |
|
) |
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained('xlm-roberta-base') |
|
|
|
|
|
|
|
|
|
def predict(review_text): |
|
pred = {} |
|
|
|
encoding_review = tokenizer.encode_plus( |
|
review_text, |
|
max_length = 250, |
|
truncation = True, |
|
add_special_tokens = True, |
|
return_token_type_ids = False, |
|
padding = "max_length", |
|
return_attention_mask = True, |
|
return_tensors = 'pt' |
|
) |
|
|
|
input_ids = encoding_review['input_ids'] |
|
attention_mask = encoding_review['attention_mask'] |
|
output = model(input_ids, attention_mask) |
|
_, prediction = torch.max(output, dim=1) |
|
if prediction == 0: |
|
pred["label"] = "Negativo" |
|
pred["score"] = f"{torch.softmax(output, dim=1)[0][0].item()*100:.2f}%" |
|
else: |
|
pred["label"] = "Positivo" |
|
pred["score"] = f"{torch.softmax(output, dim=1)[0][1].item()*100:.2f}%" |
|
|
|
return pred["label"], pred["score"] |
|
|
|
|
|
|
|
amazon_app = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Textbox(label="Introduce tu reseña aquí:", placeholder="Escribe aquí..."), |
|
outputs=[gr.Label(label="Predicción"), gr.Label(label="Puntaje")], |
|
title="Análisis de sentimientos de reseñas de productos en Español", |
|
description="Ingresa una reseña de algun producto y obtén una predicción sobre si su sentimiento es positivo o negativo. (Max. 250 caracteres)", |
|
theme="Agora", |
|
|
|
|
|
allow_flagging=False, |
|
analytics_enabled=False |
|
) |
|
|
|
|
|
amazon_app.launch() |