File size: 7,285 Bytes
c319b15
 
 
 
b39a3c2
 
c319b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29178b4
c319b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b39a3c2
c319b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08980d3
 
 
 
 
 
 
 
 
 
c319b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08980d3
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
"""UI component definitions for the Gradio interface."""

import gradio as gr

from config import HYBRID_ALPHA_UI_DEFAULT

# Modern, clean CSS styling
CUSTOM_CSS = """
.generating {
    border-color: transparent !important;
}

/* Cleaner header */
h1 {
    font-size: 2rem !important;
    font-weight: 600 !important;
    margin-bottom: 0.5rem !important;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* Compact controls */
.control-row {
    background: rgba(255, 255, 255, 0.02);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* Better spacing for controls */
.form-group {
    margin-bottom: 0.75rem;
}

/* Cleaner chatbot */
.chatbot-container {
    border-radius: 12px;
    overflow: hidden;
}

/* Context panel styling */
.context-panel {
    background: rgba(255, 255, 255, 0.02);
    border-radius: 12px;
    padding: 1rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    max-height: 600px;
    overflow-y: auto;
}

/* Better buttons */
button {
    border-radius: 8px !important;
    font-weight: 500 !important;
    transition: all 0.2s !important;
}

button:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* Cleaner input */
input[type="text"], textarea {
    border-radius: 8px !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
}

/* Compact accordion */
.accordion {
    border-radius: 8px !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    background: rgba(255, 255, 255, 0.02) !important;
}

/* Better radio buttons */
.radio-group {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* Hide info text by default, show on hover */
.info-text {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.6);
    margin-top: 0.25rem;
}
"""


def create_ui_components(available_sources):
    """Create and return all UI components.

    Args:
        available_sources: List of available document sources

    Returns:
        dict: Dictionary containing all UI components
    """
    with gr.Blocks() as demo:
        # Clean header
        gr.Markdown("# πŸ“š Document Q&A")
        gr.Markdown(
            "Ask questions about your research papers. Toggle RAG on to search documents, or off for general chat.",
            elem_classes=["info-text"],
        )

        # Main controls in a compact row
        with gr.Row(elem_classes=["control-row"]):
            with gr.Column(scale=1, min_width=120):
                rag_enabled = gr.Checkbox(
                    value=False,
                    label="πŸ” RAG Mode",
                    info="",
                )
            with gr.Column(scale=2, min_width=200, visible=False) as search_col:
                search_type = gr.Radio(
                    choices=[
                        ("MMR", "mmr"),
                        ("Similarity", "similarity"),
                        ("Hybrid", "hybrid"),
                    ],
                    value="mmr",
                    label="Search",
                    info="",
                    interactive=True,
                )
            with gr.Column(scale=2, min_width=180, visible=False) as filter_col:
                doc_filter = gr.Dropdown(
                    choices=["All Documents"] + available_sources,
                    value="All Documents",
                    label="Document",
                    info="",
                    interactive=True,
                )

        # Advanced options - more compact
        with gr.Accordion("βš™οΈ Advanced", open=False, visible=False) as advanced_options:
            with gr.Row():
                with gr.Column(scale=1):
                    query_rewriting = gr.Checkbox(
                        value=False,
                        label="Query Rewriting",
                        info="",
                    )
                with gr.Column(scale=1):
                    reranking = gr.Checkbox(
                        value=False,
                        label="Re-ranking",
                        info="",
                    )
                hybrid_alpha = gr.Slider(
                    minimum=0,
                    maximum=100,
                    value=HYBRID_ALPHA_UI_DEFAULT,
                    step=5,
                    label="Hybrid Balance",
                    info="",
                    visible=False,
                    interactive=False,
                )

        # Chat interface - main focus
        chatbot = gr.Chatbot(
            height=500,
        )

        # Context panel - cleaner, shown below chatbot when RAG is enabled
        with gr.Column(visible=False) as context_section:
            context_header = gr.Markdown(
                "### πŸ“„ Retrieved Context", elem_classes=["info-text"]
            )
            context_box = gr.Markdown(
                elem_classes=["context-panel"],
            )

        # Input area - cleaner
        with gr.Row():
            msg = gr.Textbox(
                label="",
                placeholder="Ask a question about your documents...",
                scale=9,
                container=False,
            )
            with gr.Column(scale=1, min_width=100):
                submit = gr.Button("Send", variant="primary", scale=1)
                clear = gr.Button("Clear", scale=1, variant="secondary")

        # Examples - cleaner presentation
        with gr.Accordion("πŸ’‘ Example Questions", open=False):
            gr.Examples(
                examples=[
                    "What are the main challenges for achieving net zero emissions by 2050?",
                    "What is the projected global renewable energy capacity for 2028?",
                    "How is the global EV market expected to evolve in the coming years?",
                    "What percentage of global electricity generation came from renewables in 2023?",
                    "What are the key policy recommendations for accelerating clean energy transitions?",
                    "What is the projected investment in clean energy technologies for 2024?",
                    "How do critical minerals supply chains impact renewable energy deployment?",
                    "What are the exact CO2 emissions reduction targets in the Net Zero Roadmap?",
                    "What role does energy efficiency play in reducing global energy demand?",
                    "What are the specific challenges facing developing countries in clean energy transitions?",
                ],
                inputs=msg,
                label="",
            )

    return {
        "demo": demo,
        "rag_enabled": rag_enabled,
        "search_type": search_type,
        "doc_filter": doc_filter,
        "query_rewriting": query_rewriting,
        "reranking": reranking,
        "hybrid_alpha": hybrid_alpha,
        "chatbot": chatbot,
        "context_box": context_box,
        "msg": msg,
        "submit": submit,
        "clear": clear,
        "search_col": search_col,
        "filter_col": filter_col,
        "context_section": context_section,
        "advanced_options": advanced_options,
    }