前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 gradio 创建 NLP任务 demo App

使用 gradio 创建 NLP任务 demo App

作者头像
Michael阿明
发布2023-07-31 11:40:34
2970
发布2023-07-31 11:40:34
举报

1. 文本总结 App

选一个支持中文的 文本总结模型

在这里插入图片描述
在这里插入图片描述

下载模型到本地 https://huggingface.co/csebuetnlp/mT5_multilingual_XLSum/tree/main

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
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日)正式对外开放。'}]

这个模型的总结效果好像很差,只做演示,就不去找更好的模型了

2. gr.Interface

代码语言:javascript
复制
import gradio as gr
gr.close_all()
app = gr.Interface(fn=summarize, inputs='text', outputs='text')
app.launch(share=True) # share开启公网访问

输出:

代码语言:javascript
复制
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)

点击打开浏览器即可访问,也可以在公网上访问(方便展示)

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
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)
在这里插入图片描述
在这里插入图片描述

3. 命名体识别 App

命名体识别: Named Entity Recognition

代码语言:javascript
复制
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-XI-X或者O

  • B-X表示此元素所在的片段属于X类型并且此元素在此片段的开头 Begin
  • I-X表示此元素所在的片段属于X类型并且此元素在此片段的中间Inner位置
  • OOther表示不属于任何类型。

常见后缀 X

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
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)
在这里插入图片描述
在这里插入图片描述

加一个辅助函数,把同一个实体合并起来

代码语言:javascript
复制
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 人名

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 文本总结 App
    • 2. gr.Interface
      • 3. 命名体识别 App
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档