bego
commited on
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- opinion-classifier
|
| 4 |
+
- text-classification
|
| 5 |
+
- transformers
|
| 6 |
+
- gradio
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
# Clasificador de Opiniones Multietiqueta
|
| 10 |
+
|
| 11 |
+
Este m贸dulo utiliza modelos de Transformers para preprocesar y clasificar opiniones en m煤ltiples etiquetas como queja, sugerencia, agradecimiento, felicitaci贸n, ninguna y cambio positivo personal.
|
| 12 |
+
|
| 13 |
+
## Descripci贸n
|
| 14 |
+
|
| 15 |
+
Explica brevemente qu茅 hace tu modelo, los datos en los que fue entrenado, y cualquier otra informaci贸n relevante.
|
| 16 |
+
|
| 17 |
+
## C贸mo usar
|
| 18 |
+
|
| 19 |
+
Aqu铆 hay un ejemplo de c贸mo cargar y usar el modelo:
|
| 20 |
+
|
| 21 |
+
```python
|
| 22 |
+
import pickle
|
| 23 |
+
from huggingface_hub import hf_hub_download
|
| 24 |
+
import ftfy
|
| 25 |
+
import re
|
| 26 |
+
import torch
|
| 27 |
+
import numpy as np
|
| 28 |
+
from transformers import DistilBertTokenizer, DistilBertModel
|
| 29 |
+
|
| 30 |
+
def corregir_codificacion(texto):
|
| 31 |
+
if isinstance(texto, str):
|
| 32 |
+
return ftfy.fix_text(texto)
|
| 33 |
+
return texto
|
| 34 |
+
|
| 35 |
+
def preprocesar_texto(texto):
|
| 36 |
+
texto = texto.lower()
|
| 37 |
+
texto = re.sub(r'\d+', '', texto)
|
| 38 |
+
texto = re.sub(r'[^\w\s]', '', texto)
|
| 39 |
+
return texto
|
| 40 |
+
|
| 41 |
+
class ClasificadorOpiniones:
|
| 42 |
+
def __init__(self):
|
| 43 |
+
model_path = hf_hub_download(repo_id="begoach1/opinion_classifier", filename="modelo_clasificador_reentrenado_lp_ros.pkl")
|
| 44 |
+
self.clf_combined = pickle.load(open(model_path, 'rb'))
|
| 45 |
+
self.tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-multilingual-cased')
|
| 46 |
+
self.model = DistilBertModel.from_pretrained('distilbert-base-multilingual-cased')
|
| 47 |
+
|
| 48 |
+
def clasificar_opinion(self, texto):
|
| 49 |
+
texto = corregir_codificacion(texto)
|
| 50 |
+
texto = preprocesar_texto(texto)
|
| 51 |
+
tokens = self.tokenizer(texto, padding=True, truncation=True, return_tensors='pt')
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = self.model(**tokens)
|
| 54 |
+
encoded_text = outputs.last_hidden_state[:, 0, :].numpy()
|
| 55 |
+
prediccion = self.clf_combined.predict(encoded_text)
|
| 56 |
+
etiquetas = ['queja', 'sugerencia', 'agradecimiento', 'felicitacion', 'ninguna', 'cambio_positivo_personal']
|
| 57 |
+
resultado = dict(zip(etiquetas, prediccion[0]))
|
| 58 |
+
return resultado
|
| 59 |
+
|
| 60 |
+
clasificador = ClasificadorOpiniones()
|
| 61 |
+
texto = "me gust贸 mucho, tengo m谩s confianza en m铆 misma, 隆gracias!"
|
| 62 |
+
print(clasificador.clasificar_opinion(texto))
|