书生大模型实战营第四期-入门岛-4. maas课程任务

embedded/2024/11/29 5:56:14/

maas_0">书生大模型实战营第四期-入门岛-4. maas课程任务

任务一、模型下载

任务内容

使用Hugging Face平台、魔搭社区平台(可选)和魔乐社区平台(可选)下载文档中提到的模型(至少需要下载config.json文件、model.safetensors.index.json文件),请在必要的步骤以及结果当中截图。

作业过程

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

新建一个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}")

L0-<a class=maas-task4-hf-download" />

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

创建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)

L0-<a class=maas-task4-hf-demo" />

任务二、模型上传(可选)

作业内容

将我们下载好的config.json文件(也自行添加其他模型相关文件)上传到对应HF平台和魔搭社区平台,并截图。

作业过程

上传模型文件到HF平台

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

在github的CodeSpace里面:

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

命令行登录hf,输入token(在hg创建并获取):

@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ git config --global credential.helper store
@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ huggingface-cli login_|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|_|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|_|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|_|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|_|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|To log in, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
Enter your token (input will not be visible): 
Add token as git credential? (Y/n) Y
Token is valid (permission: write).
The token `hf_internlm_test` has been saved to /home/codespace/.cache/huggingface/stored_tokens
Your token has been saved in your configured git credential helpers (store).
Your token has been saved to /home/codespace/.cache/huggingface/token
Login successful.
The current active token is: `hf_internlm_test`

创建hg项目:

# intern_study_L0_4就是model_name
@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ huggingface-cli repo create intern_study_L0_4
git version 2.47.0
git-lfs/3.5.1 (GitHub; linux amd64; go 1.21.8)You are about to create lldhsds/intern_study_L0_4
Proceed? [Y/n] YYour repo now lives at:https://huggingface.co/lldhsds/intern_study_L0_4You can clone it locally with the command below, and commit/push as usual.git clone https://huggingface.co/lldhsds/intern_study_L0_4# 将上面创建的项目克隆到本地
@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ git clone https://huggingface.co/lldhsds/intern_study_L0_4
Cloning into 'intern_study_L0_4'...
remote: Enumerating objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3 (from 1)
Unpacking objects: 100% (3/3), 1.05 KiB | 1.05 MiB/s, done.

更新项目并推送到远程仓库:

# 添加README.md文件
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ vim README.md@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ git add .
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ git commit -m "add:intern_study_L0_4"
[main 380529d] add:intern_study_L0_41 file changed, 3 insertions(+)create mode 100644 README.md
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ git push
remote: Password authentication in git is no longer supported. You must use a user access token or an SSH key instead. See https://huggingface.co/blog/password-git-deprecation
fatal: Authentication failed for 'https://huggingface.co/lldhsds/intern_study_L0_4/'
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ export hf_token="xxx"	# 此处设置hf access key@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ git remote set-url origin  https://lldhsds:$hf_token@huggingface.co/lldhsds/intern_study_L0_4
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_study_L0_4 (main) $ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 449 bytes | 449.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: -------------------------------------------------------------------------
remote: Your push was accepted, but with warnings: 
remote: - Warning: empty or missing yaml metadata in repo card
remote: help: https://huggingface.co/docs/hub/model-cards#model-card-metadata
remote: -------------------------------------------------------------------------
remote: -------------------------------------------------------------------------
remote: Please find the documentation at:
remote: https://huggingface.co/docs/hub/model-cards#model-card-metadata
remote: 
remote: -------------------------------------------------------------------------
To https://huggingface.co/lldhsds/intern_study_L0_4510dcc0..380529d  main -> main

仓库信息:

L0-<a class=maas-task4-hf-repo" />

上传模型文件到魔搭社区平台
  1. 魔搭社区注册登录,并创建model

L0-<a class=maas-modelscope-model" />

  1. 下载模型并修改
git clone https://www.modelscope.cn/lldhsds/intern_study_L0_4.git
  1. 上传模型文件
(/root/share/pre_envs/pytorch2.1.2cu12.1) root@intern-studio-50014188:~/intern_study_L0_4# ls
README.md  configuration.json# 创建config.json
(/root/share/pre_envs/pytorch2.1.2cu12.1) root@intern-studio-50014188:~/intern_study_L0_4# vim config.json# git提交三部曲
(/root/share/pre_envs/pytorch2.1.2cu12.1) root@intern-studio-50014188:~/intern_study_L0_4# git add .
(/root/share/pre_envs/pytorch2.1.2cu12.1) root@intern-studio-50014188:~/intern_study_L0_4# git commit -m "add:intern_study_L0_4"
[master ac42332] add:intern_study_L0_41 file changed, 35 insertions(+)create mode 100644 config.json# 这一步需要进行验证,moodelscope用户名+ moodelscope git token
(/root/share/pre_envs/pytorch2.1.2cu12.1) root@intern-studio-50014188:~/intern_study_L0_4# git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 128 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 692 bytes | 57.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://www.modelscope.cn/lldhsds/intern_study_L0_4.git5acaa46..ac42332  master -> master
  1. 查看仓库

上传的文件,modelspace需要审核。

L0-<a class=maas-modelscope-model-up" />

任务三、Space上传(可选)

作业内容

在HF平台上使用Spaces并把intern_cobuild部署成功,关键步骤截图。

作业过程

访问下面链接https://huggingface.co/spaces,点击右上角的Create new Space创建项目,输入项目名为intern_cobuild,并选择Static应用进行创建,创建成功后会自动跳转到一个默认的HTML页面。

创建好项目后,回到githuab的CodeSpace,接着clone并修改项目:

@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ git clone https://huggingface.co/spaces/lldhsds/intern_cobuild
Cloning into 'intern_cobuild'...
remote: Enumerating objects: 6, done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6 (from 1)
Unpacking objects: 100% (6/6), 1.88 KiB | 1.88 MiB/s, done.
@lldhsds ➜ /workspaces/codespaces-jupyter (main) $ cd intern_cobuild/
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ ls
README.md  index.html  style.css
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ mv index.html index.html.bak
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ vim index.html 

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>

推送修改:

@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ export hf_token="xxx" # 这里操作时改为
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ git remote set-url origin https://lldhsds:$hf_token@huggingface.co/spaces/lldhsds/intern_cobuild/
@lldhsds ➜ /workspaces/codespaces-jupyter/intern_cobuild (main) $ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 596 bytes | 596.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To https://huggingface.co/spaces/lldhsds/intern_cobuild/fb177af..3b8fef2  main -> main

推送成功后查看Space界面,界面已更新:

L0-<a class=maas-task4-hf-space" />


http://www.ppmy.cn/embedded/141371.html

相关文章

Day28 贪心算法 part02

122.买卖股票的最佳时机II 本题解法很巧妙,本题大家可以先自己思考一下然后再看题解,会有惊喜! class Solution {public int maxProfit(int[] prices) {//分析每一天的情况。只要保证今天买,明天卖可以不亏钱,那就是最大的利润。把每一天可以赚钱的机会都不放过,先把能挣…

基于Matlab的变压器仿真模型的建模方法(7):单相三绕组变压器的空载合闸和负载运行瞬态分析

前言: 这里推出全网最详细、最全面的基于电机动态分析的Simulink仿真模型的建模方法、经验和技巧。本节从单相三绕组变压器的基本方程出发,导出相应的等效电路电路,并据此用Matlab/Simulink的SimPowerSystems模块库的电路元件构造了一个考虑铁心损耗和主磁路饱和因素的仿真模…

uniapp在H5使用vue-router路由返回上一页不会触发销毁函数解决方法

问题&#xff1a;uniapp在H5使用vue-router路由&#xff0c;如果在H5平台上进行页面刷新操作&#xff0c;再返回上一页&#xff0c;可能会遇到beforeDestroy、destroyed、onUnload生命周期钩子不被触发的问题。这是因为在H5中&#xff0c;页面的刷新实际上是整个应用的重新加载…

光学工程是不是劝退专业?

接触光学快十年&#xff0c;四年美国工业界经验&#xff0c;谈谈对光学的看法。 门槛高。学好光学需要很多基础&#xff1a;数学、物理、半导体、电磁场等等&#xff0c;大学毕业可能只是一知半解。光学的世界非常深非常广&#xff0c;即使学了十年也只懂某一特定领域的特定知…

Docker 容器隔离的关键技术:Namespace

Docker 容器隔离的关键技术&#xff1a;Namespace 在 Docker 容器中&#xff0c;Namespace 是 Linux 内核提供的一种隔离机制&#xff0c;用于实现资源的独立性和隔离性。简单来说&#xff0c;它让每个容器感觉自己是独立运行的&#xff0c;就像一台单独的计算机一样。 Names…

java 字符串反转 详解

在 Java 中&#xff0c;字符串反转可以通过多种方法实现&#xff0c;这里将介绍几种常见的方法来达到这一目的&#xff1a; 1. 使用 StringBuilder 或 StringBuffer 这是最简单也是最常见的方法。StringBuilder 和 StringBuffer 都有一个内置的方法 reverse()&#xff0c;可以…

Hive安装 保姆级安装教程

Hive安装 保姆级安装教程 1、内嵌模式 上传 压缩包 /opt/modules 解压&#xff1a; tar -zxvf apache-hive-3.1.2-bin.tar.gz -C /opt/installs/ 重命名&#xff1a; mv apache-hive-3.1.2-bin/ hive 配置环境变量&#xff1a;vi /etc/profileexport HIVE_HOME/opt/installs…

酷!用豆包MarsCode 写了一个五子棋小游戏

作者&#xff1a;一起重学前端 前言 2017 年&#xff0c;当时大学三本毕业前端工作一年&#xff0c;去深圳找工作面试&#xff0c;在拉勾上海投&#xff0c;接到某公司前端开发团队回复询问邮箱账号&#xff0c;喜滋滋的以为可以有面试了。一看邮箱&#xff0c;抛给我一道面试…