Spaces:
Running
Running
Fix state management issues with Close Document button and Process Document Again button
Browse files
app.py
CHANGED
@@ -527,6 +527,10 @@ if 'temp_file_paths' not in st.session_state:
|
|
527 |
if 'last_processed_file' not in st.session_state:
|
528 |
st.session_state.last_processed_file = None
|
529 |
|
|
|
|
|
|
|
|
|
530 |
# Check if we need to perform a complete reset (coming from "Close Document" button)
|
531 |
if 'perform_reset' in st.session_state and st.session_state.perform_reset:
|
532 |
# List of all session state keys that should be reset, except previous_results
|
@@ -1388,37 +1392,40 @@ with main_tab1:
|
|
1388 |
with left_col:
|
1389 |
# Process button styling is now handled by global CSS
|
1390 |
|
1391 |
-
#
|
1392 |
-
if st.session_state.processed_document_active
|
1393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1394 |
|
1395 |
-
#
|
1396 |
-
|
1397 |
-
|
1398 |
-
|
1399 |
-
|
1400 |
-
|
1401 |
-
|
1402 |
-
|
1403 |
-
|
1404 |
-
|
1405 |
-
|
1406 |
-
|
1407 |
-
|
1408 |
-
|
1409 |
-
|
1410 |
-
|
1411 |
-
|
1412 |
-
|
1413 |
-
|
1414 |
-
'name': st.session_state.original_sample_name,
|
1415 |
-
'getvalue': lambda: st.session_state.original_sample_bytes,
|
1416 |
-
'read': lambda: st.session_state.original_sample_bytes,
|
1417 |
-
'seek': lambda x: None,
|
1418 |
-
'type': mime_type
|
1419 |
-
})
|
1420 |
-
else:
|
1421 |
-
process_button = st.button("Process Document")
|
1422 |
|
1423 |
# Empty container for progress indicators - will be filled during processing
|
1424 |
# Positioned right after the process button for better visibility
|
@@ -2256,9 +2263,12 @@ with main_tab1:
|
|
2256 |
with success_cols[1]:
|
2257 |
# Close button styling is now handled by global CSS
|
2258 |
|
2259 |
-
|
2260 |
-
|
2261 |
-
|
|
|
|
|
|
|
2262 |
|
2263 |
# Clean up any temporary files
|
2264 |
if 'temp_file_paths' in st.session_state:
|
@@ -2268,8 +2278,14 @@ with main_tab1:
|
|
2268 |
os.remove(temp_path)
|
2269 |
except Exception:
|
2270 |
pass # Ignore errors in cleanup
|
2271 |
-
|
2272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2273 |
|
2274 |
# Store the result in the previous results list
|
2275 |
# Add timestamp to result for history tracking
|
|
|
527 |
if 'last_processed_file' not in st.session_state:
|
528 |
st.session_state.last_processed_file = None
|
529 |
|
530 |
+
# Ensure perform_reset flag is initialized
|
531 |
+
if 'perform_reset' not in st.session_state:
|
532 |
+
st.session_state.perform_reset = False
|
533 |
+
|
534 |
# Check if we need to perform a complete reset (coming from "Close Document" button)
|
535 |
if 'perform_reset' in st.session_state and st.session_state.perform_reset:
|
536 |
# List of all session state keys that should be reset, except previous_results
|
|
|
1392 |
with left_col:
|
1393 |
# Process button styling is now handled by global CSS
|
1394 |
|
1395 |
+
# Use a key for the button based on state to force re-creation
|
1396 |
+
button_key = "process_again" if st.session_state.processed_document_active else "process_initial"
|
1397 |
+
|
1398 |
+
# Show appropriate button text based on state
|
1399 |
+
button_text = "Process Document Again" if st.session_state.processed_document_active else "Process Document"
|
1400 |
+
|
1401 |
+
# Create the button
|
1402 |
+
process_button = st.button(button_text, key=button_key)
|
1403 |
+
|
1404 |
+
# Handle sample document recreation if needed
|
1405 |
+
if process_button and st.session_state.processed_document_active and st.session_state.original_sample_bytes is not None:
|
1406 |
+
# Recreate the uploaded file from stored bytes
|
1407 |
+
from io import BytesIO
|
1408 |
+
import mimetypes
|
1409 |
|
1410 |
+
# Determine mime type based on file extension
|
1411 |
+
file_ext = os.path.splitext(st.session_state.original_sample_name)[1].lower()
|
1412 |
+
if file_ext == '.pdf':
|
1413 |
+
mime_type = 'application/pdf'
|
1414 |
+
elif file_ext in ['.jpg', '.jpeg']:
|
1415 |
+
mime_type = 'image/jpeg'
|
1416 |
+
elif file_ext == '.png':
|
1417 |
+
mime_type = 'image/png'
|
1418 |
+
else:
|
1419 |
+
mime_type = mimetypes.guess_type(st.session_state.original_sample_name)[0] or 'application/octet-stream'
|
1420 |
+
|
1421 |
+
# Create a synthetic file-like object with the same interface as UploadedFile
|
1422 |
+
uploaded_file = type('obj', (object,), {
|
1423 |
+
'name': st.session_state.original_sample_name,
|
1424 |
+
'getvalue': lambda: st.session_state.original_sample_bytes,
|
1425 |
+
'read': lambda: st.session_state.original_sample_bytes,
|
1426 |
+
'seek': lambda x: None,
|
1427 |
+
'type': mime_type
|
1428 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1429 |
|
1430 |
# Empty container for progress indicators - will be filled during processing
|
1431 |
# Positioned right after the process button for better visibility
|
|
|
2263 |
with success_cols[1]:
|
2264 |
# Close button styling is now handled by global CSS
|
2265 |
|
2266 |
+
# Define a function to clear document state
|
2267 |
+
def clear_document_state():
|
2268 |
+
# Reset all document-related session state
|
2269 |
+
st.session_state.processed_document_active = False
|
2270 |
+
st.session_state.sample_document = None
|
2271 |
+
st.session_state.last_processed_file = None
|
2272 |
|
2273 |
# Clean up any temporary files
|
2274 |
if 'temp_file_paths' in st.session_state:
|
|
|
2278 |
os.remove(temp_path)
|
2279 |
except Exception:
|
2280 |
pass # Ignore errors in cleanup
|
2281 |
+
# Clear the temp files list
|
2282 |
+
st.session_state.temp_file_paths = []
|
2283 |
+
|
2284 |
+
# Create the close button with a callback
|
2285 |
+
st.button("✕ Close Document",
|
2286 |
+
key="close_document_button",
|
2287 |
+
help="Clear current document and start over",
|
2288 |
+
on_click=clear_document_state)
|
2289 |
|
2290 |
# Store the result in the previous results list
|
2291 |
# Add timestamp to result for history tracking
|