Spaces:
Sleeping
Sleeping
File size: 31,873 Bytes
a03f45e c748665 a03f45e 750a2ed a03f45e 0d7bc87 a03f45e 8714a46 4756d93 a03f45e 750a2ed 01084ee a03f45e c748665 a03f45e c748665 a03f45e c748665 a03f45e c748665 a03f45e c748665 a03f45e d2105df a03f45e dc0d917 a03f45e 4756d93 a03f45e 5f1c9c8 a03f45e 5f1c9c8 a03f45e 5f1c9c8 a03f45e f088a2a a03f45e 01084ee a03f45e 01084ee c748665 01084ee a03f45e 01084ee a03f45e 0d7bc87 a03f45e 0d7bc87 a03f45e 8305abf a03f45e 8305abf a03f45e 8305abf a03f45e 4f17c57 a03f45e 4f17c57 a03f45e 4f17c57 a03f45e 4f17c57 a03f45e 6ab37fd a03f45e 8305abf 0d7bc87 6ca7324 a03f45e e16e7b5 7b68d5a 8f4afb4 7b68d5a 58bcaa2 8f4afb4 7b68d5a 8305abf 8f4afb4 a03f45e 8f4afb4 dc0d917 8f4afb4 dc0d917 a03f45e 6acebdb df6d792 0e1842b a03f45e 4f17c57 a03f45e 01084ee a03f45e 0d7bc87 a03f45e 71b3cce 1a3f739 a03f45e 01084ee a03f45e 1a3f739 a03f45e 1a3f739 71b3cce 1a3f739 a03f45e 1a3f739 a03f45e 01084ee a03f45e c748665 a03f45e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 |
import gradio as gr
import torch
import json
import pandas as pd
import numpy as np
from transformers import AutoTokenizer, RobertaForTokenClassification
from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from json import JSONEncoder
from faker import Faker
from keras.utils import pad_sequences
import calendar
class out_json():
def __init__(self, w,l):
self.word = w
self.label = l
class MyEncoder(json.JSONEncoder):
def default(self, obj):
return {
'word': obj.word,
'label': obj.label
}
class Model:
def __init__(self):
self.texto=""
self.idioma=""
self.modelo_ner=""
self.categoria_texto=""
##
### Función que aplica el modelo e identifica su idioma
###
def identificacion_idioma(self,text):
self.texto=text
tokenizer = AutoTokenizer.from_pretrained("papluca/xlm-roberta-base-language-detection")
model = AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
preds = torch.softmax(logits, dim=-1)
id2lang = model.config.id2label
vals, idxs = torch.max(preds, dim=1)
#retorna el idioma con mayor porcentaje
maximo=vals.max()
idioma=''
porcentaje=0
for k, v in zip(idxs, vals):
if v.item()==maximo:
idioma,porcentaje=id2lang[k.item()],v.item()
if idioma=='es':
self.idioma="es"
self.modelo_ner='BSC-LT/roberta_model_for_anonimization'
self.faker_ = Faker('es_MX')
self.model = RobertaForTokenClassification.from_pretrained(self.modelo_ner)
else:
self.idioma="en"
self.faker_ = Faker('en_US')
self.modelo_ner="dayannex/distilbert-tuned-4labels"
self.model = AutoModelForTokenClassification.from_pretrained(self.modelo_ner)
self.categorizar_texto(self.texto)
def reordenacion_tokens(self,tokens,caracter):
i=0
new_tokens=[]
ig_tokens=[]
for token in tokens:
#print('token_texto:',token,caracter)
ind=len(new_tokens)
if i<len(tokens):
if not token.startswith(caracter):
new_tokens.append(token)
i=i+1
else:
new_tokens[ind-1] = (new_tokens[ind-1] + token.replace(caracter,''))
ig_tokens.append(i)
i=i+1
return (
new_tokens,
ig_tokens
)
def reordenacion_tokens_es(self,tokens,caracter):
i=0
new_tokens=[]
ig_tokens=[] #ignorar estos indices del array de indentificadores
for token in tokens:
ind=len(new_tokens)
if i<len(tokens):
if token.startswith(caracter):
new_tokens.append(token)
i=i+1
else:
#if i==0: new_tokens.append(token)
#else:
new_tokens[ind-1] = (new_tokens[ind-1] + token.replace(caracter,''))
ig_tokens.append(i)
i=i+1
return (
new_tokens,
ig_tokens
)
def reordenacion_identificadores(self,ig_tokens,predicted_tokens_classes):
x=0
new_identificadores=[]
for token in predicted_tokens_classes:
if x not in ig_tokens:
new_identificadores.append(token)
x=x+1
else:
x=x+1
return new_identificadores
def salida_json(self,tokens,pre_tokens):
list=[]
i=0
for t in tokens:
if pre_tokens[i]!='O':
a = out_json(t.replace('##','').replace('Ġ','').replace('Ċ',''),pre_tokens[i].replace('▁',''))
list.append(a)
i=i+1
return json.dumps(list, cls=MyEncoder, ensure_ascii=False)#MyEncoder().encode(list)
def tokens_identificados(self,tokens,pre_tokens):
list=[]
i=0
for t in tokens:
if pre_tokens[i]!='O':
a = t.replace('##','').replace('Ġ','').replace('Ċ','')
list.append(a)
i=i+1
return list
def metricas_anonimizacion(self,_f,t,id):
i=0
coincidencia=0
Z=['O']
_fake_filter= [x for x in _f if x not in Z]
new_tokens_filter= self.tokens_identificados(t,id)
for token in new_tokens_filter:
if token==_fake_filter[i]:
coincidencia=coincidencia+1
i=i+1
return str(coincidencia) + "/" + str(len(_fake_filter))
def salida_texto( self,tokens,pre_tokens):
new_labels = []
current_word = None
i=0
for token in tokens:
if pre_tokens[i]=='O' or 'MISC' in pre_tokens[i]:
new_labels.append(' ' +token.replace('##','').replace('Ġ',''))
else:
new_labels.append(' ' + pre_tokens[i])
i=i+1
a=''
for i in new_labels:
a = a+i
return a
def salida_texto_anonimizado(self, ids,pre_tokens):
new_labels = []
current_word = None
i=0
for identificador in pre_tokens:
if identificador=='O' or 'OTH' in identificador:
new_labels.append(self.tokenizer.decode(ids[i]))
else:
new_labels.append(' ' + identificador)
i=i+1
a=''
for i in new_labels:
a = a+i
return a
def is_integer_string(self,value):
try:
int(value)
return True
except ValueError:
return False
def formato_salida(self,out):
a=""
for i in out:
a = a + i.replace('▁','').replace(' ','') + ' '
return a
def fake_pers(self):
return self.faker_.name(self)
def fake_word(self):
return self.faker_.word()
def fake_first_name(self):
return self.faker_.first_name()
def fake_last_name(self):
return self.faker_.last_name()
def fake_address(self):
return self.faker_.address()
def fake_sentence(self,n):
return self.faker_.sentence(nb_words=n)
def fake_text(self):
return self.faker_.text()
def fake_company(self):
return self.faker_.company()
def fake_city(self):
return self.faker_.city()
def get_day_of(self, month_name, year=2024):
months = {
'enero': 1, 'febrero': 2, 'marzo': 3, 'abril': 4, 'mayo': 5, 'junio': 6,
'julio': 7, 'agosto': 8, 'septiembre': 9, 'octubre': 10, 'noviembre': 11, 'diciembre': 12,
'january': 1, 'february': 2, 'march': 3, 'april': 4, 'may': 5, 'june': 6,
'july': 7, 'august': 8, 'september': 9, 'october': 10, 'november': 11, 'december': 12
}
month = months[month_name]
_, num_days = calendar.monthrange(year, month)
return str(num_days)
def reemplazo_fake(self,identificadores, new_tokens):
a=['Enero','January', 'February','Febrero','Marzo','March','Abril','April','Mayo','May','Junio','June','Julio','July','Agosto','August','Septiembre','September','Octubre','October','Noviembre','November','Diciembre','December']
b=['Ene','Jan', 'Feb','Mar','Mar','Abr','Apr','May','May','Jun','Jun','Jul','Jul','Ago','Aug','Sep','Oct','Nov','Dic','Dec']
i=0
new_iden=[]
for id in identificadores:
if 'PER' in id:
new_iden.append(self.fake_first_name())
elif 'ORG' in id:
new_iden.append(self.fake_company())
elif 'LOC' in id:
new_iden.append(self.fake_city())
elif 'DATE' in id:
if self.is_integer_string(new_tokens[i]):
match len(new_tokens[i]):
case 4:
new_iden.append(self.faker_.date()[:4])
case 10:
new_iden.append(self.faker_.date())
case 1:
new_iden.append(self.get_day_of('february'))
case 2:
new_iden.append(self.get_day_of('february'))
case _:
new_iden.append(id)
else:
match new_tokens[i]:
case w if w in a:
new_iden.append(self.faker_.month_name())
case w if w in b:
new_iden.append(self.faker_.month_name()[:3])
case "-":
new_iden.append("-")
case ".":
new_iden.append(".")
case ",":
new_iden.append(",")
case "/":
new_iden.append("/")
case _:
new_iden.append(id)
else:
new_iden.append(id)
i=i+1
return new_iden
###
### Función que aplica los modelo para categorizar el texto segun su contexto
###
def categorizar_texto(self,texto):
name="elozano/bert-base-cased-news-category"
tokenizer = AutoTokenizer.from_pretrained(name)
model_ = AutoModelForSequenceClassification.from_pretrained(name)
inputs_ = tokenizer(texto, padding=True, truncation=True, return_tensors="pt")
with torch.no_grad():
logits = model_(**inputs_).logits
preds = torch.softmax(logits, dim=-1)
id2lang = model_.config.id2label
vals, idxs = torch.max(preds, dim=1)
#retorna el idioma con mayor porcentaje
maximo=vals.max()
cat=''
self.categoria_texto=''
porcentaje=0
for k, v in zip(idxs, vals):
if v.item()==maximo:
cat,porcentaje=id2lang[k.item()],v.item()
self.categoria_texto=cat
return cat, porcentaje
###
### Función que aplica los modelos sobre un texto
###
def predict(self,etiquetas):
categoria, porcentaje = self.categorizar_texto(self.texto)
print(categoria, porcentaje)
self.tokenizer = AutoTokenizer.from_pretrained(self.modelo_ner)
inputs = self.tokenizer(self.texto, return_tensors="pt")
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=2)
predicted_token_class_ids = predictions[0].tolist()
predicted_tokens_classes = [self.model.config.id2label[label_id] for label_id in predicted_token_class_ids]
tokens = self.tokenizer.convert_ids_to_tokens(inputs.input_ids[0],skip_special_tokens=False)## OJO skip_special_tokens=False ojo alli esta cero y es i
predicted_tokens_classes.pop(0)
predicted_tokens_classes.pop(len(predicted_tokens_classes)-1)
tokens.pop(0)
tokens.pop(len(tokens)-1)
if (self.idioma=='es'):
inputs = self.tokenizer(self.texto, return_tensors="pt",max_length=512, truncation=True)
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=2)
predicted_token_class_ids = predictions[0].tolist()
predicted_tokens_classes = [self.model.config.id2label[label_id] for label_id in predicted_token_class_ids]
tokens = self.tokenizer.convert_ids_to_tokens(inputs.input_ids[0])
predicted_tokens_classes.pop(0)
predicted_tokens_classes.pop(len(predicted_tokens_classes)-1)
tokens.pop(0)
tokens.pop(len(tokens)-1)
new_tokens,ig_tokens=self.reordenacion_tokens_es(tokens,'Ġ')
else:
inputs = self.tokenizer(self.texto, return_tensors="pt")
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=2)
predicted_token_class_ids = predictions[0].tolist()
predicted_tokens_classes = [self.model.config.id2label[label_id] for label_id in predicted_token_class_ids]
tokens = self.tokenizer.convert_ids_to_tokens(inputs.input_ids[0])
predicted_tokens_classes.pop(0)
predicted_tokens_classes.pop(len(predicted_tokens_classes)-1)
tokens.pop(0)
tokens.pop(len(tokens)-1)
new_tokens,ig_tokens=self.reordenacion_tokens(tokens,'#')
new_identificadores = self.reordenacion_identificadores(ig_tokens,predicted_tokens_classes)
out1 = self.salida_json(new_tokens,new_identificadores)
if etiquetas:
out2 = self.salida_texto(new_tokens,new_identificadores)#solo identificadores
out3=""
coincidencia=""
else:
#out2 = self.salida_texto(new_tokens,self.reemplazo_fake(new_identificadores))
_fake=self.reemplazo_fake(new_identificadores,new_tokens)
coincidencia=self.metricas_anonimizacion(_fake,new_tokens,new_identificadores)
out2 = self.salida_texto(new_tokens,_fake)
out3 = self.salida_json(_fake,new_identificadores)
return (
out1,
str(out2),
out3,
coincidencia
)
class ModeloDataset:
def __init__(self):
self.texto=""
self.idioma=""
self.modelo_ner=""
self.categoria_texto=""
#self.tokenizer = AutoTokenizer.from_pretrained("BSC-LT/roberta_model_for_anonimization")
def reordenacion_tokens(self,tokens,caracter):
i=0
new_tokens=[]
ig_tokens=[]
for token in tokens:
ind=len(new_tokens)
if i<len(tokens):
if not token.startswith(caracter):
new_tokens.append(token)
i=i+1
else:
new_tokens[ind-1] = (new_tokens[ind-1] + token.replace(caracter,''))
ig_tokens.append(i)
i=i+1
return (
new_tokens,
ig_tokens
)
def reordenacion_tokens_es(self,tokens,caracter):
i=0
new_tokens=[]
ig_tokens=[] #ignorar estos indices del array de indentificadores
for token in tokens:
ind=len(new_tokens)
if i<len(tokens):
if token.startswith(caracter):
new_tokens.append(token)
i=i+1
else:
#if i==0: new_tokens.append(token)
#else:
new_tokens[ind-1] = (new_tokens[ind-1] + token.replace(caracter,''))
ig_tokens.append(i)
i=i+1
return (
new_tokens,
ig_tokens
)
def reordenacion_identificadores(self,ig_tokens,predicted_tokens_classes, tamano):
x=0
new_identificadores=[]
for token in predicted_tokens_classes:
if x not in ig_tokens:
if len(new_identificadores) < tamano:
new_identificadores.append(token)
x=x+1
else:
x=x+1
return new_identificadores
def is_integer_string(self,value):
try:
int(value)
return True
except ValueError:
return False
def get_day_of(self, month_name, year=2024):
months = {
'enero': 1, 'febrero': 2, 'marzo': 3, 'abril': 4, 'mayo': 5, 'junio': 6,
'julio': 7, 'agosto': 8, 'septiembre': 9, 'octubre': 10, 'noviembre': 11, 'diciembre': 12,
'january': 1, 'february': 2, 'march': 3, 'april': 4, 'may': 5, 'june': 6,
'july': 7, 'august': 8, 'september': 9, 'october': 10, 'november': 11, 'december': 12
}
month = months[month_name]
_, num_days = calendar.monthrange(year, month)
return str(num_days)
###
### Funciones para generar diversos datos fake dependiendo de la catagoria
###
def fake_pers(self):
return self.faker_.name(self)
def fake_word(self):
return self.faker_.word()
def fake_first_name(self):
return self.faker_.first_name()
def fake_last_name(self):
return self.faker_.last_name()
def fake_address(self):
return self.faker_.address()
def fake_sentence(self,n):
return self.faker_.sentence(nb_words=n)
def fake_text(self):
return self.faker_.text()
def fake_company(self):
return self.faker_.company()
def fake_city(self):
return self.faker_.city()
def reemplazo_fake(self,identificadores,new_tokens):
a=['Enero','January', 'February','Febrero','Marzo','March','Abril','April','Mayo','May','Junio','June','Julio','July','Agosto','August','Septiembre','September','Octubre','October','Noviembre','November','Diciembre','December']
b=['Ene','Jan', 'Feb','Mar','Mar','Abr','Apr','May','May','Jun','Jun','Jul','Jul','Ago','Aug','Sep','Oct','Nov','Dic','Dec']
i=0
if self.idioma=='es':
self.faker_ = Faker('es_MX')
else:
self.faker_ = Faker('en_US')
new_iden=[]
for id in identificadores:
if 'PER' in id:
new_iden.append(self.fake_first_name())
elif 'ORG' in id:
new_iden.append(self.fake_company())
elif 'LOC' in id:
new_iden.append(self.fake_city())
elif 'DATE' in id:
if self.is_integer_string(new_tokens[i]):
match len(new_tokens[i]):
case 4:
new_iden.append(self.faker_.date()[:4])
case 10:
new_iden.append(self.faker_.date())
case 1:
new_iden.append(self.get_day_of('february'))
case 2:
new_iden.append(self.get_day_of('february'))
case _:
new_iden.append(id)
else:
match new_tokens[i]:
case w if w in a:
new_iden.append(self.faker_.month_name())
case w if w in b:
new_iden.append(self.faker_.month_name()[:3])
case "-":
new_iden.append("-")
case ".":
new_iden.append(".")
case ",":
new_iden.append(",")
case "/":
new_iden.append("/")
case _:
new_iden.append(id)
else:
new_iden.append(id)
i=i+1
return new_iden
###
### Función que aplica los modelos de acuerdo al idioma detectado
###
def aplicar_modelo(self,_sentences,idioma, etiquetas):
if idioma=="es":
self.tokenizer = AutoTokenizer.from_pretrained("BSC-LT/roberta_model_for_anonimization")
tokenized_text=[self.tokenizer.tokenize(sentence[:500]) for sentence in _sentences]
ids = [self.tokenizer.convert_tokens_to_ids(x) for x in tokenized_text]
MAX_LEN=128
ids=pad_sequences(ids,maxlen=MAX_LEN,dtype="long",truncating="post", padding="post")
input_ids = torch.tensor(ids)
self.model = RobertaForTokenClassification.from_pretrained("BSC-LT/roberta_model_for_anonimization")
with torch.no_grad():
logits = self.model(input_ids).logits
predicted_token_class_ids = logits.argmax(-1)
i=0
_predicted_tokens_classes=[]
for a in predicted_token_class_ids:
_predicted_tokens_classes.append([self.model.config.id2label[t.item()] for t in predicted_token_class_ids[i]])
i=i+1
labels = predicted_token_class_ids
loss = self.model(input_ids, labels=labels).loss
new_tokens=[]
ig_tok=[]
i=0
new_identificadores=[]
for item in tokenized_text:
aux1, aux2= self.reordenacion_tokens_es(item,"Ġ")
new_tokens.append(aux1)
ig_tok.append(aux2)
for items in _predicted_tokens_classes:
aux=self.reordenacion_identificadores(ig_tok[i],items,len(new_tokens[i]))
new_identificadores.append(aux)
i=i+1
return new_identificadores, new_tokens
else:
print('idioma:',idioma)
self.tokenizer = AutoTokenizer.from_pretrained("dayannex/distilbert-tuned-4labels")
self.model = AutoModelForTokenClassification.from_pretrained("dayannex/distilbert-tuned-4labels")
sentences_list = _sentences.tolist()
inputs = self.tokenizer(sentences_list, padding=True, truncation=True, return_tensors="pt", max_length=512)
with torch.no_grad():
outputs = self.model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=2)
id2label = self.model.config.id2label
all_tokens = []
all_label_ids = []
all_labels = []
for i, sentence in enumerate(sentences_list):
tokens = self.tokenizer.convert_ids_to_tokens(inputs.input_ids[i])
label_ids = predictions[i].tolist()
labels = [id2label[label_id] for label_id in label_ids]
all_tokens.append(tokens)
all_label_ids.append(label_ids)
all_labels.append(labels)
#se eliminan el primer y ultimo elemento
for item in all_tokens:
item.pop(0)
item.pop(len(item)-1)
for item in all_labels:
item.pop(0)
item.pop(len(item)-1)
new_tokens=[]
ig_tok=[]
i=0
new_identificadores=[]
for item in all_tokens:
aux1, aux2= self.reordenacion_tokens(item,"#")
new_tokens.append(aux1)
ig_tok.append(aux2)
print('ig_tok')
print(ig_tok)
i=0
for items in all_labels:
aux=self.reordenacion_identificadores(ig_tok[i],items,len(new_tokens[i]))
new_identificadores.append(aux)
i=i+1
special_tokens = self.tokenizer.all_special_tokens
filtered_tokens = []
filtered_labels = []
tok_new=[]
lab_new=[]
#se descartan los tokens speciales
for token_linea, label_linea in zip(new_tokens, new_identificadores):
filtered_tokens = []
filtered_labels = []
for token, label in zip(token_linea, label_linea):
if token not in special_tokens:
filtered_tokens.append(token)
filtered_labels.append(label)
tok_new.append(filtered_tokens)
lab_new.append(filtered_labels)
return lab_new,tok_new #new_identificadores, new_tokens
###
### Procesa los tokens generados del texto de entradas con los tokens predichos, para generar los tokens por palabra
###
def salida_texto( self,tokens,pre_tokens):
new_labels = []
current_word = None
i=0
for token in tokens:
if pre_tokens[i]=='O' or 'MISC' in pre_tokens[i]:
new_labels.append(' ' +token.replace('▁','').replace('Ġ',''))
else:
new_labels.append(' ' + pre_tokens[i])
i=i+1
a=''
for i in new_labels:
a = a+i
return a
def salida_texto2(self, tokens,labels,etiquetas):
i=0
out=[]
for iden in labels:
if etiquetas:
out.append(self.salida_texto( iden,np.array(tokens[i])))
else:
out.append(self.salida_texto(iden,self.reemplazo_fake(np.array(tokens[i]),labels[i])))
i=i+1
return out
def unir_array(self,_out):
i=0
salida=[]
for item in _out:
salida.append("".join(str(x) for x in _out[i]))
i=i+1
return salida
def unir_columna_valores(self,df,columna):
out = ','.join(df[columna])
return out
###
### Funcion para procesar archivos json, recibe archivo
###
class utilJSON:
def __init__(self,archivo):
with open(archivo, encoding='utf-8') as f:
self.data = json.load(f)
def obtener_keys_json(self,data):
out=[]
for key in data:
out.append(key)
return(out)
###
### funcion "flatten_json" tomada de https://levelup.gitconnected.com/a-deep-dive-into-nested-json-to-data-frame-with-python-69bdabb41938
### Renu Khandelwal Jul 23, 2023
def flatten_json(self,y):
try:
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
except json.JSONDecodeError:
print("Error: The JSON document could not be decoded.")
except TypeError:
print("Error: Invalid operation or function argument type.")
except KeyError:
print("Error: One or more keys do not exist.")
except ValueError:
print("Error: Invalid value detected.")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
def obtener_dataframe(self,data):
claves=self.obtener_keys_json(data)
if len(claves)==1:
data_flattened = [self.flatten_json(class_info) for class_info in data[claves[0]]]
df = pd.DataFrame(data_flattened)
else:
data_flattened = [self.flatten_json(class_info) for class_info in data]
df = pd.DataFrame(data_flattened)
return df
modelo = ModeloDataset()
model = Model()
def get_model():
return model
###
### Función que interactúa con la interfaz Gradio para el procesamiento de texto, csv o json
###
def procesar(texto,archivo, etiquetas):
if len(texto)>0:
print('text')
model.identificacion_idioma(texto[:1700])
labels, textoProcesado, labels_fake, coincidencia= model.predict(etiquetas)
return model.idioma + "/" + model.categoria_texto,labels, textoProcesado,gr.Dataframe(),gr.File(),labels_fake, coincidencia
else:
if archivo.name.split(".")[1]=="csv":
print('csv')
#df=pd.read_csv(archivo.name,delimiter=";",encoding='latin-1')
df=pd.read_csv(archivo.name,delimiter=";")
df_new = pd.DataFrame( columns=df.columns.values)
model.identificacion_idioma(df.iloc[0][0])
modelo.idioma=model.idioma
print(model.idioma)
for item in df.columns.values:
sentences=df[item]
ides, predicted = modelo.aplicar_modelo(sentences,model.idioma,etiquetas)
out=modelo.salida_texto2( ides,predicted,etiquetas)
print('out csv:',out)
df_new[item] = modelo.unir_array(out)
return modelo.idioma,"","", df_new, df_new.to_csv(sep='\t', encoding='utf-8',index=False),"",""
else:
print('json')
if archivo.name.split(".")[1]=="json":
util = utilJSON(archivo.name)
df=util.obtener_dataframe(util.data)
df_new = pd.DataFrame( columns=df.columns.values)
model.identificacion_idioma(df.iloc[0][0])
modelo.idioma=model.idioma
for item in df.columns.values:
sentences=df[item]
print('sen')
ides, predicted = modelo.aplicar_modelo(sentences,modelo.idioma,etiquetas)
print('ap')
out=modelo.salida_texto2( ides,predicted,etiquetas)
print('sa')
print('out json:',out)
df_new[item] = modelo.unir_array(out)
print('un')
return modelo.idioma,"","", df_new, df_new.to_csv(sep='\t', encoding='utf-8',index=False),"",""
demo = gr.Interface(fn=procesar,inputs=["text",gr.File(), "checkbox"] , outputs=[gr.Label(label="idioma/categoría"),gr.Textbox(label="etiquetas"),gr.Textbox(label="texto procesado"),gr.Dataframe(label="Datos procesados en dataframe",interactive=False),gr.Textbox(label="datos csv"),gr.Textbox(label="etiquetas anonimizadas"),gr.Label(label="coincidencia tokens originales vs anonimizados")])
#
demo.launch(share=True)
|