【学习笔记】L0G4000 玩转HF/魔搭/魔乐社区

44 阅读5分钟

背景

经过这几天Linux、python、git这三类基础课程的学习,今天我终于正式开始接触大模型啦。不得不说,这个训练营的课程设计的真好,特别是对零基础的小伙伴,反正我自己是迷上了,哈哈。好啦,继续提交本次闯关的作业,以下是我今天的作业情况。

任务列表

  1. 模型下载:使用Hugging Face平台、魔搭社区平台或魔乐社区平台其中1个,下载文档中提到的模型(至少需要下载config.json文件、model.safetensors.index.json文件)
  2. 模型上传:将我们下载好的config.json文件(也自行添加其他模型相关文件)上传到对应HF平台和魔搭社区平台,并截图
  3. Space上传:在HF平台上使用Spaces并把intern_cobuild部署成功,关键步骤截图。

完成情况

作业一:模型下载

1. huggingface平台

  1. https://huggingface.co/ 上注册自己的账号(需要科学上网)
  2. 然后在huggingface平台上选择模型,这里我们使用internlm2_5-1_8b模型,地址为:internlm/internlm2_5-1_8b · Hugging Face

image.png

2. GitHub CodeSpace

接下来,我们会使用GitHub CodeSpace来创建运行环境,地址: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

考虑到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上下载了相应配置文件 2.png

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

新建一个python文件,命名为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)

运行代码,执行结果如下图所示:

4.png

作业二:模型上传

我们通过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

接着可以在CodeSpace里面,使用

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

命令进行登录,这时需要输入在huggingface平台创建的token,这里token要选择write类型。

9.png

用git提交到远程仓库。

10.png

操作成功后,在huggingface的平台上就可以看到我们创建的这个模型项目。

11.png

作业三:Space上传

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

https://huggingface.co/spaces

在创建页面中,输入项目名为intern_cobuild,并选择Static应用进行创建,如下图

5.png

创建成功后,默认页面显示如下:

6.png

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

7.png

找到该目录文件夹下的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

注意:这里push的时候会遇到认证问题,参考这篇文档:huggingface.co/blog/zh/pas… 再返回平台页面,可以看到更新后的页面如下图:

8.png

感受

通过这次实操,我对huggingface上模型的上传和下载有了更多的了解,也学习到了codespace的使用方法,这个真是个宝藏。后续,我希望通过这次训练营能让自己对大模型有更加深刻的理解,也希望大家多多关注,强烈推荐。

附训练营链接:aicarrier.feishu.cn/wiki/QtJnwe…