xomlac-NER / app.py
b3x0m's picture
Update app.py
d5d78ba verified
raw
history blame
1.4 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) -> Dict[str, Any]:
output = get_completion(input)
merged_tokens = merge_tokens(output)
return {"text": input, "entities": merged_tokens}
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()