File size: 2,402 Bytes
ceb7cfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio interface builder for email assistant UI."""

import gradio as gr
from typing import Callable, Tuple
from .config import UIConfig


def create_gradio_interface(process_fn: Callable[[str], Tuple[str, str, str, str]]) -> gr.Blocks:
    """Create Gradio interface with all components.

    Args:
        process_fn: Function that processes a query and returns
                   (subject, body, stats, chunks_html)

    Returns:
        Gradio Blocks interface ready to launch
    """
    theme = UIConfig.create_theme()

    with gr.Blocks(title=UIConfig.TITLE, theme=theme) as demo:
        gr.Markdown(f"# {UIConfig.TITLE}")

        # Query input section
        query_input = gr.Textbox(
            label=UIConfig.QUERY_LABEL,
            placeholder=UIConfig.QUERY_PLACEHOLDER,
            lines=UIConfig.QUERY_LINES
        )
        submit_btn = gr.Button(
            UIConfig.SUBMIT_BUTTON_TEXT,
            variant=UIConfig.SUBMIT_BUTTON_VARIANT
        )

        stats_output = gr.Markdown(label=UIConfig.STATS_LABEL)

        # Output section: Email and chunks side by side
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown(UIConfig.EMAIL_HEADER)
                subject_output = gr.Textbox(
                    label=UIConfig.SUBJECT_LABEL,
                    lines=UIConfig.SUBJECT_LINES
                )
                body_output = gr.Textbox(
                    label=UIConfig.BODY_LABEL,
                    lines=UIConfig.BODY_LINES
                )

            with gr.Column(scale=1):
                gr.Markdown(UIConfig.DOCUMENTS_HEADER)
                chunks_output = gr.HTML()

        # Define outputs list for reuse
        outputs = [subject_output, body_output, stats_output, chunks_output]

        # Event handlers
        submit_btn.click(
            fn=process_fn,
            inputs=[query_input],
            outputs=outputs,
            show_progress=True
        )

        query_input.submit(
            fn=process_fn,
            inputs=[query_input],
            outputs=outputs,
            show_progress=True
        )

        # Examples
        examples = gr.Examples(
            examples=UIConfig.EXAMPLES,
            inputs=[query_input]
        )

        examples.dataset.click(
            fn=process_fn,
            inputs=[query_input],
            outputs=outputs
        )

    return demo