Skip to content

Commit

Permalink
Merge pull request #44 from Sammybams/main
Browse files Browse the repository at this point in the history
fix: prevent repeated document extraction on page reload after upload
  • Loading branch information
Sammybams authored Oct 31, 2024
2 parents 41b5530 + 6af4c39 commit ef1fcca
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,50 +100,61 @@ def create_vector_store(extracted_file_paths):
# Sidebar configuration for file uploads
if 'uploaded_files' not in st.session_state:
st.session_state.uploaded_files = None
st.session_state['prev_uploaded_files'] = []

with st.sidebar:
st.subheader("Upload your document")
st.session_state.uploaded_files = st.sidebar.file_uploader(
uploaded_files = st.sidebar.file_uploader(
"Choose files", accept_multiple_files=True, type=["pdf", "txt", "pptx"], key="initial"
)

if st.session_state.uploaded_files:
if len(st.session_state.uploaded_files) > 2:
st.error("You can only upload a maximum of 2 documents.")
logging.warning("User attempted to upload more than 2 documents.")
st.session_state.uploaded_files = None
else:
valid_files = []
valid_file = True
for file in st.session_state.uploaded_files:
if allowed_files(file.name):
num_pages = file_check_num(file)
if num_pages > 50:
st.error(f"{file.name} exceeds the 50-page limit (has {num_pages} pages).")
logging.warning(f"File {file.name} exceeds the page limit.")
# Check for changes in the uploaded files
if uploaded_files:
uploaded_file_names = [file.name for file in uploaded_files]
prev_file_names = [file.name for file in st.session_state['prev_uploaded_files']]

# Check if there's any difference between the current and previous file names
if uploaded_file_names != prev_file_names:
st.session_state.uploaded_files = uploaded_files
st.session_state['prev_uploaded_files'] = uploaded_files

if len(uploaded_files) > 2:
st.error("You can only upload a maximum of 2 documents.")
logging.warning("User attempted to upload more than 2 documents.")
st.session_state.uploaded_files = None
else:
valid_files = []
valid_file = True
for file in uploaded_files:
if allowed_files(file.name):
num_pages = file_check_num(file)
if num_pages > 50:
st.error(f"{file.name} exceeds the 50-page limit (has {num_pages} pages).")
logging.warning(f"File {file.name} exceeds the page limit.")
valid_file = False
break
else:
valid_files.append(file)
else:
st.error(f"{file.name} is not a valid file type.")
logging.warning(f"Invalid file type: {file.name}")
valid_file = False
break
else:
valid_files.append(file)
else:
st.error(f"{file.name} is not a valid file type.")
logging.warning(f"Invalid file type: {file.name}")
valid_file = False
break

if valid_file and valid_files:
try:
extraction_results = extract_contents_from_doc(valid_files, "temp_dir")
vector_store = create_vector_store(extraction_results)
if vector_store:
st.session_state['vector_store'] = vector_store
st.success(f"{len(st.session_state.uploaded_files)} file(s) uploaded and processed successfully.")
logging.info("File(s) uploaded and processed successfully.")
except Exception as e:
st.error("An error occurred while processing your document. Please try again.")
logging.error(f"Error extracting content from document: {e}")

if valid_file and valid_files:
try:
extraction_results = extract_contents_from_doc(valid_files, "temp_dir")
vector_store = create_vector_store(extraction_results)
if vector_store:
st.session_state['vector_store'] = vector_store
st.success(f"{len(uploaded_files)} file(s) uploaded and processed successfully.")
logging.info("File(s) uploaded and processed successfully.")
except Exception as e:
st.error("An error occurred while processing your document. Please try again.")
logging.error(f"Error extracting content from document: {e}")
else:
st.session_state.uploaded_files = None
st.session_state['prev_uploaded_files'] = []

st.subheader("Speech output responses")
if 'speech_outputs' in st.session_state:
Expand Down Expand Up @@ -213,4 +224,4 @@ def handle_audio_message():


# Handle audio input from user
audio_value = st.experimental_audio_input("Record a voice message", key="audio_prompt", on_change=handle_audio_message)
audio_value = st.experimental_audio_input("Record a voice message", key="audio_prompt", on_change=handle_audio_message)

0 comments on commit ef1fcca

Please sign in to comment.