Hello all,
I am developing an app that allows users to upload a file to Pinecone, I have the actual uploading logic to run in the background, however, I want to remove the already uploaded files from the file_uploader widget.
Here is my code, I would greatly appreciate it if anyone has any suggestions.
def upload_and_process_files(index, openai_client, backend):
"""Render file upload UI and process files for indexing"""
st.header("Upload & Index Files")
# Initialize session state for file uploader
if "file_uploader_key" not in st.session_state:
st.session_state.file_uploader_key = str(uuid.uuid4())
files = st.file_uploader(
"Select files (txt, pdf, pptx)",
type=["txt", "pdf", "pptx"],
accept_multiple_files=True,
key=st.session_state.file_uploader_key # unique key to reset
)
if not files:
st.info("Upload files to index them for question answering")
return
upload_successful = False
files_to_process = list(files)
for f in files_to_process:
if f.name in backend.tag_manager.get_files():
st.warning(f"'{f.name}' already indexed — skipping.")
continue
if backend.tag_manager.add_tag(f.name):
temp = tempfile.NamedTemporaryFile(delete=False,
suffix=Path(f.name).suffix)
temp.write(f.getbuffer())
temp.close()
subprocess.Popen([
sys.executable, "background_tasks/background_indexer.py",
temp.name, # path, not text
backend.tag_manager.selected_name,
backend.tag_manager.user_name,
str(backend.tag_manager.selected_category_id),
f.name
])
with st.spinner(f"Indexing {f.name}…"):
while backend.tag_manager.get_not_completed_files(f.name):
time.sleep(1)
files.remove(f)
print(f"it is done indexing: {f.name}")
st.success(f"{f.name} indexed!")
else:
st.write("Tagging failed")
# ✅ Reset file uploader after successful upload
if upload_successful:
st.session_state.file_uploader_key = str(uuid.uuid4())