@@ -28,15 +28,13 @@ def log(msg):
2828# If creating when deployed:
2929#FILE = Path(tempfile.gettempdir()) / "ngram_counts.pkl"
3030# If creating locally, to be copied when deployed:
31- FILE = MODEL_DIR / "ngram_counts.pkl.bz2 "
31+ FILE = MODEL_DIR / "ngram_counts.pkl"
3232
3333def get_counts (n = 3 , dev = False ):
3434 print (f"Loading/building n-gram counts for n={ n } ..." )
3535 if os .path .exists (FILE ):
36- size = os .path .getsize (FILE )
37- raise RuntimeError (f"Found { FILE } , size={ size } bytes" )
3836 try :
39- with bz2 . BZ2File (FILE , "rb" ) as f :
37+ with open (FILE , "rb" ) as f :
4038 cache = pickle .load (f )
4139 if not isinstance (cache , dict ):
4240 raise RuntimeError (f"Loaded cache is { type (cache )} , not dict — contents: { str (cache )[:300 ]} " )
@@ -52,7 +50,7 @@ def get_counts(n=3, dev=False):
5250 print (f"Building n={ n } counts..." )
5351 cache [n ] = build_counts (n , START , END ) # only works if NLTK corpora are available
5452 print (f"Saving n-gram counts to { FILE } ..." )
55- with bz2 . BZ2File (FILE , "wb" ) as f :
53+ with open (FILE , "wb" ) as f :
5654 pickle .dump (cache ,f )
5755 except Exception as e :
5856 raise RuntimeError (f"Failed to rebuild or save n-gram counts { e } " )
0 commit comments