选一个支持中文的 文本总结模型
下载模型到本地 https://huggingface.co/csebuetnlp/mT5_multilingual_XLSum/tree/main
from transformers import pipeline
get_completion = pipeline("summarization", model="D:/huggingface/hub/csebuetnlp/mT5_multilingual_XLSum")
def summarize(input):
output = get_completion(input)
return output[0]['summary_text']
if __name__ == '__main__':
text = ('''埃菲尔铁塔高324米(1,063英尺),大约与一座81层高的建筑物同样高,并且是巴黎最高的建筑。
塔基为正方形,每边长125米(410英尺)。在建造过程中,埃菲尔铁塔超过华盛顿纪念碑,
成为世界上最高的人造建筑,并保持这一纪录长达41年,直到1930年纽约的克莱斯勒大楼完工。
它是第一个高度达到300米的建筑。由于在1957年顶部增加了广播天线,埃菲尔铁塔比克莱斯勒大楼高出5.2米(17英尺)。
不包括发射装置,埃菲尔铁塔是法国第二高的独立建筑,仅次于米拉山谷高架桥。''')
res = get_completion(text)
print(res)
输出:[{'summary_text': '法国首都巴黎的埃菲尔铁塔(Eiffel Tower)星期一(7月8日)正式对外开放。'}]
这个模型的总结效果好像很差,只做演示,就不去找更好的模型了
import gradio as gr
gr.close_all()
app = gr.Interface(fn=summarize, inputs='text', outputs='text')
app.launch(share=True) # share开启公网访问
输出:
Running on local URL: http://127.0.0.1:7860
Running on public URL: https://421c7a24af2xac50e1.gradio.live
This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)
点击打开浏览器即可访问,也可以在公网上访问(方便展示)
gr.close_all()
app = gr.Interface(
fn=summarize,
inputs=[gr.Textbox(label="输入你想要总结的文本", lines=6)],
outputs=[gr.Textbox(label="总结的文本", lines=3)],
title="使用模型总结文本",
description="使用神经网络模型对文本进行总结摘要。")
app.launch(share=True)
命名体识别: Named Entity Recognition
get_completion1 = pipeline("ner", model="D:/huggingface/hub/Davlan/distilbert-base-multilingual-cased-ner-hrl")
def ner(input_text):
out = get_completion1(input_text)
return {'text': input_text, 'entities': out}
print(get_completion1('我是michael,我正在学习AIGC的应用demo,很高兴一起分享知识'))
# [{'entity': 'B-ORG', 'score': 0.9991766, 'index': 11, 'word': 'AI', 'start': 15, 'end': 17},
# {'entity': 'I-ORG', 'score': 0.9994055, 'index': 12, 'word': '##GC', 'start': 17, 'end': 19}]
标注形式:
将每个元素标注为B-X
、I-X
或者O
。
B-X
表示此元素所在的片段属于X类型并且此元素在此片段的开头
BeginI-X
表示此元素所在的片段属于X类型并且此元素在此片段的中间
Inner位置O
Other表示不属于任何类型。常见后缀 X
gr.close_all()
app = gr.Interface(fn=ner,
inputs=[gr.Textbox(label="输入想要识别的文本", lines=6)],
outputs=[gr.Highlightedtext(label="高亮后的实体文本", lines=3)],
title="使用NER抽取实体",
description="使用神经网络模型对文本进行命名体识别。",
allow_flagging='never',
examples=['我叫michael,我住在地球', '我叫波利,我住在树上'])
app.launch(share=True)
加一个辅助函数,把同一个实体合并起来
def merge_tokens(tokens):
merged_tokens = []
for token in tokens:
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
# If current token continues the entity of the last one, merge them
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:
# Otherwise, add the token to the list
merged_tokens.append(token)
return merged_tokens
def ner(input_text):
out = get_completion1(input_text)
merged_tokens = merge_tokens(out)
return {'text': input_text, 'entities': merged_tokens}
模型识别了:波利
是一个 PER
人名