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 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""
OpenWebUI Engine Plugin

Wraps the OpenWebUI API (OpenAI-compatible) from uni-freiburg.de as an HTR engine.
Supports multiple models available on the OpenWebUI platform.
"""

from typing import Dict, Any, Optional, List
from pathlib import Path
import numpy as np
from PIL import Image
import io
import base64

from htr_engine_base import HTREngine, TranscriptionResult

try:
    from PyQt6.QtWidgets import (
        QWidget, QVBoxLayout, QHBoxLayout, QLabel, QComboBox,
        QPushButton, QCheckBox, QLineEdit, QGroupBox, QTextEdit,
        QSpinBox
    )
    from PyQt6.QtCore import Qt
    PYQT_AVAILABLE = True
except ImportError:
    PYQT_AVAILABLE = False
    QWidget = object

try:
    from openai import OpenAI
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False

try:
    from dotenv import load_dotenv
    DOTENV_AVAILABLE = True
except ImportError:
    DOTENV_AVAILABLE = False


class OpenWebUIEngine(HTREngine):
    """OpenWebUI API HTR engine plugin (OpenAI-compatible)."""

    def __init__(self):
        self.client: Optional[OpenAI] = None
        self._config_widget: Optional[QWidget] = None
        self._available_models: List[str] = []

        # Store config from load_model for batch processing
        self._loaded_config: Dict[str, Any] = {}

        # Widget references
        self._model_combo: Optional[QComboBox] = None
        self._api_key_edit: Optional[QLineEdit] = None
        self._show_key_check: Optional[QCheckBox] = None
        self._prompt_edit: Optional[QTextEdit] = None
        self._temperature_spin: Optional[QSpinBox] = None
        self._max_tokens_spin: Optional[QSpinBox] = None
        self._refresh_models_btn: Optional[QPushButton] = None

        # Default API configuration
        self.base_url = ""
        
        # Load environment variables from .env file (only once when instantiated)
        self._load_env_variables()

    def _load_env_variables(self):
        """Load environment variables from .env file if available."""
        try:
            from dotenv import load_dotenv
            # Look for .env in the project root (parent of engines/)
            env_path = Path(__file__).parent.parent / ".env"
            if env_path.exists():
                load_dotenv(env_path)
        except ImportError:
            # Silently skip if python-dotenv is not installed
            # Environment variables can still be set via OS
            pass

        # Load environment variables from .env file (if available)
        self._load_env_file()

    def _load_env_file(self):
        """Load environment variables from project root's .env file.
        
        Looks for .env in the project root directory (parent of engines/).
        Silently skips loading if python-dotenv is not installed or if .env doesn't exist.
        
        If .env loading fails or is skipped, the engine will still work if the API key
        is provided through the config dict.
        """
        if not DOTENV_AVAILABLE:
            return
            
        env_path = Path(__file__).parent.parent / ".env"
        if env_path.exists():
            load_dotenv(env_path)

    def get_name(self) -> str:
        return "OpenWebUI"

    def get_description(self) -> str:
        return "OpenWebUI API from openwebui.uni-freiburg.de (OpenAI-compatible, multiple models)"

    def is_available(self) -> bool:
        return OPENAI_AVAILABLE

    def get_unavailable_reason(self) -> str:
        if not OPENAI_AVAILABLE:
            return "OpenAI library not installed. Install with: pip install openai"
        return ""

    def get_config_widget(self) -> QWidget:
        """Create OpenWebUI configuration panel."""
        if self._config_widget is not None:
            return self._config_widget

        widget = QWidget()
        layout = QVBoxLayout()

        # API Key section
        key_group = QGroupBox("API Key")
        key_layout = QVBoxLayout()

        key_input_layout = QHBoxLayout()
        self._api_key_edit = QLineEdit()
        self._api_key_edit.setEchoMode(QLineEdit.EchoMode.Password)
        self._api_key_edit.setPlaceholderText("Enter your OpenWebUI API key")
        key_input_layout.addWidget(self._api_key_edit)

        self._show_key_check = QCheckBox("Show")
        self._show_key_check.toggled.connect(self._toggle_key_visibility)
        key_input_layout.addWidget(self._show_key_check)
        key_layout.addLayout(key_input_layout)

        key_hint = QLabel("Get your API key from https://openwebui.uni-freiburg.de")
        key_hint.setStyleSheet("color: gray; font-size: 9pt;")
        key_layout.addWidget(key_hint)

        key_group.setLayout(key_layout)
        layout.addWidget(key_group)

        # Model selection with refresh button
        model_group = QGroupBox("Model Selection")
        model_layout = QVBoxLayout()

        model_select_layout = QHBoxLayout()
        self._model_combo = QComboBox()
        self._model_combo.setMinimumWidth(300)
        model_select_layout.addWidget(self._model_combo)

        self._refresh_models_btn = QPushButton("Refresh Models")
        self._refresh_models_btn.clicked.connect(self._refresh_models)
        model_select_layout.addWidget(self._refresh_models_btn)

        model_layout.addLayout(model_select_layout)

        model_hint = QLabel("Click 'Refresh Models' to load available models from the server")
        model_hint.setStyleSheet("color: gray; font-size: 9pt;")
        model_layout.addWidget(model_hint)

        model_group.setLayout(model_layout)
        layout.addWidget(model_group)

        # Generation parameters
        params_group = QGroupBox("Generation Parameters")
        params_layout = QVBoxLayout()

        # Temperature
        temp_layout = QHBoxLayout()
        temp_layout.addWidget(QLabel("Temperature:"))
        self._temperature_spin = QSpinBox()
        self._temperature_spin.setRange(0, 100)
        self._temperature_spin.setValue(10)  # 0.1
        self._temperature_spin.setSuffix(" (×0.01)")
        temp_layout.addWidget(self._temperature_spin)
        temp_layout.addStretch()
        params_layout.addLayout(temp_layout)

        # Max tokens
        tokens_layout = QHBoxLayout()
        tokens_layout.addWidget(QLabel("Max Tokens:"))
        self._max_tokens_spin = QSpinBox()
        self._max_tokens_spin.setRange(100, 4096)
        self._max_tokens_spin.setValue(500)
        tokens_layout.addWidget(self._max_tokens_spin)
        tokens_layout.addStretch()
        params_layout.addLayout(tokens_layout)

        params_group.setLayout(params_layout)
        layout.addWidget(params_group)

        # Custom prompt section
        prompt_group = QGroupBox("Custom Prompt (Optional)")
        prompt_layout = QVBoxLayout()

        self._prompt_edit = QTextEdit()
        self._prompt_edit.setPlaceholderText(
            "Enter custom transcription prompt...\n\n"
            "Default prompt:\n"
            "Transcribe the text in this historical manuscript line image. "
            "Return only the transcribed text without any explanation or formatting."
        )
        self._prompt_edit.setMaximumHeight(120)
        prompt_layout.addWidget(self._prompt_edit)

        prompt_group.setLayout(prompt_layout)
        layout.addWidget(prompt_group)

        layout.addStretch()
        widget.setLayout(layout)

        self._config_widget = widget

        # Try to load saved API key
        self._load_saved_api_key()

        return widget

    def _toggle_key_visibility(self, checked: bool):
        """Toggle API key visibility."""
        if checked:
            self._api_key_edit.setEchoMode(QLineEdit.EchoMode.Normal)
        else:
            self._api_key_edit.setEchoMode(QLineEdit.EchoMode.Password)

    def _get_api_key_file(self) -> 'Path':
        """Get path to API key storage file."""
        from pathlib import Path
        storage_dir = Path.home() / ".trocr_gui"
        storage_dir.mkdir(exist_ok=True)
        return storage_dir / "api_keys.json"

    def _load_saved_api_key(self):
        """Load saved API key."""
        try:
            import json
            key_file = self._get_api_key_file()

            if key_file.exists():
                with open(key_file, "r") as f:
                    keys = json.load(f)

                if "openwebui" in keys:
                    self._api_key_edit.setText(keys["openwebui"])
        except Exception as e:
            print(f"Warning: Could not load saved API key: {e}")

    def _save_api_key(self):
        """Save API key."""
        try:
            import json
            key_file = self._get_api_key_file()

            # Load existing keys
            keys = {}
            if key_file.exists():
                with open(key_file, "r") as f:
                    keys = json.load(f)

            # Update key for OpenWebUI
            api_key = self._api_key_edit.text().strip()

            if api_key:
                keys["openwebui"] = api_key

                with open(key_file, "w") as f:
                    json.dump(keys, f, indent=2)
        except Exception as e:
            print(f"Warning: Could not save API key: {e}")

    def _refresh_models(self):
        """Fetch available models from OpenWebUI API."""
        api_key = self._api_key_edit.text().strip()

        if not api_key:
            self._model_combo.clear()
            self._model_combo.addItem("Please enter API key first")
            return

        try:
            # Create temporary client to fetch models
            client = OpenAI(
                base_url=self.base_url,
                api_key=api_key
            )

            # Fetch models
            models = client.models.list()

            self._available_models = []
            for model in models.data:
                self._available_models.append(model.id)

            # Update combo box
            self._model_combo.clear()
            if self._available_models:
                self._model_combo.addItems(sorted(self._available_models))
                print(f"[OpenWebUI] Loaded {len(self._available_models)} models")
            else:
                self._model_combo.addItem("No models found")

        except Exception as e:
            print(f"Error fetching models: {e}")
            self._model_combo.clear()
            self._model_combo.addItem(f"Error: {str(e)[:50]}")

    def get_config(self) -> Dict[str, Any]:
        """Extract configuration from widget controls."""
        if self._config_widget is None:
            return {}

        prompt_text = self._prompt_edit.toPlainText().strip()

        return {
            "api_key": self._api_key_edit.text().strip(),
            "model": self._model_combo.currentText(),
            "temperature": self._temperature_spin.value() / 100.0,
            "max_tokens": self._max_tokens_spin.value(),
            "custom_prompt": prompt_text if prompt_text else None,
        }

    def set_config(self, config: Dict[str, Any]):
        """Restore configuration to widget controls."""
        if self._config_widget is None:
            return

        self._api_key_edit.setText(config.get("api_key", ""))

        model = config.get("model", "")
        idx = self._model_combo.findText(model)
        if idx >= 0:
            self._model_combo.setCurrentIndex(idx)

        temp = int(config.get("temperature", 0.1) * 100)
        self._temperature_spin.setValue(temp)

        self._max_tokens_spin.setValue(config.get("max_tokens", 500))

        custom_prompt = config.get("custom_prompt", "")
        if custom_prompt:
            self._prompt_edit.setPlainText(custom_prompt)

    def load_model(self, config: Dict[str, Any]) -> bool:
        """Initialize OpenWebUI client."""
        try:
            api_key = config.get("api_key", "")

            if not api_key:
                print("Error: No API key provided. Paste your key in the field.")
                return False

            base_url = config.get("base_url", "").strip().rstrip("/")
            if not base_url:
                print("Error: No OpenWebUI base URL provided.")
                return False

            # Store config for batch processing (model, temperature, etc.)
            self._loaded_config = config.copy()

            # Save API key for future use
            if self._api_key_edit and self._api_key_edit.text().strip():
                self._save_api_key()

            self.base_url = base_url

            # Initialize client
            self.client = OpenAI(
                base_url=self.base_url,
                api_key=api_key
            )

            model = config.get("model", config.get("model_id", "unknown"))
            print(f"[OpenWebUI] Client initialized with base URL: {self.base_url}, model: {model}")
            return True

        except Exception as e:
            print(f"Error initializing OpenWebUI client: {e}")
            self.client = None
            return False

    def unload_model(self):
        """Unload OpenWebUI client."""
        if self.client is not None:
            self.client = None
        self._loaded_config = {}

    def is_model_loaded(self) -> bool:
        """Check if client is initialized."""
        return self.client is not None

    def transcribe_line(self, image: np.ndarray, config: Optional[Dict[str, Any]] = None) -> TranscriptionResult:
        """Transcribe a line image with OpenWebUI API."""
        if self.client is None:
            return TranscriptionResult(text="[OpenWebUI client not initialized]", confidence=0.0)

        if config is None:
            # First try loaded config (from batch processing), then GUI config
            if self._loaded_config:
                config = self._loaded_config
            else:
                config = self.get_config()

        try:
            # Convert numpy array to PIL Image
            if isinstance(image, np.ndarray):
                pil_image = Image.fromarray(image)
            else:
                pil_image = image

            # Convert to RGB if needed
            if pil_image.mode != 'RGB':
                pil_image = pil_image.convert('RGB')

            # Encode image to base64
            buffered = io.BytesIO()
            pil_image.save(buffered, format="PNG")
            img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')

            # Prepare prompt
            custom_prompt = config.get("custom_prompt")
            if custom_prompt:
                prompt = custom_prompt
            else:
                prompt = (
                    "Transcribe the text in this historical manuscript line image. "
                    "Return only the transcribed text without any explanation or formatting."
                )

            # Get model and parameters
            model = config.get("model", "gpt-4-vision-preview")
            temperature = config.get("temperature", 0.1)
            max_tokens = config.get("max_tokens")
            # Treat 0 as "no limit" (HTML number fields send 0 for blank)
            if max_tokens is not None and max_tokens <= 0:
                max_tokens = None

            # Call OpenWebUI API (OpenAI-compatible)
            api_kwargs = dict(
                model=model,
                messages=[
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "text",
                                "text": prompt
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": f"data:image/png;base64,{img_base64}"
                                }
                            }
                        ]
                    }
                ],
                temperature=temperature,
            )
            if max_tokens is not None:
                api_kwargs["max_tokens"] = max_tokens
            response = self.client.chat.completions.create(**api_kwargs)

            # Extract transcription
            text = response.choices[0].message.content.strip()

            # Extract usage info
            usage = {}
            if hasattr(response, 'usage') and response.usage:
                usage = {
                    "prompt_tokens": response.usage.prompt_tokens,
                    "completion_tokens": response.usage.completion_tokens,
                    "total_tokens": response.usage.total_tokens
                }

            return TranscriptionResult(
                text=text,
                confidence=1.0,  # OpenWebUI doesn't provide confidence
                metadata={
                    "provider": "openwebui",
                    "model": model,
                    "usage": usage
                }
            )

        except Exception as e:
            print(f"Error in OpenWebUI transcription: {e}")
            import traceback
            traceback.print_exc()
            return TranscriptionResult(text=f"[OpenWebUI Error: {e}]", confidence=0.0)

    def get_capabilities(self) -> Dict[str, bool]:
        """OpenWebUI capabilities."""
        return {
            "batch_processing": False,
            "confidence_scores": False,
            "beam_search": False,
            "language_model": True,
            "preprocessing": True,
        }

    def requires_line_segmentation(self) -> bool:
        """OpenWebUI VLMs can process full pages directly without segmentation."""
        return False  # VLMs process full page images