Skip to content
微信公众号

Hugging Face

简介

Hugging Face 是一个提供先进自然语言处理(NLP)工具的平台,支持 Transformer 模型的开发和应用。它拥有庞大的模型库和社区资源,能够满足从研究到工业应用的各种需求。

注册和安装

  1. 注册 Hugging Face 账户

    1. 访问 Hugging Face 官方网站,点击右上角的“Sign Up”按钮。
    2. 输入你的邮箱、用户名和密码,完成注册流程。
    3. 注册成功后,你可以访问模型库、数据集和文档,也可以管理你的个人模型和项目。
  2. 安装 Hugging Face 库

Hugging Face 提供了 transformers 库,用于加载和使用模型。你可以使用以下命令来安装它:

shell
pip install transformers
pip install transformers

(电脑须安装基础环境:Anaconda,CUDA,cuDNN,pytorch)如果你还需要安装其他依赖库,如 datasets 和 tokenizers ,可以使用以下命令:

shell
pip install transformers datasets tokenizers
pip install transformers datasets tokenizers

模型探索与下载

在模型库中搜索模型

Hugging Face 提供了一个庞大的模型库,你可以通过以下步骤来查找所需的模型:

  1. 访问 模型库页面。
  2. 在搜索栏中输入关键字,如 "GPT-2" 或 "BERT",然后点击搜索。
  3. 你可以使用左侧的过滤器按任务、框架、语言等条件筛选模型。

下载与加载模型到指定文件夹

找到所需模型后,你可以通过代码将模型下载到指定的文件夹,并加载模型:

python
from transformers import AutoModel, AutoTokenizer
# 替换为你选择的模型名称
model_name = "bert-base-uncased"
# 指定模型保存路径
cache_dir = "./my_model_cache"
# 下载并加载模型和分词器到指定文件夹
model = AutoModel.from_pretrained(model_name, cache_dir=cache_dir)
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
from transformers import AutoModel, AutoTokenizer
# 替换为你选择的模型名称
model_name = "bert-base-uncased"
# 指定模型保存路径
cache_dir = "./my_model_cache"
# 下载并加载模型和分词器到指定文件夹
model = AutoModel.from_pretrained(model_name, cache_dir=cache_dir)
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)

Hugging Face API 使用

匿名访问 API

你可以通过 Hugging Face Inference API 匿名使用预训练模型(注意:匿名访问的模型受限于公开权限):

python
import requests
API_URL = "https://api-inference.huggingface.co/models/bert-base-chinese"
# 不使用 Authorization 头以进行匿名访问
response = requests.post(API_URL, json={"inputs": "你好,Hugging Face!"})
print(response.json())
import requests
API_URL = "https://api-inference.huggingface.co/models/bert-base-chinese"
# 不使用 Authorization 头以进行匿名访问
response = requests.post(API_URL, json={"inputs": "你好,Hugging Face!"})
print(response.json())

使用 Inference API

注册并获取 API Token 后,你可以使用自己的 API Token 进行访问:

python
from transformers import pipeline
# 替换为你的实际 API Token
API_TOKEN = "your_api_token_here"
# 使用 API Token
generator = pipeline("text-generation", model="gpt2", use_auth_token=API_TOKEN)
output = generator("The future of AI is", max_length=50)
print(output)
from transformers import pipeline
# 替换为你的实际 API Token
API_TOKEN = "your_api_token_here"
# 使用 API Token
generator = pipeline("text-generation", model="gpt2", use_auth_token=API_TOKEN)
output = generator("The future of AI is", max_length=50)
print(output)

使用 Transformers 库

文本生成

在线访问

使用 Hugging Face 的 Inference API 调用中文文本生成模型:

python
import requests
API_URL = "https://api-inference.huggingface.co/models/uer/gpt2-chinesecluecorpussmall"
API_TOKEN = "your_api_token_here" # 替换为你的实际 API Token
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# 发送文本生成请求
response = requests.post(API_URL, headers=headers, json={"inputs": "你好,我是一款
语言模型,"})
print(response.json())
import requests
API_URL = "https://api-inference.huggingface.co/models/uer/gpt2-chinesecluecorpussmall"
API_TOKEN = "your_api_token_here" # 替换为你的实际 API Token
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# 发送文本生成请求
response = requests.post(API_URL, headers=headers, json={"inputs": "你好,我是一款
语言模型,"})
print(response.json())

下载到本地访问

你可以将模型下载到本地,然后使用 pipeline 进行文本生成:

python
from transformers import pipeline
# 本地加载中文GPT-2模型
generator = pipeline("text-generation", model="uer/gpt2-chinesecluecorpussmall", cache_dir="./my_model_cache")
# 生成文本
output = generator("你好,我是一款语言模型,", max_length=50, num_return_sequences=1)
print(output)
from transformers import pipeline
# 本地加载中文GPT-2模型
generator = pipeline("text-generation", model="uer/gpt2-chinesecluecorpussmall", cache_dir="./my_model_cache")
# 生成文本
output = generator("你好,我是一款语言模型,", max_length=50, num_return_sequences=1)
print(output)

文本分类

在线访问

使用 Hugging Face 的 Inference API 调用中文文本分类模型:

python
import requests
API_URL = "https://api-inference.huggingface.co/models/uer/roberta-basefinetuned-cluener2020-chinese"
API_TOKEN = "your_api_token_here" # 替换为你的实际 API Token
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# 发送文本分类请求
response = requests.post(API_URL, headers=headers, json={"inputs": "我喜欢用
Hugging Face的transformers库!"})
print(response.json())
import requests
API_URL = "https://api-inference.huggingface.co/models/uer/roberta-basefinetuned-cluener2020-chinese"
API_TOKEN = "your_api_token_here" # 替换为你的实际 API Token
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# 发送文本分类请求
response = requests.post(API_URL, headers=headers, json={"inputs": "我喜欢用
Hugging Face的transformers库!"})
print(response.json())

下载到本地访问

你可以将模型下载到本地,然后使用 pipeline 进行文本分类:

python
from transformers import pipeline
# 本地加载中文RoBERTa模型
classifier = pipeline("sentiment-analysis", model="uer/roberta-base-finetunedcluener2020-chinese", cache_dir="./my_model_cache")
# 进行情感分析
result = classifier("我喜欢用Hugging Face的transformers库!")
print(result)
from transformers import pipeline
# 本地加载中文RoBERTa模型
classifier = pipeline("sentiment-analysis", model="uer/roberta-base-finetunedcluener2020-chinese", cache_dir="./my_model_cache")
# 进行情感分析
result = classifier("我喜欢用Hugging Face的transformers库!")
print(result)

datasets 库核心方法

加载数据集

你可以通过 load_dataset 方法加载任何数据集:

python
from datasets import load_dataset
# 加载GLUE数据集
dataset = load_dataset("glue", "mrpc")
print(dataset)
from datasets import load_dataset
# 加载GLUE数据集
dataset = load_dataset("glue", "mrpc")
print(dataset)

加载磁盘数据

你可以加载本地磁盘上的数据:

python
from datasets import load_from_disk
# 从本地磁盘加载数据集
dataset = load_from_disk("./my_dataset")
print(dataset)
from datasets import load_from_disk
# 从本地磁盘加载数据集
dataset = load_from_disk("./my_dataset")
print(dataset)

本站总访问量次,本站总访客数人次
Released under the MIT License.