Spaces:
Running
Running
Aditya Bakshi
commited on
update reset_chat_history to use pop, delete temp file, and use pdf file key
Browse files- app.py +22 -14
- 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 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
if
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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[
|
| 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
|
| 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[
|
| 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[
|
| 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
|