Created
September 21, 2021 17:58
-
-
Save remram44/51772127325189e7d449518a12f397c9 to your computer and use it in GitHub Desktop.
FastText Pickle Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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