Skip to content

Commit a123e94

Browse files
authored
Merge pull request #131 from MeshkatShB/feature/use-previous-settings
Feature/use previous settings
2 parents e674732 + b616f2d commit a123e94

File tree

3 files changed

+195
-26
lines changed

3 files changed

+195
-26
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,6 @@ AgentHistoryList.json
184184

185185
# For Docker
186186
data/
187+
188+
# For Config Files (Current Settings)
189+
.config.pkl

src/utils/default_config_settings.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import os
2+
import pickle
3+
import uuid
4+
import gradio as gr
5+
6+
7+
def default_config():
8+
"""Prepare the default configuration"""
9+
return {
10+
"agent_type": "custom",
11+
"max_steps": 100,
12+
"max_actions_per_step": 10,
13+
"use_vision": True,
14+
"tool_call_in_content": True,
15+
"llm_provider": "openai",
16+
"llm_model_name": "gpt-4o",
17+
"llm_temperature": 1.0,
18+
"llm_base_url": "",
19+
"llm_api_key": "",
20+
"use_own_browser": False,
21+
"keep_browser_open": False,
22+
"headless": False,
23+
"disable_security": True,
24+
"enable_recording": True,
25+
"window_w": 1280,
26+
"window_h": 1100,
27+
"save_recording_path": "./tmp/record_videos",
28+
"save_trace_path": "./tmp/traces",
29+
"save_agent_history_path": "./tmp/agent_history",
30+
"task": "go to google.com and type 'OpenAI' click search and give me the first url",
31+
}
32+
33+
34+
def load_config_from_file(config_file):
35+
"""Load settings from a UUID.pkl file."""
36+
try:
37+
with open(config_file, 'rb') as f:
38+
settings = pickle.load(f)
39+
return settings
40+
except Exception as e:
41+
return f"Error loading configuration: {str(e)}"
42+
43+
44+
def save_config_to_file(settings, save_dir="./tmp/webui_settings"):
45+
"""Save the current settings to a UUID.pkl file with a UUID name."""
46+
os.makedirs(save_dir, exist_ok=True)
47+
config_file = os.path.join(save_dir, f"{uuid.uuid4()}.pkl")
48+
with open(config_file, 'wb') as f:
49+
pickle.dump(settings, f)
50+
return f"Configuration saved to {config_file}"
51+
52+
53+
def save_current_config(*args):
54+
current_config = {
55+
"agent_type": args[0],
56+
"max_steps": args[1],
57+
"max_actions_per_step": args[2],
58+
"use_vision": args[3],
59+
"tool_call_in_content": args[4],
60+
"llm_provider": args[5],
61+
"llm_model_name": args[6],
62+
"llm_temperature": args[7],
63+
"llm_base_url": args[8],
64+
"llm_api_key": args[9],
65+
"use_own_browser": args[10],
66+
"keep_browser_open": args[11],
67+
"headless": args[12],
68+
"disable_security": args[13],
69+
"enable_recording": args[14],
70+
"window_w": args[15],
71+
"window_h": args[16],
72+
"save_recording_path": args[17],
73+
"save_trace_path": args[18],
74+
"save_agent_history_path": args[19],
75+
"task": args[20],
76+
}
77+
return save_config_to_file(current_config)
78+
79+
80+
def update_ui_from_config(config_file):
81+
if config_file is not None:
82+
loaded_config = load_config_from_file(config_file.name)
83+
if isinstance(loaded_config, dict):
84+
return (
85+
gr.update(value=loaded_config.get("agent_type", "custom")),
86+
gr.update(value=loaded_config.get("max_steps", 100)),
87+
gr.update(value=loaded_config.get("max_actions_per_step", 10)),
88+
gr.update(value=loaded_config.get("use_vision", True)),
89+
gr.update(value=loaded_config.get("tool_call_in_content", True)),
90+
gr.update(value=loaded_config.get("llm_provider", "openai")),
91+
gr.update(value=loaded_config.get("llm_model_name", "gpt-4o")),
92+
gr.update(value=loaded_config.get("llm_temperature", 1.0)),
93+
gr.update(value=loaded_config.get("llm_base_url", "")),
94+
gr.update(value=loaded_config.get("llm_api_key", "")),
95+
gr.update(value=loaded_config.get("use_own_browser", False)),
96+
gr.update(value=loaded_config.get("keep_browser_open", False)),
97+
gr.update(value=loaded_config.get("headless", False)),
98+
gr.update(value=loaded_config.get("disable_security", True)),
99+
gr.update(value=loaded_config.get("enable_recording", True)),
100+
gr.update(value=loaded_config.get("window_w", 1280)),
101+
gr.update(value=loaded_config.get("window_h", 1100)),
102+
gr.update(value=loaded_config.get("save_recording_path", "./tmp/record_videos")),
103+
gr.update(value=loaded_config.get("save_trace_path", "./tmp/traces")),
104+
gr.update(value=loaded_config.get("save_agent_history_path", "./tmp/agent_history")),
105+
gr.update(value=loaded_config.get("task", "")),
106+
"Configuration loaded successfully."
107+
)
108+
else:
109+
return (
110+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
111+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
112+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
113+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
114+
gr.update(), "Error: Invalid configuration file."
115+
)
116+
return (
117+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
118+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
119+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
120+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
121+
gr.update(), "No file selected."
122+
)

webui.py

+70-26
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from src.browser.custom_context import BrowserContextConfig, CustomBrowserContext
4040
from src.controller.custom_controller import CustomController
4141
from gradio.themes import Citrus, Default, Glass, Monochrome, Ocean, Origin, Soft, Base
42+
from src.utils.default_config_settings import default_config, load_config_from_file, save_config_to_file, save_current_config, update_ui_from_config
4243
from src.utils.utils import update_model_dropdown, get_latest_files, capture_screenshot
4344

4445
from dotenv import load_dotenv
@@ -588,7 +589,7 @@ async def close_global_browser():
588589
await _global_browser.close()
589590
_global_browser = None
590591

591-
def create_ui(theme_name="Ocean"):
592+
def create_ui(config, theme_name="Ocean"):
592593
css = """
593594
.gradio-container {
594595
max-width: 1200px !important;
@@ -634,33 +635,33 @@ def create_ui(theme_name="Ocean"):
634635
agent_type = gr.Radio(
635636
["org", "custom"],
636637
label="Agent Type",
637-
value="custom",
638+
value=config['agent_type'],
638639
info="Select the type of agent to use",
639640
)
640641
max_steps = gr.Slider(
641642
minimum=1,
642643
maximum=200,
643-
value=100,
644+
value=config['max_steps'],
644645
step=1,
645646
label="Max Run Steps",
646647
info="Maximum number of steps the agent will take",
647648
)
648649
max_actions_per_step = gr.Slider(
649650
minimum=1,
650651
maximum=20,
651-
value=10,
652+
value=config['max_actions_per_step'],
652653
step=1,
653654
label="Max Actions per Step",
654655
info="Maximum number of actions the agent will take per step",
655656
)
656657
use_vision = gr.Checkbox(
657658
label="Use Vision",
658-
value=True,
659+
value=config['use_vision'],
659660
info="Enable visual processing capabilities",
660661
)
661662
tool_call_in_content = gr.Checkbox(
662663
label="Use Tool Calls in Content",
663-
value=True,
664+
value=config['tool_call_in_content'],
664665
info="Enable Tool Calls in content",
665666
)
666667

@@ -669,35 +670,35 @@ def create_ui(theme_name="Ocean"):
669670
llm_provider = gr.Dropdown(
670671
choices=[provider for provider,model in utils.model_names.items()],
671672
label="LLM Provider",
672-
value="openai",
673+
value=config['llm_provider'],
673674
info="Select your preferred language model provider"
674675
)
675676
llm_model_name = gr.Dropdown(
676677
label="Model Name",
677678
choices=utils.model_names['openai'],
678-
value="gpt-4o",
679+
value=config['llm_model_name'],
679680
interactive=True,
680681
allow_custom_value=True, # Allow users to input custom model names
681682
info="Select a model from the dropdown or type a custom model name"
682683
)
683684
llm_temperature = gr.Slider(
684685
minimum=0.0,
685686
maximum=2.0,
686-
value=1.0,
687+
value=config['llm_temperature'],
687688
step=0.1,
688689
label="Temperature",
689690
info="Controls randomness in model outputs"
690691
)
691692
with gr.Row():
692693
llm_base_url = gr.Textbox(
693694
label="Base URL",
694-
value='',
695+
value=config['llm_base_url'],
695696
info="API endpoint URL (if required)"
696697
)
697698
llm_api_key = gr.Textbox(
698699
label="API Key",
699700
type="password",
700-
value='',
701+
value=config['llm_api_key'],
701702
info="Your API key (leave blank to use .env)"
702703
)
703704

@@ -706,62 +707,62 @@ def create_ui(theme_name="Ocean"):
706707
with gr.Row():
707708
use_own_browser = gr.Checkbox(
708709
label="Use Own Browser",
709-
value=False,
710+
value=config['use_own_browser'],
710711
info="Use your existing browser instance",
711712
)
712713
keep_browser_open = gr.Checkbox(
713714
label="Keep Browser Open",
714-
value=os.getenv("CHROME_PERSISTENT_SESSION", "False").lower() == "true",
715+
value=config['keep_browser_open'],
715716
info="Keep Browser Open between Tasks",
716717
)
717718
headless = gr.Checkbox(
718719
label="Headless Mode",
719-
value=False,
720+
value=config['headless'],
720721
info="Run browser without GUI",
721722
)
722723
disable_security = gr.Checkbox(
723724
label="Disable Security",
724-
value=True,
725+
value=config['disable_security'],
725726
info="Disable browser security features",
726727
)
727728
enable_recording = gr.Checkbox(
728729
label="Enable Recording",
729-
value=True,
730+
value=config['enable_recording'],
730731
info="Enable saving browser recordings",
731732
)
732733

733734
with gr.Row():
734735
window_w = gr.Number(
735736
label="Window Width",
736-
value=1280,
737+
value=config['window_w'],
737738
info="Browser window width",
738739
)
739740
window_h = gr.Number(
740741
label="Window Height",
741-
value=1100,
742+
value=config['window_h'],
742743
info="Browser window height",
743744
)
744745

745746
save_recording_path = gr.Textbox(
746747
label="Recording Path",
747748
placeholder="e.g. ./tmp/record_videos",
748-
value="./tmp/record_videos",
749+
value=config['save_recording_path'],
749750
info="Path to save browser recordings",
750751
interactive=True, # Allow editing only if recording is enabled
751752
)
752753

753754
save_trace_path = gr.Textbox(
754755
label="Trace Path",
755756
placeholder="e.g. ./tmp/traces",
756-
value="./tmp/traces",
757+
value=config['save_trace_path'],
757758
info="Path to save Agent traces",
758759
interactive=True,
759760
)
760761

761762
save_agent_history_path = gr.Textbox(
762763
label="Agent History Save Path",
763764
placeholder="e.g., ./tmp/agent_history",
764-
value="./tmp/agent_history",
765+
value=config['save_agent_history_path'],
765766
info="Specify the directory where agent history should be saved.",
766767
interactive=True,
767768
)
@@ -771,7 +772,7 @@ def create_ui(theme_name="Ocean"):
771772
label="Task Description",
772773
lines=4,
773774
placeholder="Enter your task here...",
774-
value="go to google.com and type 'OpenAI' click search and give me the first url",
775+
value=config['task'],
775776
info="Describe what you want the agent to do",
776777
)
777778
add_infos = gr.Textbox(
@@ -791,7 +792,48 @@ def create_ui(theme_name="Ocean"):
791792
label="Live Browser View",
792793
)
793794

794-
with gr.TabItem("📊 Results", id=5):
795+
with gr.TabItem("📁 Configuration", id=5):
796+
with gr.Group():
797+
config_file_input = gr.File(
798+
label="Load Config File",
799+
file_types=[".pkl"],
800+
interactive=True
801+
)
802+
803+
load_config_button = gr.Button("Load Existing Config From File", variant="primary")
804+
save_config_button = gr.Button("Save Current Config", variant="primary")
805+
806+
config_status = gr.Textbox(
807+
label="Status",
808+
lines=2,
809+
interactive=False
810+
)
811+
812+
load_config_button.click(
813+
fn=update_ui_from_config,
814+
inputs=[config_file_input],
815+
outputs=[
816+
agent_type, max_steps, max_actions_per_step, use_vision, tool_call_in_content,
817+
llm_provider, llm_model_name, llm_temperature, llm_base_url, llm_api_key,
818+
use_own_browser, keep_browser_open, headless, disable_security, enable_recording,
819+
window_w, window_h, save_recording_path, save_trace_path, save_agent_history_path,
820+
task, config_status
821+
]
822+
)
823+
824+
save_config_button.click(
825+
fn=save_current_config,
826+
inputs=[
827+
agent_type, max_steps, max_actions_per_step, use_vision, tool_call_in_content,
828+
llm_provider, llm_model_name, llm_temperature, llm_base_url, llm_api_key,
829+
use_own_browser, keep_browser_open, headless, disable_security,
830+
enable_recording, window_w, window_h, save_recording_path, save_trace_path,
831+
save_agent_history_path, task,
832+
],
833+
outputs=[config_status]
834+
)
835+
836+
with gr.TabItem("📊 Results", id=6):
795837
with gr.Group():
796838

797839
recording_display = gr.Video(label="Latest Recording")
@@ -850,7 +892,7 @@ def create_ui(theme_name="Ocean"):
850892
],
851893
)
852894

853-
with gr.TabItem("🎥 Recordings", id=6):
895+
with gr.TabItem("🎥 Recordings", id=7):
854896
def list_recordings(save_recording_path):
855897
if not os.path.exists(save_recording_path):
856898
return []
@@ -871,7 +913,7 @@ def list_recordings(save_recording_path):
871913

872914
recordings_gallery = gr.Gallery(
873915
label="Recordings",
874-
value=list_recordings("./tmp/record_videos"),
916+
value=list_recordings(config['save_recording_path']),
875917
columns=3,
876918
height="auto",
877919
object_fit="contain"
@@ -911,7 +953,9 @@ def main():
911953
parser.add_argument("--dark-mode", action="store_true", help="Enable dark mode")
912954
args = parser.parse_args()
913955

914-
demo = create_ui(theme_name=args.theme)
956+
config_dict = default_config()
957+
958+
demo = create_ui(config_dict, theme_name=args.theme)
915959
demo.launch(server_name=args.ip, server_port=args.port)
916960

917961
if __name__ == '__main__':

0 commit comments

Comments
 (0)