-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 1.82 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import logging
import sys
import numpy as np
import os
from gguf.gguf_reader import GGUFReader
logger = logging.getLogger("reader")
def parse_memmap(value):
"""
normalize np.memmap values:
- uint8 1D -> UTF-8
- length 1 -> scalar python
- number -> list
- bool -> list of bools
"""
arr = np.asarray(value)
# text
if arr.dtype == np.uint8:
raw = arr.tobytes()
try:
txt = raw.decode("utf-8")
return txt
except UnicodeDecodeError:
# cannot decode as UTF8 return as intlist
return arr.tolist()
# scalar
if arr.size == 1:
return arr.reshape(()).item()
# number
if arr.dtype.kind in ("i", "u"): # int
return arr.astype(np.int64, copy=False).tolist()
if arr.dtype.kind == "f": # float
return arr.astype(np.float64, copy=False).tolist()
if arr.dtype.kind == "b": # bool
return arr.astype(np.bool_, copy=False).tolist()
# fallback
return arr.tolist()
def read_gguf_fields(gguf_file_path):
reader = GGUFReader(gguf_file_path)
print("── Model ────")
print(os.path.basename(gguf_file_path))
print("────────────────────────")
max_key_length = max(len(key) for key in reader.fields.keys())
for key, field in reader.fields.items():
value = field.parts[field.data[0]]
text = parse_memmap(value)
print(f"{key:{max_key_length}} : {text}")
if __name__ == '__main__':
if len(sys.argv) < 2:
logger.info("Usage: extractr <gguf_filepath>")
sys.exit(1)
gguf_file_path = sys.argv[1]
try:
read_gguf_fields(gguf_file_path)
except:
print("Cannot read gguf file:", gguf_file_path)