I am currently developing a chat application where users can interact with an AI model by uploading images. The image upload functionality is implemented using the following Streamlit code:
uploaded_file = st.file_uploader('Please upload an image', type=['jpg', 'jpeg', 'png'])
This setup allows the uploaded image to be used as user input, and the AI model processes it to generate responses.
However, I am facing an issue when attempting to upload multiple images sequentially. Once an image is uploaded, the system continues to treat it as an active user input, preventing new uploads from being processed correctly.
To address this issue, I need a mechanism to either remove or erase the previously uploaded image from the session state, so that the user can upload a new image without interference. Here is the relevant portion of my current implementation:
if 'uploaded_image' not in st.session_state:
st.session_state['uploaded_image'] = None
uploaded_file = st.file_uploader('Please upload an image', type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
st.session_state['uploaded_image'] = uploaded_file
# Processing the uploaded image
# ...
st.session_state['uploaded_image'] = None # Attempt to reset after processing
Despite setting st.session_state['uploaded_image'] to None after processing, the issue persists.
Could you please provide guidance or suggest a better approach to effectively handle multiple image uploads in this context?
Thank you in advance for your assistance. I look forward to your response.
A cheap trick to clear any widget is to change the key, so you could so something like this
import streamlit as st
from time import sleep
if "uploader_key" not in st.session_state:
st.session_state["uploader_key"] = 1
uploaded_file = st.file_uploader(
"Please upload an image",
type=["jpg", "jpeg", "png"],
key=st.session_state["uploader_key"],
)
if uploaded_file is not None:
with st.spinner("Processing"):
sleep(3)
# Clear the file uploader
st.session_state["uploader_key"] += 1
st.rerun()
The following is the best way to upload using “streamlit forms”. This allows clear_on_submit and this is what possibly you are looking for. This along with the tip from Blackary may be able to solve your issue. Good luck!
NOTE: In this way, the user first uploads the file by clicking “Select a file for upload” button. After upload, he still needs to click “Upload the selected file”. It is a 2-step process.
with upload_column:
with st.form(APP_ID + "/upload_form", clear_on_submit=True):
uploaded_file = st.file_uploader('Select a file for upload', type=['xls', 'xlsx'], key=APP_ID + '/file_uploader')
file_submitted = st.form_submit_button("Upload the selected file!")
if file_submitted and (uploaded_file is not None):
<perform upload related task>
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.