Spaces:
Build error
Build error
Create tools/hf.py
Browse files- tools/hf.py +20 -0
tools/hf.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tqdm
|
3 |
+
import requests
|
4 |
+
|
5 |
+
def HF_download_file(url, output_path=None):
|
6 |
+
url = url.replace("/blob/", "/resolve/").replace("?download=true", "").strip()
|
7 |
+
output_path = os.path.basename(url) if output_path is None else (os.path.join(output_path, os.path.basename(url)) if os.path.isdir(output_path) else output_path)
|
8 |
+
response = requests.get(url, stream=True, timeout=300)
|
9 |
+
|
10 |
+
if response.status_code == 200:
|
11 |
+
progress_bar = tqdm.tqdm(total=int(response.headers.get("content-length", 0)), desc=os.path.basename(url), ncols=100, unit="byte", leave=False)
|
12 |
+
|
13 |
+
with open(output_path, "wb") as f:
|
14 |
+
for chunk in response.iter_content(chunk_size=10 * 1024 * 1024):
|
15 |
+
progress_bar.update(len(chunk))
|
16 |
+
f.write(chunk)
|
17 |
+
|
18 |
+
progress_bar.close()
|
19 |
+
return output_path
|
20 |
+
else: raise ValueError(response.status_code)
|