因为网络和磁盘有限的原因,强烈不建议在 InternStudio 运行,因此这里使用CodeSpace,Github CodeSpace是Github推出的线上代码平台,提供了一系列templates,我们这里选择Jupyter Notebook进行创建环境。创建好环境后,可以进入网页版VSCode的界面,这就是CodeSpace提供给我们的在线编程环境。
1、模型下载
神奇的GitHubGodeSpace:
选择Jupyter Notebook
安装所需要的依赖:
# 安装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
创建一个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}")
# 创建文件
touch hf_download_josn.py
# 运行该文件
python hf_download_josn.py
可以看到,已经从Hugging Face上下载了相应配置文件
2、模型上传
在gitcodespaces里:
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
git config --global credential.helper store
huggingface-cli login
创建一个Access Token :
在下方的位置输入刚才的access token,注意这里是暗码不可见,不要重复输入
创建项目
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
走一遍git流程
cd intern_study_L0_4
git add .
git commit -m "add:intern_study_L0_4"
git push
回到HF上检查一下是否上传成功:
3、Space上传
创建一个HF Space(https://huggingface.co/spaces) :
在Git codespace中:
cd /workspaces/codespaces-jupyter
# 请将<your_username>替换你自己的username
git clone https://huggingface.co/spaces/<your_username>/intern_cobuild
cd /workspaces/codespaces-jupyter/intern_cobuild
复制粘贴预制的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流程:
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即可