def load(cls, filepath):
tmpdir = tempfile.mkdtemp()
error = None
logger.debug('extracting archive to: {}'.format(tmpdir))
try:
with tarfile.open(filepath, 'r:*') as ar:
ar.extractall(tmpdir)
payload = open(os.path.join(tmpdir, 'metadata'), 'rb').read()
print(payload)
meta = dill.loads(payload)
instance = cls()
for attr_name, attr_val in meta.items():
setattr(instance, attr_name, attr_val)
if os.path.exists(os.path.join(tmpdir, 'vectors')):
logger.debug('loading word vectors')
import h5py
with h5py.File(os.path.join(tmpdir, 'vectors'), 'r') as h5:
setattr(instance, '_W', h5['vectors'][:])
else:
logger.debug('no word vectors found in archive')
except Exception as e:
logger.error('encountered error: {}'.format(e))
error = e
finally:
logger.debug('cleaning up {}'.format(tmpdir))
shutil.rmtree(tmpdir)
if error is not None:
raise error
return instance
评论列表
文章目录