Spaces:
Runtime error
Runtime error
marcossalinas
commited on
Commit
•
f7cb5b1
1
Parent(s):
d4d1b89
First commit
Browse files- app.py +32 -0
- classifier.py +52 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pickle
|
3 |
+
import gradio as gr
|
4 |
+
from classifier import Classifier
|
5 |
+
|
6 |
+
def classify(txt):
|
7 |
+
with open('classifier.pickle', 'rb') as f:
|
8 |
+
classifier = pickle.load(f)
|
9 |
+
return classifier.predict(txt)
|
10 |
+
|
11 |
+
|
12 |
+
title = 'Detector de Quechua y Español'
|
13 |
+
description =( 'Bolivia lucha para que no desaparezcan los idiomas indígenas, sin embargo,' +
|
14 |
+
'es aún muy complicado acceder a recursos que ayuden a su asimilación y aprendizaje.' +
|
15 |
+
'Presentamos una herramienta de clasificación de idiomas, que si bien es una tarea sencilla, ' +
|
16 |
+
'resulta esencial para realizar tareas más complejas como la traducción automática.'
|
17 |
+
)
|
18 |
+
article = 'Demo del proyecto para Saturdays.\nAutores del modelo: Cota V. Andreina, Cusi L. Evelyn, Nina M. Juan Dilan'
|
19 |
+
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=classify,
|
22 |
+
inputs= gr.inputs.Textbox(lines=3, label='TEXTO', placeholder='Introduzca un texto'),
|
23 |
+
outputs= gr.outputs.Textbox(label='IDIOMA'),
|
24 |
+
examples = ['¿Maytaq ashkallanchikega?', 'Entonces el Inka dijo ¡Mach\'a!', '¡Aragan kanki wamraqa!', 'Señora, ¿yanapariwayta atiwaqchu?', '¿A dónde vas?'],
|
25 |
+
description = description,
|
26 |
+
title = title,
|
27 |
+
article = article,
|
28 |
+
theme = 'peach'
|
29 |
+
)
|
30 |
+
|
31 |
+
iface.launch()
|
32 |
+
|
classifier.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
# import pickle
|
3 |
+
|
4 |
+
class Classifier:
|
5 |
+
|
6 |
+
def __init__(self, dict_reemplazo, ngram_vectorizer, transformer, svm_model) -> None:
|
7 |
+
self.dict_reemplazo = dict_reemplazo
|
8 |
+
self.ngram_vectorizer = ngram_vectorizer
|
9 |
+
self.transformer = transformer
|
10 |
+
self.svm_model = svm_model
|
11 |
+
|
12 |
+
def reemplazar_caracteres_diferentes(self, texto, dictionary):
|
13 |
+
return texto.translate(dictionary)
|
14 |
+
|
15 |
+
def eliminar_ruido(self, texto, caracteres):
|
16 |
+
nuevo_texto = texto
|
17 |
+
for c in caracteres:
|
18 |
+
nuevo_texto = re.sub(c, '', nuevo_texto)
|
19 |
+
return nuevo_texto
|
20 |
+
|
21 |
+
def eliminar_espacios(self, string):
|
22 |
+
nuevo_string = string.strip()
|
23 |
+
nuevo_string = ' '.join(nuevo_string.split())
|
24 |
+
return nuevo_string
|
25 |
+
|
26 |
+
def predict(self, npt_txt):
|
27 |
+
txt = self.eliminar_espacios(
|
28 |
+
self.eliminar_ruido(
|
29 |
+
self.reemplazar_caracteres_diferentes(
|
30 |
+
self.eliminar_espacios(
|
31 |
+
self.eliminar_ruido(npt_txt, [r'[^\w\s^\´\’]'])), self.dict_reemplazo), [r'\d+', '_']))
|
32 |
+
vctr = self.transformer.transform(self.ngram_vectorizer.transform([txt]))
|
33 |
+
return 'Español' if self.svm_model.predict(vctr)[0] == 0 else 'Quechua'
|
34 |
+
|
35 |
+
# if __name__ == '__main__':
|
36 |
+
# with open('dict_reemplazo', 'rb') as f:
|
37 |
+
# dict_reemplazo = pickle.load(f)
|
38 |
+
# with open('ngram_vectorizer', 'rb') as f:
|
39 |
+
# ngram_vectorizer = pickle.load(f)
|
40 |
+
# with open('transformer', 'rb') as f:
|
41 |
+
# transformer = pickle.load(f)
|
42 |
+
# with open('svm_model', 'rb') as f:
|
43 |
+
# svm_model = pickle.load(f)
|
44 |
+
# classifier = Classifier(dict_reemplazo, ngram_vectorizer, transformer, svm_model)
|
45 |
+
# with open('classifier.pickle', 'wb') as f:
|
46 |
+
# pickle.dump(classifier, f)
|
47 |
+
|
48 |
+
# with open('classifier.pickle', 'rb') as f:
|
49 |
+
# my_classifier = pickle.load(f)
|
50 |
+
|
51 |
+
# for txt in ['¿Maytaq ashkallanchikega', 'Entonces el Inka dijo ¡Mach\'a!', '¡Aragan kanki wamraqa', 'Señora, ¿yanapariwayta atiwaqchu?', '¿A dónde vas?', '324#@$%']:
|
52 |
+
# print (my_classifier.predict(txt))
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
re
|
2 |
+
pickle
|