File size: 9,306 Bytes
29118da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Controller layer coordinating UI events with domain and application services."""

import logging
from typing import Generator, List, Dict, Any, Optional, Tuple
import gradio as gr

from src.config import GenerationConfig, HardwareSpecs
from src.registry.base import BaseModelRegistry
from src.ingestion.service import KnowledgeBaseService
from src.services.inference_service import InferenceService

logger = logging.getLogger(__name__)


class UIController:
    """Manages event callbacks for model loading, inference streaming, and document ingestion."""

    def __init__(
        self,
        registry: BaseModelRegistry,
        inference: InferenceService,
        knowledge_base: KnowledgeBaseService,
        hw_specs: HardwareSpecs,
        spaces: Optional[Any] = None,
    ) -> None:
        self.registry = registry
        self.inference = inference
        self.knowledge_base = knowledge_base
        self.hw_specs = hw_specs
        self.spaces = spaces

        # Wrap handle_inference with ZeroGPU decorator if spaces accelerator is provided
        if self.spaces is not None:
            gpu_decorator = self.spaces.GPU(duration=self.hw_specs.extended_execution_window_sec)
            self.stream_response = gpu_decorator(self.handle_inference)
        else:
            self.stream_response = self.handle_inference

    def handle_load_model(
        self,
        model_id: str,
        quantization: str
    ) -> Generator[Tuple[Any, Any, Any], None, None]:
        """Manage two-stage loading with real-time feedback and button lock."""
        logger.info("Controller: Loading model '%s' (%s)", model_id, quantization)

        loading_html = (
            f"<div style='padding: 8px 14px; border-radius: 6px; background: rgba(99, 102, 241, 0.12); "
            f"border: 1px solid #6366f1; color: #a5b4fc; font-weight: 500; font-size: 0.88rem;'>"
            f"โณ Loading <strong>{model_id}</strong> ({quantization})... Please wait.</div>"
        )
        yield (
            gr.update(value=loading_html),
            gr.update(interactive=False, value="โณ Loading..."),
            gr.update(visible=False),
        )

        try:
            success, msg = self.registry.load_model_and_processor(
                model_id=model_id,
                quantization=quantization,
            )
        except Exception as exc:
            logger.exception("Error during model load:")
            success = False
            msg = f"Unexpected failure: {str(exc)}"

        if success:
            ready_html = (
                f"<div style='padding: 8px 14px; border-radius: 6px; background: rgba(16, 185, 129, 0.12); "
                f"border: 1px solid #10b981; color: #10b981; font-weight: 500; font-size: 0.88rem;'>"
                f"โœ… {msg}</div>"
            )
            yield (
                gr.update(value=ready_html),
                gr.update(interactive=True, value="๐Ÿ”„ Reload Model"),
                gr.update(visible=True),
            )
        else:
            preview_html = (
                f"<div style='padding: 8px 14px; border-radius: 6px; background: rgba(245, 158, 11, 0.12); "
                f"border: 1px solid #f59e0b; color: #f59e0b; font-weight: 500; font-size: 0.88rem;'>"
                f"โ„น๏ธ {msg} (Preview mode active)</div>"
            )
            yield (
                gr.update(value=preview_html),
                gr.update(interactive=True, value="๐Ÿ”„ Reload Model"),
                gr.update(visible=True),
            )

    def handle_inference(
        self,
        history: List[Dict[str, Any]],
        downsample_mode: str,
        max_slice_nums: int,
        temperature: float,
        top_p: float,
        top_k: int,
        repetition_penalty: float,
        max_new_tokens: int,
        enable_thinking: bool,
        enable_rag: bool,
        rag_max_context_chars: int,
        rag_top_k: int,
        system_prompt: str,
    ) -> Generator[List[Dict[str, Any]], None, None]:
        """Execute stream generation with optional context grounding."""
        if not history:
            yield history
            return

        if history[-1]["role"] != "assistant":
            history.append({"role": "assistant", "content": ""})

        gen_config = GenerationConfig(
            downsample_mode=downsample_mode,
            max_slice_nums=int(max_slice_nums),
            temperature=float(temperature),
            top_p=float(top_p),
            top_k=int(top_k),
            repetition_penalty=float(repetition_penalty),
            max_new_tokens=int(max_new_tokens),
            enable_thinking=bool(enable_thinking),
            enable_rag=bool(enable_rag),
            rag_max_context_chars=int(rag_max_context_chars),
            rag_top_k=int(rag_top_k),
            system_prompt=system_prompt,
        )

        rag_context = ""
        if enable_rag and self.knowledge_base.chunks:
            latest_query = self.inference.extract_latest_user_query(history[:-1])
            if latest_query:
                rag_context = self.knowledge_base.retrieve_context(
                    query=latest_query,
                    top_k=int(rag_top_k),
                    max_context_chars=int(rag_max_context_chars),
                )

        formatted_messages = self.inference.parse_gradio_history(
            raw_history=history[:-1],
            system_prompt=system_prompt,
            rag_context=rag_context,
        )

        try:
            for chunk in self.inference.stream_inference(formatted_messages, gen_config):
                history[-1]["content"] = chunk
                yield history
        except Exception as exc:
            logger.exception("Inference error occurred:")
            history[-1]["content"] = f"โš ๏ธ Generation error: {str(exc)}"
            yield history

    def handle_document_upload(self, files: Optional[List[str]]) -> Tuple[Any, Any, str]:
        """Process and index uploaded documents."""
        if not files:
            names = self.knowledge_base.get_document_names()
            return (
                self.knowledge_base.get_document_table_data(),
                gr.update(choices=names, value=names[0] if names else None),
                "<span style='color: #94a3b8; font-size: 0.85rem;'>No files selected.</span>",
            )

        res = self.knowledge_base.add_documents(files)
        table_data = self.knowledge_base.get_document_table_data()
        names = self.knowledge_base.get_document_names()
        msg = (
            f"<span style='color: #10b981; font-weight: 500; font-size: 0.85rem;'>"
            f"โœ… Indexed {res['new_docs']} new file(s). "
            f"Total: {res['count']} document(s) & {res['chunks']} chunks active.</span>"
        )
        return table_data, gr.update(choices=names, value=names[0] if names else None), msg

    def handle_document_delete(self, doc_name: Optional[str]) -> Tuple[Any, Any, str]:
        """Remove a selected document from the knowledge base."""
        if not doc_name:
            names = self.knowledge_base.get_document_names()
            return (
                self.knowledge_base.get_document_table_data(),
                gr.update(choices=names, value=names[0] if names else None),
                "<span style='color: #ef4444; font-size: 0.85rem;'>Select a document to delete.</span>",
            )

        removed = self.knowledge_base.remove_document(doc_name)
        table_data = self.knowledge_base.get_document_table_data()
        names = self.knowledge_base.get_document_names()
        if removed:
            msg = f"<span style='color: #f59e0b; font-size: 0.85rem;'>๐Ÿ—‘๏ธ Removed '{doc_name}'. Remaining: {len(names)} document(s).</span>"
        else:
            msg = f"<span style='color: #ef4444; font-size: 0.85rem;'>Document '{doc_name}' not found.</span>"

        return table_data, gr.update(choices=names, value=names[0] if names else None), msg

    def handle_document_clear(self) -> Tuple[List[Any], Any, str]:
        """Clear all indexed documents."""
        self.knowledge_base.clear()
        return [], gr.update(choices=[], value=None), "<span style='color: #94a3b8; font-size: 0.85rem;'>Knowledge base cleared.</span>"

    @staticmethod
    def handle_user_submit(message: Dict[str, Any], history: List[Dict[str, Any]]) -> Tuple[List[Dict[str, Any]], Any]:
        """Parse user multimodal submission into history."""
        text = (message.get("text") or "").strip()
        files = message.get("files") or []

        if not text and not files:
            return history, gr.MultimodalTextbox(value={"text": "", "files": []})

        content_elements = [{"path": f} for f in files]
        if text:
            content_elements.append(text)

        user_payload = content_elements if len(content_elements) > 1 else content_elements[0]
        history.append({"role": "user", "content": user_payload})

        return history, gr.MultimodalTextbox(value={"text": "", "files": []})

    @staticmethod
    def handle_undo(history: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        """Remove last interaction turn."""
        return history[:-2] if len(history) >= 2 else []

    @staticmethod
    def handle_clear() -> List[Any]:
        """Reset chat conversation."""
        return []