🗝️ 前置操作 - GitHub CodeSpace
关于 GitHub CodeSpace
Github CodeSpace 是 Github 推出的线上代码平台,提供了一系列 templates(项目模板) ,供我们快速搭建项目,进行在线开发。
我们这里选择 Jupyter Notebook 进行创建环境。创建好环境后,可以进入网页版 VS Code 的界面,这就是 CodeSpace 提供给我们的在线编程环境。
每个 CodeSpace 都相当于一个虚拟机,它们是彼此独立的,因此可以单独配置对应环境。
🎯 任务1 - 模型下载
在 Hugging Face 社区下载模型
以下操作建议在 GitHub CodeSpace 中执行
- 进入模型页面: huggingface.co/internlm/in…
- 安装依赖
# 安装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_download_josn.py
touch hf_download_josn.py
# 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}")
关于 huggingface_hub
库中 “文件下载” 相关的文档: Download Files from the Hub
由于 7B 模型相对较大,在上面的代码中,我们通过变量 files_to_download
指定了需要下载的文件(模型的 config.json
文件),没有完全下载 internlm2_5-7b-chat
模型。
使用 python hf_download_josn.py
执行脚本,便可以看到下载完毕的文件。
- 新建用于下载模型全部文件的操作文件
hf_download_1_8_demo.py
touch 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)
使用 python hf_download_1_8_demo.py
命令执行,可得到结果:
🎈 任务2 - 模型上传
在 Hugging Face 社区上传模型
Hugging Face 通过 CLI 上传模型,其同样是和 Git 相关联。但由于模型文件通常较大,需要安装 Git LFS 以支持大文件系统。
- 安装 Git LFS
# 在 GitHub CodeSpace 安装
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
# 在 apt 中安装(CodeSpace里面可能会有aptkey冲突且没有足够权限,故不使用此方法)
# sudo apt-get install git-lfs
git lfs install # 直接在git环境下配置git LFS
- 安装 Hugging Face 库
pip install huggingface_hub
- 在 CLI 中登录至 Hugging Face 社区
git config --global credential.helper store # 第一次需进行该项配置,允许凭证存储
huggingface-cli login # 登录命令
此处需要键入 Token 进行登录,Token 需到 huggingface.co/settings/to… 页面添加。
输入完 Token 后,即可完成登录:
- 创建模型仓库并克隆
#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
-
添加模型文件及介绍, push 到仓库
-
将先前下载好的 config.json 文件复制到仓库目录下:
- 保存并上传至模型仓库:
cd intern_study_L0_4
git add .
git commit -m "add:intern_study_L0_4"
git push
- 如
git push
时报错,可能是首次上传需验证身份,需按下面的方式进行配置。 git remote set-url origin https://<user_name>:@huggingface.co/<repo_path> # 注意替换< >里面的内容 # user_name 为 Hugging Face 用户名 # token 为先前步骤在 Hugging Face 中新建的 Token # repo_path 为仓库地址 git pull origin # 配置完成后可以正常 push
- 上传完成,可在个人仓库中找到该模型
🎈 任务3 - 上传 Hugging Face Space
Hugging Face Spaces 是一个允许我们轻松地托管、分享和发现基于机器学习模型的应用的平台。Spaces 使得开发者可以快速将我们的模型部署为可交互的 Web 应用,且无需担心后端基础设施或部署的复杂性。
- 创建 Hugging Face Space
- 克隆 代码仓库
git clone https://huggingface.co/spaces/<your_username>/intern_cobuild
cd /workspaces/codespaces-jupyter/intern_cobuild
3. 修改仓库中的 index.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>
- 上传更新到仓库
git add .
git commit -m "update: colearn page"
git push
如 git push
时报错,可能是首次上传需验证身份,需按上文中的步骤进行配置。