Aditya Bakshi commited on
Commit
a175e7e
·
unverified ·
1 Parent(s): dc387c3

update reset_chat_history to use pop, delete temp file, and use pdf file key

Browse files
Files changed (2) hide show
  1. app.py +22 -14
  2. requirements.txt +0 -1
app.py CHANGED
@@ -136,16 +136,23 @@ def reset_chat_history():
136
  """
137
  Clear the chat history and related session state variables.
138
  """
139
- if CHAT_MESSAGES in st.session_state:
140
- del st.session_state[CHAT_MESSAGES]
141
- if IS_IT_REFINEMENT in st.session_state:
142
- del st.session_state[IS_IT_REFINEMENT]
143
- if ADDITIONAL_INFO in st.session_state:
144
- del st.session_state[ADDITIONAL_INFO]
145
- if 'pdf_file' in st.session_state:
146
- del st.session_state['pdf_file']
147
- if DOWNLOAD_FILE_KEY in st.session_state:
148
- del st.session_state[DOWNLOAD_FILE_KEY]
 
 
 
 
 
 
 
149
  st.rerun() # Reload the app
150
 
151
 
@@ -156,6 +163,7 @@ CHAT_MESSAGES = 'chat_messages'
156
  DOWNLOAD_FILE_KEY = 'download_file_name'
157
  IS_IT_REFINEMENT = 'is_it_refinement'
158
  ADDITIONAL_INFO = 'additional_info'
 
159
 
160
 
161
  logger = logging.getLogger(__name__)
@@ -358,19 +366,19 @@ def set_up_chat_ui():
358
  if prompt['files']:
359
  # Store uploaded pdf in session state
360
  uploaded_pdf = prompt['files'][0]
361
- st.session_state['pdf_file'] = uploaded_pdf
362
  # Apparently, Streamlit stores uploaded files in memory and clears on browser close
363
  # https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
364
 
365
  # Check if pdf file is uploaded
366
  # (we can use the same file if the user doesn't upload a new one)
367
- if 'pdf_file' in st.session_state:
368
  # Get validated page range
369
  (
370
  st.session_state['start_page'],
371
  st.session_state['end_page']
372
  ) = filem.validate_page_range(
373
- st.session_state['pdf_file'],
374
  st.session_state['start_page'],
375
  st.session_state['end_page']
376
  )
@@ -389,7 +397,7 @@ def set_up_chat_ui():
389
 
390
  # Get pdf contents
391
  st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
392
- st.session_state['pdf_file'],
393
  (st.session_state['start_page'], st.session_state['end_page'])
394
  )
395
  provider, llm_name = llm_helper.get_provider_model(
 
136
  """
137
  Clear the chat history and related session state variables.
138
  """
139
+ # Clear session state variables using pop with None default
140
+ st.session_state.pop(CHAT_MESSAGES, None)
141
+ st.session_state.pop(IS_IT_REFINEMENT, None)
142
+ st.session_state.pop(ADDITIONAL_INFO, None)
143
+ st.session_state.pop(PDF_FILE_KEY, None)
144
+
145
+ # Safely remove previously generated temp PPTX file
146
+ temp_pptx_path = st.session_state.pop(DOWNLOAD_FILE_KEY, None)
147
+ if temp_pptx_path:
148
+ try:
149
+ pptx_path = pathlib.Path(temp_pptx_path)
150
+ if pptx_path.exists() and pptx_path.is_file():
151
+ pptx_path.unlink()
152
+ logger.info(f"Removed temporary PPTX file: {pptx_path}")
153
+ except Exception as e:
154
+ logger.warning(f"Failed to remove temporary PPTX file {temp_pptx_path}: {e}")
155
+
156
  st.rerun() # Reload the app
157
 
158
 
 
163
  DOWNLOAD_FILE_KEY = 'download_file_name'
164
  IS_IT_REFINEMENT = 'is_it_refinement'
165
  ADDITIONAL_INFO = 'additional_info'
166
+ PDF_FILE_KEY = 'pdf_file'
167
 
168
 
169
  logger = logging.getLogger(__name__)
 
366
  if prompt['files']:
367
  # Store uploaded pdf in session state
368
  uploaded_pdf = prompt['files'][0]
369
+ st.session_state[PDF_FILE_KEY] = uploaded_pdf
370
  # Apparently, Streamlit stores uploaded files in memory and clears on browser close
371
  # https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
372
 
373
  # Check if pdf file is uploaded
374
  # (we can use the same file if the user doesn't upload a new one)
375
+ if PDF_FILE_KEY in st.session_state:
376
  # Get validated page range
377
  (
378
  st.session_state['start_page'],
379
  st.session_state['end_page']
380
  ) = filem.validate_page_range(
381
+ st.session_state[PDF_FILE_KEY],
382
  st.session_state['start_page'],
383
  st.session_state['end_page']
384
  )
 
397
 
398
  # Get pdf contents
399
  st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
400
+ st.session_state[PDF_FILE_KEY],
401
  (st.session_state['start_page'], st.session_state['end_page'])
402
  )
403
  provider, llm_name = llm_helper.get_provider_model(
requirements.txt CHANGED
@@ -10,7 +10,6 @@ pydantic==2.9.1
10
  litellm>=1.55.0
11
  google-generativeai # ~=0.8.3
12
  streamlit==1.44.1
13
- streamlit-extras>=0.3.0
14
 
15
  python-pptx~=1.0.2
16
  json5~=0.9.14
 
10
  litellm>=1.55.0
11
  google-generativeai # ~=0.8.3
12
  streamlit==1.44.1
 
13
 
14
  python-pptx~=1.0.2
15
  json5~=0.9.14