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()
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ā
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.