Commit
·
ef4098e
1
Parent(s):
deb38be
fix: add HF token Authorization header for model downloads from huggingface.co
Browse files- DeepFakeAI/utilities.py +11 -3
DeepFakeAI/utilities.py
CHANGED
|
@@ -7,6 +7,7 @@ import ssl
|
|
| 7 |
import subprocess
|
| 8 |
import tempfile
|
| 9 |
import urllib
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import List, Optional
|
| 12 |
|
|
@@ -170,10 +171,17 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non
|
|
| 170 |
if os.path.exists(download_file_path):
|
| 171 |
return
|
| 172 |
try:
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
with tqdm(total = total, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024) as progress:
|
| 176 |
-
|
| 177 |
return
|
| 178 |
except Exception as e: # try next mirror
|
| 179 |
last_error = e
|
|
|
|
| 7 |
import subprocess
|
| 8 |
import tempfile
|
| 9 |
import urllib
|
| 10 |
+
from urllib.request import Request, urlopen, urlretrieve
|
| 11 |
from pathlib import Path
|
| 12 |
from typing import List, Optional
|
| 13 |
|
|
|
|
| 171 |
if os.path.exists(download_file_path):
|
| 172 |
return
|
| 173 |
try:
|
| 174 |
+
headers = {}
|
| 175 |
+
# If downloading from Hugging Face, include token when available
|
| 176 |
+
if 'huggingface.co' in url:
|
| 177 |
+
token = os.environ.get('TOKEN') or os.environ.get('HF_TOKEN')
|
| 178 |
+
if token:
|
| 179 |
+
headers['Authorization'] = f'Bearer {token}'
|
| 180 |
+
req: Request = Request(url, headers=headers)
|
| 181 |
+
resp = urlopen(req) # type: ignore[assignment]
|
| 182 |
+
total = int(resp.headers.get('Content-Length', 0) or 0)
|
| 183 |
with tqdm(total = total, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024) as progress:
|
| 184 |
+
urlretrieve(url, download_file_path, reporthook = lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
|
| 185 |
return
|
| 186 |
except Exception as e: # try next mirror
|
| 187 |
last_error = e
|