Load model ReadError

Hello! I’m new using Streamlit and I have an error when I’m trying to load a model.

I have the following line of code:
model = pickle.load(open(‘saved_models/model.pkl’, ‘rb’))

ReadError: file could not be opened successfully

Traceback:

File "C:\Users\ovist\AppData\Local\Programs\Python\Python39\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)File "F:\Licenta\pages\models_page.py", line 44, in <module>
    model = pickle.load(open('saved_models/model.pkl', 'rb'))File "C:\Users\ovist\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\pickle_utils.py", line 39, in deserialize_model_from_bytecode
    with tarfile.open(fileobj=b, mode="r") as archive:File "C:\Users\ovist\AppData\Local\Programs\Python\Python39\lib\tarfile.py", line 1625, in open
    raise ReadError("file could not be opened successfully")

The command works fine in a local python script instead of streamlit app.
Note: I’m using Python 3.9.12

If you run the streamlit app on Streamlit Cloud you don’t have access to the Clients computer.

1 Like

@Ovi_Burcea

This error is occurring because Streamlit is not able to open the file saved_models/model.pkl. The file may not exist in the specified location or it may be inaccessible due to permission issues.

There are a few possible reasons why this error might be occurring:

  1. The file saved_models/model.pkl does not exist in the specified location.
  2. As @Franky1 mentioned, the file exists in the specified location, but the Streamlit app does not have permission to access it.
  3. The file is corrupted or was not created correctly, causing an error when Streamlit tries to open it.

To fix this error, you can try the following steps:

  1. Double-check the file path to make sure that it exists and is accessible from the current environment.
  2. As @Franky1 mentioned, make sure that the file has the correct permissions for the Streamlit app to access it.
  3. If the file is corrupted, try recreating the file by retraining the model and saving it again.
  4. You can also try loading the file in a standalone Python script to make sure it can be opened correctly.
2 Likes

Thank you all for reply! I think the problem was the file corrupted… I solved it by resaving the model, this time with the specific function from tf.keras and it worked.

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