Spaces:
Running
Running
Aditya Bakshi
commited on
Commit
·
2a61758
1
Parent(s):
0b8e886
update reset_chat_history to use pop, delete temp file, and use pdf file key
Browse files- app.py +22 -15
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -19,7 +19,6 @@ from dotenv import load_dotenv
|
|
| 19 |
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 20 |
from langchain_core.messages import HumanMessage
|
| 21 |
from langchain_core.prompts import ChatPromptTemplate
|
| 22 |
-
from streamlit_extras.bottom_container import bottom
|
| 23 |
|
| 24 |
import global_config as gcfg
|
| 25 |
import helpers.file_manager as filem
|
|
@@ -139,16 +138,23 @@ def reset_chat_history():
|
|
| 139 |
"""
|
| 140 |
Clear the chat history and related session state variables.
|
| 141 |
"""
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
if
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
st.rerun() # Reload the app
|
| 153 |
|
| 154 |
|
|
@@ -159,6 +165,7 @@ CHAT_MESSAGES = 'chat_messages'
|
|
| 159 |
DOWNLOAD_FILE_KEY = 'download_file_name'
|
| 160 |
IS_IT_REFINEMENT = 'is_it_refinement'
|
| 161 |
ADDITIONAL_INFO = 'additional_info'
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
logger = logging.getLogger(__name__)
|
|
@@ -339,19 +346,19 @@ def set_up_chat_ui():
|
|
| 339 |
if prompt['files']:
|
| 340 |
# Store uploaded pdf in session state
|
| 341 |
uploaded_pdf = prompt['files'][0]
|
| 342 |
-
st.session_state[
|
| 343 |
# Apparently, Streamlit stores uploaded files in memory and clears on browser close
|
| 344 |
# https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
|
| 345 |
|
| 346 |
# Check if pdf file is uploaded
|
| 347 |
# (we can use the same file if the user doesn't upload a new one)
|
| 348 |
-
if
|
| 349 |
# Get validated page range
|
| 350 |
(
|
| 351 |
st.session_state['start_page'],
|
| 352 |
st.session_state['end_page']
|
| 353 |
) = filem.validate_page_range(
|
| 354 |
-
st.session_state[
|
| 355 |
st.session_state['start_page'],
|
| 356 |
st.session_state['end_page']
|
| 357 |
)
|
|
@@ -370,7 +377,7 @@ def set_up_chat_ui():
|
|
| 370 |
|
| 371 |
# Get pdf contents
|
| 372 |
st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
|
| 373 |
-
st.session_state[
|
| 374 |
(st.session_state['start_page'], st.session_state['end_page'])
|
| 375 |
)
|
| 376 |
provider, llm_name = llm_helper.get_provider_model(
|
|
|
|
| 19 |
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 20 |
from langchain_core.messages import HumanMessage
|
| 21 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 22 |
|
| 23 |
import global_config as gcfg
|
| 24 |
import helpers.file_manager as filem
|
|
|
|
| 138 |
"""
|
| 139 |
Clear the chat history and related session state variables.
|
| 140 |
"""
|
| 141 |
+
# Clear session state variables using pop with None default
|
| 142 |
+
st.session_state.pop(CHAT_MESSAGES, None)
|
| 143 |
+
st.session_state.pop(IS_IT_REFINEMENT, None)
|
| 144 |
+
st.session_state.pop(ADDITIONAL_INFO, None)
|
| 145 |
+
st.session_state.pop(PDF_FILE_KEY, None)
|
| 146 |
+
|
| 147 |
+
# Safely remove previously generated temp PPTX file
|
| 148 |
+
temp_pptx_path = st.session_state.pop(DOWNLOAD_FILE_KEY, None)
|
| 149 |
+
if temp_pptx_path:
|
| 150 |
+
try:
|
| 151 |
+
pptx_path = pathlib.Path(temp_pptx_path)
|
| 152 |
+
if pptx_path.exists() and pptx_path.is_file():
|
| 153 |
+
pptx_path.unlink()
|
| 154 |
+
logger.info(f"Removed temporary PPTX file: {pptx_path}")
|
| 155 |
+
except Exception as e:
|
| 156 |
+
logger.warning(f"Failed to remove temporary PPTX file {temp_pptx_path}: {e}")
|
| 157 |
+
|
| 158 |
st.rerun() # Reload the app
|
| 159 |
|
| 160 |
|
|
|
|
| 165 |
DOWNLOAD_FILE_KEY = 'download_file_name'
|
| 166 |
IS_IT_REFINEMENT = 'is_it_refinement'
|
| 167 |
ADDITIONAL_INFO = 'additional_info'
|
| 168 |
+
PDF_FILE_KEY = 'pdf_file'
|
| 169 |
|
| 170 |
|
| 171 |
logger = logging.getLogger(__name__)
|
|
|
|
| 346 |
if prompt['files']:
|
| 347 |
# Store uploaded pdf in session state
|
| 348 |
uploaded_pdf = prompt['files'][0]
|
| 349 |
+
st.session_state[PDF_FILE_KEY] = uploaded_pdf
|
| 350 |
# Apparently, Streamlit stores uploaded files in memory and clears on browser close
|
| 351 |
# https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
|
| 352 |
|
| 353 |
# Check if pdf file is uploaded
|
| 354 |
# (we can use the same file if the user doesn't upload a new one)
|
| 355 |
+
if PDF_FILE_KEY in st.session_state:
|
| 356 |
# Get validated page range
|
| 357 |
(
|
| 358 |
st.session_state['start_page'],
|
| 359 |
st.session_state['end_page']
|
| 360 |
) = filem.validate_page_range(
|
| 361 |
+
st.session_state[PDF_FILE_KEY],
|
| 362 |
st.session_state['start_page'],
|
| 363 |
st.session_state['end_page']
|
| 364 |
)
|
|
|
|
| 377 |
|
| 378 |
# Get pdf contents
|
| 379 |
st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
|
| 380 |
+
st.session_state[PDF_FILE_KEY],
|
| 381 |
(st.session_state['start_page'], st.session_state['end_page'])
|
| 382 |
)
|
| 383 |
provider, llm_name = llm_helper.get_provider_model(
|
requirements.txt
CHANGED
|
@@ -18,7 +18,6 @@ langchain-together~=0.3.0
|
|
| 18 |
langchain-ollama~=0.3.6
|
| 19 |
langchain-openai~=0.3.28
|
| 20 |
streamlit==1.44.1
|
| 21 |
-
streamlit-extras>=0.3.0
|
| 22 |
|
| 23 |
python-pptx~=1.0.2
|
| 24 |
json5~=0.9.14
|
|
|
|
| 18 |
langchain-ollama~=0.3.6
|
| 19 |
langchain-openai~=0.3.28
|
| 20 |
streamlit==1.44.1
|
|
|
|
| 21 |
|
| 22 |
python-pptx~=1.0.2
|
| 23 |
json5~=0.9.14
|