updated
Browse files
app.py
CHANGED
@@ -2,56 +2,34 @@ import gradio as gr
|
|
2 |
import spaces
|
3 |
from transformers import pipeline
|
4 |
from typing import List, Dict, Any
|
|
|
5 |
|
6 |
def merge_tokens(tokens: List[Dict[str, any]]) -> List[Dict[str, any]]:
|
7 |
-
"""
|
8 |
-
Merges tokens that belong to the same entity into a single token.
|
9 |
-
|
10 |
-
Args:
|
11 |
-
tokens (List[Dict[str, any]]): A list of token dictionaries, each containing information about
|
12 |
-
the entity, word, start, end, and score.
|
13 |
-
|
14 |
-
Returns:
|
15 |
-
List[Dict[str, any]]: A list of merged token dictionaries, where tokens that are part of the
|
16 |
-
same entity are combined into a single token with updated word, end,
|
17 |
-
and score values.
|
18 |
-
"""
|
19 |
merged_tokens = []
|
20 |
for token in tokens:
|
21 |
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
22 |
-
# If the current token continues the entity of the last one, merge them
|
23 |
last_token = merged_tokens[-1]
|
24 |
last_token['word'] += token['word'].replace('##', '')
|
25 |
last_token['end'] = token['end']
|
26 |
last_token['score'] = (last_token['score'] + token['score']) / 2
|
27 |
else:
|
28 |
-
# Otherwise, add the token to the list
|
29 |
merged_tokens.append(token)
|
30 |
-
|
31 |
return merged_tokens
|
32 |
|
|
|
|
|
|
|
33 |
# Initialize Model
|
34 |
-
get_completion = pipeline("ner", model="kazalbrur/bangla-english-med-bert-ner")
|
35 |
|
36 |
@spaces.GPU(duration=120)
|
37 |
def ner(input: str) -> Dict[str, Any]:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
Returns:
|
46 |
-
Dict[str, Any]: A dictionary containing the original text and a list of identified entities
|
47 |
-
with merged tokens.
|
48 |
-
- "text": The original input text.
|
49 |
-
- "entities": A list of dictionaries, where each dictionary contains information
|
50 |
-
about a recognized entity, including the word, entity type, score, and positions.
|
51 |
-
"""
|
52 |
-
output = get_completion(input)
|
53 |
-
merged_tokens = merge_tokens(output)
|
54 |
-
return {"text": input, "entities": merged_tokens}
|
55 |
|
56 |
####### GRADIO APP #######
|
57 |
title = """<h1 id="title"> Bangla Banglish and English Bio-Medical Entity Recognition </h1>"""
|
@@ -60,4 +38,23 @@ description = """
|
|
60 |
- The model used for Recognizing entities [BERT-BASE-NER](https://huggingface.co/kazalbrur/bangla-english-med-bert-ner).
|
61 |
"""
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import spaces
|
3 |
from transformers import pipeline
|
4 |
from typing import List, Dict, Any
|
5 |
+
import torch
|
6 |
|
7 |
def merge_tokens(tokens: List[Dict[str, any]]) -> List[Dict[str, any]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
merged_tokens = []
|
9 |
for token in tokens:
|
10 |
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
|
|
11 |
last_token = merged_tokens[-1]
|
12 |
last_token['word'] += token['word'].replace('##', '')
|
13 |
last_token['end'] = token['end']
|
14 |
last_token['score'] = (last_token['score'] + token['score']) / 2
|
15 |
else:
|
|
|
16 |
merged_tokens.append(token)
|
|
|
17 |
return merged_tokens
|
18 |
|
19 |
+
# Determine device
|
20 |
+
device = 0 if torch.cuda.is_available() else -1
|
21 |
+
|
22 |
# Initialize Model
|
23 |
+
get_completion = pipeline("ner", model="kazalbrur/bangla-english-med-bert-ner", device=device)
|
24 |
|
25 |
@spaces.GPU(duration=120)
|
26 |
def ner(input: str) -> Dict[str, Any]:
|
27 |
+
try:
|
28 |
+
output = get_completion(input)
|
29 |
+
merged_tokens = merge_tokens(output)
|
30 |
+
return {"text": input, "entities": merged_tokens}
|
31 |
+
except Exception as e:
|
32 |
+
return {"text": input, "entities": [], "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
####### GRADIO APP #######
|
35 |
title = """<h1 id="title"> Bangla Banglish and English Bio-Medical Entity Recognition </h1>"""
|
|
|
38 |
- The model used for Recognizing entities [BERT-BASE-NER](https://huggingface.co/kazalbrur/bangla-english-med-bert-ner).
|
39 |
"""
|
40 |
|
41 |
+
css = '''
|
42 |
+
h1#title {
|
43 |
+
text-align: center;
|
44 |
+
}
|
45 |
+
'''
|
46 |
+
|
47 |
+
theme = gr.themes.Soft()
|
48 |
+
demo = gr.Blocks(css=css, theme=theme)
|
49 |
+
|
50 |
+
with demo:
|
51 |
+
gr.Markdown(title)
|
52 |
+
gr.Markdown(description)
|
53 |
+
gr.Interface(
|
54 |
+
fn=ner,
|
55 |
+
inputs=[gr.Textbox(label="Enter Your Text to Find Entities", lines=10)],
|
56 |
+
outputs=[gr.HighlightedText(label="Text with entities")],
|
57 |
+
allow_flagging="never"
|
58 |
+
)
|
59 |
+
|
60 |
+
demo.launch()
|