Spaces:
Runtime error
Runtime error
adding objective and fixing punctuations
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from nltk import word_tokenize
|
|
7 |
from nltk.util import ngrams
|
8 |
from unidecode import unidecode
|
9 |
nltk.download('punkt')
|
|
|
10 |
|
11 |
# leemos diccionario de entidades
|
12 |
diccionario = pd.read_excel('diccionario.xlsx')
|
@@ -18,14 +19,30 @@ entities_dict = {}
|
|
18 |
for i in all_dicts:
|
19 |
entities_dict.update(i)
|
20 |
|
21 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
diccionario = entities_dict.copy()
|
24 |
tokens = word_tokenize(text, language = 'spanish')
|
25 |
-
tokens_lower = [unidecode(token.lower()) for token in tokens] # tokens en minuscula
|
|
|
26 |
|
27 |
dict_tokens = {tokens_lower[i]: tokens[i] for i in range(len(tokens))}
|
28 |
-
dict_keys = {unidecode(key.lower()): key for key in diccionario.keys()}
|
|
|
29 |
|
30 |
# presencia de ngrams
|
31 |
ngram_range = 5 # rango de ngramas a evaluar
|
@@ -39,7 +56,7 @@ def predict(text):
|
|
39 |
grams_detected.update({nmin: intersection})
|
40 |
|
41 |
sep = '%$·'
|
42 |
-
tmp_text =
|
43 |
for i in range(5, 1, -1):
|
44 |
try:
|
45 |
# obtener todos los ngramas de nivel "i"
|
@@ -69,6 +86,10 @@ def predict(text):
|
|
69 |
labeled_tokens.append((token, None))
|
70 |
|
71 |
|
|
|
|
|
|
|
|
|
72 |
# SERNAC CLASSIFICATION
|
73 |
|
74 |
with open('sernac_model.pkl', 'rb') as model:
|
@@ -76,36 +97,70 @@ def predict(text):
|
|
76 |
|
77 |
labels = [label for label in clf.classes_]
|
78 |
|
79 |
-
|
|
|
80 |
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
# SERNAC CATEGORIES CLASSIFICATION
|
85 |
|
86 |
-
|
87 |
-
clf = pickle.load(model)
|
88 |
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
92 |
|
93 |
-
|
94 |
|
95 |
-
|
|
|
96 |
|
97 |
|
98 |
# DEMO
|
99 |
demo = gr.Interface(
|
100 |
predict,
|
101 |
-
inputs = gr.Textbox(placeholder = "Ingresa el
|
102 |
-
outputs = [gr.Highlightedtext(label = '
|
|
|
|
|
|
|
|
|
103 |
examples=[
|
104 |
-
['este septiembre iremos manejando a
|
105 |
-
['no puedo, tengo que irme desde san pedro hasta la reina y luego hasta san pedro de la paz'],
|
106 |
-
['Buenas tardes, hace unas semanas compre un suzuki swift a derco de santiago, llevaba 2 semanas y la caja de cambios se echó a perder. Tengo asegurado el auto con BCI, pero aun no obtengo respuesta. ']
|
|
|
|
|
107 |
],
|
108 |
-
title = '
|
109 |
)
|
110 |
|
111 |
demo.launch()
|
|
|
7 |
from nltk.util import ngrams
|
8 |
from unidecode import unidecode
|
9 |
nltk.download('punkt')
|
10 |
+
import re
|
11 |
|
12 |
# leemos diccionario de entidades
|
13 |
diccionario = pd.read_excel('diccionario.xlsx')
|
|
|
19 |
for i in all_dicts:
|
20 |
entities_dict.update(i)
|
21 |
|
22 |
+
def f_remove_accents(old: str):
|
23 |
+
|
24 |
+
'''
|
25 |
+
Función que limpia acentos de las letras.
|
26 |
+
old: texto a limpiar (str)
|
27 |
+
'''
|
28 |
+
|
29 |
+
new = re.sub(r'[àáâãäå]', 'a', old)
|
30 |
+
new = re.sub(r'[èéêë]', 'e', new)
|
31 |
+
new = re.sub(r'[ìíîï]', 'i', new)
|
32 |
+
new = re.sub(r'[òóôõö]', 'o', new)
|
33 |
+
new = re.sub(r'[ùúûü]', 'u', new)
|
34 |
+
return new
|
35 |
+
|
36 |
+
def predict(text: str, goal = ''):
|
37 |
|
38 |
diccionario = entities_dict.copy()
|
39 |
tokens = word_tokenize(text, language = 'spanish')
|
40 |
+
#tokens_lower = [unidecode(token.lower()) for token in tokens] # tokens en minuscula
|
41 |
+
tokens_lower = [f_remove_accents(token.lower()) for token in tokens] # tokens en minuscula
|
42 |
|
43 |
dict_tokens = {tokens_lower[i]: tokens[i] for i in range(len(tokens))}
|
44 |
+
#dict_keys = {unidecode(key.lower()): key for key in diccionario.keys()}
|
45 |
+
dict_keys = {f_remove_accents(key.lower()): key for key in diccionario.keys()}
|
46 |
|
47 |
# presencia de ngrams
|
48 |
ngram_range = 5 # rango de ngramas a evaluar
|
|
|
56 |
grams_detected.update({nmin: intersection})
|
57 |
|
58 |
sep = '%$·'
|
59 |
+
tmp_text = ' '.join(tokens_lower)
|
60 |
for i in range(5, 1, -1):
|
61 |
try:
|
62 |
# obtener todos los ngramas de nivel "i"
|
|
|
86 |
labeled_tokens.append((token, None))
|
87 |
|
88 |
|
89 |
+
# CLASSIFICATION
|
90 |
+
|
91 |
+
input = np.array([text, goal], ndmin = 2)
|
92 |
+
|
93 |
# SERNAC CLASSIFICATION
|
94 |
|
95 |
with open('sernac_model.pkl', 'rb') as model:
|
|
|
97 |
|
98 |
labels = [label for label in clf.classes_]
|
99 |
|
100 |
+
probas = clf.predict_proba(input)
|
101 |
+
sernac_probas = {labels[i]: float(probas[0][i]) for i in range(probas.shape[1])}
|
102 |
|
103 |
+
sernac_categories, other_categories = {}, {}
|
104 |
|
105 |
+
if clf.predict(input) == 'SERNAC':
|
|
|
106 |
|
107 |
+
# SERNAC CATEGORIES CLASSIFICATION
|
|
|
108 |
|
109 |
+
with open('sernac_categories_model.pkl', 'rb') as model:
|
110 |
+
clf = pickle.load(model)
|
111 |
+
|
112 |
+
labels = [label for label in clf.classes_]
|
113 |
+
|
114 |
+
probas = clf.predict_proba(input)
|
115 |
+
|
116 |
+
sernac_categories = {labels[i]: float(probas[0][i]) for i in range(probas.shape[1])}
|
117 |
+
|
118 |
+
else:
|
119 |
+
|
120 |
+
# OTHER CATEGORIES CLASSIFICATION
|
121 |
+
|
122 |
+
with open('other_categories_model.pkl', 'rb') as model:
|
123 |
+
clf = pickle.load(model)
|
124 |
+
|
125 |
+
labels = [label for label in clf.classes_]
|
126 |
+
|
127 |
+
probas = clf.predict_proba(input)
|
128 |
+
|
129 |
+
other_categories = {labels[i]: float(probas[0][i]) for i in range(probas.shape[1])}
|
130 |
+
|
131 |
+
objective_categories = {}
|
132 |
+
if goal != '':
|
133 |
+
|
134 |
+
with open('objective_model.pkl', 'rb') as model:
|
135 |
+
clf = pickle.load(model)
|
136 |
+
|
137 |
+
labels = [label for label in clf.classes_]
|
138 |
|
139 |
+
probas = clf.predict_proba(input)
|
140 |
|
141 |
+
objective_categories = {labels[i]: float(probas[0][i]) for i in range(probas.shape[1])}
|
142 |
|
143 |
+
# RETURN
|
144 |
+
return labeled_tokens, sernac_probas, sernac_categories, other_categories, objective_categories
|
145 |
|
146 |
|
147 |
# DEMO
|
148 |
demo = gr.Interface(
|
149 |
predict,
|
150 |
+
inputs = [gr.Textbox(placeholder = "Ingresa el reclamo acá", label = 'Reclamo'), gr.Textbox(placeholder = "Ingresa el objetivo acá (opcional)", label = 'Objetivo')],
|
151 |
+
outputs = [gr.Highlightedtext(label = 'Entidades detectadas'),
|
152 |
+
gr.outputs.Label(label = 'Clasificación SERNAC'),
|
153 |
+
gr.outputs.Label(label = 'Clasificación categorías SERNAC'),
|
154 |
+
gr.outputs.Label(label = 'Clasificación categorías No SERNAC'),
|
155 |
+
gr.outputs.Label(label = 'Clasificación objetivo')],
|
156 |
examples=[
|
157 |
+
['este septiembre iremos manejando a tEmUco en un tóyòtA para pasar las fiestas patrias', 'ir a temuco'],
|
158 |
+
['no puedo, tengo que irme desde san pedro hasta la reina y luego hasta san pedro de la paz', ''],
|
159 |
+
['Buenas tardes, hace unas semanas compre un suzuki swift a derco de santiago, llevaba 2 semanas y la caja de cambios se echó a perder. Tengo asegurado el auto con BCI, pero aun no obtengo respuesta.', 'exijo una explicación!'],
|
160 |
+
['Tengo un toyota urban cruiser 1.3 año 2010 el cual consume mucho aceite y nunca me han respondido si tiene alguna solución o garantía me gustaría que fueran más concretas las respuestas gracias', 'Obtener una solucion Que reparación hay que hacer o si tiene garantía?'],
|
161 |
+
['Mi auto del año presenta Falla de motor y sensores siendo que lo compre nuevo 0km y tiene recién 5400kms.. Es un Peugeot 2008 gti... El servicio es como las pelotas.. Me mandaron a un servicio técnico en Calama que estaba cerrado', '']
|
162 |
],
|
163 |
+
title = 'Demo ML'
|
164 |
)
|
165 |
|
166 |
demo.launch()
|