簡單介紹網站建設的一般流程專業(yè)公司網絡推廣
前言
在使用huggingface把玩各種大模型時,如果選擇從遠程加載模型,這個過程可能因為網絡問題而非常耗時甚至直接失敗,所以把模型、分詞器等相關文件下載到本地,再直接從本地加載就成了不可回避的流程。
在進入具體版本的模型后,我們可以去Files and Versions這個菜單項下面找到需要下載到本地的全部模型(以WizardCoder為例)
第一步是獲取我們想要下載的文件的下載時url
在文件大小的右側,可以看到一個向下的箭頭表示下載,鼠標移動到箭頭上,右鍵,選擇“復制鏈接地址”,這樣就得到了下載時url
我們將這些url存放到list中。
第二步就是python代碼,這里除了用于請求的request庫,我還使用了tqdm庫,tqdm也可通過pip install來安裝,它的作用是在下載較大的文件時,我們可以在終端看到下載的速度和進度
import requests
import os
from tqdm import tqdmurls = ["https://huggingface.co/WizardLM/WizardCoder-15B-V1.0/resolve/main/pytorch_model.bin"
]filepath = "WizardCoder/WizardCoder-15B-V1.0"def download_file(url):filename = url.split("/")[-1]download_path = os.path.join(filepath, filename)response = requests.get(url, stream=True, verify=False)response.raise_for_status()file_size = int(response.headers.get("Content-Length", 0)) # 獲取待下載的文件大小chunk_size = 8192 # 讀取的數據塊的大小是8千字節(jié)with open(download_path, "wb") as file, tqdm(total=file_size, unit="B", unit_scale=True, unit_divisor=1024, desc=filename) as progress_bar:for chunk in response.iter_content(chunk_size=chunk_size):if chunk:file.write(chunk)progress_bar.update(1)for url in urls:download_file(url)