Retrieval Augmented Generation Module 3 Error on import flask_app

I am unable to run this lab due to the following error with the lab (before running and user code):

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[6], line 1
----> 1 import flask_app

File ~/work/flask_app.py:12
     10 from utils import generate_embedding
     11 # Initialize models globally to load them once
---> 12 reranker = FlagReranker('BAAI/bge-reranker-base', cache_dir=os.environ["MODEL_M3"], use_fp16=False)
     14 app = Flask(__name__)
     16 @app.route('/.well-known/ready', methods=['GET'])
     17 def readiness_check():

File /usr/local/lib/python3.12/site-packages/FlagEmbedding/inference/reranker/encoder_only/base.py:66, in BaseReranker.__init__(self, model_name_or_path, use_fp16, query_instruction_for_rerank, query_instruction_format, passage_instruction_for_rerank, passage_instruction_format, trust_remote_code, cache_dir, devices, batch_size, query_max_length, max_length, normalize, **kwargs)
     34 def __init__(
     35     self,
     36     model_name_or_path: str,
   (...)     50     **kwargs: Any,
     51 ):
     52     super().__init__(
     53         model_name_or_path=model_name_or_path,
     54         use_fp16=use_fp16,
   (...)     64         **kwargs
     65     )
---> 66     self.tokenizer = AutoTokenizer.from_pretrained(
     67         model_name_or_path, 
     68         trust_remote_code=trust_remote_code, 
     69         cache_dir=cache_dir
     70     )
     71     self.model = AutoModelForSequenceClassification.from_pretrained(
     72         model_name_or_path, 
     73         trust_remote_code=trust_remote_code, 
     74         cache_dir=cache_dir
     75     )

File /usr/local/lib/python3.12/site-packages/transformers/models/auto/tokenization_auto.py:834, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
    831     return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
    833 # Next, let's try to use the tokenizer_config file to get the tokenizer class.
--> 834 tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
    835 if "_commit_hash" in tokenizer_config:
    836     kwargs["_commit_hash"] = tokenizer_config["_commit_hash"]

File /usr/local/lib/python3.12/site-packages/transformers/models/auto/tokenization_auto.py:666, in get_tokenizer_config(pretrained_model_name_or_path, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, **kwargs)
    663     token = use_auth_token
    665 commit_hash = kwargs.get("_commit_hash", None)
--> 666 resolved_config_file = cached_file(
    667     pretrained_model_name_or_path,
    668     TOKENIZER_CONFIG_FILE,
    669     cache_dir=cache_dir,
    670     force_download=force_download,
    671     resume_download=resume_download,
    672     proxies=proxies,
    673     token=token,
    674     revision=revision,
    675     local_files_only=local_files_only,
    676     subfolder=subfolder,
    677     _raise_exceptions_for_gated_repo=False,
    678     _raise_exceptions_for_missing_entries=False,
    679     _raise_exceptions_for_connection_errors=False,
    680     _commit_hash=commit_hash,
    681 )
    682 if resolved_config_file is None:
    683     logger.info("Could not locate the tokenizer configuration file, will try to use the model config instead.")

File /usr/local/lib/python3.12/site-packages/transformers/utils/hub.py:402, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)
    399 user_agent = http_user_agent(user_agent)
    400 try:
    401     # Load from URL or cache if already cached
--> 402     resolved_file = hf_hub_download(
    403         path_or_repo_id,
    404         filename,
    405         subfolder=None if len(subfolder) == 0 else subfolder,
    406         repo_type=repo_type,
    407         revision=revision,
    408         cache_dir=cache_dir,
    409         user_agent=user_agent,
    410         force_download=force_download,
    411         proxies=proxies,
    412         resume_download=resume_download,
    413         token=token,
    414         local_files_only=local_files_only,
    415     )
    416 except GatedRepoError as e:
    417     resolved_file = _get_cache_file_to_return(path_or_repo_id, full_filename, cache_dir, revision)

File /usr/local/lib/python3.12/site-packages/huggingface_hub/utils/_validators.py:114, in validate_hf_hub_args.<locals>._inner_fn(*args, **kwargs)
    111 if check_use_auth_token:
    112     kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.__name__, has_token=has_token, kwargs=kwargs)
--> 114 return fn(*args, **kwargs)

File /usr/local/lib/python3.12/site-packages/huggingface_hub/file_download.py:1014, in hf_hub_download(repo_id, filename, subfolder, repo_type, revision, library_name, library_version, cache_dir, local_dir, user_agent, force_download, proxies, etag_timeout, token, local_files_only, headers, endpoint, resume_download, force_filename, local_dir_use_symlinks)
    994     return _hf_hub_download_to_local_dir(
    995         # Destination
    996         local_dir=local_dir,
   (...)   1011         local_files_only=local_files_only,
   1012     )
   1013 else:
-> 1014     return _hf_hub_download_to_cache_dir(
   1015         # Destination
   1016         cache_dir=cache_dir,
   1017         # File info
   1018         repo_id=repo_id,
   1019         filename=filename,
   1020         repo_type=repo_type,
   1021         revision=revision,
   1022         # HTTP info
   1023         endpoint=endpoint,
   1024         etag_timeout=etag_timeout,
   1025         headers=hf_headers,
   1026         proxies=proxies,
   1027         token=token,
   1028         # Additional options
   1029         local_files_only=local_files_only,
   1030         force_download=force_download,
   1031     )

File /usr/local/lib/python3.12/site-packages/huggingface_hub/file_download.py:1159, in _hf_hub_download_to_cache_dir(cache_dir, repo_id, filename, repo_type, revision, endpoint, etag_timeout, headers, proxies, token, local_files_only, force_download)
   1152 if (
   1153     os.name == "nt"
   1154     and len(os.path.abspath(blob_path)) > 255
   1155     and not os.path.abspath(blob_path).startswith("\\\\?\\")
   1156 ):
   1157     blob_path = "\\\\?\\" + os.path.abspath(blob_path)
-> 1159 Path(lock_path).parent.mkdir(parents=True, exist_ok=True)
   1161 # pointer already exists -> immediate return
   1162 if not force_download and os.path.exists(pointer_path):

File /usr/local/lib/python3.12/pathlib.py:1311, in Path.mkdir(self, mode, parents, exist_ok)
   1307 """
   1308 Create a new directory at this given path.
   1309 """
   1310 try:
-> 1311     os.mkdir(self, mode)
   1312 except FileNotFoundError:
   1313     if not parents or self.parent == self:

OSError: [Errno 30] Read-only file system: '.models/.locks/models--BAAI--bge-reranker-base'






import flask_app
import weaviate_server
from utils import (
    generate_with_single_input,
    print_object_properties,
    display_widget
)
import unittests
Could not cache non-existence of file. Will ignore error and continue. Error: [Errno 30] Read-only file system: '.models/models--BAAI--bge-base-en-v1.5/.no_exist/a5beb1e3e68b9ab74eb54cfd186867f64f240e1a/adapter_config.json'
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[2], line 1
----> 1 import flask_app
      2 import weaviate_server
      3 from utils import (
      4     generate_with_single_input,
      5     print_object_properties,
      6     display_widget
      7 )

File ~/work/flask_app.py:12
     10 from utils import generate_embedding
     11 # Initialize models globally to load them once
---> 12 reranker = FlagReranker('BAAI/bge-reranker-base', cache_dir=os.environ["MODEL_M3"], use_fp16=False)
     14 app = Flask(__name__)
     16 @app.route('/.well-known/ready', methods=['GET'])
     17 def readiness_check():

File /usr/local/lib/python3.12/site-packages/FlagEmbedding/inference/reranker/encoder_only/base.py:66, in BaseReranker.__init__(self, model_name_or_path, use_fp16, query_instruction_for_rerank, query_instruction_format, passage_instruction_for_rerank, passage_instruction_format, trust_remote_code, cache_dir, devices, batch_size, query_max_length, max_length, normalize, **kwargs)
     34 def __init__(
     35     self,
     36     model_name_or_path: str,
   (...)     50     **kwargs: Any,
     51 ):
     52     super().__init__(
     53         model_name_or_path=model_name_or_path,
     54         use_fp16=use_fp16,
   (...)     64         **kwargs
     65     )
---> 66     self.tokenizer = AutoTokenizer.from_pretrained(
     67         model_name_or_path, 
     68         trust_remote_code=trust_remote_code, 
     69         cache_dir=cache_dir
     70     )
     71     self.model = AutoModelForSequenceClassification.from_pretrained(
     72         model_name_or_path, 
     73         trust_remote_code=trust_remote_code, 
     74         cache_dir=cache_dir
     75     )

File /usr/local/lib/python3.12/site-packages/transformers/models/auto/tokenization_auto.py:834, in AutoTokenizer.from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
    831     return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
    833 # Next, let's try to use the tokenizer_config file to get the tokenizer class.
--> 834 tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
    835 if "_commit_hash" in tokenizer_config:
    836     kwargs["_commit_hash"] = tokenizer_config["_commit_hash"]

File /usr/local/lib/python3.12/site-packages/transformers/models/auto/tokenization_auto.py:666, in get_tokenizer_config(pretrained_model_name_or_path, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, **kwargs)
    663     token = use_auth_token
    665 commit_hash = kwargs.get("_commit_hash", None)
--> 666 resolved_config_file = cached_file(
    667     pretrained_model_name_or_path,
    668     TOKENIZER_CONFIG_FILE,
    669     cache_dir=cache_dir,
    670     force_download=force_download,
    671     resume_download=resume_download,
    672     proxies=proxies,
    673     token=token,
    674     revision=revision,
    675     local_files_only=local_files_only,
    676     subfolder=subfolder,
    677     _raise_exceptions_for_gated_repo=False,
    678     _raise_exceptions_for_missing_entries=False,
    679     _raise_exceptions_for_connection_errors=False,
    680     _commit_hash=commit_hash,
    681 )
    682 if resolved_config_file is None:
    683     logger.info("Could not locate the tokenizer configuration file, will try to use the model config instead.")

File /usr/local/lib/python3.12/site-packages/transformers/utils/hub.py:402, in cached_file(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)
    399 user_agent = http_user_agent(user_agent)
    400 try:
    401     # Load from URL or cache if already cached
--> 402     resolved_file = hf_hub_download(
    403         path_or_repo_id,
    404         filename,
    405         subfolder=None if len(subfolder) == 0 else subfolder,
    406         repo_type=repo_type,
    407         revision=revision,
    408         cache_dir=cache_dir,
    409         user_agent=user_agent,
    410         force_download=force_download,
    411         proxies=proxies,
    412         resume_download=resume_download,
    413         token=token,
    414         local_files_only=local_files_only,
    415     )
    416 except GatedRepoError as e:
    417     resolved_file = _get_cache_file_to_return(path_or_repo_id, full_filename, cache_dir, revision)

File /usr/local/lib/python3.12/site-packages/huggingface_hub/utils/_validators.py:114, in validate_hf_hub_args.<locals>._inner_fn(*args, **kwargs)
    111 if check_use_auth_token:
    112     kwargs = smoothly_deprecate_use_auth_token(fn_name=fn.__name__, has_token=has_token, kwargs=kwargs)
--> 114 return fn(*args, **kwargs)

File /usr/local/lib/python3.12/site-packages/huggingface_hub/file_download.py:1014, in hf_hub_download(repo_id, filename, subfolder, repo_type, revision, library_name, library_version, cache_dir, local_dir, user_agent, force_download, proxies, etag_timeout, token, local_files_only, headers, endpoint, resume_download, force_filename, local_dir_use_symlinks)
    994     return _hf_hub_download_to_local_dir(
    995         # Destination
    996         local_dir=local_dir,
   (...)   1011         local_files_only=local_files_only,
   1012     )
   1013 else:
-> 1014     return _hf_hub_download_to_cache_dir(
   1015         # Destination
   1016         cache_dir=cache_dir,
   1017         # File info
   1018         repo_id=repo_id,
   1019         filename=filename,
   1020         repo_type=repo_type,
   1021         revision=revision,
   1022         # HTTP info
   1023         endpoint=endpoint,
   1024         etag_timeout=etag_timeout,
   1025         headers=hf_headers,
   1026         proxies=proxies,
   1027         token=token,
   1028         # Additional options
   1029         local_files_only=local_files_only,
   1030         force_download=force_download,
   1031     )

File /usr/local/lib/python3.12/site-packages/huggingface_hub/file_download.py:1159, in _hf_hub_download_to_cache_dir(cache_dir, repo_id, filename, repo_type, revision, endpoint, etag_timeout, headers, proxies, token, local_files_only, force_download)
   1152 if (
   1153     os.name == "nt"
   1154     and len(os.path.abspath(blob_path)) > 255
   1155     and not os.path.abspath(blob_path).startswith("\\\\?\\")
   1156 ):
   1157     blob_path = "\\\\?\\" + os.path.abspath(blob_path)
-> 1159 Path(lock_path).parent.mkdir(parents=True, exist_ok=True)
   1161 # pointer already exists -> immediate return
   1162 if not force_download and os.path.exists(pointer_path):

File /usr/local/lib/python3.12/pathlib.py:1311, in Path.mkdir(self, mode, parents, exist_ok)
   1307 """
   1308 Create a new directory at this given path.
   1309 """
   1310 try:
-> 1311     os.mkdir(self, mode)
   1312 except FileNotFoundError:
   1313     if not parents or self.parent == self:

OSError: [Errno 30] Read-only file system: '.models/.locks/models--BAAI--bge-reranker-base'

are you running codes locally?

No, this is the hosted page that’s part of the course, here: /courses/retrieval-augmented-generation/lesson/99emt4kl/building-rag-systems-with-a-vector-database

I just changed the directory and it worked. os.environ["MODEL_M3"] = ".models_v2"

The .models directory (where HuggingFace caches the reranker weights) ended up mounted as read-only, so it can’t create the lock files it needs during model loading. That’s the OSError: [Errno 30] you saw.

Your fix of pointing MODEL_M3 to .models_v2 works, but it forces a full re-download of the model into a new directory. The cleaner approach (as @Deepti_Prasad mentioned in a similar thread):

  1. Go to File → Open in the lab’s Jupyter interface
  2. Delete all files in the working directory
  3. Click the (three dots) menu → Get Latest Version
  4. Kernel → Restart & Clear Output
  5. Re-run from the top

The RAG Module 3 labs were updated recently to fix dependency issues, so pulling the latest version should give you a clean .models directory with correct permissions.