Spaces:
Sleeping
Sleeping
bego
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import ftfy
|
4 |
+
import re
|
5 |
+
import torch
|
6 |
+
import numpy as np
|
7 |
+
from transformers import DistilBertTokenizer, DistilBertModel
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
|
10 |
+
def corregir_codificacion(texto):
|
11 |
+
if isinstance(texto, str):
|
12 |
+
return ftfy.fix_text(texto)
|
13 |
+
return texto
|
14 |
+
|
15 |
+
def preprocesar_texto(texto):
|
16 |
+
texto = texto.lower()
|
17 |
+
texto = re.sub(r'\d+', '', texto)
|
18 |
+
texto = re.sub(r'[^\w\s]', '', texto)
|
19 |
+
return texto
|
20 |
+
|
21 |
+
class ClasificadorOpiniones:
|
22 |
+
def __init__(self):
|
23 |
+
model_path = hf_hub_download(repo_id="begoach1/opinion_classifier", filename="modelo_clasificador_reentrenado_lp_ros.pkl")
|
24 |
+
self.clf_combined = pickle.load(open(model_path, 'rb'))
|
25 |
+
self.tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-multilingual-cased')
|
26 |
+
self.model = DistilBertModel.from_pretrained('distilbert-base-multilingual-cased')
|
27 |
+
|
28 |
+
def clasificar_opinion(self, texto):
|
29 |
+
texto = corregir_codificacion(texto)
|
30 |
+
texto = preprocesar_texto(texto)
|
31 |
+
tokens = self.tokenizer(texto, padding=True, truncation=True, return_tensors='pt')
|
32 |
+
with torch.no_grad():
|
33 |
+
outputs = self.model(**tokens)
|
34 |
+
encoded_text = outputs.last_hidden_state[:, 0, :].numpy()
|
35 |
+
prediccion = self.clf_combined.predict(encoded_text)
|
36 |
+
etiquetas = ['queja', 'sugerencia', 'agradecimiento', 'felicitacion', 'ninguna', 'cambio_positivo_personal']
|
37 |
+
resultado = dict(zip(etiquetas, prediccion[0]))
|
38 |
+
return resultado
|
39 |
+
|
40 |
+
def clasificar(texto):
|
41 |
+
clasificador = ClasificadorOpiniones()
|
42 |
+
resultado = clasificador.clasificar_opinion(texto)
|
43 |
+
return resultado
|
44 |
+
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=clasificar,
|
47 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Escribe tu opini贸n aqu铆..."),
|
48 |
+
outputs="json",
|
49 |
+
title="Clasificador de Opiniones Multietiqueta",
|
50 |
+
description="Ingresa un texto de opini贸n para obtener las etiquetas correspondientes."
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch()
|