File size: 3,108 Bytes
853a4c5
cb27a07
853a4c5
cb27a07
853a4c5
cb27a07
 
 
 
 
 
 
 
 
 
 
 
 
f99c3a8
 
 
cb27a07
f99c3a8
 
 
 
 
 
 
 
 
853a4c5
cb27a07
 
 
 
 
 
 
 
 
 
 
8556407
cb27a07
 
 
 
 
 
 
8556407
6d3242d
f99c3a8
853a4c5
cb27a07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34e29ac
853a4c5
f99c3a8
 
853a4c5
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
import gradio as gr
import spaces
from transformers import pipeline
from typing import List, Dict, Any

def merge_tokens(tokens: List[Dict[str, any]]) -> List[Dict[str, any]]:
    """
    Merges tokens that belong to the same entity into a single token.

    Args:
        tokens (List[Dict[str, any]]): A list of token dictionaries, each containing information about 
                                       the entity, word, start, end, and score.

    Returns:
        List[Dict[str, any]]: A list of merged token dictionaries, where tokens that are part of the 
                              same entity are combined into a single token with updated word, end, 
                              and score values.
    """
    merged_tokens = []
    for token in tokens:
        if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
            # If the current token continues the entity of the last one, merge them
            last_token = merged_tokens[-1]
            last_token['word'] += token['word'].replace('##', '')
            last_token['end'] = token['end']
            last_token['score'] = (last_token['score'] + token['score']) / 2
        else:
            # Otherwise, add the token to the list
            merged_tokens.append(token)

    return merged_tokens

# Initialize Model
get_completion = pipeline("ner", model="dslim/bert-base-NER", device=0)

@spaces.GPU(duration=120)
def ner(input: str) -> Dict[str, Any]:
    """
    Performs Named Entity Recognition (NER) on the given input text and merges tokens that belong 
    to the same entity into a single entity.

    Args:
        input (str): The input text to analyze for named entities.

    Returns:
        Dict[str, Any]: A dictionary containing the original text and a list of identified entities 
                        with merged tokens.
                        - "text": The original input text.
                        - "entities": A list of dictionaries, where each dictionary contains information 
                          about a recognized entity, including the word, entity type, score, and positions.
    """
    output = get_completion(input)
    merged_tokens = merge_tokens(output)
    return {"text": input, "entities": merged_tokens}

####### GRADIO APP #######
title = """<h1 id="title"> Named Entity Recognition </h1>"""

description = """
- The model used for Recognizing entities [BERT-BASE-NER](https://huggingface.co./dslim/bert-base-NER).
"""

css = '''
h1#title {
  text-align: center;
}
'''

theme = gr.themes.Soft()
demo = gr.Blocks(css=css, theme=theme)

with demo:
  gr.Markdown(title)
  gr.Markdown(description)
  interface = gr.Interface(fn=ner,
                    inputs=[gr.Textbox(label="Text to find entities", lines=10)],
                    outputs=[gr.HighlightedText(label="Text with entities")],
                    allow_flagging="never",
                    examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])

demo.launch()