任务概览:
1、模型下载:使用Hugging Face平台、魔搭社区平台(可选)和魔乐社区平台(可选)下载文档中提到的模型(至少需要下载config.json文件、model.safetensors.index.json文件),请在必要的步骤以及结果当中截图。
2、模型上传(可选):将我们下载好的config.json文件(也自行添加其他模型相关文件)上传到对应HF平台和魔搭社区平台,并截图。
3、Space上传(可选):在HF平台上使用Spaces并把intern_cobuild部署成功,关键步骤截图。
解题开始:
1、模型下载
注册Hugging Face 平台
填写相关信息后,验证e-mail,成功注册。
2、InternLM模型下载
我们进入Models模块,进行InternLM的搜索,并下载
Models - Hugging Face
https://huggingface.co/models
这里我以internlm2_5-1_8b举例,搜索Hugging Face上该模型的结果。
进入GitHub的codespaces,前提是需要创建自己的Repository,并且需要将仓库克隆到本地。你可以在仓库页面上找到“Clone or download”按钮,点击它并选择“Open in desktop”来使用GitHub Desktop进行克隆,或者复制仓库的URL,使用命令行工具如git clone进行克隆。完成后可以进入如下图的界面,否则会提示Setting up your codespace。
我们先在终端中安装相关的依赖。
# 安装transformers
pip install transformers==4.38
pip install sentencepiece==0.1.99
pip install einops==0.8.0
pip install protobuf==5.27.2
pip install accelerate==0.33.0
以下载模型的配置文件为例,先新建一个hf_demo.py 文件
touch hf_demo.py
内容如下:
import os
from huggingface_hub import hf_hub_download
# 指定模型标识符
repo_id = "internlm/internlm2_5-7b"
# 指定要下载的文件列表
files_to_download = [
{"filename": "config.json"},
{"filename": "model.safetensors.index.json"}
]
# 创建一个目录来存放下载的文件
local_dir = f"{repo_id.split('/')[1]}"
os.makedirs(local_dir, exist_ok=True)
# 遍历文件列表并下载每个文件
for file_info in files_to_download:
file_path = hf_hub_download(
repo_id=repo_id,
filename=file_info["filename"],
local_dir=local_dir
)
print(f"{file_info['filename']} file downloaded to: {file_path}")
运行该文件(注意文件目录请在该文件所在目录下运行该文件)
python hf_demo.py
可以看到,已经从Hugging Face上下载了相应配置文件
当然,这只是一个配置文件,并没有没有完全下载internlm2_5-7b-chat模型,因为空间有限,这里下载一个internlm2_5-1_8B模型作为demo运行,方法和前面是一样的。
以下载模型的配置文件为例,先新建一个hf_demo.py 文件
touch hf_demo_1.8.py
内容如下(可能因为版本迭代,代码与教程稍有区别,亲测可用):
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2_5-1_8b", trust_remote_code=True, force_download=True)
model = AutoModelForCausalLM.from_pretrained("internlm/internlm2_5-1_8b", torch_dtype=torch.float16, trust_remote_code=True, force_download=True)
model = model.eval()
inputs = tokenizer(["A beautiful flower"], return_tensors="pt")
gen_kwargs = {
"max_length": 128,
"top_p": 0.8,
"temperature": 0.8,
"do_sample": True,
"repetition_penalty": 1.0
}
output = model.generate(**inputs, **gen_kwargs)
output = tokenizer.decode(output[0].tolist(), skip_special_tokens=True)
print(output)
运行该文件(注意文件目录请在该文件所在目录下运行该文件)
python hf_demo_1.8.py
根据题目要求,成功下载两个文件
接下来说一下Hugging Face Spaces的使用
登录https://huggingface.co/spaces,在创建页面中,输入项目名为intern_cobuild
,并选择Static
应用进行创建
切换回codespaces,并且克隆下来
cd /workspaces/codespaces-jupyter
# 请将<your_username>替换你自己的username
git clone https://huggingface.co/spaces/<your_username>/intern_cobuild
cd /workspaces/codespaces-jupyter/intern_cobuild
找到该目录文件夹下的index.html文件,修改我们的html代码
保存后就可以push到远程仓库上了,它会自动更新页面。
git add .
git commit -m "update: colearn page"
git push
3、模型上传和Space上传(可选)
通过CLI上传 Hugging Face同样是跟Git相关联,通常大模型的模型文件都比较大,因此我们需要安装git lfs,对大文件系统支持。
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
# sudo apt-get install git-lfs # CodeSpace里面可能会有aptkey冲突且没有足够权限
git lfs install # 直接在git环境下配置git LFS
pip install huggingface_hub
发现无权限,创建一个可写的token
接着可以在CodeSpace里面,使用
git config --global credential.helper store
huggingface-cli login
创建项目
cd /workspaces/codespaces-jupyter
#intern_study_L0_4就是model_name
huggingface-cli repo create intern_study_L0_4
# 克隆到本地 your_huggingface_name 注意替换成你自己的
git clone https://huggingface.co/{your_huggingface_name}/intern_study_L0_4
克隆好之后,刷新文件目录可以看到克隆好的intern_study_L0_4
文件夹。
克隆好之后,刷新文件目录可以看到克隆好的intern_study_L0_4
文件夹。
我们可以把训练好的模型保存进里面,这里只是演示,上传我们刚刚下载好的config.json,把它复制粘贴进这个文件夹里面,还可以写一个demo.md文件,比如可以粘贴以下内容:
# 书生浦语大模型实战营(第四期)
- hugging face模型上传测试
- 更多内容请访问 https://github.com/InternLM/Tutorial/tree/camp4
用git提交到远程仓库
cd intern_study_L0_4
git add .
git commit -m "add:intern_study_L0_4"
git push
注意,如果git push 报错,可能是第一次上传时需要验证,请使用以下命令,注意替换<>里面的内容,然后再次git push一下就可以了
git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
# 如 git remote set-url origin https://blank:hf_xxxxxxxxxxx@huggingface.co/blank/intern_study_L0_4
# 这里blank和hf_xxxxxxxxxxxx只是示例 请替换为你的username和之前申请的access token
git pull origin
现在可以在Hugging Face的个人profile里面看到这个model