St.session_state initialize but not find

Hello community! :smiling_face:

It’s me again! I’m trying to load my model in a st.session_state from the landing page and access it in another page (multipage app).

However, it’s telling me that it can’t find the attribute! I’ve changed the code back and forth according to what I’m reading, but nothing works.

Here’s the architecture of my code if it helps!

Home.py

#... import the libraries

## ----------------------------- SETUP PAGE --------------------------------- ##

st.set_page_config(page_title='AISSYR', 
                   page_icon='asset/fav32.png', 
                   layout='wide')


## --------------------------- LOADING MODEL -------------------------------- ##

if 'model' not in st.session_state:
    with st.spinner('Loading model...'):
        fct.load_model()


utils/functions.py


@st.cache_resource(show_spinner=False)
def load_model():
    try:
        dagshub.init(DAGSHUB_REPO, DAGSHUB_REPO_OWNER, mlflow=True)

        st.session_state['model'] = mlflow.pyfunc.load_model(MODEL_URI)

    except Exception as e:
        st.error(f"Error loading model: {e}")
        return None

pages/detect.py

# some code not interesting in this case

print(st.session_state.model)

Error Message

AttributeError: st.session_state has no attribute “model”. Did you forget to initialize it?

Thanks in advance for you advice!! :raised_hands:

This is not necessary to store the model reference in session state, you are just adding memory loads to the server from each user who uses your app and making the code unnecessarily complicated. Calling load_model() directly is enough.

Thanks @ferdy

You load it into the cache, but if you don’t set it to a variable, you can’t reuse it in your application?

Here, I load my model as soon as I launch my application, but I don’t use it on the main page (Home.py), but only in my functions file that I call from another page (detection.py). If I assign the model to a variable in Home, I won’t be able to call it in my functions file, hence the need to go through the st.session_state.

If this doesn’t work, isn’t it because of the PyFuncModel format?

You have home and prediction modules or files. Now in what file do you use your model? In prediction or in another file?

Here my architecture

pages/
├── annotation.py
├── archive.py
├── detect.py
├── login.py
├── main_page.py
└── register.py
utils
└── functions.py
Home.py

My function load_model is in utils/functions, I call it in my Home to download it during the application launch, but I use it via an other function in utils/functions call detect() itself use in pages/detect.py :sweat_smile:

So actually, I would like to load it in first place during the app launch, but use the model later.

Are you deploying this in community cloud? Downloading something is not guaranteed to always work.

Granting you all have the files needed, and your load_model is in utils/functions, you can import that to the pages/detect.

Currently, yes but doesn’t include the load model right now.

for your example, yes that’s not the problem, for the moment I’m loading it into the function for detection. I mainly wanted to find a way to do it upstream, to make inference smoother and have it at application launch and not when the first person makes a prediction.

Really thank you about your ideas anyway, it’s appreciated ! :smiling_face:

Do not use st.session_state inside a cached function.

Yes, I’ve done it differently. That’s work now

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