Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from typing import List, Dict, Any
|
4 |
+
|
5 |
+
def merge_tokens(tokens: List[Dict[str, any]]) -> List[Dict[str, any]]:
|
6 |
+
merged_tokens = []
|
7 |
+
for token in tokens:
|
8 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
9 |
+
last_token = merged_tokens[-1]
|
10 |
+
last_token['word'] += token['word'].replace('##', '')
|
11 |
+
last_token['end'] = token['end']
|
12 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
13 |
+
else:
|
14 |
+
merged_tokens.append(token)
|
15 |
+
|
16 |
+
return merged_tokens
|
17 |
+
|
18 |
+
get_completion = pipeline("ner", model="b3x0m/bert-xomlac-ner")
|
19 |
+
|
20 |
+
def ner(input: str) -> Dict[str, Any]:
|
21 |
+
output = get_completion(input)
|
22 |
+
merged_tokens = merge_tokens(output)
|
23 |
+
return {"text": input, "entities": merged_tokens}
|
24 |
+
|
25 |
+
css = '''
|
26 |
+
h1#title {
|
27 |
+
text-align: center;
|
28 |
+
}
|
29 |
+
'''
|
30 |
+
|
31 |
+
theme = gr.themes.Soft()
|
32 |
+
demo = gr.Blocks(css=css, theme=theme)
|
33 |
+
|
34 |
+
with demo:
|
35 |
+
interface = gr.Interface(fn=ner,
|
36 |
+
inputs=[gr.Textbox(label="Input text", lines=10)],
|
37 |
+
outputs=[gr.HighlightedText(label="Output")],
|
38 |
+
allow_flagging="never",
|
39 |
+
examples=["灵符山道场之外,玄玉子、赵成等诸多灵符山高层落座。", "李雷和韩梅梅今天一起去北京旅游。"])
|
40 |
+
|
41 |
+
demo.launch()
|