How to save and load files temporarily, for each user, in each browsing session?

Iā€™ve built an Auto-ML app built via the Ludwig library (which itself is built on top of tensorflow)

Once trained, a given Machine Learning Model is saved as follows in Streamlit Sharingā€™s current working directory:
model.save(cwd)

Then the saved model is loaded as follows:
modelLoaded = LudwigModel.load(cwd)

Currently that ML model is saved permanently in Streamlit Sharing, thus available to all users using the app - whereas Iā€™d like to only temporarily have these models saved and loaded for each user, in their own browsing session.

Iā€™ve tried a few things via Tempfile but no luck so farā€¦ :thinking_face:

Any suggestions welcome! :slight_smile:

Hey Charly, hereā€™s an unorganized dump of thoughts :smiley:

This makes me think Session State, could you save the models inside state, and when the session expires the model disappears with the state?

If you think SessionState doesnā€™t keep the model for long enough and want to write on disk, in the Session State gist thereā€™s a way to retrieve the session info:

    ctx = ReportThread.get_report_ctx()

    this_session = None

    current_server = Server.get_current()
    if hasattr(current_server, '_session_infos'):
        # Streamlit < 0.56
        session_infos = Server.get_current()._session_infos.values()
    else:
        session_infos = Server.get_current()._session_info_by_id.values()

    for session_info in session_infos:
        s = session_info.session

I have never tried this, but maybe you could prefix your file with the session info so when you reload the model you use this ID to load the correct model/folder of models. I just fear the session info will change as frequently as the session state solution above so thereā€™s no real plus at doing this on disk rather than in session state.

If writing on disk I have a repo somewhere where at the beginning of the app I just scan the cwd for all the model files and delete those that were created after x days. pathlib does that really well.

Oh I think I remember I asked the user to add itā€™s username in the app (easier to use than a session id) and I created a folder for the user in cwd that I would regularly scan to purge. Last problem is because Streamlit Sharing, thereā€™s a chance the same user lands on 2 different containers and as such donā€™t see their model if they force reload the app during anyā€¦cloud event like reboot. So the best solution would be ā€œstore the model with a TTL in Firestoreā€ :smiley:

Good luck triaging my brainstorm ahah
Fanilo :balloon:

1 Like

Thanks Fanilo!
Iā€™ll try these and will feed back here :slight_smile:

1 Like