2023年 huggingface.co 国内可用 IP,修改/etc/hosts 可高速访问,附代码

2023年10月30更新

目前反馈所有IP已经不可用,有兴趣的同学可以尝试,理论上这个方法适用于所有内容

原理分析

huggingface.co 使用的 CDN 与其它 GFW 清单重合,很多 IP 无法使用。

解决办法是找到与 google、fb 无重合的 IP 段。

几百上千 G 的模型文件怎么搞,走代理还是肉身背硬盘拷?

step 1. 查找全球可用 IP

https://17ce.com/site/dns/20230915_fc0261d053b311eeac17e316c626d952:1.html

挑选 IP 的时候注意避开敏感名称

step 2,配置 hosts 以后验证

13.33.174.80   huggingface.co www.huggingface.co cdn-lfs.huggingface.co
  1. git clone 模型
git lfs clone https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat
  1. 手动下载模型
wget https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat/resolve/main/pytorch_model-00001-of-00003.bin

通过程序自动测试可用 IP

import re
import requests

iplist = "https://17ce.com/site/dns/20230915_fc0261d053b311eeac17e316c626d952:1.html"

text = requests.get(iplist)

# 使用正则表达式匹配 IP 地址的模式
ip_pattern = r'>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<'

# 使用 findall 函数找到所有匹配的 IP 地址
ip_addresses = re.findall(ip_pattern, str(text.content))
print("found {} ip".format(len(ip_addresses)))

ip_addresses = set(ip_addresses)
for ip in ip_addresses:
    url = f"http://{ip}/baichuan-inc/Baichuan2-13B-Chat/resolve/main/tokenizer_config.json"

    header = {
        "Host": "huggingface.co"
    }
    try:
        response = requests.head(url, headers=header, timeout=5)
        print(f"{ip} connect success")
    except:
        log = ""

多线程版本

import re
import requests
from concurrent.futures import ThreadPoolExecutor

def check_ip(ip):
    url = f"http://{ip}/baichuan-inc/Baichuan2-13B-Chat/resolve/main/tokenizer_config.json"
    header = {
        "Host": "huggingface.co"
    }
    try:
        response = requests.head(url, headers=header, timeout=3)
        print(f"{ip} : success")
    except Exception as e:
        ip = ""


def main():
    iplist = [
        "https://17ce.com/site/dns/20240312_477424e0e05511eebb8373aada47cca9:1.html"
    ]

    for ip_list in iplist:
        text = requests.get(ip_list)
        ip_pattern = r'>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<'
        ip_addresses = re.findall(ip_pattern, str(text.content))
        ip_addresses = set(ip_addresses)
        print("Found {} IPs".format(len(ip_addresses)))

        with ThreadPoolExecutor(max_workers=10) as executor:
            executor.map(check_ip, ip_addresses)

if __name__ == "__main__":
    main()

发表评论