refactor: moved functions to utils for more readability

This commit is contained in:
meshkatshb
2025-01-22 12:08:19 +03:30
parent 0e152d557d
commit dce50eb01b
2 changed files with 74 additions and 71 deletions

View File

@@ -39,7 +39,7 @@ from src.browser.config import BrowserPersistenceConfig
from src.browser.custom_context import BrowserContextConfig, CustomBrowserContext
from src.controller.custom_controller import CustomController
from gradio.themes import Citrus, Default, Glass, Monochrome, Ocean, Origin, Soft, Base
from src.utils.default_config_settings import default_config, load_config_from_file, save_config_to_file
from src.utils.default_config_settings import default_config, load_config_from_file, save_config_to_file, save_current_config, update_ui_from_config
from src.utils.utils import update_model_dropdown, get_latest_files, capture_screenshot
from dotenv import load_dotenv
@@ -809,50 +809,6 @@ def create_ui(config, theme_name="Ocean"):
interactive=False
)
def update_ui_from_config(config_file):
if config_file is not None:
loaded_config = load_config_from_file(config_file.name)
if isinstance(loaded_config, dict):
return (
gr.update(value=loaded_config.get("agent_type", "custom")),
gr.update(value=loaded_config.get("max_steps", 100)),
gr.update(value=loaded_config.get("max_actions_per_step", 10)),
gr.update(value=loaded_config.get("use_vision", True)),
gr.update(value=loaded_config.get("tool_call_in_content", True)),
gr.update(value=loaded_config.get("llm_provider", "openai")),
gr.update(value=loaded_config.get("llm_model_name", "gpt-4o")),
gr.update(value=loaded_config.get("llm_temperature", 1.0)),
gr.update(value=loaded_config.get("llm_base_url", "")),
gr.update(value=loaded_config.get("llm_api_key", "")),
gr.update(value=loaded_config.get("use_own_browser", False)),
gr.update(value=loaded_config.get("keep_browser_open", False)),
gr.update(value=loaded_config.get("headless", False)),
gr.update(value=loaded_config.get("disable_security", True)),
gr.update(value=loaded_config.get("enable_recording", True)),
gr.update(value=loaded_config.get("window_w", 1280)),
gr.update(value=loaded_config.get("window_h", 1100)),
gr.update(value=loaded_config.get("save_recording_path", "./tmp/record_videos")),
gr.update(value=loaded_config.get("save_trace_path", "./tmp/traces")),
gr.update(value=loaded_config.get("save_agent_history_path", "./tmp/agent_history")),
gr.update(value=loaded_config.get("task", "")),
"Configuration loaded successfully."
)
else:
return (
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), "Error: Invalid configuration file."
)
return (
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
gr.update(), "No file selected."
)
load_config_button.click(
fn=update_ui_from_config,
inputs=[config_file_input],
@@ -865,32 +821,6 @@ def create_ui(config, theme_name="Ocean"):
]
)
def save_current_config(*args):
current_config = {
"agent_type": args[0],
"max_steps": args[1],
"max_actions_per_step": args[2],
"use_vision": args[3],
"tool_call_in_content": args[4],
"llm_provider": args[5],
"llm_model_name": args[6],
"llm_temperature": args[7],
"llm_base_url": args[8],
"llm_api_key": args[9],
"use_own_browser": args[10],
"keep_browser_open": args[11],
"headless": args[12],
"disable_security": args[13],
"enable_recording": args[14],
"window_w": args[15],
"window_h": args[16],
"save_recording_path": args[17],
"save_trace_path": args[18],
"save_agent_history_path": args[19],
"task": args[20],
}
return save_config_to_file(current_config)
save_config_button.click(
fn=save_current_config,
inputs=[