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