2
2
import inspect
3
3
import os
4
4
import sys
5
+ import hashlib
5
6
from argparse import ArgumentParser
6
7
7
8
# These are configurable, but highly dependant on the shipped model
@@ -34,10 +35,15 @@ def print_help(parser: ArgumentParser) -> None:
34
35
print ()
35
36
print (description )
36
37
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 )
39
45
print ("CompDec v1.0.0" )
40
- print ("Model hash: {}" .format (hash ))
46
+ print ("Model hash: {}" .format (hash . hexdigest () ))
41
47
42
48
def load_samples_from_file (sample_path ):
43
49
import numpy
@@ -83,6 +89,15 @@ def softmax(predictions):
83
89
for i in range (len (CLASS_NAMES )):
84
90
print ("{:9}: {:2.2f}%" .format (CLASS_NAMES [i ], normalized_predictions [i ] * 100 ))
85
91
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
+
86
101
def main ():
87
102
parser = ArgumentParser (add_help = False )
88
103
parser .add_argument ("-v" , "--version" , action = "store_true" , help = "print the program's version" )
@@ -95,16 +110,11 @@ def main():
95
110
if options .help :
96
111
print_help (parser )
97
112
elif options .version :
98
- print_version ()
113
+ assert_files ([options .model ])
114
+ print_version (options .model )
99
115
elif len (options .file ) > 0 :
100
116
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 )
108
118
109
119
predict (paths , options .model )
110
120
else :
0 commit comments