Skip to content

Commit 9797e35

Browse files
committed
update frontend
1 parent daf3a53 commit 9797e35

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

gui/gui_experiment.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pathlib import Path
1515
from typing import Literal
1616

17-
from crab import AgentPolicy, Benchmark, Experiment, MessageType
17+
from crab import ActionOutput, AgentPolicy, Benchmark, Experiment, MessageType
1818

1919

2020
class GuiExperiment(Experiment):
@@ -54,23 +54,21 @@ def get_prompt(self):
5454

5555
def step(self, it) -> bool:
5656
if self.display_callback:
57-
self.display_callback(f"Step {self.step_cnt}:", "ai")
57+
self.display_callback("CRAB is Thinking...", "system")
5858

5959
prompt = self.get_prompt()
6060
self.log_prompt(prompt)
6161

6262
try:
6363
response = self.agent_policy.chat(prompt)
64-
if self.display_callback:
65-
self.display_callback(f"Planning next action...", "ai")
6664
except Exception as e:
6765
if self.display_callback:
68-
self.display_callback(f"Error: {str(e)}", "ai")
66+
self.display_callback(f"Error: {str(e)}", "error")
6967
self.write_main_csv_row("agent_exception")
7068
return True
71-
69+
7270
if self.display_callback:
73-
self.display_callback(f"Executing: {response}", "ai")
71+
self.display_callback(f"Acting: {response}", "action")
7472
return self.execute_action(response)
7573

7674
def execute_action(self, response: list[ActionOutput]) -> bool:
@@ -85,30 +83,25 @@ def execute_action(self, response: list[ActionOutput]) -> bool:
8583
if benchmark_result.terminated:
8684
if self.display_callback:
8785
self.display_callback(
88-
f"✓ Task completed! Results: {self.metrics}", "ai"
86+
f"✓ Task completed! Results: {self.metrics}", "system"
8987
)
9088
self.write_current_log_row(action)
9189
self.write_current_log_row(benchmark_result.info["terminate_reason"])
9290
return True
9391

9492
if self.display_callback:
95-
self.display_callback(
96-
f'Action "{action.name}" completed in {action.env}. '
97-
f"Progress: {self.metrics}", "ai"
98-
)
93+
self.display_callback("Action completed.\n>>>>>", "system")
9994
self.write_current_log_row(action)
10095
self.step_cnt += 1
10196
return False
10297

10398
def start_benchmark(self):
104-
if self.display_callback:
105-
self.display_callback("Starting benchmark...", "ai")
10699
try:
107100
super().start_benchmark()
108101
except KeyboardInterrupt:
109102
if self.display_callback:
110-
self.display_callback("Experiment interrupted.", "ai")
103+
self.display_callback("Experiment interrupted.", "error")
111104
self.write_main_csv_row("experiment_interrupted")
112105
finally:
113106
if self.display_callback:
114-
self.display_callback("Experiment finished.", "ai")
107+
self.display_callback("Experiment finished.", "error")

gui/main.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
1414
import warnings
1515
from pathlib import Path
16+
from typing import Literal
1617
from uuid import uuid4
1718

1819
import customtkinter as ctk
@@ -50,8 +51,10 @@ def get_model_instance(model_key: str):
5051

5152
def assign_task():
5253
task_description = input_entry.get()
54+
if not task_description.strip():
55+
return
5356
input_entry.delete(0, "end")
54-
display_message(task_description)
57+
display_message(task_description, "user")
5558

5659
try:
5760
model = get_model_instance(model_dropdown.get())
@@ -65,31 +68,44 @@ def assign_task():
6568
agent_policy=agent_policy,
6669
log_dir=log_dir,
6770
)
68-
71+
6972
experiment.set_display_callback(display_message)
7073

7174
def run_experiment():
7275
try:
7376
experiment.start_benchmark()
7477
except Exception as e:
75-
display_message(f"Error: {str(e)}", "ai")
78+
display_message(f"Error: {str(e)}", "error")
7679

7780
import threading
81+
7882
thread = threading.Thread(target=run_experiment, daemon=True)
7983
thread.start()
80-
84+
8185
except Exception as e:
82-
display_message(f"Error: {str(e)}", "ai")
86+
display_message(f"Error: {str(e)}", "error")
8387

8488

85-
def display_message(message, sender="user"):
89+
def display_message(
90+
message, category: Literal["system", "user", "action", "error"] = "system"
91+
):
8692
chat_display.configure(state="normal")
87-
if sender == "user":
88-
chat_display.insert("end", f"User: {message}\n", "user")
89-
else:
90-
chat_display.insert("end", f"AI: {message}\n", "ai")
91-
chat_display.tag_config("user", justify="left", foreground="blue")
92-
chat_display.tag_config("ai", justify="right", foreground="green")
93+
if category == "user":
94+
chat_display.insert("end", f"{message}\n", "user")
95+
elif category == "system":
96+
chat_display.insert("end", f"{message}\n", "system")
97+
elif category == "error":
98+
chat_display.insert("end", f"{message}\n", "error")
99+
elif category == "action":
100+
chat_display.insert("end", f"{message}\n", "action")
101+
chat_display.tag_config(
102+
"user", justify="right", foreground="lightblue", wrap="word"
103+
)
104+
chat_display.tag_config("system", justify="left", foreground="gray", wrap="word")
105+
chat_display.tag_config(
106+
"action", justify="left", foreground="lightgreen", wrap="word"
107+
)
108+
chat_display.tag_config("error", justify="left", foreground="red", wrap="word")
93109
chat_display.configure(state="disabled")
94110
chat_display.see("end")
95111
app.update_idletasks()
@@ -98,7 +114,7 @@ def display_message(message, sender="user"):
98114
if __name__ == "__main__":
99115
log_dir = (Path(__file__).parent / "logs").resolve()
100116

101-
ctk.set_appearance_mode("System")
117+
ctk.set_appearance_mode("dark")
102118
ctk.set_default_color_theme("blue")
103119

104120
app = ctk.CTk()
@@ -118,7 +134,7 @@ def display_message(message, sender="user"):
118134
model_dropdown.pack(pady=10, padx=10, fill="x")
119135

120136
chat_display_frame = ctk.CTkFrame(app, width=480, height=880)
121-
chat_display_frame.pack(pady=10, expand=True, fill="y")
137+
chat_display_frame.pack(pady=10, padx=10, expand=True, fill="both")
122138
chat_display = ctk.CTkTextbox(
123139
chat_display_frame, width=480, height=880, state="disabled", font=normal_font
124140
)

gui/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929

3030
_CACHED_HOST_OS = None
3131

32+
3233
def check_host_os() -> HostOS:
3334
global _CACHED_HOST_OS
3435

3536
if _CACHED_HOST_OS is None:
3637
import platform
38+
3739
host_os = platform.system().lower()
3840

3941
if host_os == "linux":
@@ -47,6 +49,7 @@ def check_host_os() -> HostOS:
4749

4850
return _CACHED_HOST_OS
4951

52+
5053
@evaluator(env_name="ubuntu")
5154
def empty_evaluator_linux() -> bool:
5255
return False

0 commit comments

Comments
 (0)