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

玩转「HF/魔搭/魔乐」平台

模型下载

Hugging Face 下载到 GitHub CodeSpace

  1. CodeSpace创建环境:
# 安装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. 下载internlm2_5-7b-chat的配置文件
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

在这里插入图片描述
3. 下载internlm2_5-chat-1_8b并打印示例输出

touch hf_download_1_8_demo.py
import torch
from transformers import AutoTokenizer, AutoModelForCausalLMtokenizer = 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)

Space上传

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

<!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>
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(具体获取方式见下文 "2.1.5 模型上传")
git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
例如:
git remote set-url origin https://jack:hf_xxxxx@huggingface.co/spaces/jack/intern_cobuild/
然后再次git push即可

效果:
在这里插入图片描述

模型上传

上传到HuggingFace

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

创建README.md

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

提交到远程仓库

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 tokengit pull origin

在这里插入图片描述
在这里插入图片描述
可以看到我们的config.json上传到huggingface的model里了

上传到魔搭社区

环境配置
为ms_demo创建一个可用的conda虚拟环境,可以和其他环境区分开来

# 激活环境
conda activate /root/share/pre_envs/pytorch2.1.2cu12.1# 安装 modelscope
pip install modelscope -t /root/myenv/maas
pip install numpy==1.26.0  -t /root/myenv/maas
pip install packaging -t /root/myenv/maas

注意:为了保证能够找到我们每次装的依赖,每次新建一个终端之后都需要导出path 如果不改变终端,导出一次就够了

export PATH=$PATH:/root/myenv/maas/bin
export PYTHONPATH=/root/myenv/maas:$PYTHONPATH

创建我们的demo目录

mkdir -p /root/ms_demo

internlm2_5-7b-chat 考虑到7B的模型文件比较大,这里我们先采用modelscope的cli工具(当然hugging face也有)来下载指定文件,在命令行输入以下命令

modelscope download \--model 'Shanghai_AI_Laboratory/internlm2_5-7b-chat' \tokenizer.json config.json model.safetensors.index.json \--local_dir '/root/ms_demo'

刷新一下文件目录,就能看到在ms_demo中下载了指定的json文件。

internlm2_5-1_8b-chat
modelscope download \--model 'Shanghai_AI_Laboratory/internlm2_5-1_8b-chat' \tokenizer.json config.json model.safetensors.index.json \--local_dir '/root/ms_demo'

魔乐社区平台

魔乐社区(Modelers)是一个提供多样化、开源模型的平台,旨在促进开发者和研究人员在最先进的模型和流行应用上进行协作。

下载internlm2_5-chat-1_8b模型

这里我们可以继续使用我们刚刚创建的InterStudio开发机

cd /
mkdir ml_demo
cd ml_demo

然后我们可以下载该模型,这里

#确保安装git-lfs 保证大文件的正常下载
apt-get install git-lfs
git lfs install
#clone 仓库
git clone https://modelers.cn/Intern/internlm2_5-1_8b-chat.git

刷新一下文件夹,即可在ml_demo中找到下载好的模型文件,在魔乐社区中,还推荐了一个新的深度学习开发套件openMind Library,除了常用的Transforms的API,也可以探索如何使用openMind来加载模型

#确保按指南安装好openmind后
from openmind import AutoModel
model = AutoModel.from_pretrained("Intern/internlm2_5-1_8b-chat", trust_remote_code=True")

openMind Library是一个深度学习开发套件,通过简单易用的API支持模型预训练、微调、推理等流程。 openMind Library通过一套接口兼容PyTorch和MindSpore等主流框架,同时原生支持昇腾NPU处理器。

上传模型

在魔乐社区一般有两种方法,第一种是安装好openmid后使用openmind的API来上传文件,另一个就是用git命令来推送文件,跟一般的git工作流相类似。

http://www.lryc.cn/news/477973.html

相关文章:

  • 鸿蒙系统的优势 开发 环境搭建 开发小示例
  • python批量合并excel文件
  • AWS S3 JavaScript SDK(v3)常用操作
  • 数据结构——图的基本操作
  • 掌握全球速递:在表格中高效利用国际快递公式查询快递
  • 【MySQL系列】字符集设置
  • Vue2进阶之Vue3高级用法
  • 基于微信的追星小程序+ssm(lw+演示+源码+运行)
  • 【51单片机】串口通信原理 + 使用
  • 优选算法第五讲:位运算模块
  • 【07】Maven项目多环境打包配置
  • 嵌入式Linux入门具备:C语言基础与基本驱动学习(2):Linux GIibc IO基础
  • 【微服务】Docker 容器化
  • [前端] 为网站侧边栏添加搜索引擎模块
  • 解决CORS (跨源资源共享) 错误
  • Redis 实现分布式缓存
  • Chrome与火狐哪个浏览器的移动版本更流畅
  • 7篇Python爬虫实例,直接代码可运行,全网最全,注释超详细(适合收藏)——2、爬取图片信息。
  • 25.停车场管理系统(基于web的Java项目)
  • 展览搭建公司怎么跟展会主办打好交道
  • 软件开发方法
  • 「Mac畅玩鸿蒙与硬件24」UI互动应用篇1 - 灯光控制小项目
  • 十二:java web(4)-- Spring核心基础
  • new和malloc有什么区别,他们的用法是什么?malloc分配失败会导致什么问题
  • 了解SQLExpress数据库
  • geoserver创建一个根据属性显示不同形状的点样式
  • 中国遗传学会2024全国学术研讨会在长沙成功召开
  • Android Studio 多工程公用module引用
  • (实战)WebApi第9讲:EFCore性能优化(IQueryable延迟查询、取消跟踪机制)
  • Java实现pdf转图片