R-Kentaren commited on
Commit
22333c2
ยท
verified ยท
1 Parent(s): fa8cca2

Create tools/huggingface.py

Browse files
Files changed (1) hide show
  1. tools/huggingface.py +30 -0
tools/huggingface.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tqdm
3
+ import requests
4
+
5
+ try:
6
+ import wget
7
+ except:
8
+ wget = None
9
+
10
+ def HF_download_file(url, output_path=None):
11
+ url = url.replace("/blob/", "/resolve/").replace("?download=true", "").strip()
12
+ 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)
13
+
14
+ if wget != None:
15
+ wget.download(url, out=output_path)
16
+ return output_path
17
+ else:
18
+ response = requests.get(url, stream=True, timeout=300)
19
+
20
+ if response.status_code == 200:
21
+ progress_bar = tqdm.tqdm(total=int(response.headers.get("content-length", 0)), desc=os.path.basename(url), ncols=100, unit="byte", leave=False)
22
+
23
+ with open(output_path, "wb") as f:
24
+ for chunk in response.iter_content(chunk_size=10 * 1024 * 1024):
25
+ progress_bar.update(len(chunk))
26
+ f.write(chunk)
27
+
28
+ progress_bar.close()
29
+ return output_path
30
+ else: raise ValueError(response.status_code)