mirror of
				https://github.com/xlang-ai/OSWorld.git
				synced 2024-04-29 12:26:03 +03:00 
			
		
		
		
	fixed bugs in server/main.py (_create_pywinauto_node and get_screen_size) finished migration of a few task configs to Windows fixed bug in python.py
		
			
				
	
	
		
			87 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import datetime
 | 
						|
import json
 | 
						|
import logging
 | 
						|
import os
 | 
						|
import sys
 | 
						|
import time
 | 
						|
import argparse
 | 
						|
from desktop_env.envs.desktop_env import DesktopEnv
 | 
						|
 | 
						|
#  Logger Configs {{{ # 
 | 
						|
logger = logging.getLogger()
 | 
						|
logger.setLevel(logging.DEBUG)
 | 
						|
 | 
						|
datetime_str: str = datetime.datetime.now().strftime("%Y%m%d@%H%M%S")
 | 
						|
 | 
						|
file_handler = logging.FileHandler(os.path.join("logs", "normal-{:}.log".format(datetime_str)), encoding="utf-8")
 | 
						|
debug_handler = logging.FileHandler(os.path.join("logs", "debug-{:}.log".format(datetime_str)), encoding="utf-8")
 | 
						|
stdout_handler = logging.StreamHandler(sys.stdout)
 | 
						|
sdebug_handler = logging.FileHandler(os.path.join("logs", "sdebug-{:}.log".format(datetime_str)), encoding="utf-8")
 | 
						|
 | 
						|
file_handler.setLevel(logging.INFO)
 | 
						|
debug_handler.setLevel(logging.DEBUG)
 | 
						|
stdout_handler.setLevel(logging.INFO)
 | 
						|
sdebug_handler.setLevel(logging.DEBUG)
 | 
						|
 | 
						|
formatter = logging.Formatter(
 | 
						|
    fmt="\x1b[1;33m[%(asctime)s \x1b[31m%(levelname)s \x1b[32m%(module)s/%(lineno)d-%(processName)s\x1b[1;33m] \x1b[0m%(message)s")
 | 
						|
file_handler.setFormatter(formatter)
 | 
						|
debug_handler.setFormatter(formatter)
 | 
						|
stdout_handler.setFormatter(formatter)
 | 
						|
sdebug_handler.setFormatter(formatter)
 | 
						|
 | 
						|
stdout_handler.addFilter(logging.Filter("desktopenv"))
 | 
						|
sdebug_handler.addFilter(logging.Filter("desktopenv"))
 | 
						|
 | 
						|
logger.addHandler(file_handler)
 | 
						|
logger.addHandler(debug_handler)
 | 
						|
logger.addHandler(stdout_handler)
 | 
						|
logger.addHandler(sdebug_handler)
 | 
						|
#  }}} Logger Configs # 
 | 
						|
 | 
						|
logger = logging.getLogger("desktopenv.main")
 | 
						|
 | 
						|
 | 
						|
def human_agent():
 | 
						|
    """
 | 
						|
    Runs the Gym environment with human input.
 | 
						|
    """
 | 
						|
    parser = argparse.ArgumentParser()
 | 
						|
    parser.add_argument('-p', '--path', type=str, default=r"C:\Users\tianbaox\Documents\Virtual Machines\Ubuntu3\Ubuntu3.vmx", help="Path to the virtual machine .vmx file.")
 | 
						|
    parser.add_argument('-s', '--snapshot', type=str, default='init_state', help="Name of the snapshot to restore.")
 | 
						|
    parser.add_argument('-e', '--example', type=str, help="Path to the example json file.")
 | 
						|
    args = parser.parse_args(sys.argv[1:])
 | 
						|
 | 
						|
    example_path = args.example if args.example is not None and os.path.exists(args.example) else \
 | 
						|
        'evaluation_examples/examples/multi_apps/5990457f-2adb-467b-a4af-5c857c92d762.json'
 | 
						|
    with open(example_path, "r", encoding="utf-8") as f:
 | 
						|
        example = json.load(f)
 | 
						|
        if args.snapshot is not None:
 | 
						|
            example['snapshot'] = args.snapshot
 | 
						|
 | 
						|
    assert os.path.exists(args.path), "The specified path to the .vmx file does not exist."
 | 
						|
    env = DesktopEnv(
 | 
						|
        path_to_vm=args.path,
 | 
						|
        snapshot_name=args.snapshot,
 | 
						|
        action_space="computer_13"
 | 
						|
    )
 | 
						|
    # reset the environment to certain snapshot
 | 
						|
    observation = env.reset(task_config=example)
 | 
						|
    done = False
 | 
						|
    logger.info('\x1b[32m[TASK INSTRUCTION]: \x1b[32;3m%s\x1b[0m', example["instruction"])
 | 
						|
 | 
						|
    input("Press Enter to start human operation...")
 | 
						|
    human_start_time = time.time()
 | 
						|
    input("Press Enter to finish human operation.")
 | 
						|
    print("Time elapsed of human operation: %.2f" % (time.time() - human_start_time))
 | 
						|
 | 
						|
    result = env.evaluate()
 | 
						|
    logger.info("Result: %.2f", result)
 | 
						|
 | 
						|
    # env.close()
 | 
						|
    logger.info("Environment closed.")
 | 
						|
 | 
						|
 | 
						|
if __name__ == "__main__":
 | 
						|
    human_agent()
 |