flair PermissionError: [Errno 13] Permission denied: '/root/.cache'

Question:

I am calling the python script with the flair package with a www-data user (no sudo rights). The models are in path for which that user has access rights, which I have set flair.cache_root = Path("tools/flair")

However, when I run the script with that user I get a Permission Error:

tagger = MultiTagger.load([\"flair/ner-german-large\", \"de-pos\"])
File \"/usr/local/lib/python3.7/dist-packages/flair/models/sequence_tagger_model.py\", line 1330, in load
model = SequenceTagger.load(model_name)
File \"/usr/local/lib/python3.7/dist-packages/flair/nn.py\", line 88, in load
state = torch.load(f, map_location='cpu')
File \"/usr/local/lib/python3.7/dist-packages/torch/serialization.py\", line 594, in load
return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
File \"/usr/local/lib/python3.7/dist-packages/torch/serialization.py\", line 853, in _load
result = unpickler.load()
File \"/usr/local/lib/python3.7/dist-packages/flair/embeddings/token.py\", line 1297, in __setstate__
state_dict=d[\"model_state_dict\"],
File \"/usr/local/lib/python3.7/dist-packages/flair/embeddings/token.py\", line 818, in __init__
self.tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(model, **kwargs)
File \"/usr/local/lib/python3.7/dist-packages/transformers/models/auto/tokenization_auto.py\", line 435, in from_pretrained
return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
File \"/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py\", line 1680, in from_pretrained
user_agent=user_agent,
File \"/usr/local/lib/python3.7/dist-packages/transformers/file_utils.py\", line 1279, in cached_path
local_files_only=local_files_only,
File \"/usr/local/lib/python3.7/dist-packages/transformers/file_utils.py\", line 1426, in get_from_cache
os.makedirs(cache_dir, exist_ok=True)
File \"/usr/lib/python3.7/os.py\", line 211, in makedirs
makedirs(head, exist_ok=exist_ok)
File \"/usr/lib/python3.7/os.py\", line 211, in makedirs
makedirs(head, exist_ok=exist_ok)
File \"/usr/lib/python3.7/os.py\", line 221, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/root/.cache'

Can I avoid using /root/.cache? I don’t want to edit the read-write rights of that directory. If I run the script as root it works fine. How do I run it as the other user? I am running it on Ubuntu.

Asked By: sandboxj

||

Answers:

The error is caused by the transformer model that flair loads. The cache directory for transformers has to be specified in additional by setting the environment variable TRANSFORMERS_CACHE=/path/to/transformers

Answered By: sandboxj

Use below code in your script before creating model instance.

import os
import flair

os.environ['FLAIR_CACHE_ROOT'] = './cache'
flair.cache_root = Path(os.environ['FLAIR_CACHE_ROOT']+"/my/path/.flair")
Answered By: Prashant Sahoo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.