探索如何充分利用 Hugging Face、魔搭社区和魔乐社区的资源和工具,学习模型下载、上传以及创建专属Space,玩转三大平台。
需要实现的任务:1. 下载模型 2. 上传模型 3. Space上传
1. HF 平台
HF官网: https://huggingface.co/
1.1 InternLM模型下载
internlm2_5-1_8b:https://huggingface.co/internlm/internlm2_5-1_8b
因为网络和磁盘有限的原因,强烈不建议在 InternStudio 运行,因此这里使用CodeSpace(https://github.com/codespaces)
Github CodeSpace是Github推出的线上代码平台,提供了一系列templates,如Jupyter Notebook。
在终端中下载一下依赖:
1 2 3 4 5 6
| 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.2 下载internlm2_5-7b-chat的配置文件
接下来,下载模型的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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}")
|
运行该文件,从HF中下载配置文件:

1.3 下载internlm2_5-chat-1_8b并打印示例输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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 }
|
上述代码使用了internlm2_5-1_8b模型做了一个续写任务,输出如下:

1.4 Hugging Face Spaces的使用
Spaces (https://huggingface.co/spaces)使得开发者可以快速将我们的模型部署为可交互的 web 应用,且无需担心后端基础设施或部署的复杂性。

找到该目录文件夹下的index.html文件,修改我们的html代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!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到远程仓库上了,它会自动更新页面:
1 2 3
| git add . git commit -m "update: colearn page" git push
|
若报错:
1
| git remote set-url origin https://wendyflv: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@huggingface.co/spaces/wendyflv/intern_cobuild/
|
再次进入Space界面:

1.5 模型上传
安装git lfs,对大文件系统支持:

使用huggingface-cli login命令进行登录:

创建项目intern_study_L0_4:

创建README.md文件:

用git提交到远程仓库:

前往HF:wendyflv/intern_study_L0_4 · Hugging Face

2. 魔搭社区
魔搭社区旨在汇集来自AI社区的最先进的机器学习模型,并简化在实际应用中使用AI模型的流程。通过ModelScope,用户可以轻松地探索、推理、微调和部署各种AI模型。
2.1 下载模型
安装下必要的包


使用modelscope的cli工具来下载internlm2_5-7b-chat

2.2 上传模型
先在魔搭社区创建一个新模型,命名intern_study_L0_4
下载该模型:

进入README.md文件,修改

然后提交

这里报错,需要使用token作为密码

上传成功!https://www.modelscope.cn/models/wendyflv/intern_study_L0_4
3. 魔乐社区
魔乐社区是一个提供多样化、开源模型的平台,旨在促进开发者和研究人员在最先进的模型和流行应用上进行协作。
为模型文件创建目录:

下载模型:

model-00001-of-00002.safetensors 这个文件没找到?
尝试手动下载,也不行?