当前位置: 首页 > article >正文

四期书生大模型实战营(【入门岛】- 第4关 | 玩转HF/魔搭/魔乐社区)

文章目录

  • 1. 任务介绍
    • 闯关任务
  • 2. 平台介绍
    • 2.1. HF 平台
      • 2.1.1. HF的Transformers库介绍
    • 2.2. GitHub CodeSpaces 和 Hugging Face Spaces 介绍。
      • 2.2.1. GitHub CodeSpaces
      • 2.2.2. Hugging Face Spaces
      • 2.2.3. 总结
  • 3. 模型下载
    • 3.1. GitHub CodeSpaces使用
    • 3.1.下载internlm2_5-7b-chat的配置文件
    • 3.2. 下载internlm2_5-chat-1_8b并打印示例输出
  • 3. Space上传(可选)
    • 3.1. Hugging Face Spaces的使用
    • 3.2. 创建页面
    • 3.3. 回到我们的CodeSpace clone项目
    • 3.4. 保存后push到远程仓库上(它会自动更新页面)
  • 4. 模型上传(可选)
    • 4.1. 安装git lfs
    • 4.2. 获取HF登录过程中需要输入用户的Access Tokens
    • 4.3. 在CodeSpace里面登录
    • 4.4. 创建项目
    • 4.5. 用git提交到远程仓库

1. 任务介绍

闯关任务

闯关任务需要在关键步骤当中截图!

任务描述时间
模型下载使用Hugging Face平台、魔搭社区平台(可选)和魔乐社区平台(可选)下载文档中提到的模型(至少需要下载config.json文件、odel.safetensors.index.json文件),请在必要的步骤以及结果当中截图。20min
模型上传(可选)将我们下载好的config.json文件(也自行添加其他模型相关文件)上传到对应HF平台和魔搭社区平台,并截图。10min
Space上传(可选)在HF平台上使用Spaces并把intern_cobuild部署成功,关键步骤截图。10min

优秀作业必做可选内容 请将作业发布到知乎、CSDN等任一社交媒体,将作业链接提交到以下问卷,助教老师批改后将获得 50 算力点奖励!!!

提交地址:https://aicarrier.feishu.cn/share/base/form/shrcnUqshYPt7MdtYRTRpkiOFJd

2. 平台介绍

2.1. HF 平台

Hugging Face
最初专注于开发聊天机器人服务。尽管他们的聊天机器人项目并未取得预期的成功,但他们在GitHub上开源的Transformers库却意外地在机器学习领域引起了巨大轰动。如今,Hugging Face已经发展成为一个拥有超过100,000个预训练模型和10,000个数据集的平台,被誉为机器学习界的GitHub。

2.1.1. HF的Transformers库介绍

在正式下载之前,介绍一下HF的Transformers库,作为HF最核心的项目,它可以:
在这里插入图片描述

  • 直接使用预训练模型进行推理
  • 提供了大量预训练模型可供使用
  • 使用预训练模型进行迁移学习 因此在使用HF前,我们需要下载Transformers等一些常用依赖库

这里我们以internlm2_5-1_8b举例,查看Hugging Face上该模型的地址

https://huggingface.co/internlm/internlm2_5-1_8b

2.2. GitHub CodeSpaces 和 Hugging Face Spaces 介绍。

后面的下载和上传需要用到GitHub CodeSpaces 和 Hugging Face Spaces。下面介绍一下他们的异同:GitHub CodeSpacesHugging Face Spaces 都是强大的云端开发环境,分别由 GitHub 和 Hugging Face 提供,设计用来简化不同类型项目的开发流程。以下是它们的功能、特点和适用场景:


2.2.1. GitHub CodeSpaces

GitHub CodeSpaces 是 GitHub 提供的一个基于云的开发环境,旨在让开发者可以快速创建和配置所需的开发环境,直接在浏览器或本地支持远程连接的 IDE(如 VS Code)中使用。这极大地减少了传统开发环境的配置时间,尤其适用于需要频繁切换项目的开发者。

特点和功能

  • 环境定制:支持 Docker 容器的自定义配置,使你可以配置和存储特定的开发环境,供团队中的其他成员直接使用。
  • 快速启动:开发者可以从 GitHub 仓库快速启动一个新的开发空间,无需手动安装依赖或配置环境。
  • 资源管理:提供不同配置的资源选项(如 CPU、RAM 配置),适用于不同类型的项目,从简单的脚本开发到大型应用。
  • 集成 GitHub 工作流:自动与 GitHub 仓库关联,便于代码版本控制和提交。
  • 多端支持:可在本地的 VS Code 使用,也可直接在浏览器中使用。

适用场景

  • 用于快速搭建和部署开发环境,减少开发配置时间。
  • 团队合作开发,确保所有开发者在一致的环境中进行代码编辑。
  • 适合需要频繁切换或同时处理多个项目的开发者。

2.2.2. Hugging Face Spaces

Hugging Face Spaces 是 Hugging Face 提供的云端应用托管平台,主要面向 AI 和机器学习应用的展示和交互。开发者可以将自己的模型部署到 Hugging Face Spaces 上,用户可以直接在网页中进行交互,试用模型的功能。

特点和功能

  • 支持 Gradio 和 Streamlit:可直接使用 Gradio 或 Streamlit 等流行框架,快速构建交互式前端界面。
  • 轻松部署机器学习模型:支持 Hugging Face 的 Transformers、Diffusers 等库的快速集成,便于展示和使用预训练模型。
  • 免费与付费资源:免费用户可以使用有限的计算资源,专业和企业用户可以选择更高的资源配置,适合部署资源需求更高的项目。
  • 开源和社区分享:支持公开或私有空间的选择,开发者可以将模型分享给社区使用,便于 AI 应用的测试和推广。

适用场景

  • 部署和展示 AI 模型的交互界面,便于用户直接体验模型功能。
  • 面向数据科学、AI 开发人员,便于共享和推广模型和项目。
  • 适用于快速构建可视化界面,并希望与社区分享的机器学习项目。

2.2.3. 总结

  • GitHub CodeSpaces:适用于通用软件开发环境的快速配置和共享,专注于代码编辑和调试。
  • Hugging Face Spaces:专为机器学习和 AI 模型展示而设计,支持互动界面创建和模型托管。

两者都有助于提高开发效率,一个专注于代码编辑环境,一个专注于 AI 模型展示和体验。

3. 模型下载

模型下载 ,这里需要进入Hugging Face的官网进行注册:https://huggingface.co/

3.1. GitHub CodeSpaces使用

因为网络和磁盘有限的原因,强烈不建议在 InternStudio 运行,因此这里使用CodeSpace。

https://github.com/codespaces

Github CodeSpace是Github推出的线上代码平台,提供了一系列templates,我们这里选择Jupyter Notebook进行创建环境。创建好环境后,可以进入网页版VSCode的界面,这就是CodeSpace提供给我们的在线编程环境。
在这里插入图片描述
在这里插入图片描述

在界面下方的终端(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

运行结果:

@banjr ➜ /workspaces/codespaces-jupyter (main) $ # 安装transformers
p install sentencepiece==0.1.99
pip install einops==0.8.0
pip install protobuf==5.27.2
pip install accelerate==0.33.0@banjr ➜ /workspaces/codespaces-jupyter (main) $ pip install transformers==4.38
Collecting transformers==4.38
  Downloading transformers-4.38.0-py3-none-any.whl.metadata (131 kB)
Requirement already satisfied: filelock in /home/codespace/.local/lib/python3.12/site-packages (from transformers==4.38) (3.13.1)
Collecting huggingface-hub<1.0,>=0.19.3 (from transformers==4.38)
  Downloading huggingface_hub-0.26.1-py3-none-any.whl.metadata (13 kB)
Requirement already satisfied: numpy>=1.17 in /home/codespace/.local/lib/python3.12/site-packages (from transformers==4.38) (2.1.2)
Requirement already satisfied: packaging>=20.0 in /home/codespace/.local/lib/python3.12/site-packages (from transformers==4.38) (24.1)
Requirement already satisfied: pyyaml>=5.1 in /home/codespace/.local/lib/python3.12/site-packages (from transformers==4.38) (6.0.2)
Collecting regex!=2019.12.17 (from transformers==4.38)
  Downloading regex-2024.9.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (40 kB)
Requirement already satisfied: requests in /home/codespace/.local/lib/python3.12/site-packages (from transformers==4.38) (2.32.3)
Collecting tokenizers<0.19,>=0.14 (from transformers==4.38)
  Downloading tokenizers-0.15.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.7 kB)
Collecting safetensors>=0.4.1 (from transformers==4.38)
  Downloading safetensors-0.4.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.8 kB)
Requirement already satisfied: tqdm>=4.27 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from transformers==4.38) (4.66.4)
Requirement already satisfied: fsspec>=2023.5.0 in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.19.3->transformers==4.38) (2024.2.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.19.3->transformers==4.38) (4.9.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/codespace/.local/lib/python3.12/site-packages (from requests->transformers==4.38) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in /home/codespace/.local/lib/python3.12/site-packages (from requests->transformers==4.38) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/codespace/.local/lib/python3.12/site-packages (from requests->transformers==4.38) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /home/codespace/.local/lib/python3.12/site-packages (from requests->transformers==4.38) (2024.8.30)
Downloading transformers-4.38.0-py3-none-any.whl (8.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.5/8.5 MB 53.1 MB/s eta 0:00:00
Downloading huggingface_hub-0.26.1-py3-none-any.whl (447 kB)
Downloading regex-2024.9.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (797 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 797.0/797.0 kB 5.4 MB/s eta 0:00:00
Downloading safetensors-0.4.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (434 kB)
Downloading tokenizers-0.15.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.6/3.6 MB 46.3 MB/s eta 0:00:00
Installing collected packages: safetensors, regex, huggingface-hub, tokenizers, transformers
Successfully installed huggingface-hub-0.26.1 regex-2024.9.11 safetensors-0.4.5 tokenizers-0.15.2 transformers-4.38.0

[notice] A new release of pip is available: 24.2 -> 24.3
[notice] To update, run: python3 -m pip install --upgrade pip
@banjr ➜ /workspaces/codespaces-jupyter (main) $ pip install sentencepiece==0.1.99
Collecting sentencepiece==0.1.99
  Downloading sentencepiece-0.1.99.tar.gz (2.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.6/2.6 MB 28.9 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: sentencepiece
  Building wheel for sentencepiece (pyproject.toml) ... \
done
  Created wheel for sentencepiece: filename=sentencepiece-0.1.99-cp312-cp312-linux_x86_64.whl size=1287194 sha256=b7585537f47518fcdfb9191f604d89a69b231550fc9dcc074e90f3ec98caecba
  Stored in directory: /home/codespace/.cache/pip/wheels/e0/8c/e0/65e33b1f4b8462dfc537a0cac02e5c03e1207564c300e4bde5
Successfully built sentencepiece
Installing collected packages: sentencepiece
Successfully installed sentencepiece-0.1.99

[notice] A new release of pip is available: 24.2 -> 24.3
[notice] To update, run: python3 -m pip install --upgrade pip
@banjr ➜ /workspaces/codespaces-jupyter (main) $ pip install einops==0.8.0
Collecting einops==0.8.0
  Downloading einops-0.8.0-py3-none-any.whl.metadata (12 kB)
Downloading einops-0.8.0-py3-none-any.whl (43 kB)
Installing collected packages: einops
Successfully installed einops-0.8.0

[notice] A new release of pip is available: 24.2 -> 24.3
[notice] To update, run: python3 -m pip install --upgrade pip
@banjr ➜ /workspaces/codespaces-jupyter (main) $ pip install protobuf==5.27.2
Collecting protobuf==5.27.2
  Downloading protobuf-5.27.2-cp38-abi3-manylinux2014_x86_64.whl.metadata (592 bytes)
Downloading protobuf-5.27.2-cp38-abi3-manylinux2014_x86_64.whl (309 kB)
Installing collected packages: protobuf
Successfully installed protobuf-5.27.2

[notice] A new release of pip is available: 24.2 -> 24.3
[notice] To update, run: python3 -m pip install --upgrade pip
@banjr ➜ /workspaces/codespaces-jupyter (main) $ pip install accelerate==0.33.0
Collecting accelerate==0.33.0
  Downloading accelerate-0.33.0-py3-none-any.whl.metadata (18 kB)
Collecting numpy<2.0.0,>=1.17 (from accelerate==0.33.0)
  Downloading numpy-1.26.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)
Requirement already satisfied: packaging>=20.0 in /home/codespace/.local/lib/python3.12/site-packages (from accelerate==0.33.0) (24.1)
Requirement already satisfied: psutil in /home/codespace/.local/lib/python3.12/site-packages (from accelerate==0.33.0) (6.0.0)
Requirement already satisfied: pyyaml in /home/codespace/.local/lib/python3.12/site-packages (from accelerate==0.33.0) (6.0.2)
Requirement already satisfied: torch>=1.10.0 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from accelerate==0.33.0) (2.3.0)
Requirement already satisfied: huggingface-hub>=0.21.0 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from accelerate==0.33.0) (0.26.1)
Requirement already satisfied: safetensors>=0.3.1 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from accelerate==0.33.0) (0.4.5)
Requirement already satisfied: filelock in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==0.33.0) (3.13.1)
Requirement already satisfied: fsspec>=2023.5.0 in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==0.33.0) (2024.2.0)
Requirement already satisfied: requests in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==0.33.0) (2.32.3)
Requirement already satisfied: tqdm>=4.42.1 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==0.33.0) (4.66.4)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/codespace/.local/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==0.33.0) (4.9.0)
Requirement already satisfied: sympy in /home/codespace/.local/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (1.13.1)
Requirement already satisfied: networkx in /home/codespace/.local/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (3.2.1)
Requirement already satisfied: jinja2 in /home/codespace/.local/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (3.1.4)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.105)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.105)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.105)
Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (8.9.2.26)
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.3.1)
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (11.0.2.54)
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (10.3.2.106)
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (11.4.5.107)
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.0.106)
Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (2.20.5)
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from torch>=1.10.0->accelerate==0.33.0) (12.1.105)
Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/python/3.12.1/lib/python3.12/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate==0.33.0) (12.6.77)
Requirement already satisfied: MarkupSafe>=2.0 in /home/codespace/.local/lib/python3.12/site-packages (from jinja2->torch>=1.10.0->accelerate==0.33.0) (3.0.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/codespace/.local/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==0.33.0) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in /home/codespace/.local/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==0.33.0) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/codespace/.local/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==0.33.0) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /home/codespace/.local/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==0.33.0) (2024.8.30)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/codespace/.local/lib/python3.12/site-packages (from sympy->torch>=1.10.0->accelerate==0.33.0) (1.3.0)
Downloading accelerate-0.33.0-py3-none-any.whl (315 kB)
Downloading numpy-1.26.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.0/18.0 MB 67.3 MB/s eta 0:00:00
Installing collected packages: numpy, accelerate
  Attempting uninstall: numpy
    Found existing installation: numpy 2.1.2
    Uninstalling numpy-2.1.2:
      Successfully uninstalled numpy-2.1.2
Successfully installed accelerate-0.33.0 numpy-1.26.4

[notice] A new release of pip is available: 24.2 -> 24.3
[notice] To update, run: python3 -m pip install --upgrade pip
@banjr ➜ /workspaces/codespaces-jupyter (main) $ 

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

考虑到个人GitHub CodeSpace硬盘空间有限(32GB可用),而7B的模型相对较大,这里我们先演示如何下载模型文件夹的特定文件。考虑到CodeSpace平台上默认的用户权限不是root权限,这里为方便演示直接在工作区创建文件,即/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上下载了相应配置文件
在这里插入图片描述

虽然在这里我们没有完全下载internlm2_5-7b-chat模型,但是在实战营课程中,我们的InternStudio平台的 /root/share 目录下已经提供了InterLM2.5系列的模型,可以找到它们作为model_name_or_path进行使用,如/root/share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat

3.2. 下载internlm2_5-chat-1_8b并打印示例输出

那么如果我们需想要下载一个完整的模型文件怎么办呢?创建一个python文件用于下载internlm2_5-1_8B模型并运行。下载速度跟网速和模型参数量大小相关联,如果网速较慢的小伙伴可以只尝试下载1.8b模型对应的config.json文件以及其他配置文件。

touch hf_download_1_8_demo.py

在这里插入图片描述
注意到在CodeSpace平台上是没有GPU资源的,因此我们Python代码中只使用CPU进行推理,我们需要修改跟CUDA有关的API,在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)

等待几分钟后,会在控制台返回模型生成的结果(解除注释后)

没有解除注释:
在这里插入图片描述
在这里插入图片描述
解除注释后:
在这里插入图片描述

这里以“A beautiful flower”开头,模型对其进行“续写”,InternLM的模型拥有强大的数学方面的能力。这边它输出的文本似乎是关于一个数学问题。

具体这道题的意思是:
一个美丽的花盆由一个底部的圆柱体和顶部的半球体组成。底部圆柱体的半径是 10 ,顶部半球体的半径也是 10。求这个花盆的体积。

3. Space上传(可选)

3.1. Hugging Face Spaces的使用

Hugging Face Spaces 是一个允许我们轻松地托管、分享和发现基于机器学习模型的应用的平台。Spaces 使得开发者可以快速将我们的模型部署为可交互的 web 应用,且无需担心后端基础设施或部署的复杂性。 首先访问以下链接,进入Spaces。在右上角点击Create new Space进行创建:
https://huggingface.co/spaces
在这里插入图片描述

3.2. 创建页面

在创建页面中,输入项目名为intern_cobuild,并选择Static应用进行创建
在这里插入图片描述
创建成功后会自动跳转到一个默认的HTML页面。创建好项目后,回到我们的CodeSpace,接着clone项目。
在这里插入图片描述

3.3. 回到我们的CodeSpace clone项目

注意这里请替换你自己的username

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>

在这里插入图片描述

3.4. 保存后push到远程仓库上(它会自动更新页面)

保存后就可以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(具体获取方式见下文 “4.2 获取登录过程中需要输入的用户的Access Tokens”)
git remote set-url origin https://<user_name>:@huggingface.co/<repo_path>
例如:
git remote set-url origin https://jack:hf_xxxxx@huggingface.co/spaces/jack/intern_cobuild/
然后再次git push即可

这个命令用于更新 Git 仓库的远程 URL,将其设置为 Hugging Face 的仓库地址。具体来看:

  1. git remote set-url origin <URL>

    • git remote set-url 是 Git 的命令,用于更改已存在的远程仓库 URL。
    • origin 是默认的远程仓库名称,表示要修改的仓库的名称。
  2. https://<user_name>:<token>@huggingface.co/<repo_path>

    • https:// 后面的部分指定新的远程仓库 URL。
    • <user_name> 是你的 Hugging Face 用户名。
    • <token> 是 Hugging Face 提供的访问令牌,通常用于身份验证,避免每次提交时都要手动输入用户名和密码。
    • huggingface.co/<repo_path> 是 Hugging Face 仓库的地址和路径。

这样配置后,Git 会使用指定的用户凭证自动连接到 Hugging Face 仓库,简化了推送和拉取代码的流程。

再次进入Space界面,就可以看到实战营的共建活动。
在这里插入图片描述

4. 模型上传(可选)

4.1. 安装git lfs

  • 通过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

这个命令是在 Linux 系统上安装 git-lfs(Git Large File Storage) 的脚本,具体分解如下:

  1. curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh
    • curl 是一个命令行工具,用于从 URL 下载内容。
    • -s 参数让 curl 静默运行,不输出下载过程的进度信息。

总结

这个命令通过下载并执行 GitHub 提供的 git-lfs 安装脚本,以便自动完成 Git LFS 的设置和依赖安装。

4.2. 获取HF登录过程中需要输入用户的Access Tokens

使用huggingface-cli login命令进行登录,登录过程中需要输入用户的Access Tokens,获取时,需要先验证email
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

完成验证后,点击create new token,创建一个类型为“Write”的token,并请复制好token后要存储在合适的地方
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.3. 在CodeSpace里面登录

接着可以在CodeSpace里面,使用

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

命令进行登录,这时需要输入刚刚的token
在这里插入图片描述

在这里插入图片描述

4.4. 创建项目

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

克隆好之后,刷新文件目录可以看到克隆好的intern_study_L0_4文件夹。

我们可以把训练好的模型保存进里面,这里考虑到网速问题,只上传我们刚刚下载好的config.json,把它复制粘贴进这个文件夹里面,还可以写一个README.md文件,比如可以粘贴以下内容:

# 书生浦语大模型实战营camp4
- hugging face模型上传测试
- 更多内容请访问 https://github.com/InternLM/Tutorial/tree/camp4

在这里插入图片描述

4.5. 用git提交到远程仓库

现在可以用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,也可以直接将下面的Url输入到浏览器网址栏上

https://huggingface.co/<user_name>/intern_study_L0_4

在这里插入图片描述


http://www.kler.cn/news/368187.html

相关文章:

  • 图论BFS
  • 【gRPC】什么是RPC——介绍一下RPC
  • 温湿传感器(学习笔记下)
  • uniapp:上拉加载更多、下拉刷新、页面滚动到指定位置
  • 挑战Java面试题复习第2天,百折不挠
  • Qt:QtCreator使用
  • Sql执行较慢的排查方式
  • 带你用Go实现二维码小游戏(上)
  • 10.23Python_Matplotlib_‘backend_interagg‘ has no attribute
  • smartctl硬盘检查工具
  • docker入门(三)自定义部署docker镜
  • Vuetify3响应式布局
  • c#时间对象(时间间隔)相关计算
  • 【已解决】edge浏览器会保存历史验证码?新版Edge如何关闭自动填充表单功能?
  • JMeter实战之——模拟登录
  • js实现数据版购物车(实现购物车所有功能)
  • CSS基础—网页布局(重点!)
  • JavaWeb合集18-接口管理Swager
  • YOLOv5/v8/v10/v11详细介绍:网络结构,创新点
  • 升级Unity后产生的Objects内存泄露现象
  • 面试总结(2024/10/16)
  • ubuntu 硬盘扩容
  • python对文件的读写操作
  • 快速入门HTML
  • 影刀RPA实战:验证码识别功能指令
  • 华为手机卡住了怎么办?,快来试试布局性能优化,让你的手机发挥极致性能!!!