GitHub CodeSpace的使用
在 https://github.com/codespaces中选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
! [在这里插入图片描述] (https://i-blog.csdnimg.cn/direct/8b776bd964104aabb124eb23d18fb52f.png)
安装成功后,开始下载internlm2_5-7b-chat的配置文件,在/workspaces/codespaces-jupyter 目录下创建 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}")
在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)
Hugging Face Spaces的使用`
https://huggingface.co/spaces,在创建页面中,输入项目名为intern_cobuild,并选择Static应用进行创建
git clone https://huggingface.co/spaces/<your_username>/intern_cobuild
cd /intern_cobuild
模型上传
通过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
git config --global credential.helper store
huggingface-cli login