NoQuest commited on
Commit
bca9652
·
1 Parent(s): d57b221

uploader and downloader file

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