NoQuest commited on
Commit
0a4e529
·
1 Parent(s): c3b3c40

DownloadUploadbleOk2.py

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