|
| 1 | +import requests |
| 2 | +import json |
| 3 | +import os |
| 4 | +import struct |
| 5 | + |
| 6 | +def make_request(url, params=None): |
| 7 | + print(f"Making request to {url}...") |
| 8 | + response = requests.get(url, params=params) |
| 9 | + if response.status_code == 200: |
| 10 | + return json.loads(response.text) |
| 11 | + else: |
| 12 | + print(f"Request failed with status code {response.status_code}") |
| 13 | + return None |
| 14 | + |
| 15 | +def check_magic_and_version(filename): |
| 16 | + with open(filename, 'rb') as f: |
| 17 | + # Read the first 6 bytes from the file |
| 18 | + data = f.read(6) |
| 19 | + |
| 20 | + # Unpack the binary data, interpreting the first 4 bytes as a little-endian unsigned int |
| 21 | + # and the next 2 bytes as a little-endian unsigned short |
| 22 | + magic, version = struct.unpack('<I H', data) |
| 23 | + |
| 24 | + print(f"magic: 0x{magic:08x}, version: 0x{version:04x}, file: {filename}") |
| 25 | + |
| 26 | + return magic, version |
| 27 | + |
| 28 | +def download_file(url, destination): |
| 29 | + print(f"Downloading {url} to {destination}...") |
| 30 | + response = requests.get(url, stream=True) |
| 31 | + if response.status_code == 200: |
| 32 | + with open(destination, 'wb') as f: |
| 33 | + total_downloaded = 0 |
| 34 | + for chunk in response.iter_content(chunk_size=1024): |
| 35 | + if chunk: # filter out keep-alive new chunks |
| 36 | + f.write(chunk) |
| 37 | + total_downloaded += len(chunk) |
| 38 | + if total_downloaded >= 10485760: # 10 MB |
| 39 | + print('.', end='', flush=True) |
| 40 | + total_downloaded = 0 |
| 41 | + print("\nDownload complete.") |
| 42 | + |
| 43 | + # Creating a symbolic link from destination to "model.bin" |
| 44 | + if os.path.isfile("model.bin"): |
| 45 | + os.remove("model.bin") # remove the existing link if any |
| 46 | + os.symlink(destination, "model.bin") |
| 47 | + else: |
| 48 | + print(f"Download failed with status code {response.status_code}") |
| 49 | + |
| 50 | +def get_user_choice(model_list): |
| 51 | + # Print the enumerated list |
| 52 | + print("\n") |
| 53 | + for i, (model_id, rfilename) in enumerate(model_list): |
| 54 | + print(f"{i+1}: Model ID: {model_id}, RFilename: {rfilename}") |
| 55 | + |
| 56 | + # Get user's choice |
| 57 | + choice = input("Choose a model to download by entering the corresponding number: ") |
| 58 | + try: |
| 59 | + index = int(choice) - 1 |
| 60 | + if 0 <= index < len(model_list): |
| 61 | + # Return the chosen model |
| 62 | + return model_list[index] |
| 63 | + else: |
| 64 | + print("Invalid choice.") |
| 65 | + except ValueError: |
| 66 | + print("Invalid input. Please enter a number corresponding to a model.") |
| 67 | + except IndexError: |
| 68 | + print("Invalid choice. Index out of range.") |
| 69 | + |
| 70 | + return None |
| 71 | + |
| 72 | +import argparse |
| 73 | + |
| 74 | +def main(): |
| 75 | + # Create an argument parser |
| 76 | + parser = argparse.ArgumentParser(description='Process the model version.') |
| 77 | + parser.add_argument('-v', '--version', type=int, default=0x0003, |
| 78 | + help='an integer for the version to be used') |
| 79 | + |
| 80 | + # Parse the arguments |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + # Define the parameters |
| 84 | + params = { |
| 85 | + "author": "TheBloke", # Filter by author |
| 86 | + "tags": "llama" |
| 87 | + } |
| 88 | + |
| 89 | + models = make_request('https://huggingface.co/api/models', params=params) |
| 90 | + if models is None: |
| 91 | + return |
| 92 | + |
| 93 | + model_list = [] |
| 94 | + # Iterate over the models |
| 95 | + for model in models: |
| 96 | + model_id = model['id'] |
| 97 | + model_info = make_request(f'https://huggingface.co/api/models/{model_id}') |
| 98 | + if model_info is None: |
| 99 | + continue |
| 100 | + |
| 101 | + for sibling in model_info.get('siblings', []): |
| 102 | + rfilename = sibling.get('rfilename') |
| 103 | + if rfilename and 'q5_1' in rfilename: |
| 104 | + model_list.append((model_id, rfilename)) |
| 105 | + |
| 106 | + model_choice = get_user_choice(model_list) |
| 107 | + if model_choice is not None: |
| 108 | + model_id, rfilename = model_choice |
| 109 | + url = f"https://huggingface.co/{model_id}/resolve/main/{rfilename}" |
| 110 | + download_file(url, rfilename) |
| 111 | + _, version = check_magic_and_version(rfilename) |
| 112 | + if version != args.version: |
| 113 | + print(f"Warning: Expected version {args.version}, but found different version in the file.") |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + main() |
0 commit comments