Skip to content

Instantly share code, notes, and snippets.

@twobob
Created October 22, 2023 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twobob/807d7e3107fe2d19083c2594319a22c9 to your computer and use it in GitHub Desktop.
Save twobob/807d7e3107fe2d19083c2594319a22c9 to your computer and use it in GitHub Desktop.
FILENAME = "airoboros-c34b-3.1.2.Q5_K_M.gguf" downloader
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from huggingface_hub import hf_hub_url
import requests
import threading
from threading import Lock
from IPython.display import display, clear_output
# Initialize shared variable and lock
downloaded_bytes = 0
lock = Lock()
# Function to update progress bar
def progress_bar(file_size):
global downloaded_bytes
progress = (downloaded_bytes / file_size) * 100
print(f"Download Progress: {progress:.2f}%", end='\r')
# Function to add space before caps
def add_space_before_caps_fixed(input_str):
output_str = ""
add_space = False
encountered_slash = False
for char in input_str:
if char == '/':
encountered_slash = True
if add_space and char.isupper() and not encountered_slash:
output_str += " "
output_str += char
if char.isupper() and not encountered_slash:
add_space = True
return output_str
# Shared stop event for all threads
stop_event = threading.Event()
# Function for downloading a chunk of the file
def download_chunk(url, start_byte, end_byte, chunk_num, destination_path, file_size):
global downloaded_bytes
headers = {'Range': f"bytes={start_byte}-{end_byte}"}
response = requests.get(url, headers=headers, stream=True, allow_redirects=True)
if response.status_code != 206:
print(f"HTTP Error: {response.status_code}")
return
with open(f"{destination_path}.part{chunk_num}", "wb") as f:
for data in response.iter_content(1024):
if stop_event.is_set():
print("Early exit requested.")
return
f.write(data)
with lock:
downloaded_bytes += len(data)
progress_bar(file_size)
# Function for merging the downloaded chunks
def merge_files(destination_path, num_chunks):
print("Downloads Complete. Uniting files")
with open(destination_path, "wb") as f_out:
for i in range(num_chunks):
with open(f"{destination_path}.part{i}", "rb") as f_in:
f_out.write(f_in.read())
os.remove(f"{destination_path}.part{i}")
# Function for multi-threaded download
def multi_threaded_download(url, destination_path):
global downloaded_bytes
downloaded_bytes = 0 # Reset for each new download
response = requests.head(url, allow_redirects=True)
if response.status_code != 200:
print(f"HTTP Error: {response.status_code}")
return
file_size = int(response.headers.get("content-length", 0))
NUM_OF_THREADS = 4
chunk_size = file_size // NUM_OF_THREADS
futures = []
with ThreadPoolExecutor(max_workers=NUM_OF_THREADS) as executor:
for i in range(NUM_OF_THREADS):
start_byte = i * chunk_size
end_byte = start_byte + chunk_size - 1 if i < NUM_OF_THREADS - 1 else file_size - 1
futures.append(executor.submit(download_chunk, url, start_byte, end_byte, i, destination_path, file_size))
for future in as_completed(futures):
future.result()
if not stop_event.is_set():
merge_files(destination_path, NUM_OF_THREADS)
# Main program
REPO_ID = "TheBloke/Airoboros-c34B-3.1.2-GGUF"
FILENAME = "airoboros-c34b-3.1.2.Q5_K_M.gguf"
destination_path = f"C:\\webui\\models\\{add_space_before_caps_fixed(REPO_ID)}\\{FILENAME}"
destination_directory = os.path.dirname(destination_path)
os.makedirs(destination_directory, exist_ok=True)
print(f"Directory ensured: {destination_directory}")
download_url = hf_hub_url(REPO_ID, FILENAME)
print("Downloading model file...")
try:
multi_threaded_download(download_url, destination_path)
print("Task completed.")
except KeyboardInterrupt:
stop_event.set()
print("Early exit initiated.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment