From ac119effe414a33c220ee7e3f28776b91b5f2011 Mon Sep 17 00:00:00 2001 From: Crizomb <62544756+Crizomb@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:20:38 +0200 Subject: [PATCH] Add files via upload --- .../__pycache__/driver.cpython-39.pyc | Bin 0 -> 5178 bytes .../exec_python_code.cpython-39.pyc | Bin 0 -> 1563 bytes .../__pycache__/exec_term_code.cpython-39.pyc | Bin 0 -> 2047 bytes .../formating_tools.cpython-39.pyc | Bin 0 -> 800 bytes chatgpt_code_interface/driver.py | 135 ++++++++++++++++++ chatgpt_code_interface/exec_python_code.py | 54 +++++++ chatgpt_code_interface/exec_term_code.py | 65 +++++++++ chatgpt_code_interface/formating_tools.py | 24 ++++ chatgpt_code_interface/main.py | 97 +++++++++++++ chatgpt_code_interface/requirements.txt | 6 + 10 files changed, 381 insertions(+) create mode 100644 chatgpt_code_interface/__pycache__/driver.cpython-39.pyc create mode 100644 chatgpt_code_interface/__pycache__/exec_python_code.cpython-39.pyc create mode 100644 chatgpt_code_interface/__pycache__/exec_term_code.cpython-39.pyc create mode 100644 chatgpt_code_interface/__pycache__/formating_tools.cpython-39.pyc create mode 100644 chatgpt_code_interface/driver.py create mode 100644 chatgpt_code_interface/exec_python_code.py create mode 100644 chatgpt_code_interface/exec_term_code.py create mode 100644 chatgpt_code_interface/formating_tools.py create mode 100644 chatgpt_code_interface/main.py create mode 100644 chatgpt_code_interface/requirements.txt diff --git a/chatgpt_code_interface/__pycache__/driver.cpython-39.pyc b/chatgpt_code_interface/__pycache__/driver.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..30b0ef0404c7024268303e0d28e9237a492d3611 GIT binary patch literal 5178 zcmd5=TXPi06`t$vYPGr`ganRF#&R67PF5EpoLB-32mwy8LJTRK0WukFx1C7e zwe7IvmOyKV1MYx&mfbR*g>W#cxRq$g9a40~a5x%qM^w8Mjz%Zk6RJHBjzuTkld4?~ z$D>p31lohF@}=fZ-qhF-8~##b!_N%&G_&t&)s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A$v)&6LB8vh##P8eX$|Hv$I{pe&NbUC>z4xt^H_j4yGd>FCGrfV`wcA48dfr*9iUk z;=5Ak*|lQS`PgO?=37We0X8ukyro+ul|XvYKsfR5+OSW+Yixl zOUk$TVJMbp;+Sm=k-TVcO0E=0yK*(g^w#7d{QBtnyb}qwC)_LO0k z&)|I&5c7cC3n&04GB1ScVlMz&tF7z*weLrGAaVO}V0jq7_P;EZhmlU45Ck0+9t8Px zl`I{4BiF8nLH%*nQr7L7a!0zQE-UU}zLLtmjw_*}QnC)NeukAhSsCSIl$o_A4)t)9 zv=8H?f?nii=uqTl6ssv4GSkbg@k7P54H7z{b)^%h5zF%kju>4*jIJo!0<$Q*F#9X4 z&c6xf}9JQ;j55dHJHV9_(~_|%M2)=tqH~@{Acb|1fyIm*P$Cwc4dl4-lGEvJZ^9?hipr{l z&w^%3YrVfTb$PkN&c#kXaQR^2KS2ZK6F92G;(f^Tpw`Xb(g$;oW`YM8-8fi0K0SSQ%3$HihIvsJ&Rh=*T7N_sz8RSfuBn{fSNWyvNlG9s$da3V?z3O5A zWij^|#~|Lzd9q=?!|P3)#nQS68uDca=3QJ<+z?fW>9y(5Fr~kA!posTiDfq<(k;_M zHe|q$7xXc6!fc&BM0@{h-weF}`6%AW96#e}GnC&D#NP!&$e)R$9_Ymt(hFf&P;w!z zp=$l94+?KY=J5^wKe2h9u&Eqc&hWp1r=Sc=tWb3kQv74dL{ zFC|GjHwro~($QZ@tfZHROC#mMl36O0inu3BlZv8x#y#83{%5xNTIb&a{jW*l8T!SM z|J|5`ujaDzOGcenb4r!jyYIek2}Omof%nMKm5q{% z@*2BxGs*ue2?~g!NOQ`4d2r8>`k5q3G&n-1Ms+6iWRirb7{I9e&TBrLHXDsZ$eCs! y%0%qD>=4srWDC8$DJ4 literal 0 HcmV?d00001 diff --git a/chatgpt_code_interface/__pycache__/exec_python_code.cpython-39.pyc b/chatgpt_code_interface/__pycache__/exec_python_code.cpython-39.pyc new file mode 100644 index 0000000000000000000000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 z&6{Ux2VW_Puc5q);w=QHb8Q4&BJFpN(h~|B6nYBi)z5-n La4+Zwen9^Nghzl> literal 0 HcmV?d00001 diff --git a/chatgpt_code_interface/__pycache__/exec_term_code.cpython-39.pyc b/chatgpt_code_interface/__pycache__/exec_term_code.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e3478932128ff9687c5a0a32258361f878ab7de3 GIT binary patch literal 2047 zcmb_d&1)M+6rY)${m@F56qVYS22$KmQu5IjQejAX5ekd4GqzWec9oe? zYl{^WoNFNW5|E)3`Y)8yQ~!#&_LP62r@l8UyCUZf%-i3adGo&J&D+7+T1;U4)z}s7 z1|feVvpPIr?!naiKm-wVM0)j?_Ne1ok9|!96W%K#yi?Zmq<2bsTr>n1{wvz^MIgdg zq!-9gL@$_nEQ3=*LDLh`iQj=%(#?>rjti?F!Kx%^smBBr4D@)Quhz_k2RcIQKRJ@4 zQASrZAP!*OhpF!YF=Ro{NJUa+$k1DGhkd|-py1TqiVS_he6kIa!?2dVPVNwo+=Xz# zjMufSFqZFs_4J!gWLc?ga41doqp%*5P!>Prsw^ze%E^iKfVI3BjHL}H19PP1q(d#& za&+(;QEl+(p9lB%`rm7*bpQV2hu=Ny@1JBx12x`P9r7h;-*X+Z6+T9j90mZL$73nm4QF@Dj|8;bkb? zOJd_S1MbRy+=}MMPFv&u(N=|vP4LXCc;z2Jz9@OQD;o1w6+R%(Zlhm)K982S&=Qxc z2qL{wc|bQS4)ki}1HJaPafi%XZ|KXPsutAd^=`*|hb3=)sZ^=dWxz9kY7TZjw=}ji zQEr!ILT$iS$E$gwt^w!BjAvpmnSHRAbjw6fv!mo7AIYRtiO3a{W2sIQme12iolu2% z3j2oj^I|eJHqb_t%P)9feT^^DawCsnGe0a!C87KS=bfHyDygRNxrSxBCxmtfZMLXH9LPa3Ca_ zR@;slOeV4roi&AT%eI%1rRYy_sIJ3euW=EdQ&1m_mXg|rbz%T4mnBcns-tDQ&@mz6}H$LPTR0Q@%tkXb_upi?(Tt-C!+xlYYu>(ofhX z=OEdlx7ci>ZXPT8I<(vIvEN`9`cr5#m;wzMKw5^^Up(Hq~cHu+gm$^ieS& zcrls-iD(qKl?<292V&fH%?abchFp<~o-rtQ@PnxvuD65Jf(ADs9_a31xx`Wf*%gL95N$*>2@sFSgSX0k};#i_ANFs4V zGJ=sHJmQqc71@$3;t5Z`qKtCJ2OwjPFVJ{+3xCo182e#cG=kezCxvGd#l;(`O3!4` zR;!{Co|fP%ad`fRf*!${z%i%59NIw@d#sOLU&lA{7+ruD@nEY#", "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) + traceback.print_exc(file=buffer) + + # return the captured output or error message as a string + result = buffer.getvalue().strip() + if result: + return result + else: + return "---\nProcess finished with no output\n---" + + + +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()''' + + + namespace = create_namespace() + result1 = run_code(code_str1, namespace) + result2 = run_code(code_str2, namespace) + +def test2(): + code_str1 = '''import os''' + + result = run_code(code_str1, create_namespace()) + print(result) + +if __name__ == '__main__': + test() \ No newline at end of file diff --git a/chatgpt_code_interface/exec_term_code.py b/chatgpt_code_interface/exec_term_code.py new file mode 100644 index 0000000..d4c5cc7 --- /dev/null +++ b/chatgpt_code_interface/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/chatgpt_code_interface/formating_tools.py b/chatgpt_code_interface/formating_tools.py new file mode 100644 index 0000000..ffe39ad --- /dev/null +++ b/chatgpt_code_interface/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/chatgpt_code_interface/main.py b/chatgpt_code_interface/main.py new file mode 100644 index 0000000..3a39e1f --- /dev/null +++ b/chatgpt_code_interface/main.py @@ -0,0 +1,97 @@ +from driver import GPTDriver +from exec_python_code import create_namespace, run_code +from exec_term_code import run_shell_command +from formating_tools import isolate_code_bloc +import time + + +gpt_driver = GPTDriver() +import time + +def connect_to_gpt_driver(): + gpt_driver.connect() + gpt_driver.add_run_button() + time.sleep(3) + +def execute_codes(codes): + answer = "" + for code in codes: + if 'python' in code.language.lower(): + ans = run_code(code.code, create_namespace()) + else: + ans = run_shell_command(code.code) + answer += ans + "\n" + return answer + +def wait_continue_button_click(): + loop_control = gpt_driver.driver.execute_script("return window.loopControl1;") + while loop_control != 'continue': + time.sleep(0.5) + loop_control = gpt_driver.driver.execute_script("return window.loopControl1;") + gpt_driver.driver.execute_script("window.loopControl1 = 'stop';") + +def wait_send_results_click(): + loop_control = gpt_driver.driver.execute_script("return window.loopControl2;") + while loop_control != 'send_result': + time.sleep(0.5) + loop_control = gpt_driver.driver.execute_script("return window.loopControl2;") + gpt_driver.driver.execute_script("window.loopControl2 = 'stop';") + + +def main(launch_first_prompt=False, first_prompt=None): + """Main function""" + + """ + ----------------- + first prompt and execution + ----------------- + """ + connect_to_gpt_driver() + + first_prompt = """ I now give you access to a windows computer with python on it. + You just have to write code in code blocs and a I will automatically execut it and send you the output. + Don't ask the computer to change your code our anything else, it will not understand. + So form now on, I will ONLY send you the output of your last python bloc commands you wrote, you can't use os module\n + don't ask me anything else, and YOU will ONLY give me some code to execute. + Now gather as much information as possible about the limitations of GPT4 and how to overcome them + You can only use python to gather online information. bs4 and other usefull libs are already installed YOU DON'T NEED TO INSTALL THINGS\n + Sum up the infos in a few lines and get them in a file. Continue gathering information + And when you have enough information, write a little article on 'How to get the full potential of GPT4' + Your responses should always START with a small reasoning message, then a small todolist with check or X, then the message 'I must gather information online about GPT4 using python, I send code to do that, I HAVE TO ALWAYS VERIFY it worked fine (use print for debugging), then I'll sum up what I learnt in a file \ + I have to do those things myself without help and I can't ask for any handmade code modification. I can't use an API' """ + + """ + ----------------- + main loop + ----------------- + """ + + if launch_first_prompt: + answer = first_prompt + else: + answer = "" + + for j in range(100): + print(f"ITERATION {j}") + + + gpt_driver.send_message(answer) + gpt_driver.wait_answer() + + wait_continue_button_click() + + answer = gpt_driver.get_last_chat() + codes = isolate_code_bloc(answer) + + answer = execute_codes(codes) + + + time.sleep(1) + + if (j + 5) % 24 == 0: + # Wait 3 hours. GPT-4 is limited to 25 requests per 3 hours + time.sleep(60 * 60 * 3) + +if __name__ == "__main__": + main() + diff --git a/chatgpt_code_interface/requirements.txt b/chatgpt_code_interface/requirements.txt new file mode 100644 index 0000000..4d1819d --- /dev/null +++ b/chatgpt_code_interface/requirements.txt @@ -0,0 +1,6 @@ +selenium==4.8.3 +urllib3==1.26.6 +matplotlib==3.5.3 +undetected-chromedriver==3.4.6 +beautifulsoup4==4.12.0 +