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
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:
The file saved_models/model.pkl does not exist in the specified location.
As @Franky1 mentioned, the file exists in the specified location, but the Streamlit app does not have permission to access it.
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:
Double-check the file path to make sure that it exists and is accessible from the current environment.
As @Franky1 mentioned, make sure that the file has the correct permissions for the Streamlit app to access it.
If the file is corrupted, try recreating the file by retraining the model and saving it again.
You can also try loading the file in a standalone Python script to make sure it can be opened correctly.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.