xomlac-NER / app.py
b3x0m's picture
Update app.py
0742d78 verified
raw
history blame
1.79 kB
import gradio as gr
from transformers import pipeline
from typing import List, Dict, Any
def merge_tokens(tokens: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
merged_tokens = []
for token in tokens:
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
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:
merged_tokens.append(token)
return merged_tokens
get_completion = pipeline("ner", model="b3x0m/bert-xomlac-ner")
def ner(input: str) -> str:
output = get_completion(input)
merged_tokens = merge_tokens(output)
entity_map = {
"PER": "tên người",
"LOC": "địa điểm",
"ORG": "tổ chức",
"MISC": "vị trí",
}
result = []
for token in merged_tokens:
entity = token['entity']
if entity in entity_map: # Filter only relevant entities
entity_label = entity_map.get(entity, "khác")
result.append(f"{token['word']} ({entity_label})")
return ", ".join(result)
css = '''
h1#title {
text-align: center;
}
'''
theme = gr.themes.Soft()
demo = gr.Blocks(css=css, theme=theme)
with demo:
interface = gr.Interface(fn=ner,
inputs=[gr.Textbox(label="Input text", lines=10)],
outputs=[gr.HighlightedText(label="Output")],
allow_flagging="never",
examples=["灵符山道场之外,玄玉子、赵成等诸多灵符山高层落座。", "李雷和韩梅梅今天一起去北京旅游。"])
demo.launch()