File size: 3,563 Bytes
4b23b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcf07c8
4b23b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Upload stage for the Loci Similes GUI."""

from __future__ import annotations

try:
    import gradio as gr
except ImportError as exc:
    missing = getattr(exc, "name", None)
    base_msg = (
        "Optional GUI dependencies are missing. Install them via "
        "'pip install locisimiles[gui]' (Python 3.13+ also requires the "
        "audioop-lts backport) to use the Gradio interface."
    )
    if missing and missing != "gradio":
        raise ImportError(f"{base_msg} (missing package: {missing})") from exc
    raise ImportError(base_msg) from exc

from utils import validate_and_notify, load_csv_preview


def build_upload_stage() -> tuple[gr.Step, dict]:
    """Build the upload stage UI.
    
    Returns:
        Tuple of (Step component, dict of components for external access)
    """
    components = {}
    
    with gr.Step("Upload Files", id=0) as step:
        gr.Markdown("### πŸ“„ Step 1: Upload Documents")
        gr.Markdown("Upload two CSV files containing Latin text segments. Each CSV must have two columns: `seg_id` and `text`.")
        
        with gr.Row():
            with gr.Column():
                gr.Markdown("**πŸ” Query Document**")
                gr.Markdown("The document in which you want to find intertextual references.")
                components["query_upload"] = gr.File(
                    label="Upload Query CSV",
                    file_types=[".csv"],
                    type="filepath",
                )
                components["query_preview"] = gr.Dataframe(
                    label="Query Document Preview",
                    interactive=False,
                    visible=False,
                    max_height=400,
                    wrap=True,
                )
            
            with gr.Column():
                gr.Markdown("**πŸ“š Source Document**")
                gr.Markdown("The document to search for potential references.")
                components["source_upload"] = gr.File(
                    label="Upload Source CSV",
                    file_types=[".csv"],
                    type="filepath",
                )
                components["source_preview"] = gr.Dataframe(
                    label="Source Document Preview",
                    interactive=False,
                    visible=False,
                    max_height=400,
                    wrap=True,
                )
        
        with gr.Row():
            components["next_btn"] = gr.Button("Next: Configuration β†’", variant="primary", size="lg")
    
    return step, components


def setup_upload_handlers(components: dict, file_states: dict) -> None:
    """Set up event handlers for the upload stage.
    
    Args:
        components: Dictionary of UI components from build_upload_stage
        file_states: Dictionary with query_file_state and source_file_state
    """
    # Query file upload handler
    components["query_upload"].change(
        fn=lambda f: (validate_and_notify(f), load_csv_preview(f), f),
        inputs=components["query_upload"],
        outputs=[
            components["query_upload"],
            components["query_preview"],
            file_states["query_file_state"],
        ],
    )
    
    # Source file upload handler
    components["source_upload"].change(
        fn=lambda f: (validate_and_notify(f), load_csv_preview(f), f),
        inputs=components["source_upload"],
        outputs=[
            components["source_upload"],
            components["source_preview"],
            file_states["source_file_state"],
        ],
    )