SIGN IN SIGN UP

A framework to enable multimodal models to operate a computer.

0 0 0 Python
import pyautogui
import platform
import time
import math
2024-01-07 07:06:52 -08:00
from operate.utils.misc import convert_percent_to_decimal
2024-01-12 16:00:34 -08:00
class OperatingSystem:
def write(self, content):
try:
content = content.replace("\\n", "\n")
for char in content:
pyautogui.write(char)
except Exception as e:
print("[OperatingSystem][write] error:", e)
2024-01-12 16:00:34 -08:00
def press(self, keys):
try:
for key in keys:
pyautogui.keyDown(key)
time.sleep(0.1)
for key in keys:
pyautogui.keyUp(key)
except Exception as e:
print("[OperatingSystem][press] error:", e)
2024-01-12 16:00:34 -08:00
def mouse(self, click_detail):
try:
x = convert_percent_to_decimal(click_detail.get("x"))
y = convert_percent_to_decimal(click_detail.get("y"))
if click_detail and isinstance(x, float) and isinstance(y, float):
self.click_at_percentage(x, y)
2024-01-12 16:00:34 -08:00
except Exception as e:
print("[OperatingSystem][mouse] error:", e)
2024-01-07 08:44:04 -08:00
def click_at_percentage(
2024-01-12 16:00:34 -08:00
self,
x_percentage,
y_percentage,
duration=0.2,
circle_radius=50,
circle_duration=0.5,
):
try:
screen_width, screen_height = pyautogui.size()
x_pixel = int(screen_width * float(x_percentage))
y_pixel = int(screen_height * float(y_percentage))
2024-01-12 16:00:34 -08:00
pyautogui.moveTo(x_pixel, y_pixel, duration=duration)
2024-01-12 16:00:34 -08:00
start_time = time.time()
while time.time() - start_time < circle_duration:
angle = ((time.time() - start_time) / circle_duration) * 2 * math.pi
x = x_pixel + math.cos(angle) * circle_radius
y = y_pixel + math.sin(angle) * circle_radius
pyautogui.moveTo(x, y, duration=0.1)
2024-01-12 16:00:34 -08:00
pyautogui.click(x_pixel, y_pixel)
except Exception as e:
print("[OperatingSystem][click_at_percentage] error:", e)