当前位置:   article > 正文

实用篇| huggingface网络不通

实用篇| huggingface网络不通

之前文章《Transformer原理》中介绍过,Transformers 是由 Hugging Face 开发的一个包,支持加载目前绝大部分的预训练模型。随着 BERT、GPT 等大规模语言模型的兴起,越来越多的公司和研究者采用 Transformers 库来构建应用。

Hugging Face是一家美国公司,专门开发用于构建机器学习应用的工具。该公司的代表产品是其为自然语言处理应用构建的transformers库,以及允许用户共享机器学习模型和数据集的平台。

现在很多工具都是基于transformers进行二次开发,其中包括之前介绍的《研究篇| 一款深入浅出的微调框架》的LLama Factory和《微调实操一: 增量预训练(Pretraining)》的MedicalGPT。

但是我们经常出现下载模型或者数据集出现如下网络错误:

requests.exceptions.ConnectionError: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /api/whoami-v2 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fc793a70990>: Failed to establish a new connection: [Errno 101] Network is unreachable'))"), '(Request ID: 9e2e7970-7254-447a-9b72-29f4f7ff8d1f)')
  • 1

image.png

主要是因为huggingface.co需要科学上网,这个导致开发者不得不使用代理或者国外服务器.

今天给大家介绍2种破解huggingface大法, 从此告别网络不通难题

01 hf-mirror 镜像网站

hf-mirror.com是Huggingface的镜像网站,它提供了一个备用的域名来访问Huggingface的资源和功能。它的作用类似于一个代理服务器,可以帮助我们绕过无法访问huggingface.co的限制,正常地获取所需的模型和数据集。不需要开发自己去购买代理服务.

1.1 hr-mirror配置

1.1.1 安装依赖

pip install -U huggingface_hub
  • 1

1.1.2 设置镜像endpoint

export HF_ENDPOINT=https://hf-mirror.com
  • 1
或者
  • 1
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
  • 1

1.1.3 下载模型

huggingface-cli download --resume-download --local-dir-use-symlinks False bigscience/bloom-560m --local-dir bloom-560m
  • 1

目录.cache空间不足时使用–cache-dir传参更改下载缓存路径

huggingface-cli download --resume-download --local-dir-use-symlinks False stabilityai/stable-diffusion-xl-base-1.0 --local-dir stabilityai --cache-dir stabilityai.cache
  • 1

02 魔塔

另外一种方式, 使用魔塔, ModelScope魔搭社区-ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单。目前LLama Factory就是使用这个方式解决了hugging face网络问题, 他们代码就是集成了魔塔工具

def try_download_model_from_ms(model_args: "ModelArguments") -> str:
    if not use_modelscope() or os.path.exists(model_args.model_name_or_path):
        return model_args.model_name_or_path

    try:
        from modelscope import snapshot_download

        revision = "master" if model_args.model_revision == "main" else model_args.model_revision
        return snapshot_download(model_args.model_name_or_path, revision=revision, cache_dir=model_args.cache_dir)
    except ImportError:
        raise ImportError("Please install modelscope via `pip install modelscope -U`")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

上面的方式是代码集成的方式,还有就是可以手动下载模型

git lfs install
mkdir THUDM
cd THUDM
git clone https://www.modelscope.cn/ZhipuAI/chatglm2-6b.git


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/638343
推荐阅读
相关标签
  

闽ICP备14008679号