File size: 2,094 Bytes
3eea826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab98ba8
3eea826
 
ddf219b
3eea826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

model_checkpoint = 'zinoubm/bert-finetuned-ner'
model = pipeline(
    "token-classification", model=model_checkpoint,
)


def concat_prediction(prediction):
    entity = prediction[0]['entity'][2:]
    start = prediction[0]['start']
    end = prediction[-1]['end']
    return {
        'entity': entity,
        'start': start,
        'end': end}


def concat_predictions(predictions):
    concatenated_predictions = []
    for_concat = []
    for i in range(len(predictions)):
        if predictions[i]['entity'].startswith('B'):
            for_concat.append(predictions[i])
            j = i+1
            while j < len(predictions) and predictions[j]['entity'].startswith('I'):
                for_concat.append(predictions[j])
                j += 1

            concatenated_predictions.append(concat_prediction(for_concat))
            for_concat = []

    return concatenated_predictions


title = 'Extended Name Entity Recognition'

examples = [
    "Does Chicago have any stores and does Joe live here?",
    "My name is Sylvain and I work at Hugging Face in Brooklyn."
]


article = '''
# How to use this interface
Here's the [notebook](https://colab.research.google.com/drive/1xAsW1YNC38NEHXn0XMZVL_W-VPNYD-TO?usp=sharing) used to train the model used in this app.
Using the interface is very easy, just type some text that and the model will give the names of entities in one of these categories:

- **org** : organization
- **per** : person
- **geo** : location
- **tim** :  dates and times
- **gpe** : Geopolitical Entity
- **art**
- **nat**
- **eve**

just hit **Submit** to see the results.You can also try some of the provided examples.
'''


def predict(text):
    output = model(text)
    return {"text": text, "entities": concat_predictions(output)}


demo = gr.Interface(predict,
                    gr.Textbox(placeholder="Enter sentence here..."),
                    gr.HighlightedText(),
                    title=title,
                    examples=examples,
                    article=article)

demo.launch()