Skip to content

Commit 126e623

Browse files
Add model hash to version
1 parent 6631908 commit 126e623

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

compdec/compdec.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import inspect
33
import os
44
import sys
5+
import hashlib
56
from argparse import ArgumentParser
67

78
# These are configurable, but highly dependant on the shipped model
@@ -34,10 +35,15 @@ def print_help(parser: ArgumentParser) -> None:
3435
print()
3536
print(description)
3637

37-
def print_version() -> None:
38-
hash = ""
38+
def print_version(model_path) -> None:
39+
hash = hashlib.sha256()
40+
with open(model_path, "rb") as file:
41+
block = file.read(4096)
42+
while len(block) > 0:
43+
hash.update(block)
44+
block = file.read(4096)
3945
print("CompDec v1.0.0")
40-
print("Model hash: {}".format(hash))
46+
print("Model hash: {}".format(hash.hexdigest()))
4147

4248
def load_samples_from_file(sample_path):
4349
import numpy
@@ -83,6 +89,15 @@ def softmax(predictions):
8389
for i in range(len(CLASS_NAMES)):
8490
print("{:9}: {:2.2f}%".format(CLASS_NAMES[i], normalized_predictions[i] * 100))
8591

92+
def assert_files(paths):
93+
files_exist = True
94+
for path in paths:
95+
if not os.path.exists(path):
96+
print("Error: File does not exist:", path)
97+
files_exist = False
98+
if not files_exist:
99+
quit(1)
100+
86101
def main():
87102
parser = ArgumentParser(add_help=False)
88103
parser.add_argument("-v", "--version", action="store_true", help="print the program's version")
@@ -95,16 +110,11 @@ def main():
95110
if options.help:
96111
print_help(parser)
97112
elif options.version:
98-
print_version()
113+
assert_files([options.model])
114+
print_version(options.model)
99115
elif len(options.file) > 0:
100116
paths = ["".join(file) for file in options.file]
101-
files_exist = True
102-
for path in paths:
103-
if not os.path.exists(path):
104-
print("Error: File does not exist:", path)
105-
files_exist = False
106-
if not files_exist:
107-
quit(1)
117+
assert_files(paths)
108118

109119
predict(paths, options.model)
110120
else:

0 commit comments

Comments
 (0)