logo

LLM 框架--Huggingface

王哲峰 / 2024-06-15


目录

Huggging Face NLP ecosystem

img

NLP 介绍

NLP 任务:

Transformers

简介

为什么要用 transformers?

安装

pip:

$ pip install transformers

conda:

$ conda install conda-forge::transformers

使用

快速上手

  1. 使用 pipeline 判断正负面情绪
from transformers import pipeline

classifier = pipeline("sentiment-analysis")
res1 = classifier("We are very happy to introduce pipeline to the transformers repository.")
res2 = classifier(
    ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"]
)
print(res)
[{'label': 'POSITIVE', 'score': 0.9996980428695679}]
  1. 从给定文本中抽取问题答案
from transformers import pipeline

question_answerer = pipeline("question-answering")
res = question_answerer({
    "question": "What is the name of the repository ?",
    "context": "Pipeline has been included in the huggingface/transformers repository",
})
print(res)
  1. 可以在任务中下载、上传、使用任意预训练模型。
from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
model = AutoModel.from_pretrained("google-bert/bert-base-uncase")
inputs = tokenizer("Hello world!", return_tensors = "pt")
outputs = model(**inputs)

工具 pipeline

https://huggingface.co/docs/transformers/main_classes/pipelines#pipelines

可用的 pipeline:

Fine-tuning Pretrained Model

处理数据

Fine-tuning model

分享 Models 和 Tokenizers

模型

Datasets

Tokenizers

Accelerate

Hugging Face Hub

参考