Skip to content

Instantly share code, notes, and snippets.

@remram44
Created September 21, 2021 17:58
Show Gist options
  • Save remram44/51772127325189e7d449518a12f397c9 to your computer and use it in GitHub Desktop.
Save remram44/51772127325189e7d449518a12f397c9 to your computer and use it in GitHub Desktop.
FastText Pickle Wrapper
from fastText import load_model
import tempfile
class PickleableFastText(object):
def __init__(self, model):
self._wrapped_model = model
def __getattr__(self, key):
return getattr(self._wrapped_model, key)
def __getstate__(self):
with tempfile.NamedTemporaryFile(suffix='.bin') as tmp:
self._wrapped_model.save_model(tmp.name)
tmp.flush()
tmp.seek(0, 0)
return tmp.read()
def __setstate__(self, state):
with tempfile.NamedTemporaryFile(suffix='.bin') as tmp:
tmp.write(state)
tmp.flush()
self._wrapped_model = load_model(tmp.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment