Cannot load .joblib file

When running Streamlit on my local host, it runs perfectly fine. However, when I deploy it, the log said it cannot find the file in the model directory.
The log:

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

    exec(code, module.__dict__)

  File "/app/predictive-app/app/app.py", line 17, in <module>

    models = load_models()

  File "/app/predictive-app/app/model_loader.py", line 11, in load_models

    for filename in os.listdir(model_directory):

FileNotFoundError: [Errno 2] No such file or directory: '../model'

This is my code to load the directory

import joblib
import os

# Specify the directory where the models are stored
model_directory = "../model"


def load_models():
    # Load all models from the model directory
    models = {}
    for filename in os.listdir(model_directory):
        if filename.endswith(".joblib"):
            model_name = os.path.splitext(filename)[0]
            model_path = os.path.join(model_directory, filename)
            try:
                model = joblib.load(model_path)
                models[model_name] = model
            except Exception as e:
                raise RuntimeError(f"Error loading model: {model_name} - {str(e)}")

    if not models:
        raise RuntimeError("Models not found")

    return models

It says that the model directory does not exist.

Hi Goyo,

Thank you for replying.

I think I am not missing the “model” directory

Streamlit cCoud runs python in the root of your repository, so the path to the model directory would be ./model.

1 Like

Thank you very much, Goyo. It’s work!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.