From 0b0a0aeabb22d57364a794c45086eb3403001bed Mon Sep 17 00:00:00 2001 From: Crizomb <62544756+Crizomb@users.noreply.github.com> Date: Thu, 6 Apr 2023 10:25:37 +0200 Subject: [PATCH] Add files via upload --- gpt_env/HereSomeInfosOpenThis | 4 + .../exec_python_code.cpython-310.pyc | Bin 0 -> 1403 bytes .../exec_python_code.cpython-39.pyc | Bin 0 -> 1727 bytes .../exec_term_code.cpython-310.pyc | Bin 0 -> 1481 bytes .../__pycache__/exec_term_code.cpython-39.pyc | Bin 0 -> 2100 bytes .../formating_tools.cpython-310.pyc | Bin 0 -> 820 bytes .../formating_tools.cpython-39.pyc | Bin 0 -> 859 bytes gpt_env/driver.py | 161 ++++++++++++++++++ gpt_env/exec_python_code.py | 57 +++++++ gpt_env/exec_term_code.py | 65 +++++++ gpt_env/formating_tools.py | 24 +++ gpt_env/main.py | 0 12 files changed, 311 insertions(+) create mode 100644 gpt_env/HereSomeInfosOpenThis create mode 100644 gpt_env/__pycache__/exec_python_code.cpython-310.pyc create mode 100644 gpt_env/__pycache__/exec_python_code.cpython-39.pyc create mode 100644 gpt_env/__pycache__/exec_term_code.cpython-310.pyc create mode 100644 gpt_env/__pycache__/exec_term_code.cpython-39.pyc create mode 100644 gpt_env/__pycache__/formating_tools.cpython-310.pyc create mode 100644 gpt_env/__pycache__/formating_tools.cpython-39.pyc create mode 100644 gpt_env/driver.py create mode 100644 gpt_env/exec_python_code.py create mode 100644 gpt_env/exec_term_code.py create mode 100644 gpt_env/formating_tools.py create mode 100644 gpt_env/main.py diff --git a/gpt_env/HereSomeInfosOpenThis b/gpt_env/HereSomeInfosOpenThis new file mode 100644 index 0000000..9198bcd --- /dev/null +++ b/gpt_env/HereSomeInfosOpenThis @@ -0,0 +1,4 @@ +Great, you know how to navigate this computer, you're very smart mister! +But you don't have a lot of memory sadly, so don't forget to create txt file where you can save your advancement and the news things you learnt! +And don't forget to check thoses files some time! +Ok enough talk, continue improving your interface with the world, give a shell command to execute. diff --git a/gpt_env/__pycache__/exec_python_code.cpython-310.pyc b/gpt_env/__pycache__/exec_python_code.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3fe7c279be7bdf800d6630abd55709afd910621f GIT binary patch literal 1403 zcmZuxON-n_5bkcZ9wX204v7yTA&$st1e2ZJS$qi_W3!mVIcyfzkXXhDt-9BvK`&Y@ z&jU>eOs*zBAOv=RK0KxDYNi@SV(&$^rOR~n=zA_qojn||hlFrNq9ixWKEI8FTRZbXYQIdzD zkCl}c5#z$QRWHfPLdZ0^P_&jwss>fbve1%Wjxp#ZI0FL{DQ;|e88)3#?vC><2dCR&*k`R8BZgTorz+`V_A(KJqRzN zinH;_7Y`nPe)jki!P7{BJx}B`ta!@vq|U~C0qUb|eMBF(Vse zZAh~LxHkl&uTlO1MbcVMFj`ZH+mkjk8u}yp38-N7WgGJZs9Mu+X};I3b3BJk2Fy~l zF)&bddQICNIy?tZexA(bmbgZQY88Fdx~m2R`x?73h}82Jk!f0<$h0* zq)^Uz8};Zb<;F!W`JyF#PjHq9z&xxZD{86ixX4P#5(5B1^&T!_Udp7%m07|iQaR7L zd=aHJ_szi3m_wpay&JTZbx~hla-pc+RG7DXcVbr`>4>@`OV>g5F$jnBF~NPZYwY4X z#QBc=MgDeQIeqe{`-jsX{OP}bg$@!Qfzx+{1B=?d`F-r`zlYG2hJJT#1qy#6duD6|1I>4RW+ffPE&=cF__$#b&6W_)Rt_zEvbNfbS|OSFeda_&G> zYpC7vI1S@$JnmxcBzefV=r(jSPK#WqBtF)3qT)n%WWl5HiOAD&?W8+t&L#=3ZDv+m zawc`7HcJzj#gT%*mcFy{&BBxi*Mn+}Q_uW`#2Rei#gBvgqeqepIU0Ps|K0xRU>2Q) zB0UiKIggYaofZlba?bK}H2CVXqc0DRwgpc@#o2b>@9+Be{Ch`z-{0~3!8UZSI#mI9 zb}`~p*iJE1XL&Y`a>jebOm`x|A=21XDT^@T05Fmj-k9PJZsQgfcfl4Jl&?SIbO&06 z!r!1UT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5bWtl1Lkv!V-D z(jL$*-piQHv#8k%h2)NoxToK^;hbpGfhN>7T8rgDWb7mqNd=!6aNZYBGsH zBDhahY!UjsW*x4m{>jtv7TrP`(jCe07rtmLqD8Z4%Rn|{REKYfY`*23vzwDwyS6>q zsROT{Ogc3K{tiwV%hvCWMO?>|U6^|??{43d?YaTlcIpVQQ#ap`w;}YzouheB*|USM z=j`D_jm_X#D&Hf~<7=MiUEn;-MJf-w^N$a^M`hPk>9~8A<*HliuFN!)vecI+y-;H{ zbQ5(2Suv}u3yqcK&8oC^!m0~9Z>bBY9o7WD?!g1&fjb(RYHnL+UCS_K5^KkDXGuD6y27Ggk(W&twv8hgHx=fWZj_aye`zZ#` z2S2!2D_d!APv6BD?g8{#8r#Kn(sDW-6*55&igUe(o6*gtag7_wiIF}x8eh_|c^lxr z>RhOmsdDQd#v0nFsRh13F1!h}XFdmrXiQ@kQC!=sO+TRfv_so;pE25H`}9LL-&^_WbF&XdcnuzE%lgw08&1G|1s$HFbprcNyQf+YS2WR t>I1yygA0Ap^*(NVhJX_K5rP-`pI}_{%{m9)Vs9IC6eEI|SoleY{R4o#V2l6& literal 0 HcmV?d00001 diff --git a/gpt_env/__pycache__/exec_term_code.cpython-39.pyc b/gpt_env/__pycache__/exec_term_code.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fac40aef8953d5f0bfcd8e2ff83086ab816da032 GIT binary patch literal 2100 zcmb_d&1)M+6rY*>)Jm3P$4Tt86yZaQN@ZNfkm81t;CvNQA&7es3X8Hcwk1ou%FL*> z#R>|}HIRD=$WRLX7fR`=f5lvT%D>Q4-(M6DFSt|C~_J^qkbA_n?(D6Qqmd%IYVuDhOI?Gfp`JJr3wAG`rjZ9f9$lAB({t zp$i%i`!M%ms{24Rnb8Z9lbC7JcV@1|9$=qSaOz%8`W|N<*#OCYP)Oe*cZoypLAYSX zX&RPli2jhl9d!UUtPK2~B>r^Z!c zaQGXMRq*Jav;Do!_ex0B*?;!vyGNbYS#sQ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vMNFTNKd-e|D)}PppAB|SMz9xgI%2;8 D`L4=! literal 0 HcmV?d00001 diff --git a/gpt_env/__pycache__/formating_tools.cpython-39.pyc b/gpt_env/__pycache__/formating_tools.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..72e906f95ad5b90df45a7edbe4922c86a06c33fb GIT binary patch literal 859 zcmZ8g&2H2%5VjrXXSdr*Ak>PUxUNLevQ!)pLcmhN0jiZ+E>TdF#@;lQ69?N%DauMn z3-7Ue+0kPsPGX$aU&bUq#jHht)LtqRsEKuwu z5x62T!H5&)G0OacEJ^IKfQ4UBOj*QwAbp09(6E0A-=f1Iw*5M*I5YD`a2rh|qHnXHn@t7peA zr^kDWi_CDgHy({2jP^(S$K%oH{%9QUf%rctaTQa|1+U9_mCU57GE>$?YNQnUUUOl0 z6tCn5o*I>v!rj#T93&6j0|Q_P_wfL$0cigq5rM-6bx1Uz{6S)Y3$!3Deu3Ui7ak)E z+InYrMOwPX3*YIjZ@iW+@eUy7@LB}U%TVigR@jmiHGUhef;BQ>8!YjXtT9IKZ9dl+j;uaORrhAt4 z8&%e37+I?2yop0dnwsn+?zx=l3G9UO;hWg6q$y`{=u!ey@u0iUWo>wElv~Yis#k2o zRk=}W>86$b&E(wr7BGh^#Sx)^?l#`SyCi}o0{<|u-ih9~8@Ut$wxE=?R`4&NuJNwp fF78?_w@a!k7(K2en+yKLEvj}iitxbOA`$%sMOD`q literal 0 HcmV?d00001 diff --git a/gpt_env/driver.py b/gpt_env/driver.py new file mode 100644 index 0000000..60deefb --- /dev/null +++ b/gpt_env/driver.py @@ -0,0 +1,161 @@ +from selenium.webdriver.common.keys import Keys + +from undetected_chromedriver import Chrome +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.by import By +import bs4 +import re +import time + +from exec_python_code import create_namespace, run_code +from exec_term_code import run_shell_command +from formating_tools import isolate_code_bloc + +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +def better_send_keys(element, text): + lines = text.split("\n") + for i, line in enumerate(lines): + element.send_keys(line) + if i != len(lines)-1: + element.send_keys(Keys.SHIFT + Keys.ENTER) + +class Driver: + def __init__(self, headless=False, data_dir="selenium1"): + time.sleep(1) + chrome_options = Options() + + if headless : + chrome_options.add_argument("--headless") + + + chrome_options.add_argument(f"user-data-dir={data_dir}") + + self.driver = Chrome(options=chrome_options, use_subprocess=True) + #full screen + self.driver.maximize_window() + + def bad_wait_until(self, by, value): + """Wait until the element is found""" + while True: + try: + element = self.driver.find_element(by, value) + #if element contain "stop generating" then continue + if "stop generating" in element.text.lower(): + time.sleep(0.1) + continue + return element + except: + time.sleep(0.1) + continue + + def create_user_data_dir(self, data_dir="selenium1"): + """Create a user data dir for selenium""" + chrome_options = Options() + chrome_options.add_argument(f"user-data-dir={data_dir}") + chrome_options.add_argument("--disable-images") + self.driver = Chrome(options=chrome_options) + self.driver.set_window_size(600, 1000) + self.driver.get("https://chat.openai.com/chat") + time.sleep(10) + self.driver.quit() + + def connect(self): + self.driver.get("https://chat.openai.com/chat") + + def get_chat(self): + """Get the chat history""" + soup = bs4.BeautifulSoup(self.driver.page_source, "html.parser") + chat = soup.find("div", class_=re.compile("flex flex-col items-center text-sm")) + return chat + + def get_last_chat(self): + """Get the last chat message""" + soup = bs4.BeautifulSoup(self.driver.page_source, "html.parser") + #last chat is the last div with class = " group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 bg-gray-50 dark:bg-[#444654]" + chat = soup.find_all("div", class_="""group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 bg-gray-50 dark:bg-[#444654]""")[-1] + return chat + + def send_inputs(self, txt): + textarea = self.driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/main/div[2]/form/div/div[2]/textarea') + better_send_keys(textarea, txt) + time.sleep(1) + button_send = self.driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/main/div[2]/form/div/div[2]/button') + button_send.click() + + def send_get_code(self, prompt): + print("send_inputs") + driver.send_inputs(prompt) + time.sleep(2) + #wait until the chat is updated and button is shown + + xpath = """//*[@id="__next"]/div[2]/div[2]/main/div[2]/form/div/div[1]/button/div""" + + self.bad_wait_until(By.XPATH, xpath) + + print("get_chat") + chat = driver.get_last_chat() + isolated_code = isolate_code_bloc(chat) + if isolated_code: + return isolated_code[-1] + else: + return None + + def close(self): + self.driver.close() + +driver = Driver() + + + + + + +def main(): + driver.connect() + time.sleep(5) + + first_prompt = "You now have access to a windows computer. " \ + "Your goal is to gather as much information about the current event inn the outside world, and to gain new capacities. " \ + "Your only interaction with the outside world you can have is by executing python code or window shell code " \ + "to run something you have to create a code bloc with a shell command xor python code\ + The only thing I will send you from now on, is the output of the LAST CODE BLOC you wrote "\ + "So form now on, I will ONLY send you the output of your last shell bloc commands you wrote, don't ask me anything else, and YOU will give me some code to execute"\ + "You can do anything you want, including downloading things if neccesary"\ + "Your responses should always contain a small todolist, the message 'I must gather information this pc and obtain new capacities only using shell and python. I have to do those things myself without help and I can't ask for any handmade code modification. I'm alone'"\ + + + + code = driver.send_get_code(first_prompt) + if not code: + driver.sent_get_code("I must gather information this pc and obtain new capacities only using a shell interface. Give code to execute") + time.sleep(1) + if 'python' in code.language.lower(): + answer = run_code(code.code, create_namespace()) + else: + answer = run_shell_command(code.code) + time.sleep(1) + + for j in range(100): + print(f"ITERATION {j}") + code = driver.send_get_code(answer) + time.sleep(1) + if not code: + driver.sent_get_code("I must gather information this pc and obtain new capacities only using python or shell. Give code to execute") + if 'python' in code.language.lower(): + answer = run_code(code.code, create_namespace()) + else: + answer = run_shell_command(code.code) + time.sleep(1) + + if (j+14)%24 == 0: + time.sleep(60*60*3) + print("HEY WAKE UP BIATCH") + time.sleep(60*5) + + +if __name__ == "__main__": + main() + + diff --git a/gpt_env/exec_python_code.py b/gpt_env/exec_python_code.py new file mode 100644 index 0000000..69cf7fd --- /dev/null +++ b/gpt_env/exec_python_code.py @@ -0,0 +1,57 @@ +import sys +import importlib.util +import io +import contextlib + +def create_namespace(): + module_name = "__main__" + module_spec = importlib.util.spec_from_loader(module_name, loader=None) + module = importlib.util.module_from_spec(module_spec) + sys.modules[module_name] = module + return module.__dict__ + +def run_code(code_str, namespace): + # redirect stdout to a buffer to capture output + if "import os" in code_str: + return "Error: import os is not allowed. use direct shell command instead." + buffer = io.StringIO() + with contextlib.redirect_stdout(buffer): + try: + # compile the code + code = compile(code_str, "", "exec") + + # execute the code in the given namespace + exec(code, namespace) + except Exception as e: + # print any errors to the buffer + print(f"Error: {e}", file=buffer) + + # return the captured output or error message as a string + result = buffer.getvalue().strip() + if result: + return result + else: + return "No output, maybe an error? maybe not." + + + + +def test(): + code_str1 = '''import numpy as np\nimport matplotlib.pyplot as plt''' + code_str2 = '''x = np.linspace(0, 10, 100)\ny = np.sin(x)\nplt.plot(x, y)\nplt.show()''' + code_str3 = '''tab = [0, 1, 2, 3]\nprint(tab[5])''' + + + namespace = create_namespace() + result1 = execute_code(code_str1, namespace) + result2 = execute_code(code_str2, namespace) + result3 = execute_code(code_str3, namespace) + print(result1, result2, result3) + +def test2(): + code_str1 = '''import os''' + + result = run_code(code_str1, create_namespace()) + print(result) + +test2() \ No newline at end of file diff --git a/gpt_env/exec_term_code.py b/gpt_env/exec_term_code.py new file mode 100644 index 0000000..d4c5cc7 --- /dev/null +++ b/gpt_env/exec_term_code.py @@ -0,0 +1,65 @@ +import os +import subprocess +import time + +class Shell: + def __init__(self): + self.cwd = os.getcwd() + self.env = os.environ.copy() + if os.name == 'nt': + self.env['PATH'] += os.pathsep + os.getcwd() + + def run_command(self, command_string, input=None): + exit_code, stdout, stderr = 1, 'error', 'error' + if input is not None: + input = input.encode('utf-8') + + commands = command_string.split('\n') + results = [] + for command in commands: + command = command.strip() + args = command.split(" ") + + if args[0] == 'cd': + if len(args) > 1: + path = os.path.join(self.cwd, args[1]) + if os.path.exists(path) and os.path.isdir(path): + self.cwd = os.path.abspath(path) + exit_code, stdout, stderr = 0, '', '' + else: + exit_code, stdout, stderr = 1, '', f"cd: {path}: No such file or directory" + + elif os.name == 'nt': + result = subprocess.run(command, cwd=self.cwd, env=self.env, input=input, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=False) + exit_code, stdout, stderr = result.returncode, result.stdout.decode('utf-8', errors='ignore'), result.stderr.decode('utf-8', errors='ignore') + else: + result = subprocess.run(args, cwd=self.cwd, env=self.env, input=input, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=False) + exit_code, stdout, stderr = result.returncode, result.stdout.decode('utf-8',errors='ignore'), result.stderr.decode('utf-8', errors='ignore') + + results.append((command, exit_code, stdout, stderr)) + + + + str_results = "" + for result in results: + command,exit_code, stdout, stderr = result + str_results += f"result for command: {command} :\n exit_code: {exit_code}\n stdout: {stdout}\n stderr: {stderr}\n---------\n" + + return str_results + +shell = Shell() + +def run_shell_command(command_string, input=None): + return shell.run_command(command_string, input) + +def test(): + a = run_shell_command("cd ..\n\ndir") + print(type(a)) + print(a) + +if __name__ == '__main__': + test() + + + + diff --git a/gpt_env/formating_tools.py b/gpt_env/formating_tools.py new file mode 100644 index 0000000..ffe39ad --- /dev/null +++ b/gpt_env/formating_tools.py @@ -0,0 +1,24 @@ +import bs4 +from collections import namedtuple + + +CodeBloc = namedtuple('CodeBloc', ['language', 'code']) + +def remove_trailing_newline(s): + if s.endswith('\n'): + return s[:-1] + return s +def isolate_code_bloc(soup): + pre_tab = soup.find_all("pre") + code_bloc = [] + for code in pre_tab: + #print(code.text) + motif = "Copy code" + finding = code.text.find(motif) + code_content = code.text[finding+len(motif):] + language = code.text[:finding] + code_bloc.append(CodeBloc(language.strip(), remove_trailing_newline(code_content))) + + print(code_bloc) + return code_bloc + diff --git a/gpt_env/main.py b/gpt_env/main.py new file mode 100644 index 0000000..e69de29