1 注册Hugging Face 平台,创建Hugging Face Spaces
2 InternLM模型下载
(1)下载internlm2_5-7b-chat的配置文件
创建名为hf_download_josn.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}")
(2)下载internlm2_5-chat-1_8b并打印示例输出
创建名为hf_download_1_8_demo.py的文件,复制以下代码并运行:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2_5-1_8b", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("internlm/internlm2_5-1_8b", torch_dtype=torch.float16, trust_remote_code=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)
输出如下图:
4 Hugging Face Spaces的使用
创建项目intern_cobuild并
clone。
在CodeSpace终端输入:
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代码为:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>My static Space</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
iframe {
width: 430px;
height: 932px;
border: none;
}
</style>
</head>
<body>
<iframe src="https://colearn.intern-ai.org.cn/cobuild" title="description"></iframe>
</body>
</html>
保存后就可以push到远程仓库上
git add .
git commit -m "update: colearn page"
git push
如果报错:remote: Password authentication in git is no longer supported. You must use a user access token or an SSH key instead.
请再次设置这个项目的验证,这个地方需要用户的Access Tokens(具体获取方式见下文 "2.1.5 模型上传")
git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
例如:
git remote set-url origin https://jack:hf_xxxxx@huggingface.co/spaces/jack/intern_cobuild/
然后再次git push即可
进入Space界面,就可以看到如图所示:
5 模型上传
安装git lfs,并通过CLI上传 Hugging Face
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
使用huggingface-cli login命令进行登录。需要输入用户的Access Tokens,在Hugging Face中创建一个类型为“Write”的token。
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_github_name 注意替换成你自己的
git clone https://huggingface.co/{your_github_name}/intern_study_L0_4
把config.json复制粘贴进这个文件夹里面 ,并且写一个README.md文件,内容如下。
# 书生浦语大模型实战营camp4
- 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
即可以在Hugging Face的个人profile里面看到这个model,如图: