Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,24 @@
|
|
1 |
-
import re, subprocess, openai, random,
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import streamlit as st
|
5 |
from streamlit_chat import message
|
6 |
|
7 |
# some helper vars and functions
|
|
|
8 |
if 'displayChat' not in st.session_state:
|
9 |
with st.spinner('Setting up plugins...'):
|
10 |
os.system('mkdir files')
|
11 |
os.system('mkdir plugins')
|
12 |
googleS = requests.get('https://raw.githubusercontent.com/d3n7/GPT-4-Unlimited-Tools/main/plugins/google.py').content
|
13 |
-
googlePath = os.path.join(
|
14 |
with open(googlePath, 'wb') as f:
|
15 |
f.write(googleS)
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
if 'followup' not in st.session_state:
|
19 |
st.session_state['followup'] = False
|
20 |
if 'followupPrompt' not in st.session_state:
|
@@ -27,7 +31,9 @@ if 'history' not in st.session_state:
|
|
27 |
st.session_state['history'] = []
|
28 |
if 'running' not in st.session_state:
|
29 |
st.session_state['running'] = False
|
30 |
-
|
|
|
|
|
31 |
sysPrompt = 'You now have access to some commands to help complete the user\'s request. You are able to access the user\'s machine with these commands. In every message you send, include "COMMAND: " with your command at the end. Here is a list of commands with explanations of how they are used:\n{}\n When you use a command, the user will respond with "Response: " followed by the output of the commmand. Use this output to help the user complete their request.'
|
32 |
|
33 |
def formatTable(table):
|
@@ -43,14 +49,33 @@ st.markdown('Made by [d3nt](https://github.com/d3n7) to give GPT-4 access to any
|
|
43 |
#User inputs
|
44 |
st.markdown('### OpenAI Settings')
|
45 |
openaikey = st.text_input('OpenAI API Key', type='password')
|
|
|
46 |
modelV = st.selectbox('Model', ('GPT-4', 'GPT-3.5-Turbo'))
|
47 |
st.markdown('### Editable Knowledge Base\nDelete any commands will not need to save tokens and increase accuracy.\n\nBe careful with the Raw Translation column. This is code that gets executed by your machine.')
|
48 |
-
d = {'GPT Commands': ['GOOGLE("question")', 'PYTHON(script.py)', 'MAKEFILE("content\\nhere", filename.txt)', 'READFILE(filename.txt)', 'LISTFILES()'],
|
49 |
-
'GPT Explanations': ['Search Google with the given text and return the results', 'Run a python script with the given file name. Do not use quotes for the filename argument.', 'Make a file with the given content and file name.', 'Read the content of a given filename', 'List the files you have access to'],
|
50 |
-
'Raw Translation': ['python plugins/google.py {}', 'python files/{}', 'echo {} > files/{}', 'cat files/{}', 'ls files']
|
51 |
}
|
52 |
df = pd.DataFrame(data=d, dtype='string')
|
53 |
commandTable = st.experimental_data_editor(df, use_container_width=True, num_rows='dynamic')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
st.markdown('### Chat')
|
55 |
prompt = st.text_input('Message')
|
56 |
col1, col2, col3, col4 = st.columns(4)
|
@@ -77,6 +102,7 @@ def runCmd(flag):
|
|
77 |
with st.spinner('Running command \'' + st.session_state['command'] + '\''):
|
78 |
try:
|
79 |
p = subprocess.Popen(st.session_state['command'], shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
|
|
|
80 |
st.session_state['followupPrompt'] = 'Response: ' + p.communicate()[0].decode("utf-8")
|
81 |
except subprocess.CalledProcessError as e:
|
82 |
st.session_state['followupPrompt'] = 'Response: ' + e.output.decode("utf-8")
|
@@ -88,6 +114,7 @@ def runCmd(flag):
|
|
88 |
|
89 |
if st.session_state['running']:
|
90 |
st.session_state['running'] = False
|
|
|
91 |
if prompt != '' and openaikey != '':
|
92 |
if (newSession or st.session_state['history'] == []) and (not st.session_state['followup']):
|
93 |
st.session_state['history'] = [{'role': 'system', 'content': sysPrompt.format(formatTable(commandTable))}]
|
@@ -103,8 +130,8 @@ if st.session_state['running']:
|
|
103 |
response = askGPT(prompt)
|
104 |
|
105 |
#parse GPT commands, possible recursion
|
106 |
-
if len(
|
107 |
-
cmd =
|
108 |
stem = ''
|
109 |
rawArgs = ''
|
110 |
cmdId = -1
|
@@ -174,12 +201,12 @@ if st.session_state['displayChat']:
|
|
174 |
if i['role'] == 'user':
|
175 |
if not showAll:
|
176 |
if 'Response:' not in i['content']:
|
177 |
-
message(i['content'], is_user=True)
|
178 |
else:
|
179 |
-
message(i['content'], is_user=True)
|
180 |
elif i['role'] == 'assistant':
|
181 |
if not showAll:
|
182 |
if 'COMMAND' not in i['content']:
|
183 |
-
message(i['content'])
|
184 |
else:
|
185 |
-
message(i['content'])
|
|
|
1 |
+
import re, os, subprocess, openai, random, regex, shutil
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import streamlit as st
|
5 |
from streamlit_chat import message
|
6 |
|
7 |
# some helper vars and functions
|
8 |
+
thispath = os.path.realpath(os.path.dirname(__file__)
|
9 |
if 'displayChat' not in st.session_state:
|
10 |
with st.spinner('Setting up plugins...'):
|
11 |
os.system('mkdir files')
|
12 |
os.system('mkdir plugins')
|
13 |
googleS = requests.get('https://raw.githubusercontent.com/d3n7/GPT-4-Unlimited-Tools/main/plugins/google.py').content
|
14 |
+
googlePath = os.path.join(thispath), 'plugins', 'google.py')
|
15 |
with open(googlePath, 'wb') as f:
|
16 |
f.write(googleS)
|
17 |
+
blipS = requests.get('https://raw.githubusercontent.com/d3n7/GPT-4-Unlimited-Tools/main/plugins/blip3.py').content
|
18 |
+
blipPath = os.path.join(thispath), 'plugins', 'blip2.py')
|
19 |
+
with open(blipPath, 'wb') as f:
|
20 |
+
f.write(blipS)
|
21 |
+
st.session_state['displayChat'] = False
|
22 |
if 'followup' not in st.session_state:
|
23 |
st.session_state['followup'] = False
|
24 |
if 'followupPrompt' not in st.session_state:
|
|
|
31 |
st.session_state['history'] = []
|
32 |
if 'running' not in st.session_state:
|
33 |
st.session_state['running'] = False
|
34 |
+
if 'downloadable' not in st.session_state:
|
35 |
+
st.session_state['downloadable'] = False
|
36 |
+
regx = [r"([A-Z]+\(((?:[^()\"']|(?:\"[^\"]*\")|(?:'[^']*')|\((?1)*\))*)\))", r'''(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\b[^,]+)'''] #parsing commands, parsing arguments
|
37 |
sysPrompt = 'You now have access to some commands to help complete the user\'s request. You are able to access the user\'s machine with these commands. In every message you send, include "COMMAND: " with your command at the end. Here is a list of commands with explanations of how they are used:\n{}\n When you use a command, the user will respond with "Response: " followed by the output of the commmand. Use this output to help the user complete their request.'
|
38 |
|
39 |
def formatTable(table):
|
|
|
49 |
#User inputs
|
50 |
st.markdown('### OpenAI Settings')
|
51 |
openaikey = st.text_input('OpenAI API Key', type='password')
|
52 |
+
replicatekey = st.text_input('(OPTIONAL) Replicate API Key', type='password')
|
53 |
modelV = st.selectbox('Model', ('GPT-4', 'GPT-3.5-Turbo'))
|
54 |
st.markdown('### Editable Knowledge Base\nDelete any commands will not need to save tokens and increase accuracy.\n\nBe careful with the Raw Translation column. This is code that gets executed by your machine.')
|
55 |
+
d = {'GPT Commands': ['GOOGLE("question")', 'PYTHON(script.py)', 'MAKEFILE("content\\nhere", filename.txt)', 'READFILE(filename.txt)', 'LISTFILES()', 'BLIP("What\'s in this image?", img1.jpg)'],
|
56 |
+
'GPT Explanations': ['Search Google with the given text and return the results', 'Run a python script with the given file name. Do not use quotes for the filename argument.', 'Make a file with the given content and file name.', 'Read the content of a given filename', 'List the files you have access to', 'Ask BLIP-2, a vision model, a given question about a given image'],
|
57 |
+
'Raw Translation': ['python plugins/google.py {}', 'python files/{}', 'echo {} > files/{}', 'cat files/{}', 'ls files', 'python plugins/blip2.py {} {}']
|
58 |
}
|
59 |
df = pd.DataFrame(data=d, dtype='string')
|
60 |
commandTable = st.experimental_data_editor(df, use_container_width=True, num_rows='dynamic')
|
61 |
+
cola, colb, colc = st.columns(3)
|
62 |
+
st.markdown('### Upload/Download')
|
63 |
+
uploadFile = st.file_uploader('')
|
64 |
+
with cola:
|
65 |
+
if st.button('Upload'):
|
66 |
+
if uploadFile:
|
67 |
+
fname = os.path.join(thispath, 'files', uploadFile.name.split('.')[-1])
|
68 |
+
with open(fname, 'wb') as f:
|
69 |
+
f.write(uploadFile.getbuffer())
|
70 |
+
st.write('Success')
|
71 |
+
with colb:
|
72 |
+
if st.button('Download'):
|
73 |
+
shutil.make_archive(os.join(thispath, 'files.zip'), 'zip', os.join(thispath, 'files'))
|
74 |
+
st.session_state['downloadable'] = True
|
75 |
+
with colc:
|
76 |
+
if st.session_state['downloadble']:
|
77 |
+
with open(os.path.join(thispath, 'files.zip'), 'rb') as f:
|
78 |
+
st.download_button('Download Zip', f, file_name='file.zip', key='filezip')
|
79 |
st.markdown('### Chat')
|
80 |
prompt = st.text_input('Message')
|
81 |
col1, col2, col3, col4 = st.columns(4)
|
|
|
102 |
with st.spinner('Running command \'' + st.session_state['command'] + '\''):
|
103 |
try:
|
104 |
p = subprocess.Popen(st.session_state['command'], shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
|
105 |
+
p.wait()
|
106 |
st.session_state['followupPrompt'] = 'Response: ' + p.communicate()[0].decode("utf-8")
|
107 |
except subprocess.CalledProcessError as e:
|
108 |
st.session_state['followupPrompt'] = 'Response: ' + e.output.decode("utf-8")
|
|
|
114 |
|
115 |
if st.session_state['running']:
|
116 |
st.session_state['running'] = False
|
117 |
+
os.environ['REPLICATE_API_TOKEN'] = replicatekey
|
118 |
if prompt != '' and openaikey != '':
|
119 |
if (newSession or st.session_state['history'] == []) and (not st.session_state['followup']):
|
120 |
st.session_state['history'] = [{'role': 'system', 'content': sysPrompt.format(formatTable(commandTable))}]
|
|
|
130 |
response = askGPT(prompt)
|
131 |
|
132 |
#parse GPT commands, possible recursion
|
133 |
+
if len(regex.findall(regx[0], response)) >= 1:
|
134 |
+
cmd = regex.findall(regx[0], response)[0][0]
|
135 |
stem = ''
|
136 |
rawArgs = ''
|
137 |
cmdId = -1
|
|
|
201 |
if i['role'] == 'user':
|
202 |
if not showAll:
|
203 |
if 'Response:' not in i['content']:
|
204 |
+
message(i['content'], is_user=True, key=random.randint(1,9000))
|
205 |
else:
|
206 |
+
message(i['content'], is_user=True, key=random.randint(1,9000))
|
207 |
elif i['role'] == 'assistant':
|
208 |
if not showAll:
|
209 |
if 'COMMAND' not in i['content']:
|
210 |
+
message(i['content'], key=random.randint(1,9000))
|
211 |
else:
|
212 |
+
message(i['content'], key=random.randint(1,9000))
|