simplex 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of simplex might be problematic. Click here for more details.

simplex/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .simplex import Simplex
2
+
3
+ __version__ = "0.1.7"
4
+ __all__ = ["Simplex"]
simplex/constants.py ADDED
@@ -0,0 +1 @@
1
+ BASE_URL = "https://u3mvtbirxf.us-east-1.awsapprunner.com"
simplex/simplex.py ADDED
@@ -0,0 +1,187 @@
1
+ from playwright.sync_api import Page, sync_playwright
2
+ from PIL import Image
3
+ import requests
4
+ from typing import List
5
+ import io
6
+
7
+ from .utils import center_bbox, screenshot_to_image
8
+
9
+ BASE_URL = "https://u3mvtbirxf.us-east-1.awsapprunner.com"
10
+
11
+ class Simplex:
12
+ def __init__(self, api_key: str, driver: Page = None):
13
+ """
14
+ Initialize Simplex instance
15
+
16
+ Args:
17
+ api_key (str): API key for authentication
18
+ driver (playwright.sync_api.Page, optional): Playwright page object. If not provided,
19
+ a new headless browser instance will be created.
20
+ """
21
+ self.api_key = api_key
22
+
23
+ if driver is None:
24
+ self.playwright = sync_playwright().start()
25
+ self.browser = self.playwright.chromium.launch(headless=True)
26
+ self.driver = self.browser.new_page()
27
+ else:
28
+ self.driver = driver
29
+ self.browser = None
30
+ self.playwright = None
31
+
32
+ def __del__(self):
33
+ """Cleanup Playwright resources"""
34
+ if self.browser:
35
+ self.browser.close()
36
+ if self.playwright:
37
+ self.playwright.stop()
38
+
39
+ def find_element(self, element_description: str, state: Image.Image | None = None) -> List[int]:
40
+ """
41
+ Find an element in the screenshot using the element description
42
+
43
+ Args:
44
+ element_description (str): Description of the element to find
45
+ screenshot (PIL.Image.Image): Screenshot of the page
46
+
47
+ Returns:
48
+ bounding_box (tuple): [x1, y1, x2, y2] bounding box of the found element
49
+ """
50
+ if state is None:
51
+ state = self.take_stable_screenshot()
52
+
53
+ endpoint = f"{BASE_URL}/find-element"
54
+
55
+ # Convert PIL Image to bytes
56
+ img_byte_arr = io.BytesIO()
57
+ state.save(img_byte_arr, format='PNG')
58
+ img_byte_arr = img_byte_arr.getvalue()
59
+
60
+ # Prepare multipart form data
61
+ files = {
62
+ 'image_data': ('screenshot.png', img_byte_arr, 'image/png'),
63
+ 'element_description': (None, element_description),
64
+ 'api_key': (None, self.api_key)
65
+ }
66
+ # Make the request
67
+ response = requests.post(
68
+ endpoint,
69
+ files=files
70
+ )
71
+
72
+ # Print the results
73
+ print(f"Status Code: {response.status_code}")
74
+ if response.status_code == 200:
75
+ res = response.json()
76
+ bbox = [int(res['x1']), int(res['y1']), int(res['x2']), int(res['y2'])]
77
+ return bbox
78
+ else:
79
+ print("Error:", response.text)
80
+
81
+ def step_to_action(self, step_description: str, state: Image.Image | None = None) -> List[List[str]]:
82
+ """
83
+ Convert a step description to an action
84
+
85
+ Args:
86
+ step_description (str): Description of the step to convert to action
87
+ screenshot (PIL.Image.Image): Screenshot of the page
88
+
89
+ Returns:
90
+ action (List[List[str, str]]): List of actions to perform
91
+ """
92
+ if state is None:
93
+ state = self.take_stable_screenshot()
94
+
95
+ endpoint = f"{BASE_URL}/step_to_action"
96
+
97
+ # Convert PIL Image to bytes
98
+ img_byte_arr = io.BytesIO()
99
+ state.save(img_byte_arr, format='PNG')
100
+ img_byte_arr = img_byte_arr.getvalue()
101
+
102
+ # Prepare form data
103
+ files = {
104
+ 'image_data': ('screenshot.png', img_byte_arr, 'image/png'),
105
+ 'step': (None, step_description),
106
+ 'api_key': (None, self.api_key)
107
+ }
108
+
109
+ # Make the request
110
+ response = requests.post(
111
+ endpoint,
112
+ files=files
113
+ )
114
+
115
+ # Handle response
116
+ if response.status_code == 200:
117
+ res = response.json()
118
+ actions = res.split('\n')
119
+ actions = [action.split(',') for action in actions]
120
+ actions = [[action.strip() for action in action_pair] for action_pair in actions]
121
+ return actions
122
+ else:
123
+ print(f"Error: {response.status_code}")
124
+ print(response.text)
125
+ return []
126
+
127
+ def goto(self, url: str) -> None:
128
+ """
129
+ Navigate to a URL
130
+ """
131
+ self.driver.goto(url)
132
+
133
+ def execute_action(self, action: List[List[str]], state: Image.Image | None = None) -> None:
134
+ """
135
+ Execute an action with playwright driver
136
+
137
+ Args:
138
+ action (List[List[str]]): List of actions to perform
139
+ """
140
+ action_type, description = action
141
+ if state is None:
142
+ state = self.take_stable_screenshot()
143
+
144
+ try:
145
+ if action_type == "CLICK":
146
+ bbox = self.find_element(description, state)
147
+ center_x, center_y = center_bbox(bbox)
148
+ self.driver.mouse.click(center_x, center_y)
149
+
150
+ elif action_type == "HOVER":
151
+ bbox = self.find_element(description, state)
152
+ center_x, center_y = center_bbox(bbox)
153
+ self.driver.mouse.move(center_x, center_y)
154
+
155
+ elif action_type == "TYPE":
156
+ self.driver.keyboard.type(description)
157
+
158
+ elif action_type == "SCROLL":
159
+ self.driver.mouse.wheel(0, int(description))
160
+
161
+ elif action_type == "WAIT":
162
+ self.driver.wait_for_timeout(int(description))
163
+
164
+ except Exception as e:
165
+ print(f"Error executing action: {e}")
166
+ return None
167
+
168
+ def do(self, step_description: str) -> None:
169
+ """
170
+ Execute a step description
171
+ """
172
+ state = self.take_stable_screenshot()
173
+ actions = self.step_to_action(step_description, state)
174
+ for action in actions:
175
+ self.execute_action(action)
176
+
177
+ def take_stable_screenshot(self) -> Image.Image:
178
+ """
179
+ Take a screenshot after ensuring the page is in a stable state.
180
+
181
+ Returns:
182
+ PIL.Image.Image: Screenshot of the current page
183
+ """
184
+ self.driver.wait_for_load_state('networkidle')
185
+ return screenshot_to_image(self.driver.screenshot())
186
+
187
+
simplex/utils.py ADDED
@@ -0,0 +1,12 @@
1
+ from typing import List
2
+ from PIL import Image
3
+ import io
4
+ def center_bbox(bbox: List[int]) -> List[int]:
5
+ """
6
+ Calculate the center coordinates of a bounding box
7
+ """
8
+ return [(bbox[0] + bbox[2]) // 2, (bbox[1] + bbox[3]) // 2]
9
+
10
+
11
+ def screenshot_to_image(screenshot: bytes) -> Image:
12
+ return Image.open(io.BytesIO(screenshot))
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Simplex Labs, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.1
2
+ Name: simplex
3
+ Version: 1.0.0
4
+ Summary: Official Python SDK for Simplex API
5
+ Home-page: https://github.com/shreyka/simplex-python
6
+ Author: Simplex Labs, Inc.
7
+ Author-email: founders@simplex.sh
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Requires-Python: >=3.6
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: openai>=1.0.0
17
+ Requires-Dist: python-dotenv>=0.19.0
18
+ Requires-Dist: tiktoken>=0.5.0
19
+ Requires-Dist: click>=8.0.0
20
+ Requires-Dist: rich>=13.0.0
21
+ Requires-Dist: prompt_toolkit>=3.0.0
22
+ Requires-Dist: playwright>=1.0.0
23
+ Requires-Dist: Pillow>=9.0.0
24
+
25
+ # Simplex AI Python SDK
26
+
27
+ A Python SDK for Simplex AI that enables browser automation using natural language commands.
28
+
29
+ ## Installation
@@ -0,0 +1,10 @@
1
+ simplex/__init__.py,sha256=1mbM4XUk0FNW161WOkM4ayC1s_QSsaBEls6PZ0iBScY,74
2
+ simplex/constants.py,sha256=nIXF2oVNNNknXweXAlmE-KBM9QjJtYw9osXVYjvloN0,59
3
+ simplex/simplex.py,sha256=p3dGJFdyQReW2qHX-yFn9Vi6Bm8-QJ_x6RK66s1KkM8,6246
4
+ simplex/utils.py,sha256=UrD4Ena3yk0POmxxyiqMszzPbTscTCJpMP4xZFDAuOc,339
5
+ simplex-1.0.0.dist-info/LICENSE,sha256=Xh0SJjYZfNI71pCNMB40aKlBLLuOB0blx5xkTtufFNQ,1075
6
+ simplex-1.0.0.dist-info/METADATA,sha256=4BL_hKn_yaVMoCpGSsUxTdVwwb2trncuwu7QyEFyE_M,917
7
+ simplex-1.0.0.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
8
+ simplex-1.0.0.dist-info/entry_points.txt,sha256=3veL2w3c5vxb3dm8I_M8Fs-370n1ZnvD8uu1nSsL7z8,45
9
+ simplex-1.0.0.dist-info/top_level.txt,sha256=cbMH1bYpN0A3gP-ecibPRHasHoqB-01T_2BUFS8p0CE,8
10
+ simplex-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.7.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ simplex = simplex.cli:main
@@ -0,0 +1 @@
1
+ simplex