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…
Hey Charly, here’s an unorganized dump of thoughts
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()
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”