Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForTokenClassification,AutoTokenizer,pipeline
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
model = AutoModelForTokenClassification.from_pretrained('uer/roberta-base-finetuned-cluener2020-chinese',local_files_only=True)#cache_dir="C:\2023\Huggingface_4_12\Gradio Tutorial",force_download=True)
|
5 |
+
# model = AutoModelForTokenClassification.from_pretrained('C:\\2023\Huggingface_4_12\Gradio Tutorial\cache3\Huggingface_4_12\Gradio Tutorial\models--uer--roberta-base-finetuned-cluener2020-chinese\blobs\3d20fdef0b0f04d283e1693ef4c030b133fa7c3c')
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-finetuned-cluener2020-chinese',local_files_only=True)
|
7 |
+
|
8 |
+
|
9 |
+
ner_pipeline = pipeline('ner', model=model, tokenizer=tokenizer)
|
10 |
+
examples=["江苏警方通报特斯拉冲进店铺","李沐,深度学习专家。\
|
11 |
+
李沐于2004年进入上海交通大学计算机科学与工程系进行本科学习;2009年至2010年担任香港科技大学研究助理;2011年至2012年担任百度高级研究员;2012年至2017年在美国卡内基梅隆大学攻读博士学位。2019年编著的《动手学深度学习》出版。\
|
12 |
+
李沐专注于分布式系统和机器学习算法的研究。"]
|
13 |
+
|
14 |
+
def ner(text):
|
15 |
+
output1 = ner_pipeline(text)
|
16 |
+
|
17 |
+
output = [output1[0]]
|
18 |
+
if output[0]['entity'][1] == '-':
|
19 |
+
output[0]['entity'] = output[0]['entity'][2:len(output[0]['entity'])]
|
20 |
+
# j = 0
|
21 |
+
for i in range(1,len(output1)):
|
22 |
+
if output1[i]['entity'][1] == '-':
|
23 |
+
output1[i]['entity'] = output1[i]['entity'][2:len(output1[i]['entity'])]
|
24 |
+
dict1 = output1[i]
|
25 |
+
u = len(output) - 1
|
26 |
+
dict0 = output[u]
|
27 |
+
if (dict0['end'] == dict1['start']) and (dict0['entity'] == dict1['entity']):
|
28 |
+
dict = {
|
29 |
+
'entity':dict0['entity'],
|
30 |
+
'score':min(dict0['score'],dict1['score']),
|
31 |
+
'index':dict1['index'],
|
32 |
+
'word':dict0['word']+dict1['word'],
|
33 |
+
'start':dict0['start'],
|
34 |
+
'end':dict1['end'],
|
35 |
+
}
|
36 |
+
|
37 |
+
output[len(output) - 1] = dict
|
38 |
+
else:
|
39 |
+
dict = dict1
|
40 |
+
output.append(dict)
|
41 |
+
# print('output_before',output)
|
42 |
+
# print('output_after',output)
|
43 |
+
return {"text": text, "entities": output}
|
44 |
+
|
45 |
+
demo = gr.Interface(ner,
|
46 |
+
gr.Textbox(placeholder="Enter sentence here..."),
|
47 |
+
gr.HighlightedText(),
|
48 |
+
examples=examples)
|
49 |
+
|
50 |
+
demo.launch()
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|