Load a model in .h5 format to make prediction

I have already trained a model and the weights are loaded in a .h5 file. I have uploaded the file to GitHub repository using LFS. Now when I try to use that model in my app using

url = 'https://github.com/jithincheriyan/Web_App/blob/master/Transformer_BERT_Model.h5'
filename = url.split('/')[-1]
trained_model.load_weights(urllib.request.urlretrieve(url, filename))

The following error appears:

Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 350, in _run_scriptexec(code, module.__dict__)
File "/app/web_app/Streamlit_App.py", line 43, in <module>st.text(L1_test([comment]))
File "/app/web_app/Streamlit_App.py", line 19, in L1_test trained_model.load_weights(urllib.request.urlretrieve(url, filename))
File "/home/appuser/venv/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2172, in load_weights
if _is_hdf5_filepath(filepath):
File "/home/appuser/venv/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2785, in _is_hdf5_filepath
return (filepath.endswith('.h5') or filepath.endswith('.keras') or
AttributeError: 'tuple' object has no attribute 'endswith'

Hi! Seems that your “filepath” variable is a tuple, and tuples dont have the “.endswith” function. Since string tuples usually look like this:

mytuple = ("apple", "banana", "cherry")

You can check every string of the tuple:

for string in mytuple:
   if string.endswith(’.h5’):
      #do something

It’s just an example of how to use endswith() on strings. Hope it helps you!

1 Like