Created
May 29, 2023 15:43
-
-
Save takuma104/aaedd400e21b37d1e4297fe483e90695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import struct | |
import sys | |
from pprint import pprint | |
def parse_safetensors_header(file_path): | |
# modified from https://huggingface.co/docs/safetensors/metadata_parsing | |
with open(file_path, 'rb') as f: | |
header_bytes = f.read(8) | |
# Interpret the bytes as a little-endian unsigned 64-bit integer | |
length_of_header = struct.unpack('<Q', header_bytes)[0] | |
# Read length_of_header bytes starting from the 9th byte | |
header_bytes = f.read(length_of_header) | |
# Interpret the bytes as a JSON object | |
header = json.loads(header_bytes) | |
return header | |
if __name__ == '__main__': | |
file_path = sys.argv[1] | |
metadata = parse_safetensors_header(file_path)['__metadata__'] | |
pprint(metadata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: https://civitai.com/models/13239/light-and-shadow