Skip to content

Commit 896e89a

Browse files
add selective import for osworld module and tests.
1 parent 4f14015 commit 896e89a

File tree

2 files changed

+225
-15
lines changed

2 files changed

+225
-15
lines changed

src/agentlab/benchmarks/osworld.py

Lines changed: 210 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import importlib.util
23
import json
34
import logging
45
import os
@@ -12,8 +13,6 @@
1213
import numpy as np
1314
from bgym import AbstractActionSet
1415
from dataclasses_json import DataClassJsonMixin
15-
from desktop_env.actions import KEYBOARD_KEYS, X_MAX, Y_MAX
16-
from desktop_env.desktop_env import DesktopEnv
1716
from PIL import Image
1817

1918
from agentlab.benchmarks.abstract_env import (
@@ -27,8 +26,212 @@
2726
tag_screenshot,
2827
)
2928

30-
logger = logging.getLogger(__name__)
29+
spec = importlib.util.find_spec("desktop_env")
30+
if spec is not None: # desktop_env is available
31+
from desktop_env.actions import KEYBOARD_KEYS, X_MAX, Y_MAX
32+
from desktop_env.desktop_env import DesktopEnv
33+
else:
34+
# If desktop_env is not available, set to None or default values
35+
DesktopEnv = None
36+
KEYBOARD_KEYS = [
37+
"\t",
38+
"\n",
39+
"\r",
40+
" ",
41+
"!",
42+
'"',
43+
"#",
44+
"$",
45+
"%",
46+
"&",
47+
"'",
48+
"(",
49+
")",
50+
"*",
51+
"+",
52+
",",
53+
"-",
54+
".",
55+
"/",
56+
"0",
57+
"1",
58+
"2",
59+
"3",
60+
"4",
61+
"5",
62+
"6",
63+
"7",
64+
"8",
65+
"9",
66+
":",
67+
";",
68+
"<",
69+
"=",
70+
">",
71+
"?",
72+
"@",
73+
"[",
74+
"\\",
75+
"]",
76+
"^",
77+
"_",
78+
"`",
79+
"a",
80+
"b",
81+
"c",
82+
"d",
83+
"e",
84+
"f",
85+
"g",
86+
"h",
87+
"i",
88+
"j",
89+
"k",
90+
"l",
91+
"m",
92+
"n",
93+
"o",
94+
"p",
95+
"q",
96+
"r",
97+
"s",
98+
"t",
99+
"u",
100+
"v",
101+
"w",
102+
"x",
103+
"y",
104+
"z",
105+
"{",
106+
"|",
107+
"}",
108+
"~",
109+
"accept",
110+
"add",
111+
"alt",
112+
"altleft",
113+
"altright",
114+
"apps",
115+
"backspace",
116+
"browserback",
117+
"browserfavorites",
118+
"browserforward",
119+
"browserhome",
120+
"browserrefresh",
121+
"browsersearch",
122+
"browserstop",
123+
"capslock",
124+
"clear",
125+
"convert",
126+
"ctrl",
127+
"ctrlleft",
128+
"ctrlright",
129+
"decimal",
130+
"del",
131+
"delete",
132+
"divide",
133+
"down",
134+
"end",
135+
"enter",
136+
"esc",
137+
"escape",
138+
"execute",
139+
"f1",
140+
"f10",
141+
"f11",
142+
"f12",
143+
"f13",
144+
"f14",
145+
"f15",
146+
"f16",
147+
"f17",
148+
"f18",
149+
"f19",
150+
"f2",
151+
"f20",
152+
"f21",
153+
"f22",
154+
"f23",
155+
"f24",
156+
"f3",
157+
"f4",
158+
"f5",
159+
"f6",
160+
"f7",
161+
"f8",
162+
"f9",
163+
"final",
164+
"fn",
165+
"hanguel",
166+
"hangul",
167+
"hanja",
168+
"help",
169+
"home",
170+
"insert",
171+
"junja",
172+
"kana",
173+
"kanji",
174+
"launchapp1",
175+
"launchapp2",
176+
"launchmail",
177+
"launchmediaselect",
178+
"left",
179+
"modechange",
180+
"multiply",
181+
"nexttrack",
182+
"nonconvert",
183+
"num0",
184+
"num1",
185+
"num2",
186+
"num3",
187+
"num4",
188+
"num5",
189+
"num6",
190+
"num7",
191+
"num8",
192+
"num9",
193+
"numlock",
194+
"pagedown",
195+
"pageup",
196+
"pause",
197+
"pgdn",
198+
"pgup",
199+
"playpause",
200+
"prevtrack",
201+
"print",
202+
"printscreen",
203+
"prntscrn",
204+
"prtsc",
205+
"prtscr",
206+
"return",
207+
"right",
208+
"scrolllock",
209+
"select",
210+
"separator",
211+
"shift",
212+
"shiftleft",
213+
"shiftright",
214+
"sleep",
215+
"stop",
216+
"subtract",
217+
"tab",
218+
"up",
219+
"volumedown",
220+
"volumemute",
221+
"volumeup",
222+
"win",
223+
"winleft",
224+
"winright",
225+
"yen",
226+
"command",
227+
"option",
228+
"optionleft",
229+
"optionright",
230+
]
231+
X_MAX = 1920
232+
Y_MAX = 1080
31233

234+
logger = logging.getLogger(__name__)
32235
COMPUTER_13_ACTIONS_OAI_CHATCOMPLETION_TOOLS = [
33236
{
34237
"type": "function",
@@ -359,6 +562,10 @@ def __init__(
359562
"os_type": os_type,
360563
"enable_proxy": enable_proxy,
361564
}
565+
if DesktopEnv is None:
566+
raise ImportError(
567+
"desktop_env is not installed. Please install it (use `make osworld`) to use OSWorld Gym."
568+
)
362569
self.env = DesktopEnv(
363570
action_space=action_space,
364571
provider_name=provider_name,

tests/benchmarks/test_osworld.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33
from unittest.mock import patch
44

55
import pytest
6+
import importlib.util
67

7-
# Check if desktop_env is available
8-
try:
9-
import desktop_env
10-
11-
DESKTOP_ENV_AVAILABLE = True
12-
except ImportError:
8+
spec = importlib.util.find_spec("desktop_env")
9+
if spec is None:
1310
DESKTOP_ENV_AVAILABLE = False
11+
OSWorldActionSet = None
12+
OsworldEnvArgs = None
13+
OsworldGym = None
14+
else:
15+
# If desktop_env is available, import the necessary classes
16+
from agentlab.benchmarks.osworld import (
17+
OSWorldActionSet,
18+
OsworldEnvArgs,
19+
OsworldGym,
20+
)
21+
DESKTOP_ENV_AVAILABLE = True
22+
1423

1524
# Skip the entire module if desktop_env is not available
1625
pytestmark = pytest.mark.skipif(not DESKTOP_ENV_AVAILABLE, reason="desktop_env not installed")
1726

18-
from agentlab.benchmarks.osworld import (
19-
OSWorldActionSet,
20-
OsworldEnvArgs,
21-
OsworldGym,
22-
)
23-
2427

2528
def mock_task_config() -> dict:
2629
"""Mock task configuration for testing."""

0 commit comments

Comments
 (0)