|
4 | 4 | from pathlib import Path
|
5 | 5 | from typing import Dict, Optional
|
6 | 6 | import requests
|
| 7 | +import json |
| 8 | +import gradio as gr |
| 9 | +import uuid |
7 | 10 |
|
8 | 11 | from langchain_anthropic import ChatAnthropic
|
9 | 12 | from langchain_mistralai import ChatMistralAI
|
@@ -196,12 +199,14 @@ def update_model_dropdown(llm_provider, api_key=None, base_url=None):
|
196 | 199 | else:
|
197 | 200 | return gr.Dropdown(choices=[], value="", interactive=True, allow_custom_value=True)
|
198 | 201 |
|
| 202 | + |
199 | 203 | class MissingAPIKeyError(Exception):
|
200 | 204 | """Custom exception for missing API key."""
|
| 205 | + |
201 | 206 | def __init__(self, provider: str, env_var: str):
|
202 | 207 | provider_display = PROVIDER_DISPLAY_NAMES.get(provider, provider.upper())
|
203 | 208 | super().__init__(f"💥 {provider_display} API key not found! 🔑 Please set the "
|
204 |
| - f"`{env_var}` environment variable or provide it in the UI.") |
| 209 | + f"`{env_var}` environment variable or provide it in the UI.") |
205 | 210 |
|
206 | 211 |
|
207 | 212 | def encode_image(img_path):
|
@@ -270,3 +275,70 @@ async def capture_screenshot(browser_context):
|
270 | 275 | return encoded
|
271 | 276 | except Exception as e:
|
272 | 277 | return None
|
| 278 | + |
| 279 | + |
| 280 | +class ConfigManager: |
| 281 | + def __init__(self): |
| 282 | + self.components = {} |
| 283 | + self.component_order = [] |
| 284 | + |
| 285 | + def register_component(self, name: str, component): |
| 286 | + """Register a gradio component for config management.""" |
| 287 | + self.components[name] = component |
| 288 | + if name not in self.component_order: |
| 289 | + self.component_order.append(name) |
| 290 | + return component |
| 291 | + |
| 292 | + def save_current_config(self): |
| 293 | + """Save the current configuration of all registered components.""" |
| 294 | + current_config = {} |
| 295 | + for name in self.component_order: |
| 296 | + component = self.components[name] |
| 297 | + # Get the current value from the component |
| 298 | + current_config[name] = getattr(component, "value", None) |
| 299 | + |
| 300 | + return save_config_to_file(current_config) |
| 301 | + |
| 302 | + def update_ui_from_config(self, config_file): |
| 303 | + """Update UI components from a loaded configuration file.""" |
| 304 | + if config_file is None: |
| 305 | + return [gr.update() for _ in self.component_order] + ["No file selected."] |
| 306 | + |
| 307 | + loaded_config = load_config_from_file(config_file.name) |
| 308 | + |
| 309 | + if not isinstance(loaded_config, dict): |
| 310 | + return [gr.update() for _ in self.component_order] + ["Error: Invalid configuration file."] |
| 311 | + |
| 312 | + # Prepare updates for all components |
| 313 | + updates = [] |
| 314 | + for name in self.component_order: |
| 315 | + if name in loaded_config: |
| 316 | + updates.append(gr.update(value=loaded_config[name])) |
| 317 | + else: |
| 318 | + updates.append(gr.update()) |
| 319 | + |
| 320 | + updates.append("Configuration loaded successfully.") |
| 321 | + return updates |
| 322 | + |
| 323 | + def get_all_components(self): |
| 324 | + """Return all registered components in the order they were registered.""" |
| 325 | + return [self.components[name] for name in self.component_order] |
| 326 | + |
| 327 | + |
| 328 | +def load_config_from_file(config_file): |
| 329 | + """Load settings from a config file (JSON format).""" |
| 330 | + try: |
| 331 | + with open(config_file, 'r') as f: |
| 332 | + settings = json.load(f) |
| 333 | + return settings |
| 334 | + except Exception as e: |
| 335 | + return f"Error loading configuration: {str(e)}" |
| 336 | + |
| 337 | + |
| 338 | +def save_config_to_file(settings, save_dir="./tmp/webui_settings"): |
| 339 | + """Save the current settings to a UUID.json file with a UUID name.""" |
| 340 | + os.makedirs(save_dir, exist_ok=True) |
| 341 | + config_file = os.path.join(save_dir, f"{uuid.uuid4()}.json") |
| 342 | + with open(config_file, 'w') as f: |
| 343 | + json.dump(settings, f, indent=2) |
| 344 | + return f"Configuration saved to {config_file}" |
0 commit comments