area444 commited on
Commit
c8846a7
1 Parent(s): edd49bb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flair.data import Sentence
2
+ from flair.models import SequenceTagger
3
+ import gradio as gr
4
+
5
+ title = "Spanish Named Entity Recognition (NER) - Demo"
6
+
7
+ # Load tagger
8
+ tagger1 = SequenceTagger.load("aymurai/anonymizer-beto-cased-flair")
9
+ tagger2 = SequenceTagger.load("flair/ner-spanish-large")
10
+
11
+ # Examples
12
+ mytext1="1. DECLARAR EXTINGUIDA LA ACCIÓN PENAL en este caso por cumplimiento de la suspensión del proceso a prueba, y SOBRESEER a EZEQUIEL CAMILO MARCONNI, DNI 11.222.333, en orden a los delitos de lesiones leves agravadas, amenazas simples y agravadas por el uso de armas, en contra de Chuchita Perez de 50 años."
13
+ mytext2="El sombrío Prudhon, imbuído, sin duda, en las ideas de los Santos Padres de la Iglesia que predicaban el desden por los bienes terrenales, decía que la pobreza es una ley de nuestra naturaleza, ley bajo la cual hemos sido constituídos, de donde se deduce que el pauperismo es mal que no tiene remedio ni cura."
14
+
15
+ # Function with NER Models
16
+ def ner_builder(model_, text_):
17
+ if model_=="NER (BETO->Judicial)":
18
+ # make example sentence
19
+ sentence = Sentence(text_)
20
+ # predict NER tags
21
+ tagger1.predict(sentence)
22
+ tags_tokens = []
23
+ for entity in sentence.get_spans('ner'):
24
+ tags_tokens.append(entity)
25
+ elif model_=="NER (CoNLL-03 Spanish)":
26
+ # make example sentence
27
+ sentence = Sentence(text_)
28
+ # predict NER tags
29
+ tagger2.predict(sentence)
30
+ tags_tokens = []
31
+ # iterate over entities and print
32
+ for entity in sentence.get_spans('ner'):
33
+ tags_tokens.append(entity)
34
+ # return predicted NER spans
35
+ return f"""Con {model_} se encontraron las etiquetas: {tags_tokens} """
36
+
37
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
38
+ gr.HTML("<h1>"+title+"</h1>"+"""
39
+ <p>Aplicación de Procesamiento de Lenguaje Natural (PNL), para el reconocimiento de entidades (nombres, organizaciones, ubicaciones, entre otras), dentro de un texto.</p>
40
+ """)
41
+ gr.Interface(
42
+ ner_builder,
43
+ [
44
+ #gr.Slider(1, 10, value=2, label="Número", info="Choose between 1 and 10"),
45
+ gr.Dropdown(
46
+ ["NER (BETO->Judicial)", "NER (CoNLL-03 Spanish)"], label="Modelo", info="Elige un modelo (NER pipeline)"
47
+ ),
48
+ gr.Textbox(placeholder="Escribe tu texto aquí...", label="Texto", info="Pega un texto o da clic en un ejemplo"),
49
+ ],
50
+ "text",
51
+ #theme="soft",
52
+ #title=title,
53
+ examples=[
54
+ ["NER (BETO->Judicial)", str(mytext1)],
55
+ ["NER (CoNLL-03 Spanish)", str(mytext2)],
56
+ ]
57
+ )
58
+ gr.HTML("""
59
+ <h2>¿Por qué usar Flair AI?<h2>
60
+ <a href="https://towardsdatascience.com/benchmark-ner-algorithm-d4ab01b2d4c3">Por sus resultados superiores o similares a otros modelos pero sin límite de 512 tokens</a>
61
+ """)
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch()