使用Hugging Face平台下载模型

使用GitHub CodeSpace平台作为我们的工作环境

https://github.com/codespaces

为了模型的正常运行,我们在终端安装以下依赖库

# 安装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

这里我已经安装了

下载internlm2_5-7b-chat的配置文件

由于个人GitHub CodeSpace硬盘空间有限(32GB可用),而7B的模型相对较大,这里我们仅下载模型文件夹的特定文件。为方便演示,直接在/workspaces/codespaces-jupyter 目录下载。这里演示如何下载模型的配置文件:

首先,新建一个下载脚本hf_download_josn.py 文件

touch 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}")

运行脚本

python hf_download_josn.py

可以看到,已经从Hugging Face上下载了相应配置文件

Hugging Face Spaces的使用

Hugging Face Spaces 是一个允许我们轻松地托管、分享和发现基于机器学习模型的应用的平台。Spaces 使得开发者可以快速将我们的模型部署为可交互的 web 应用,且无需担心后端基础设施或部署的复杂性。

首先访问以下链接,进入Spaces。在右上角点击Create new Space进行创建:

https://huggingface.co/spaces

创建成功后会自动跳转到一个默认的HTML页面。

创建好项目后,回到我们的CodeSpace,接着clone项目。

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

进入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

使用huggingface-cli login命令进行登录,登录过程中需要输入用户的Access Tokens。

创建一个类型为“Write”的token,并请复制好token后要存储在合适的地方

在CodeSpace里面,使用以下命令进行登录

git config --global credential.helper store
huggingface-cli login

需要输入刚刚的token(直接粘贴就行,不显示)

输入y进行确定后登录成功

创建项目

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

如果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


Logo

昇腾计算产业是基于昇腾系列(HUAWEI Ascend)处理器和基础软件构建的全栈 AI计算基础设施、行业应用及服务,https://devpress.csdn.net/organization/setting/general/146749包括昇腾系列处理器、系列硬件、CANN、AI计算框架、应用使能、开发工具链、管理运维工具、行业应用及服务等全产业链

更多推荐