Пример #1
0
 def _create_embeddings_from_file(embed_file, embed_dsz, embed_sha1,
                                  data_download_cache, vocab, unif,
                                  keep_unused):
     embed_file = EmbeddingDownloader(embed_file, embed_dsz, embed_sha1,
                                      data_download_cache).download()
     EmbeddingT = baseline.GloVeModel if mime_type(
         embed_file) == 'text/plain' else baseline.Word2VecModel
     return EmbeddingT(embed_file,
                       vocab,
                       unif_weight=unif,
                       keep_unused=keep_unused)
Пример #2
0
def unzip_model(path):
    """If the path for a model file is a zip file, unzip it in /tmp and return the unzipped path"""
    if not os.path.exists(path) or not mime_type(path) == "application/zip":
        return path
    with open(path, 'rb') as f:
        sha1 = hashlib.sha1(f.read()).hexdigest()
    temp_dir = os.path.join("/tmp/", sha1)
    if not os.path.exists(temp_dir):
        print("unzipping model before exporting")
        with zipfile.ZipFile(path, "r") as zip_ref:
            zip_ref.extractall(temp_dir)
    temp_dir = os.path.join(temp_dir, os.listdir(temp_dir)[0])
    path = os.path.join(temp_dir,
                        [x[:-6] for x in os.listdir(temp_dir)
                         if 'index' in x][0])
    return path