mirror of
https://github.com/browser-use/web-ui.git
synced 2026-03-22 11:17:17 +08:00
refactor: moved functions to utils for more readability
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import pickle
|
||||
import uuid
|
||||
import gradio as gr
|
||||
|
||||
|
||||
def default_config():
|
||||
@@ -47,3 +48,75 @@ def save_config_to_file(settings, save_dir="./tmp/webui_settings"):
|
||||
with open(config_file, 'wb') as f:
|
||||
pickle.dump(settings, f)
|
||||
return f"Configuration saved to {config_file}"
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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."
|
||||
)
|
||||
|
||||
72
webui.py
72
webui.py
@@ -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=[
|
||||
|
||||
Reference in New Issue
Block a user