Spaces:
Sleeping
Sleeping
DownloadUploadbleOk.py
Browse files- DownloadUploadbleOk.py +103 -0
DownloadUploadbleOk.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
from huggingface_hub import HfFolder, hf_hub_url
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
import tqdm
|
7 |
+
from requests.adapters import HTTPAdapter
|
8 |
+
from requests.exceptions import ConnectionError, RequestException, Timeout
|
9 |
+
from tqdm.contrib.concurrent import thread_map
|
10 |
+
from pathlib import Path
|
11 |
+
import time
|
12 |
+
|
13 |
+
# Save your token
|
14 |
+
HfFolder.save_token('') # Replace with your actual token
|
15 |
+
|
16 |
+
# Define the repository to download from
|
17 |
+
repo_id = "NoQuest/LLmSave"
|
18 |
+
repo_type = "model"
|
19 |
+
|
20 |
+
# Local path where you want to save the downloaded files
|
21 |
+
local_folder_path = "./models"
|
22 |
+
|
23 |
+
# Variable to specify the file or directory to download
|
24 |
+
download_target = "LLamandementFineTuneSansNotation16Q.gguf" # Change this to the desired file or directory name
|
25 |
+
|
26 |
+
print(f"Downloading {download_target} from {repo_id} to {local_folder_path}...")
|
27 |
+
|
28 |
+
# Create the local directory if it doesn't exist
|
29 |
+
os.makedirs(local_folder_path, exist_ok=True)
|
30 |
+
|
31 |
+
# Print the URL for debugging
|
32 |
+
print(f"URL: {hf_hub_url(repo_id, download_target, repo_type=repo_type)}")
|
33 |
+
|
34 |
+
def get_session(max_retries=5):
|
35 |
+
session = requests.Session()
|
36 |
+
if max_retries:
|
37 |
+
session.mount('https://cdn-lfs.huggingface.co', HTTPAdapter(max_retries=max_retries))
|
38 |
+
session.mount('https://huggingface.co', HTTPAdapter(max_retries=max_retries))
|
39 |
+
# ... (add authentication if needed)
|
40 |
+
return session
|
41 |
+
|
42 |
+
def get_single_file(url, output_folder, start_from_scratch=False, max_retries=7):
|
43 |
+
filename = Path(url.rsplit('/', 1)[1])
|
44 |
+
output_path = output_folder / filename
|
45 |
+
attempt = 0
|
46 |
+
while attempt < max_retries:
|
47 |
+
attempt += 1
|
48 |
+
session = get_session()
|
49 |
+
headers = {}
|
50 |
+
mode = 'wb'
|
51 |
+
if output_path.exists() and not start_from_scratch:
|
52 |
+
# Resume download
|
53 |
+
r = session.get(url, stream=True, timeout=20)
|
54 |
+
total_size = int(r.headers.get('content-length', 0))
|
55 |
+
if output_path.stat().st_size >= total_size:
|
56 |
+
return
|
57 |
+
headers = {'Range': f'bytes={output_path.stat().st_size}-'}
|
58 |
+
mode = 'ab'
|
59 |
+
try:
|
60 |
+
with session.get(url, stream=True, headers=headers, timeout=30) as r:
|
61 |
+
r.raise_for_status()
|
62 |
+
total_size = int(r.headers.get('content-length', 0))
|
63 |
+
block_size = 1024 * 1024 # 1MB
|
64 |
+
tqdm_kwargs = {'total': total_size, 'unit': 'iB', 'unit_scale': True, 'bar_format': '{l_bar}{bar}|{n_fmt}/{total_fmt}{rate_fmt}'}
|
65 |
+
with open(output_path, mode) as f:
|
66 |
+
with tqdm.tqdm(**tqdm_kwargs) as t:
|
67 |
+
for data in r.iter_content(block_size):
|
68 |
+
f.write(data)
|
69 |
+
t.update(len(data))
|
70 |
+
break # Exit loop if successful
|
71 |
+
except (RequestException, ConnectionError, Timeout) as e:
|
72 |
+
print(f"Error downloading {filename}: {e}.")
|
73 |
+
print(f"That was attempt {attempt}/{max_retries}.", end='')
|
74 |
+
if attempt < max_retries:
|
75 |
+
print(f"Retry begins in {2**attempt} seconds.")
|
76 |
+
time.sleep(2**attempt)
|
77 |
+
else:
|
78 |
+
print("Failed to download after the maximum number of attempts.")
|
79 |
+
|
80 |
+
def start_download_threads(file_list, output_folder, start_from_scratch=False, threads=4):
|
81 |
+
thread_map(lambda url: get_single_file(url, output_folder, start_from_scratch=start_from_scratch), file_list, max_workers=threads, disable=True)
|
82 |
+
|
83 |
+
def download_model_files(model, branch, links, output_folder, start_from_scratch=False, threads=4):
|
84 |
+
output_folder = Path(output_folder)
|
85 |
+
#output_folder.mkdir(parents=True, exist_ok=True)
|
86 |
+
output_folder.mkdir(parents=True, exist_ok=True)
|
87 |
+
|
88 |
+
# ... (add metadata writing if needed)
|
89 |
+
print(f"Downloading the model to {output_folder}")
|
90 |
+
start_download_threads(links, output_folder, start_from_scratch=start_from_scratch, threads=threads)
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
# Download the specified file or directory
|
95 |
+
session = get_session()
|
96 |
+
links = [hf_hub_url(repo_id, download_target, repo_type=repo_type)]
|
97 |
+
|
98 |
+
branch = "main"
|
99 |
+
|
100 |
+
download_model_files(repo_id, branch, links, local_folder_path)
|
101 |
+
|
102 |
+
print("Download complete!")
|
103 |
+
|