Huggingface pipline使用

pipline是Huggingface的一个基本工具,可以理解为一个端到端(end-to-end)的一键调用Transformer模型的工具。它具备了数据预处理、模型处理、模型输出后处理等步骤,可以直接输入原始数据,给出预测结果,十分方便。

1.文本分类

from transformers import pipeline
#文本分类
classifier = pipeline("sentiment-analysis")
result = classifier("I hate you")[0]
print(result)
result = classifier("I love you")[0]
print(result)

{'label': 'NEGATIVE', 'score': 0.9991129040718079}

{'label': 'POSITIVE', 'score': 0.9998656511306763}

2.文本翻译

from transformers import pipeline
#翻译为德语
translator = pipeline("translation_en_to_de")
sentence = "Hugging Face is a technology company based in New York and Paris"
translator(sentence, max_length=40)

[{'translation_text': 'Hugging Face ist ein Technologieunternehmen mit Sitz in New York und Paris.'}]

3.文本生成

from transformers import pipeline
#文本生成
text_generator = pipeline("text-generation")
text_generator("As far as I am concerned, I will",max_length=50,do_sample=False)

[{'generated_text': 'As far as I am concerned, I will be the first to admit that I am not a fan of the idea of a "free market." I think that the idea of a free market is a bit of a stretch. I think that the idea'}]

4.完形填空

from transformers import pipeline
#完形填空
unmasker = pipeline("fill-mask")
sentence = 'HuggingFace is creating a <mask> that the community uses to solve NLP tasks.'
unmasker(sentence)

[{'score': 0.17927546799182892, 'token': 3944, 'token_str': ' tool', 'sequence': 'HuggingFace is creating a tool that the community uses to solve NLP tasks.'},

{'score': 0.1134939193725586, 'token': 7208, 'token_str': ' framework', 'sequence': 'HuggingFace is creating a framework that the community uses to solve NLP tasks.'},

{'score': 0.052435602992773056, 'token': 5560, 'token_str': ' library', 'sequence': 'HuggingFace is creating a library that the community uses to solve NLP tasks.'},

{'score': 0.034935541450977325, 'token': 8503, 'token_str': ' database', 'sequence': 'HuggingFace is creating a database that the community uses to solve NLP tasks.'},

{'score': 0.028602560982108116, 'token': 17715, 'token_str': ' prototype', 'sequence': 'HuggingFace is creating a prototype that the community uses to solve NLP tasks.'}]

5.阅读理解

from transformers import pipeline
#阅读理解
question_answerer = pipeline("question-answering")
context = r"""
Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a 
question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune 
a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.
"""
result = question_answerer(question="What is extractive question answering?",context=context)
print(result)
result = question_answerer(question="What is a good example of a question answering dataset?",context=context)
print(result)

{'score': 0.6177281141281128, 'start': 34, 'end': 95, 'answer': 'the task of extracting an answer from a text given a question'}

{'score': 0.5152307152748108, 'start': 148, 'end': 161, 'answer': 'SQuAD dataset'}

6.命名实体识别

from transformers import pipeline
#命名实体识别
ner_pipe = pipeline("ner")
sequence = """Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO,
therefore very close to the Manhattan Bridge which is visible from the window."""
for entity in ner_pipe(sequence):print(entity)

{'entity': 'I-ORG', 'score': 0.99957865, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2}

{'entity': 'I-ORG', 'score': 0.9909764, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7}

{'entity': 'I-ORG', 'score': 0.9982224, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12}

{'entity': 'I-ORG', 'score': 0.9994879, 'index': 4, 'word': 'Inc', 'start': 13, 'end': 16}

{'entity': 'I-LOC', 'score': 0.9994344, 'index': 11, 'word': 'New', 'start': 40, 'end': 43}

{'entity': 'I-LOC', 'score': 0.99931955, 'index': 12, 'word': 'York', 'start': 44, 'end': 48}

{'entity': 'I-LOC', 'score': 0.9993794, 'index': 13, 'word': 'City', 'start': 49, 'end': 53}

{'entity': 'I-LOC', 'score': 0.98625815, 'index': 19, 'word': 'D', 'start': 79, 'end': 80}

{'entity': 'I-LOC', 'score': 0.95142686, 'index': 20, 'word': '##UM', 'start': 80, 'end': 82}

{'entity': 'I-LOC', 'score': 0.9336589, 'index': 21, 'word': '##BO', 'start': 82, 'end': 84}

{'entity': 'I-LOC', 'score': 0.9761654, 'index': 28, 'word': 'Manhattan', 'start': 114, 'end': 123}

{'entity': 'I-LOC', 'score': 0.9914629, 'index': 29, 'word': 'Bridge', 'start': 124, 'end': 130}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/578.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二级分销小程序怎么做

【二级分销小程序功能介绍】 二级分销小程序是一款专门为企业提供分销管理的移动应用程序。它的主要功能包括商品管理、订单管理、分销设置、分销商等级、分销佣金、分销海报等方面&#xff0c;下面我们逐一进行介绍&#xff1a; 1. 商品管理&#xff1a; 二级分销小程序可以…

尚医通04:Axios Node Npm bable webpack+前端工程改造

目录 本日学习 内容介绍 Axios Node NPM包管理器 Babel 模块化 Webpack 搭建前端工程 前端框架介绍 前端开发过程介绍 登录改造成本地接口 本日学习 1. 了解Axios :他是异步请求用的&#xff0c;前后端。 用于在浏览器和 Node.js 中发送 HTTP 请求。它支持从服务器…

[桌面运维] 显示器 色准,色域,色深,分辨率,带宽,刷新率的基本概念,图像呈现的基本原理

⬜⬜⬜ &#x1f430;&#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea;(*^▽^*)欢迎光临 &#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea;&#x1f430;⬜⬜⬜ ✏️write in front✏️ &#x1f4dd;个人主页&#xff1a;陈丹宇jmu &am…

redis 设置expire=0和直接调用delete删除key的区别

背景 问题起源于生产上应用redis做的一个临时缓存的场景&#xff0c;调用方首先调用某个容器的生成接口&#xff0c;该接口会生成临时缓存的数据放到redis中&#xff0c;然后调用方在调用获取数据内容的接口从任何一台容器中获取放置在redis中的临时数据。 还原问题场景 由于…

华为数通智选交换机S5735S-L24T4S-QA2无法SSH远程访问

以前都是按照华为S5700交换机开启SSH远程访问方法配置不同网段通过静态路由实现互通,华为S5700交换机开启ssh远程登陆,现在新买的华为数通智选交换机S5735S-L24T4S-QA2,也是按照这步骤配置,令人不解的是,竟然无法ssh访问,仔细看了配置也没有发现问题,在华为eNSP模拟器上验…

初识mysql数据库之复合查询

目录 一、多表查询的概念 二、笛卡尔积 1. 笛卡尔积的概念 2. 笛卡尔积使用案例 2.1 显示雇员名、雇员工资以及所在部门的名字 2.2 显示部门号为10的部门名&#xff0c;员工名和工资 2.3 显示所有员工的姓名、工资和工资级别 3. 自连接 3.1 自连接的概念 3.2 自连接案…

微信小程序中的条件渲染和列表渲染,wx:if ,wx:elif,wx:else,wx:for,wx:key的使用,以及block标记和hidden属性的说明

微信小程序中的条件渲染和列表渲染 1. 条件渲染1.1. 语法格式 (wx:if, wx:elif ,wx:else)1.2. block标记1.3. hidden属性1.4. wx:if 与 hidden 的对比 2. 列表渲染2.1. wx:for 语法格式 及 wx:key的使用 1. 条件渲染 1.1. 语法格式 (wx:if, wx:elif ,wx:else) <view wx:if…

MachineLearningWu_13_AGI

AGI的全称是artificial general intelligence&#xff0c;通用人工智能&#xff0c;而我们现在做的关于医学影像的分析&#xff0c;可以说完全是ANI。 而我们使用MLP对于大脑中神经网络的模拟更是完全不同于人类大脑的行为。

如何开发一个软件

一、软件开发流程 1. 需求分析 主要有两个文档&#xff0c;一个需求规格说明书和一个产品原型图 2. 设计 UI设计、数据库设计、接口设计 3. 编码 项目代码编写、单元测试 4. 测试 测试用例、测试报告 5. 上线运维 软件环境安装、配置 二、角色分工 项目经理 对整个…

flutter开发实战-Running Gradle task ‘assembleDebug‘ 的解决方法

flutter开发实战-Running Gradle task ‘assembleDebug‘ 的解决方法 使用Android studio经常出现Running Gradle task ‘assembleDebug‘问题&#xff0c;记录一下解决方法。 一、在Android目录下更改build.gradle 将repositories中的google(), mavenCentral() repositori…

IIS 部署的应用禁用HTTP TRACE / TRACK方法【原理扫描】

远程Web服务器支持TRACE和/或TRACK方法。 TRACE和TRACK是用于调试Web服务器连接的HTTP方法。 直接在网站Web.config文件中进行如下操作&#xff1a;在Web.config中的<system.webServer>节点内添加以下配置即可&#xff1a; <security> <requestFiltering> &…

数据结构day2(2023.7.15)

一、Xmind整理&#xff1a; 二、课上练习&#xff1a; 练习1&#xff1a;定义车的信息&#xff1a;品牌&#xff0c;单价&#xff0c;颜色&#xff0c;车牌号 struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌…

XXF(x-forwarded-for)

IP伪造 TCP/IP层面的IP伪造很难实现&#xff0c;因为更改后很难实现正常的TCP通信&#xff0c;但在HTTP层面的伪造就显得很容易。可以通过伪造XFF头进行IP伪造 XFF字段 X-Forwarded-For(XFF)是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTT…

MySQL基础

MySQL基础 数据库基础&#xff08;重点&#xff09;一.登陆选项的认识二.什么是数据库三.见一见数据库四.主流数据库五.服务器、数据库、表之间的关系六.MySQL架构七.SQL分类八.存储引擎 数据库基础&#xff08;重点&#xff09; 一.登陆选项的认识 mysql -h 127.0.0.1 -P 33…

计算机网络 day5 子网划分 - IP包 - arp协议

目录 子网划分 为什么需要子网划分&#xff1f; 我们为什么不直接使用一个A类的IP地址给一家2000人的公司使用呢&#xff1f; 子网划分本质 子网划分的步骤&#xff1a; 实验&#xff1a;将192.168.1.0/24 划分为4个小网段 --》192.168.1.0/26 减少的IP地址去哪里了&…

“深入了解API和Python的完美结合:构建强大、灵活的应用程序“

引言&#xff1a; 在当前科技高速发展的时代&#xff0c;应用程序是各行各业的重要组成部分。而API&#xff08;Application Programming Interface&#xff09;作为不同系统之间实现通信和数据交换的桥梁&#xff0c;具有至关重要的作用。而Python作为一种强大而灵活的编程语言…

【C++】位图和布隆过滤器

文章目录 位图概念难点代码 布隆过滤器概念插入查找删除优缺点代码 位图 概念 所谓位图&#xff0c;就是用每一个比特位位来存放某种状态&#xff0c;适用于海量数据&#xff0c;数据无重复的场景。通常是用来判断某个数据存不存在的。 给40亿个不重复的无符号整数&#xff…

RestFul风格讲解

以前是localhost:8080/user?methodadd&uid1; RestFul风格是以/接上的 localhost:8080/user/马云/6562 package com.qf.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annot…

【华为机试】HJ17 坐标移动详解+完整源代码示例

忙碌了一周&#xff0c;一直没时间更新&#xff0c;趁着周末来更新第二个题目。 题目 题目解析 这个题目相比于上一个题目来说&#xff0c;会简单一些&#xff0c;不涉及到那些复杂的算法&#xff0c;就是对于字符串的处理。 算法步骤 输入一个字符串根据分号&#xff0c;将…

《银行法律法规》三、银行管理——1、银行管理基础

第一章 银行管理基础 第一节 商业银行的组织架构 考点1 商业银行组织架构的内涵★★ 含义 商业银行组织架构是银行各部分按照一定的排列顺序、 空间位置、 聚集状态、 联系方式以及各部分之间相互关系组成的一个有机系统。 这个系统涉及商业银行前台、 中台、 后台业务运…