dquisi commited on
Commit
51254c8
1 Parent(s): c26da6f

Create new file

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
2
+ import gradio as gr
3
+ import torch
4
+ id2label = {
5
+ "0": "B-LOC",
6
+ "1": "B-MISC",
7
+ "2": "B-ORG",
8
+ "3": "B-PER",
9
+ "4": "I-LOC",
10
+ "5": "I-MISC",
11
+ "6": "I-ORG",
12
+ "7": "I-PER",
13
+ "8": "O"
14
+ }
15
+ tokenizer = AutoTokenizer.from_pretrained('mrm8488/TinyBERT-spanish-uncased-finetuned-ner')
16
+ model = AutoModelForTokenClassification.from_pretrained('mrm8488/TinyBERT-spanish-uncased-finetuned-ner')
17
+ def get_objects(text):
18
+ input_ids = torch.tensor(tokenizer.encode(text)).unsqueeze(0)
19
+ outputs = model(input_ids)
20
+ last_hidden_states = outputs[0]
21
+ personajes=[]
22
+ locaciones=[]
23
+ for m in last_hidden_states:
24
+ for index, n in enumerate(m):
25
+ if(index > 0 and index <= len(text.split(" "))):
26
+ if('LOC' in id2label[str(torch.argmax(n).item())]):
27
+ locaciones.append(text.split(" ")[index-1]+"=> \n")
28
+ if('PER' in id2label[str(torch.argmax(n).item())]):
29
+ personajes.append(text.split(" ")[index-1]+"=> \n")
30
+ return ''.join(personajes) + "Ubicaciones:\n" + ''.join(locaciones)
31
+ def change_objects(text, objetos):
32
+ for personaje in objetos.split('\n'):
33
+ if ('=>' in personaje and len(personaje.split('=>')) > 0):
34
+ text = text.replace(personaje.split("=>")[0], personaje.split("=>")[1])
35
+ return text
36
+
37
+ demo = gr.Blocks()
38
+ with demo:
39
+ cuento = gr.Textbox(lines=2)
40
+ objetos = gr.Textbox()
41
+ label = gr.Label()
42
+ b1 = gr.Button("Identificar Pesonajes y Ubicaciones")
43
+ b2 = gr.Button("Cambiar objetos")
44
+ b1.click(get_objects, inputs=cuento, outputs=objetos)
45
+ b2.click(change_objects, inputs=[cuento, objetos], outputs=label)
46
+ demo.launch()