Spaces:
Running
Running
Fix sample document processing and close functionality
Browse files- Fixed auto-processing of sample documents not running
- Improved Process button clarity based on document state
- Enhanced close button appearance and functionality
- Added tracking of sample document processing
- Fixed issues with document state management
- Added more descriptive button text
app.py
CHANGED
@@ -1203,6 +1203,8 @@ with main_tab1:
|
|
1203 |
st.session_state.sample_just_loaded = False
|
1204 |
if 'processed_document_active' not in st.session_state:
|
1205 |
st.session_state.processed_document_active = False
|
|
|
|
|
1206 |
|
1207 |
# Use uploaded_file or sample_document if available
|
1208 |
if 'sample_document' in st.session_state and st.session_state.sample_document is not None:
|
@@ -1224,6 +1226,8 @@ with main_tab1:
|
|
1224 |
# Set auto-process flag in session state if this is a newly loaded sample
|
1225 |
if st.session_state.sample_just_loaded:
|
1226 |
st.session_state.auto_process_sample = True
|
|
|
|
|
1227 |
st.session_state.sample_just_loaded = False
|
1228 |
|
1229 |
# Clear sample document after use to avoid interference with future uploads
|
@@ -1242,7 +1246,11 @@ with main_tab1:
|
|
1242 |
|
1243 |
# Process button - flush left with similar padding as file browser
|
1244 |
with left_col:
|
1245 |
-
|
|
|
|
|
|
|
|
|
1246 |
|
1247 |
# Empty container for progress indicators - will be filled during processing
|
1248 |
# Positioned right after the process button for better visibility
|
@@ -1289,17 +1297,18 @@ with main_tab1:
|
|
1289 |
|
1290 |
# processed_document_active is already initialized at the top of the function
|
1291 |
|
1292 |
-
#
|
1293 |
-
process_now = process_button or st.session_state.auto_process_sample
|
1294 |
|
1295 |
-
#
|
1296 |
-
|
|
|
|
|
|
|
1297 |
st.info("Automatically processing sample document...")
|
1298 |
|
1299 |
-
#
|
1300 |
-
#
|
1301 |
-
|
1302 |
-
should_process = process_button or (st.session_state.auto_process_sample and not st.session_state.processed_document_active)
|
1303 |
|
1304 |
if should_process:
|
1305 |
# Reset auto-process flag to avoid processing on next rerun
|
@@ -2081,9 +2090,25 @@ with main_tab1:
|
|
2081 |
with success_cols[0]:
|
2082 |
metadata_placeholder.success("**Document processed successfully**")
|
2083 |
with success_cols[1]:
|
2084 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2085 |
# Clear the session state
|
2086 |
st.session_state.processed_document_active = False
|
|
|
|
|
|
|
2087 |
# Rerun to reset the page
|
2088 |
st.rerun()
|
2089 |
|
@@ -2092,6 +2117,12 @@ with main_tab1:
|
|
2092 |
result_copy = result.copy()
|
2093 |
result_copy['timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M")
|
2094 |
|
|
|
|
|
|
|
|
|
|
|
|
|
2095 |
# Generate more descriptive file name for the result
|
2096 |
original_name = Path(result.get('file_name', uploaded_file.name)).stem
|
2097 |
|
|
|
1203 |
st.session_state.sample_just_loaded = False
|
1204 |
if 'processed_document_active' not in st.session_state:
|
1205 |
st.session_state.processed_document_active = False
|
1206 |
+
if 'sample_document_processed' not in st.session_state:
|
1207 |
+
st.session_state.sample_document_processed = False
|
1208 |
|
1209 |
# Use uploaded_file or sample_document if available
|
1210 |
if 'sample_document' in st.session_state and st.session_state.sample_document is not None:
|
|
|
1226 |
# Set auto-process flag in session state if this is a newly loaded sample
|
1227 |
if st.session_state.sample_just_loaded:
|
1228 |
st.session_state.auto_process_sample = True
|
1229 |
+
# Mark that this is a sample document being processed
|
1230 |
+
st.session_state.sample_document_processed = True
|
1231 |
st.session_state.sample_just_loaded = False
|
1232 |
|
1233 |
# Clear sample document after use to avoid interference with future uploads
|
|
|
1246 |
|
1247 |
# Process button - flush left with similar padding as file browser
|
1248 |
with left_col:
|
1249 |
+
# Make the button more clear about its function
|
1250 |
+
if st.session_state.processed_document_active:
|
1251 |
+
process_button = st.button("Process Document Again")
|
1252 |
+
else:
|
1253 |
+
process_button = st.button("Process Document")
|
1254 |
|
1255 |
# Empty container for progress indicators - will be filled during processing
|
1256 |
# Positioned right after the process button for better visibility
|
|
|
1297 |
|
1298 |
# processed_document_active is already initialized at the top of the function
|
1299 |
|
1300 |
+
# We'll determine processing logic below
|
|
|
1301 |
|
1302 |
+
# Check if this is an auto-processing situation
|
1303 |
+
auto_processing = st.session_state.auto_process_sample and not st.session_state.processed_document_active
|
1304 |
+
|
1305 |
+
# Show a message if auto-processing is happening
|
1306 |
+
if auto_processing:
|
1307 |
st.info("Automatically processing sample document...")
|
1308 |
|
1309 |
+
# Determine if we should process the document
|
1310 |
+
# Either process button was clicked OR auto-processing is happening
|
1311 |
+
should_process = process_button or auto_processing
|
|
|
1312 |
|
1313 |
if should_process:
|
1314 |
# Reset auto-process flag to avoid processing on next rerun
|
|
|
2090 |
with success_cols[0]:
|
2091 |
metadata_placeholder.success("**Document processed successfully**")
|
2092 |
with success_cols[1]:
|
2093 |
+
# Use a more visually distinctive close button
|
2094 |
+
st.markdown("""
|
2095 |
+
<style>
|
2096 |
+
div[data-testid="stButton"] button {
|
2097 |
+
background-color: #f8f9fa;
|
2098 |
+
border: 1px solid #dee2e6;
|
2099 |
+
padding: 0.25rem 0.5rem;
|
2100 |
+
font-size: 0.875rem;
|
2101 |
+
border-radius: 0.2rem;
|
2102 |
+
}
|
2103 |
+
</style>
|
2104 |
+
""", unsafe_allow_html=True)
|
2105 |
+
|
2106 |
+
if st.button("✕ Close Document", key="close_document_button", help="Clear current document and start over"):
|
2107 |
# Clear the session state
|
2108 |
st.session_state.processed_document_active = False
|
2109 |
+
# Reset any active document data
|
2110 |
+
if 'current_result' in st.session_state:
|
2111 |
+
del st.session_state.current_result
|
2112 |
# Rerun to reset the page
|
2113 |
st.rerun()
|
2114 |
|
|
|
2117 |
result_copy = result.copy()
|
2118 |
result_copy['timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M")
|
2119 |
|
2120 |
+
# Store if this was a sample document
|
2121 |
+
if 'sample_document_processed' in st.session_state and st.session_state.sample_document_processed:
|
2122 |
+
result_copy['sample_document'] = True
|
2123 |
+
# Reset the flag
|
2124 |
+
st.session_state.sample_document_processed = False
|
2125 |
+
|
2126 |
# Generate more descriptive file name for the result
|
2127 |
original_name = Path(result.get('file_name', uploaded_file.name)).stem
|
2128 |
|