File size: 1,015 Bytes
49574d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""UI state management for the document intelligence demo.

Per-session state via factory function + shared caches.
"""

import hashlib


def create_initial_state() -> dict:
    """Create a fresh per-session state dictionary.

    Returns:
        Initial state with all fields set to defaults.
    """
    return {
        "uploaded_file_hash": None,
        "uploaded_file_bytes": None,
        "parsed_result": {},
        "page_images": [],
        "figures_info": [],
        "selected_figure": None,
        "last_csv": None,
        "current_figure_index": 0,
        "conversation_history": [],
        "current_image_path": None,
    }


# Module-level shared caches (keyed by content hash, safe to share across sessions)
parse_cache: dict[str, dict] = {}
page_cache: dict[str, list] = {}


def hash_bytes(data: bytes) -> str:
    """Generate SHA256 hash of bytes.

    Args:
        data: Bytes to hash.

    Returns:
        Hex string of SHA256 hash.
    """
    return hashlib.sha256(data).hexdigest()