File size: 23,159 Bytes
59aaeae
 
 
 
 
 
 
 
 
7647e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
 
 
 
 
 
 
 
7647e70
 
 
 
59aaeae
 
 
 
 
 
 
 
 
 
 
 
7647e70
 
 
 
 
59aaeae
7647e70
 
 
59aaeae
7647e70
 
 
59aaeae
7647e70
 
 
59aaeae
7647e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59aaeae
7647e70
 
59aaeae
7647e70
 
59aaeae
7647e70
 
 
ef3661a
7647e70
 
 
 
 
 
 
 
59aaeae
7647e70
177e4d6
 
7647e70
 
 
 
177e4d6
 
7647e70
 
 
 
 
177e4d6
 
 
7647e70
 
 
 
 
 
 
 
 
59aaeae
 
7647e70
 
 
 
 
 
 
 
59aaeae
 
7647e70
 
 
75ead00
7647e70
59aaeae
7647e70
 
75ead00
7647e70
 
 
 
 
59aaeae
7647e70
 
 
59aaeae
7647e70
 
59aaeae
7647e70
 
75ead00
7647e70
 
 
 
 
 
 
75ead00
7647e70
 
75ead00
7647e70
 
75ead00
7647e70
 
 
59aaeae
7647e70
 
 
 
 
 
 
 
 
 
 
75ead00
7647e70
75ead00
7647e70
 
 
 
 
 
 
 
 
 
75ead00
7647e70
 
 
 
 
 
75ead00
7647e70
 
 
75ead00
7647e70
 
 
 
 
75ead00
7647e70
59aaeae
7647e70
 
 
 
 
 
59aaeae
7647e70
 
 
 
622c90f
7647e70
 
f468027
7647e70
 
 
 
 
 
 
 
 
 
f468027
7647e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a60ed6
7647e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f468027
7647e70
 
 
 
 
 
 
f468027
7647e70
 
 
ef3661a
7647e70
 
 
 
 
 
 
ef3661a
7647e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef3661a
7647e70
 
 
 
 
 
 
 
ef3661a
7647e70
 
 
ef3661a
7647e70
 
622c90f
7647e70
 
 
 
 
 
59aaeae
 
7647e70
 
3261541
7647e70
 
 
ef3661a
7647e70
 
e672e11
7647e70
 
 
 
 
e404682
7647e70
 
 
 
 
 
59aaeae
7647e70
 
 
 
 
 
 
 
 
 
59aaeae
7647e70
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import os
import streamlit as st
import json
import sys
import time
import base64
from pathlib import Path
import io
from datetime import datetime
import logging

# Import modules
from preprocessing import convert_pdf_to_images, preprocess_image
from ocr_processing import process_file
from ui_components import (
    ProgressReporter, 
    create_sidebar_options, 
    display_results, 
    create_file_uploader,
    display_about_tab,
    display_previous_results,
    display_document_with_images
)
from utils import get_base64_from_image, handle_temp_files, format_timestamp
from error_handler import handle_ocr_error, check_file_size
from constants import (
    MAX_FILE_SIZE_MB, 
    MAX_PAGES, 
    DOCUMENT_TYPES, 
    DOCUMENT_LAYOUTS,
    CUSTOM_PROMPT_TEMPLATES,
    LAYOUT_PROMPT_ADDITIONS
)

# Import the StructuredOCR class and config from the local files
from structured_ocr import StructuredOCR
from config import MISTRAL_API_KEY

# Import utilities for handling previous results
from ocr_utils import create_results_zip

# Configure logging
logging.basicConfig(level=logging.INFO, 
                   format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger("app")

# Set favicon path
favicon_path = os.path.join(os.path.dirname(__file__), "static/favicon.png")

# Set page configuration
st.set_page_config(
    page_title="Historical OCR",
    page_icon=favicon_path if os.path.exists(favicon_path) else "πŸ“œ",
    layout="wide",
    initial_sidebar_state="expanded"
)

def initialize_session_state():
    """Initialize all session state variables"""
    # Initialize session state for storing previous results if not already present
    if 'previous_results' not in st.session_state:
        st.session_state.previous_results = []

    # Initialize temp file tracking
    if 'temp_file_paths' not in st.session_state:
        st.session_state.temp_file_paths = []
        
    # Initialize last processed file tracking to fix "Process Document Again" button
    if 'last_processed_file' not in st.session_state:
        st.session_state.last_processed_file = None
    
    # Important: Initialize the reset flag
    if 'perform_reset' not in st.session_state:
        st.session_state.perform_reset = False
        
    # Initialize other session state variables
    if 'auto_process_sample' not in st.session_state:
        st.session_state.auto_process_sample = False
    if 'sample_just_loaded' not in st.session_state:
        st.session_state.sample_just_loaded = False
    if 'processed_document_active' not in st.session_state:
        st.session_state.processed_document_active = False
    if 'sample_document_processed' not in st.session_state:
        st.session_state.sample_document_processed = False
    if 'sample_document' not in st.session_state:
        st.session_state.sample_document = None
    if 'original_sample_bytes' not in st.session_state:
        st.session_state.original_sample_bytes = None
    if 'original_sample_name' not in st.session_state:
        st.session_state.original_sample_name = None
    if 'is_sample_document' not in st.session_state:
        st.session_state.is_sample_document = False
    
    # Check if we need to perform a complete reset (coming from "Close Document" button)
    if 'perform_reset' in st.session_state and st.session_state.perform_reset:
        # Save previous results
        previous_results = st.session_state.previous_results
        
        # Clean up any temporary files
        if 'temp_file_paths' in st.session_state and st.session_state.temp_file_paths:
            handle_temp_files(st.session_state.temp_file_paths)
            
        # Clear all session state variables except previous_results
        for key in list(st.session_state.keys()):
            if key not in ['previous_results']:
                # We will manually reset the perform_reset flag at the end
                if key != 'perform_reset':
                    st.session_state.pop(key, None)
        
        # Restore previous results
        st.session_state.previous_results = previous_results
        
        # Reinitialize session state variables
        st.session_state.temp_file_paths = []
        st.session_state.last_processed_file = None
        st.session_state.auto_process_sample = False
        st.session_state.sample_just_loaded = False
        st.session_state.processed_document_active = False
        st.session_state.sample_document_processed = False
        st.session_state.sample_document = None
        st.session_state.original_sample_bytes = None
        st.session_state.original_sample_name = None
        st.session_state.is_sample_document = False
        
        # Turn off reset flag - this must be done last
        st.session_state.perform_reset = False
        
        # Force this to be a complete reset cycle
        return

def show_example_documents():
    """Show example documents section"""
    st.subheader("Example Documents")
    
    # Add a simplified info message about examples
    st.markdown("""
    This app can process various historical documents:
    - Historical photographs, maps, and manuscripts
    - Handwritten letters and documents
    - Printed books and articles
    - Multi-page PDFs
    """)
    
    # Add CSS to make the dropdown match the column width
    st.markdown("""
    <style>
    /* Make the selectbox container match the full column width */
    .main .block-container .element-container:has([data-testid="stSelectbox"]) {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Make the actual selectbox control take the full width */
    .stSelectbox > div > div {
        width: 100% !important;
        max-width: 100% !important;
    }
    </style>
    """, unsafe_allow_html=True)
    
    # Sample document URLs dropdown with clearer label
    sample_urls = [
        "Select a sample document",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/a-la-carte.pdf",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/magician-or-bottle-cungerer.jpg",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/handwritten-letter.jpg",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/magellan-travels.jpg",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/milgram-flier.png",
        "https://huggingface.co/spaces/milwright/historical-ocr/resolve/main/input/baldwin-15th-north.jpg"
    ]
    
    sample_names = [
        "Select a sample document",
        "Restaurant Menu (PDF)",
        "The Magician (Image)",
        "Handwritten Letter (Image)",
        "Magellan Travels (Image)",
        "Milgram Flier (Image)",
        "Baldwin Street (Image)"
    ]
    
    # Initialize sample_document in session state if it doesn't exist
    if 'sample_document' not in st.session_state:
        st.session_state.sample_document = None
    
    selected_sample = st.selectbox("Select a sample document from `~/input`", options=range(len(sample_urls)), format_func=lambda i: sample_names[i])
    
    if selected_sample > 0:
        selected_url = sample_urls[selected_sample]
        
        # Add process button for the sample document
        if st.button("Load Sample Document"):
            try:
                import requests
                from io import BytesIO
                
                with st.spinner(f"Downloading {sample_names[selected_sample]}..."):
                    response = requests.get(selected_url)
                    response.raise_for_status()
                    
                    # Extract filename from URL
                    file_name = selected_url.split("/")[-1]
                    
                    # Create a BytesIO object from the downloaded content
                    file_content = BytesIO(response.content)
                    
                    # Store as a UploadedFile-like object in session state
                    class SampleDocument:
                        def __init__(self, name, content, content_type):
                            self.name = name
                            self._content = content
                            self.type = content_type
                            self.size = len(content)
                        
                        def getvalue(self):
                            return self._content
                            
                        def read(self):
                            return self._content
                            
                        def seek(self, position):
                            # Implement seek for compatibility with some file operations
                            return
                            
                        def tell(self):
                            # Implement tell for compatibility
                            return 0
                    
                    # Determine content type based on file extension
                    if file_name.lower().endswith('.pdf'):
                        content_type = 'application/pdf'
                    elif file_name.lower().endswith(('.jpg', '.jpeg')):
                        content_type = 'image/jpeg'
                    elif file_name.lower().endswith('.png'):
                        content_type = 'image/png'
                    else:
                        content_type = 'application/octet-stream'
                    
                    # Reset any document state before loading a new sample
                    if st.session_state.processed_document_active:
                        # Clear previous document state
                        st.session_state.processed_document_active = False
                        st.session_state.last_processed_file = None
                        
                        # Clean up any temporary files from previous processing
                        if st.session_state.temp_file_paths:
                            handle_temp_files(st.session_state.temp_file_paths)
                            st.session_state.temp_file_paths = []
                    
                    # Save download info in session state
                    st.session_state.sample_document = SampleDocument(
                        name=file_name,
                        content=response.content,
                        content_type=content_type
                    )
                    
                    # Store original bytes for reprocessing
                    st.session_state.original_sample_bytes = response.content
                    st.session_state.original_sample_name = file_name
                    
                    # Set state flags
                    st.session_state.sample_just_loaded = True
                    st.session_state.is_sample_document = True
                    # Generate a unique identifier for the sample document
                    st.session_state.last_processed_file = f"{file_name}_{len(response.content)}"
                    
                    # Force rerun to load the document
                    st.rerun()
            except Exception as e:
                st.error(f"Error downloading sample document: {str(e)}")
                st.info("Please try uploading your own document instead.")
    else:  
        # If no sample is selected, clear the sample document in session state
        st.session_state.sample_document = None

def process_document(uploaded_file, left_col, right_col, sidebar_options):
    """Process the uploaded document and display results"""
    if uploaded_file is None:
        return
        
    # Check file size (cap at 50MB)
    file_size_mb = len(uploaded_file.getvalue()) / (1024 * 1024)
    
    if file_size_mb > MAX_FILE_SIZE_MB:
        with left_col:
            st.error(f"File too large ({file_size_mb:.1f} MB). Maximum file size is {MAX_FILE_SIZE_MB}MB.")
        return
        
    # Check if this is a new file (different from the last processed file)
    current_file_identifier = f"{uploaded_file.name}_{len(uploaded_file.getvalue())}"
    if st.session_state.last_processed_file != current_file_identifier:
        # Reset processed_document_active if a new file is uploaded
        st.session_state.processed_document_active = False
    
    # Process button - flush left with similar padding as file browser
    with left_col:
        # Use a key for the button based on state to force re-creation
        button_key = "process_again" if st.session_state.processed_document_active else "process_initial"
        
        # Show appropriate button text based on state
        button_text = "Process Document Again" if st.session_state.processed_document_active else "Process Document"
        
        # Create the button
        process_button = st.button(button_text, key=button_key)
        
        # Handle sample document recreation if needed
        if process_button and st.session_state.processed_document_active and st.session_state.original_sample_bytes is not None:
            # Recreate the uploaded file from stored bytes
            from io import BytesIO
            import mimetypes
            
            # Determine mime type based on file extension
            file_ext = os.path.splitext(st.session_state.original_sample_name)[1].lower()
            if file_ext == '.pdf':
                mime_type = 'application/pdf'
            elif file_ext in ['.jpg', '.jpeg']:
                mime_type = 'image/jpeg'
            elif file_ext == '.png':
                mime_type = 'image/png'
            else:
                mime_type = mimetypes.guess_type(st.session_state.original_sample_name)[0] or 'application/octet-stream'
            
            # Create a synthetic file-like object with the same interface as UploadedFile
            uploaded_file = type('obj', (object,), {
                'name': st.session_state.original_sample_name,
                'getvalue': lambda: st.session_state.original_sample_bytes,
                'read': lambda: st.session_state.original_sample_bytes,
                'seek': lambda x: None,
                'type': mime_type
            })
        
        # Empty container for progress indicators - will be filled during processing
        # Positioned right after the process button for better visibility
        progress_placeholder = st.empty()
        
        # Image preprocessing preview - automatically show only the preprocessed version
        if any(sidebar_options["preprocessing_options"].values()) and uploaded_file.type.startswith('image/'):
            st.markdown("**Preprocessed Preview**")
            try:
                # Create a container for the preview to better control layout
                with st.container():
                    processed_bytes = preprocess_image(uploaded_file.getvalue(), sidebar_options["preprocessing_options"])
                    # Use use_container_width=True for responsive design
                    st.image(io.BytesIO(processed_bytes), use_container_width=True)
                
                # Show preprocessing metadata in a well-formatted caption
                meta_items = []
                if sidebar_options["preprocessing_options"].get("document_type", "standard") != "standard":
                    meta_items.append(f"Document type ({sidebar_options['preprocessing_options']['document_type']})")
                if sidebar_options["preprocessing_options"].get("grayscale", False):
                    meta_items.append("Grayscale")
                if sidebar_options["preprocessing_options"].get("denoise", False):
                    meta_items.append("Denoise")
                if sidebar_options["preprocessing_options"].get("contrast", 0) != 0:
                    meta_items.append(f"Contrast ({sidebar_options['preprocessing_options']['contrast']})")
                if sidebar_options["preprocessing_options"].get("rotation", 0) != 0:
                    meta_items.append(f"Rotation ({sidebar_options['preprocessing_options']['rotation']}Β°)")
                
                # Only show "Applied:" if there are actual preprocessing steps
                if meta_items:
                    meta_text = "Applied: " + ", ".join(meta_items)
                    st.caption(meta_text)
            except Exception as e:
                st.error(f"Error in preprocessing: {str(e)}")
                st.info("Try using grayscale preprocessing for PNG images with transparency")
        
        # Container for success message (will be filled after processing)
        # No extra spacing needed as it will be managed programmatically
        metadata_placeholder = st.empty()
    
    # Check if this is an auto-processing situation
    auto_processing = st.session_state.auto_process_sample and not st.session_state.processed_document_active
        
    # Show a message if auto-processing is happening
    auto_processing_message = st.empty()
    if auto_processing:
        auto_processing_message.info("Automatically processing sample document...")
        
    # Determine if we should process the document
    # Either process button was clicked OR auto-processing is happening
    should_process = process_button or auto_processing
    
    if should_process:
        # Reset auto-process flag to avoid processing on next rerun
        if st.session_state.auto_process_sample:
            st.session_state.auto_process_sample = False
            
        # Move the progress indicator reference to just below the button
        progress_reporter = ProgressReporter(progress_placeholder).setup()
        
        try:
            # Process the document
            result = process_file(
                uploaded_file=uploaded_file,
                use_vision=sidebar_options["use_vision"],
                preprocessing_options=sidebar_options["preprocessing_options"],
                progress_reporter=progress_reporter,
                pdf_dpi=sidebar_options.get("pdf_dpi", 150),
                max_pages=sidebar_options.get("max_pages", 3),
                pdf_rotation=sidebar_options.get("pdf_rotation", 0),
                custom_prompt=sidebar_options.get("custom_prompt", ""),
                perf_mode=sidebar_options.get("perf_mode", "Quality")
            )
            
            # Display results
            display_results(result, right_col, sidebar_options.get("custom_prompt", ""))
            
            # Set processed_document_active to True when a new document is processed
            st.session_state.processed_document_active = True
            
            # Clear the auto-processing message
            auto_processing_message.empty()
            
            # Store information about this processed file to track when new files are uploaded
            if uploaded_file is not None:
                st.session_state.last_processed_file = current_file_identifier
            
            # Display success message with close button for dismissing processed documents
            success_cols = st.columns([5, 1])
            with success_cols[0]:
                metadata_placeholder.success("**Document processed successfully**")
            with success_cols[1]:
                # Define a function to clear document state
                def clear_document_state():
                    # Reset all document-related session state
                    st.session_state.processed_document_active = False
                    st.session_state.sample_document = None
                    st.session_state.last_processed_file = None
                    
                    # Clear any remaining state flag if we're showing examples
                    st.session_state.perform_reset = True
                    
                # Create the close button with a callback
                st.button("βœ• Close Document", 
                          key="close_document_button", 
                          help="Clear current document and start over",
                          on_click=clear_document_state)
            
            # Store the result in the previous results list
            # Add timestamp to result for history tracking
            result_copy = result.copy()
            result_copy['timestamp'] = format_timestamp()
            
            # Store if this was a sample document
            if 'is_sample_document' in st.session_state and st.session_state.is_sample_document:
                result_copy['sample_document'] = True
            
            # Add to session state, keeping the most recent 20 results
            st.session_state.previous_results.insert(0, result_copy)
            if len(st.session_state.previous_results) > 20:
                st.session_state.previous_results = st.session_state.previous_results[:20]
                
        except Exception as e:
            st.error(f"Error processing document: {str(e)}")
            
            # Log the error
            import logging
            logging.error(f"Document processing error: {str(e)}", exc_info=True)

def main():
    """Main application function"""
    # Initialize session state
    initialize_session_state()
    
    # Apply custom CSS
    from ui.layout import load_css
    load_css()
    
    # Create sidebar options
    sidebar_options = create_sidebar_options()
    
    # Create main layout with tabs
    main_tab1, main_tab2, main_tab3 = st.tabs(["Document Processing", "Previous Results", "About"])
    
    with main_tab1:
        # Create a two-column layout for file upload and results
        left_col, right_col = st.columns([1, 1])
        
        with left_col:
            # Create file uploader
            uploaded_file = create_file_uploader()
            
            # Check if we have a sample document loaded
            if ('sample_document' in st.session_state and 
                st.session_state.sample_document is not None):
                
                # Use the sample document instead of the uploaded file
                uploaded_file = st.session_state.sample_document
                
                # Just reset the sample document loading flags after it's been used
                if st.session_state.sample_just_loaded:
                    st.session_state.sample_just_loaded = False
                    st.session_state.sample_document_processed = True
                    st.session_state.auto_process_sample = True
            
            # Only process document if available
            if uploaded_file is not None:
                process_document(uploaded_file, left_col, right_col, sidebar_options)
            else:
                # Clear any remaining state flag if we're showing examples
                st.session_state.processed_document_active = False
                
                # Show example documents section
                show_example_documents()
    
    with main_tab2:
        # Previous results tab
        display_previous_results()
    
    with main_tab3:
        # About tab
        display_about_tab()

# Run the application
if __name__ == "__main__":
    main()