【项目实战】HuggingFace教程,初步实战,使用HF做一些小型任务

一、前期准备工作

1.会使用Conda创建自己的虚拟环境
2.会激活自己的虚拟环境
3.了解一定pytorch基础

官方要求:
python 3.6+
pytorch 1.1.0+
TensorFlow 2.0+
本文使用的环境:
python 3.7.1
pytorch 1.13.1 py3.7_cuda11.7_cudnn8_0
tensorflow 1.15.0
官方要求图片实例

二、学习pipline

pipeline是Hugging Face Transformers库中的一个高层API,旨在简化各种自然语言处理任务的执行。通过它,用户可以在几行代码内实现从模型加载到推理的整个流程,而不需要深入了解模型的架构细节。

pipeline 支持多种常见任务,包括:

  • 文本分类(如情感分析):对输入文本进行分类,返回类别标签和置信度。
  • 问答:基于上下文回答问题。
  • 文本生成(如对话生成):基于输入提示生成文本片段。
  • 翻译:将文本从一种语言翻译成另一种语言。
  • 填空(填充掩码):完成缺失的词或短语,适用于填空任务。

通过指定任务名称,如pipeline("sentiment-analysis"),可以直接加载相关的预训练模型和分词器,使开发过程更加高效直观。

2.1.试运行代码,使用HuggingFace下载模型

Hugging Face的pipeline方法下载的模型默认会保存在用户目录下的.cache文件夹中,具体路径是:
在这里插入图片描述

C:/Users/11874/.cache/huggingface/transformers/

在这里插入图片描述

  • 这里的代码是因为需要网络代理(科学上网)才可以下载huggingface的模型
  • 查找自己网络代理中的端口号,本文的端口号是7890

2.2. 例子1,情感检测分析(只有积极和消极两个状态)

# 这里从Hugging Face的Transformers库中导入pipeline函数
# pipeline是一个高层API,便于直接调用预训练模型完成特定任务。
from transformers import pipeline

# 这里使用Python的os模块设置了环境变量,将代理服务器的地址和端口号设置为
# 127.0.0.1:7890。这种设置通常用于需要通过代理访问互联网的情况
# 帮助解决从Hugging Face Hub下载模型时的网络连接问题。
import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

# 这里通过pipeline创建了一个情感分析任务的管道。它会自动下载并加载一个适合情感分析的预训练模型(如基于BERT或DistilBERT的模型),并准备好用于推理。
classifier = pipeline("sentiment-analysis")

# 这里调用classifier对输入的句子执行情感分析。模型会根据句子内容预测情感标签(例如"positive"或"negative"),并返回分类结果及其置信度。
res = classifier("I have been waiting for a HuggingFace course my whole life.")

print(res)

输出:

[{'label': 'NEGATIVE', 'score': 0.9484281539916992}]

2.3. 例子2,文本生成

from transformers import pipeline

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

# 通过pipeline函数创建了一个文本生成的管道,并指定模型为distilgpt2,
# 这是一个较轻量的GPT-2模型。pipeline会自动从Hugging Face Hub下载该模型。
generator = pipeline("text-generation", model="distilgpt2")

res = generator(
    "In my home, I have a", # 对此内容扩写
    max_length = 30, # 生成的文本长度上限为30个token。
    num_return_sequences = 2, # 生成两个不同的文本序列,提供不同的生成结果。
)

print(res)

输出:

[{'generated_text': 'In my home, I have a daughter, my son and her own daughter, and I have a son and daughter whose mom has been a patient with'}, 
{'generated_text': 'In my home, I have a couple dogs. Those were all my pets.\n“I started out in the farmhouse. I used to'}]

三、学会使用Tokenizer & Model

3.1.tokenizer(分词器)是处理文本数据的重要组件

  • exp1代码:使用了Hugging Face提供的高层次pipeline接口默认加载一个预训练的情感分析模型。这种方式简单易用,适合快速原型开发,但使用的具体模型和tokenizer不明确。

  • exp2代码:则显式地加载了特定的模型distilbert-base-uncased-finetuned-sst-2-english及其对应的tokenizer。通过AutoModelForSequenceClassificationAutoTokenizer,用户可以更灵活地选择和定制模型。这种方式适合对模型进行微调或需要特定模型功能的情况。

from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

# exp 1:这段代码使用了Hugging Face提供的高层次pipeline接口,默认加载一个预训练的情感分析模型。这种方式简单易用,适合快速原型开发,但使用的具体模型和tokenizer不明确。
classifier = pipeline("sentiment-analysis")
res = classifier("I have been waiting for a HuggingFace course my whole life.")
print(res)

# exp 2:这段代码则显式地加载了特定的模型distilbert-base-uncased-finetuned-sst-2-english及其对应的tokenizer。通过AutoModelForSequenceClassification和AutoTokenizer,用户可以更灵活地选择和定制模型。这种方式适合对模型进行微调或需要特定模型功能的情况。
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
res = classifier("I have been waiting for a HuggingFace course my whole life.")
print(res)

输出:

[{'label': 'POSITIVE', 'score': 0.9433633089065552}]
[{'label': 'POSITIVE', 'score': 0.9433633089065552}]

3.2.tokenizer对字符串处理过程

from transformers import AutoTokenizer, AutoModelForSequenceClassification

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

#这一步使用tokenizer对输入的句子进行编码。
sequence = "Playing computer game is simple."
res = tokenizer(sequence)
print(res) # {'input_ids': [101, 2652, 3274, 2208, 2003, 3722, 1012, 102], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1]}

# 这一行调用tokenizer.tokenize()方法将句子拆分为tokens(子词或词)。
# 在自然语言处理(NLP)中,分词通常是将文本切分为可以被模型处理的最小单位。
# 输出结果将是一个tokens的列表,例如:
tokens = tokenizer.tokenize(sequence) 
print(tokens) # ['Playing', 'computer', 'game', 'is', 'simple', '.']

# 这里使用tokenizer.convert_tokens_to_ids()方法将分词后的tokens转换为对应的ID。
# 每个token都有一个唯一的ID,这些ID可以被模型理解。
ids = tokenizer.convert_tokens_to_ids(tokens)
print(ids) # [2652, 3274, 2208, 2003, 3722, 1012]

# 使用tokenizer.decode()方法将token ID转换回原始字符串。这个过程将ID映射回对应的tokens,并将它们合并成一个可读的文本。
decoded_string = tokenizer.decode(ids)
print(decoded_string) # playing computer game is simple.

输出:

{'input_ids': [101, 2652, 3274, 2208, 2003, 3722, 1012, 102], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1]}
['playing', 'computer', 'game', 'is', 'simple', '.']
[2652, 3274, 2208, 2003, 3722, 1012]
playing computer game is simple.

四、pytorch的简单使用

from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

# 1.还是用之前的pipeline应用
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# 2.通常不止一个句子,这里我们多放一个句子,用list
text_Train = ["I love you, my wife.",
              "Suzhou is the worst place!"]
# 3.输出分析的情感
res = classifier(text_Train)
print(res)

# 4.使用分词器对输入的文本进行批处理,设置padding=True和truncation=True确保输入序列的长度一致。
max_length=512限制了输入的最大长度,return_tensors="pt"表示将输出转换为PyTorch的张量格式。
batch = tokenizer(text_Train, padding=True, truncation=True, max_length=512, return_tensors="pt")
print(batch)

# 5.torch.no_grad()用于关闭梯度计算,以节省内存和加速计算,因为在推理过程中不需要更新模型参数。
# model(**batch)将预处理过的批量输入传递给模型,返回的outputs包含了模型的原始输出(logits)。
# 使用F.softmax(outputs.logits, dim=1)计算每个类的概率分布。
# torch.argmax(predictions, dim=1)用于确定概率最高的类别标签,表示每个输入文本的最终情感预测。
with torch.no_grad():
    print("====================")
    outputs = model(**batch)
    print(outputs)
    predictions = F.softmax(outputs.logits, dim=1)
    print(predictions)
    labels = torch.argmax(predictions, dim=1)
    print(labels)

输出:
在这里插入图片描述

五、模型的保存save & 加载load

1.保存:这里指定了保存路径为当前工作目录下的一个名为saved的文件夹。如果没有特殊路径指定,模型和分词器会默认保存在你运行代码的当前目录下的saved文件夹中。这个文件夹会包含:

  • tokenizer配置文件,例如tokenizer_config.jsonvocab.txtspecial_tokens_map.json等。

  • 模型配置文件权重,如config.jsonpytorch_model.bin,这些文件包含模型的结构和权重。

2.加载:代码在后续使用时可以通过指定保存的目录(例如saved)来加载已经保存的模型分词器,避免重新下载模型。这在离线使用或需要跨项目共享时非常有用。

from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890"# !!!选自己代理的端口号,每个人的不一样

# 还是用之前的pipeline应用
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# 1.保存: tokenizer和model到一个目录下面
save_directory = "saved"  # 保存路径为当前工作目录下的一个名为saved的文件夹
tokenizer.save_pretrained(save_directory)
model.save_pretrained(save_directory)

# 2.加载:想再次加载他,使用下列方法
tok = AutoTokenizer.from_pretrained(save_directory)
mod = AutoModelForSequenceClassification.from_pretrained(save_directory)

输出:
在这里插入图片描述

六、学会自己找一个模型来玩

  1. 打开huggingface官网链接
  2. 点击Model
    3.模型过滤器:寻找自己需要的模型,下图,红色框框内是模型的:任务需求、所用的库、数据集、关于什么语言、许可证
  • Task:用于指定模型适合执行的任务类型,例如文本分类、翻译、图像生成等。
  • Libraries:指定模型所属的框架或库,例如 Transformers、Diffusers、TensorFlow、PyTorch 等。
  • Dataset:用于筛选经过特定数据集训练或验证的模型。
  • Language:用于筛选支持特定语言的模型。如英语、中文、法语。
  • License:用于筛选模型的使用许可,例如 MIT、Apache-2.0、或 CC-BY 等。不同的许可证会规定模型在商业和非商业项目中的使用限制。

在这里插入图片描述
4.实战,facebook对文本的总结模型

  • task中勾选summarization
    在这里插入图片描述
  • 选择facebook/bart-large-cnn模型
    在这里插入图片描述
  • 点进去后,有该模型的介绍,向下滑动,会有教你怎么使用这个模型,将红框内代码内容复制到python中跑一次,记住要使用代理的端口号
    在这里插入图片描述
    红框内代码如下,加上代理相关代码:
    pipeline中的参数代表就是上面介绍的过滤器
# 下列两行代码是一个意思,主要是展示pipeline中的参数代表就是上面介绍的过滤器
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
summarizer = pipeline(task = "summarization", model="facebook/bart-large-cnn")

全部代码如下:

from transformers import pipeline
# !!!选自己代理的端口号,每个人的不一样
import os
os.environ["http_proxy"] = "http://127.0.0.1:7890" # !!!选自己代理的端口号,每个人的不一样
os.environ["https_proxy"] = "http://127.0.0.1:7890" # !!!选自己代理的端口号,每个人的不一样
# !!!选自己代理的端口号,每个人的不一样
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison.  Her next court appearance is scheduled for May 18.
"""
print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))

输出:
在这里插入图片描述

七、微调Finetune自己的模型

huggingface微调模型doc
在这里插入图片描述

八、学会使用huggingface的文档!

非常重要,HF的文档可以找到任何你想找到的东西

在这里插入图片描述

<think>好的,我现在需要帮助用户找到在国内安装vLLM的方法或指南。用户之前已经提供了一些相关的引用内容,我需要仔细分析这些引用,并结合自己的知识,给出合适的步骤。 首先,用户提供的引用[2]和[3]都涉及vLLM的安装。引用[2]是一个实战指南,详细列出了安装步骤,包括创建虚拟环境、配置镜像源、安装核心组件等。引用[3]提到了vLLM官网的安装说明,但可能国内访问较慢,所以用户需要国内的方法。 接下来,我需要确保步骤的正确性。引用[2]中的步骤包括使用清华的镜像源,这对国内用户来说非常重要,可以加速下载。同时,安装vLLM时自动匹配CUDA版本,但用户需要确认CUDA版本是否兼容,比如检查驱动版本是否>=525.85.12。此外,创建虚拟环境可以避免依赖冲突,这也是推荐的法。 另外,引用[1]提到安装LLaMA Factory、AutoGPTQ和vLLM,但用户现在只关注vLLM,所以可以忽略其他部分。需要注意的是,用户可能已经安装了Python和相关工具,但最好在步骤中提醒安装必要的依赖,比如CUDA Toolkit。 还需要考虑可能的错误情况,比如网络问题导致安装失败,或者CUDA版本不匹配。解决方法可能包括更换镜像源、升级驱动等。此外,验证安装是否成功,比如运行简单的测试代码,可以确保安装正确。 最后,生成相关问题时要围绕安装过程中可能遇到的难点,比如镜像配置、环境问题、版本兼容性等,帮助用户进一步解决问题。</think>[用户可见层] ### 国内安装 vLLM 的详细步骤(适配网络环境与依赖加速) #### 一、前置条件准备 1. **Python 环境** - Python ≥3.8(推荐3.9+),使用以下命令验证版本: ```bash python3 --version ``` - 若未安装,可通过[清华镜像站](https://mirrors.tuna.tsinghua.edu.cn/help/python/)下载安装包 2. **CUDA 驱动兼容性** - 确认NVIDIA驱动版本 ≥525.85.12(支持CUDA 11.8+)[^2]: ```bash nvidia-smi ``` - 若驱动版本过低,需通过[NVIDIA官网](https://www.nvidia.cn/Download/index.aspx?lang=cn)更新 #### 二、安装流程(镜像加速版) 1. **创建隔离虚拟环境** ```bash python3 -m venv vllm_env # 创建名为vllm_env的虚拟环境 source vllm_env/bin/activate # 激活环境(Linux/Mac) ``` 2. **配置国内镜像源** ```bash pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn ``` 3. **安装核心组件** ```bash pip install vllm # 自动匹配CUDA版本(默认安装最新稳定版) pip install transformers==4.37.0 # 指定兼容版本[^3] ``` 4. **验证安装** ```python from vllm import LLM llm = LLM(model="facebook/opt-125m") # 加载小型测试模型 print("安装成功!") ``` #### 三、常见问题解决方案 | 问题现象 | 解决方法 | |---------|----------| | `CUDA error: no kernel image` | 检查CUDA版本与vLLM要求是否匹配,可尝试指定版本:<br>`pip install vllm==0.3.3 --force-reinstall` | | 下载模型超时 | 使用HuggingFace镜像站:<br>`export HF_ENDPOINT=https://hf-mirror.com` | | 内存不足报错 | 添加`--gpu-memory-utilization 0.8`参数限制显存使用 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值