File size: 3,372 Bytes
dcf747d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbb519
a51f1a0
dcf747d
 
 
 
a51f1a0
dcf747d
 
3975411
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
import gradio as gr
from transformers import pipeline
import spacy
from itertools import groupby

title = "Named Entity Recognition with spaCy"
description = "en_core_web"

nlp_trf = spacy.load("en_core_web_trf")
nlp_sm = spacy.load("en_core_web_sm")
nlp_md = spacy.load("en_core_web_md")
nlp_lg = spacy.load("en_core_web_lg")

def doc_to_spans(doc):
    tokens = [(tok.text, tok.idx, tok.ent_type_) for tok in doc]
    results = []
    entities = set()
    for entity, group in groupby(tokens, key=lambda t: t[-1]):
        if not entity:
            continue
        group = list(group)
        _, start, _ = group[0]
        word, last, _ = group[-1]
        text = ' '.join(item[0] for item in group)
        end = last + len(word)
        results.append(
            {"entity": entity, "start": start, "end": end})
        entities.add(entity)
    return results

#define a function to process your input and output
def ner(text, model):
    if model == "en_core_web_trf":
        nlp = nlp_trf
    elif model == "en_core_web_sm":
        nlp = nlp_sm
    elif model == "en_core_web_md":
        nlp = nlp_md
    elif model == "en_core_web_lg":
        nlp = nlp_lg
    doc = nlp(text)
    results = doc_to_spans(doc)
    return {"text": text, "entities": results}

#create input and output objects
#input object1
input1 = gr.Textbox(label="Text")
#input object2
input2 = gr.Dropdown(label="Model",
                     choices=["en_core_web_trf", "en_core_web_sm", "en_core_web_md", "en_core_web_lg"],
                     value="en_core_web_trf")
#output object
output = gr.HighlightedText(label="Output")
#example object
examples = [
            ["TDC A/S provides communications and entertainment solutions in Denmark. It operates through Nuuday and TDC NET segments. The company designs, builds, and operates broadband and mobile networks; and provides technical support to customers and networks. It offers services, such as landline voice, TV and streaming, broadband, Internet and network, mobility, and other services. The company provides its products and services under the YouSee, Hiper, Telmore, Blockbuster, TDC Business, TDC Erhverv, Fullrate, NetDesign, and Relatel brands. It serves consumer and business customers. The company was founded in 1882 and is based in Copenhagen, Denmark. TDC A/S is a subsidiary of DK Telekommunikation ApS."],
            ["Giddy Inc., doing business as Boxed Wholesale, offers online wholesale and retailing services. The company provides cleaning and laundry, kitchen, paper, skin care, hair care, and grocery products. Additionally, it offers diapers and organic products. Giddy Inc. was founded in 2013 and is based in Edison, New Jersey."],
            ["United Iron And Steel Manufacturing Company (P.L.C.) produces and sells iron and steel products in Jordan. It is also involved in trading scrap iron. The company was incorporated in 1992 and is headquartered in Amman, Jordan. United Iron And Steel Manufacturing Company (P.L.C.) is a subsidiary of Manaseer Group Corporation."]
            ]
#create interface
gui = gr.Interface(cache_examples=False,
                   title=title,
                   description=description,
                   fn=ner,
                   inputs=[input1, input2],
                   outputs=[output],
                   examples=examples)

#display the interface
gui.launch()