在hugface下载llm模型
一、使用镜像网站
- 镜像网站:hf-mirror.com
1 | import os |
- 镜像网站二:https://aliendao.cn/#/
- 镜像网站三:https://aifasthub.com/models
二、使用huggingface-cli命令行工具
pip下载工具
1
pip install -U huggingface_hub -i https://pypi.tuna.tsinghua.edu.cn/simple/
这里使用了清华的镜像源进行全局下载。
设置系统环境变量
将系统变量HF_ENDPOINT 设置为 https://hf-mirror.com,然后运行您的脚本,如1
export HF_ENDPOINT=https://hf-mirror.com
模型下载
基本命令1
huggingface-cli download --resume-download --local-dir-use-symlinks False ${模型在huggingface上的名字} ${模型文件名}.gguf --local-dir ${模型存储路径}
Gated Model
如果需要下载限制访问的模型(Gated Model),则需要添加 –token 参数,并使用在 Hugging Face 官网获得的访问令牌。1
huggingface-cli download --token hr_*** --resume-download --local-dir-use-symlinks False ${模型在huggingface上的名字} ${模型文件名}.gguf --local-dir ${模型存储路径}
三、 直接使用huggingface模型
- 安装
1
pip install -U sentence-transformers
- 使用 (Sentence-Transformers)
1
2
3
4
5
6from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings) - 使用 (HuggingFace Transformers)
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
30
31
32
33from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
四、用脚本在镜像站下载
为了简化下载流程,单独制作了一个脚本(hf_down.py):
使用方式(以下载llama3-8b为例):
找到你要的模型地址:
1
https://hf-mirror.com/
输入完整的内容
1
python3 hf_down.py --dtype 1 --down_dir meta-llama/Meta-Llama-3-8B --local_dir D:/AI/models/Llama-3-8B
或者其简化版:
1
python3 hf_down.py --t 1 --d meta-llama/Meta-Llama-3-8B --l D:/AI/models/Llama-3-8B
也可以用极简版(下载模型时候可用):
- 默认dtype=1
- 默认local_dir位置为
脚本当前位置
+模型位置的最后一段
(如此处:./Meta-Llama-3-8B
)1
python3 hf_down.py --d meta-llama/Meta-Llama-3-8B
- 可以先运行脚本,后输入地址依次输入:dtype(默认为1)、down_dir(必填)、local_dir( 默认为
1
python3 hf_down.py
脚本当前位置
+模型位置的最后一段
)
具体脚本如下:
1 | import os |
评论