玩转Hugging Face社区 - 第四期书生大模型实战营实验记录(入门岛-第4关)

121 阅读3分钟

我目前正在参加“书生大模型实战营”。这是一个旨在帮助学员掌握大模型开发和应用的实战课程。

为了更好地记录完成过程,我根据官方提供的教程文档提取了核心步骤,并去掉了详细的背景知识介绍和说明,这样后续一个手册查找起来会更加直观。

但建议大家在实际学习过程中还是多看看原文,因为原文档确实非常的详细和完整,方便了解每一步的具体原因和背后的原理,这样有助于更牢固地掌握知识,提高实战能力。

入门岛-第4关

本地环境:Win11。

原文:github.com/InternLM/Tu…

完成任务步骤记录

任务一:模型下载

目标:使用Hugging Face平台下载文档中提到的模型(至少需要下载 config.json 文件、model.safetensors.index.json 文件),并在必要步骤及结果中截图。

完成所需时间:20分钟,大概花了15分钟,前提是准备好上网工具。

步骤

  1. 准备事项

  2. 开发环境

    • 打开 GitHub CodeSpace , 创建Jupyter Notebook环境,类似网页版VSCode。
    • 启动好后,在Terminal选项卡中安装环境
    # 安装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
    

  1. 模型文件下载到本地

    • 新建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
    

任务二:模型上传

目标:将下载好的 config.json 文件上传到Hugging Face平台,并截图。

完成所需时间:10分钟,注意push前设置token。

步骤

  1. 环境准备
  • 提前安装Hugging Face CLI方便后续上传。
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
  • 准备Hugging Face Token方便后续登录,记得存好Token。

image.png image.png

  • 登录Hugging Face CLI
git config --global credential.helper store
huggingface-cli login

登录过程中需要使用上一步的Token。

image.png

  1. 建立模型仓库
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 clone https://huggingface.co/flyfive0315/intern_study_L0_4
  • 然后将“任务一”中的config.json复制到CodeSpace的intern_study_L0_4文件夹中
  • 再创建一个README.md文件,内容如下:
# 书生浦语大模型实战营camp4
- hugging face模型上传测试
- 更多内容请访问 https://github.com/InternLM/Tutorial/tree/camp4

3. 上传模型文件:

cd intern_study_L0_4
git add .
git commit -m "add:intern_study_L0_4"
# 注意设置这一步git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
git remote set-url origin https://flyfive0315:xxxxxx@huggingface.co/flyfive0315/intern_study_L0_4
git push

4. 结果:

  • 直接访问以下地址
https://huggingface.co/flyfive0315/intern_study_L0_4

任务三:Space上传

目标:在Hugging Face平台上使用Spaces并把 intern_cobuild 部署成功,关键步骤截图。

完成所需时间:10分钟,几分钟应该就差不多。

步骤

  1. Hugging Face Spaces创建项目:

image.png

  1. CodeSpace 中检出项目并修改代码:
  • 检出上一步创建的项目
cd /workspaces/codespaces-jupyter
# 请将<your_username>替换你自己的username
git clone https://huggingface.co/spaces/flyfive0315/intern_cobuild
cd /workspaces/codespaces-jupyter/intern_cobuild
  • 修改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>

3. 推送代码:

  • 推送代码到仓库
git add .
git commit -m "update: colearn page"
# 注意设置这一步 git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
git remote set-url origin https://flyfive0315:xxxxxx@huggingface.co/spaces/flyfive0315/intern_cobuild
git push

4. 结果

  • 再次进入Space界面。