lgaleana commited on
Commit
bdf4e20
1 Parent(s): f012ade

Amends to code execution

Browse files
Files changed (3) hide show
  1. actions.py +1 -1
  2. app.py +2 -3
  3. components.py +32 -26
actions.py CHANGED
@@ -91,7 +91,7 @@ def execute_task(task_id: int, active_index: int, error_value, *args):
91
  import traceback
92
 
93
  traceback.print_exc()
94
- outputs[active_index] = "ERROR"
95
  return outputs + [
96
  gr.HighlightedText.update(
97
  value=[(f"Error in Task {task_id} :: {e}", "ERROR")],
 
91
  import traceback
92
 
93
  traceback.print_exc()
94
+ outputs[active_index] = f"ERROR :: {e}"
95
  return outputs + [
96
  gr.HighlightedText.update(
97
  value=[(f"Error in Task {task_id} :: {e}", "ERROR")],
app.py CHANGED
@@ -14,9 +14,8 @@ with gr.Blocks() as demo:
14
  <br>There are 2 types of tasks.
15
  <br>
16
  <br>**AI Task**: Ask ChatGPT to do something for you. Eg, summarize a text.
17
- <br>**Code Task**: ChatGPT will create a python function to do something for you. Eg, get the text from a website.
18
- <br> Use this task for things that ChatGPT can't do on its own, like access the internet or iterate over 4K+ tokens.
19
- <br> The code must be generated before executing the tasks.
20
  <br>
21
  <br>Output from previous tasks can be referenced in subsequen tasks with {tn}. Max 10 tasks allowed (for now).
22
  """
 
14
  <br>There are 2 types of tasks.
15
  <br>
16
  <br>**AI Task**: Ask ChatGPT to do something for you. Eg, summarize a text.
17
+ <br>**Code Task**: You will need code to do certain things that ChatGPT can't do, like access the internet or iterate over 4k+ tokens.
18
+ <br> With this task, ChatGPT will generate code for you that can be executed on the fly. The code must be generated before executing the task.
 
19
  <br>
20
  <br>Output from previous tasks can be referenced in subsequen tasks with {tn}. Max 10 tasks allowed (for now).
21
  """
components.py CHANGED
@@ -1,5 +1,6 @@
1
  import json
2
  import re
 
3
  from concurrent.futures import ThreadPoolExecutor
4
  from abc import ABC, abstractmethod
5
  from typing import Any, Dict, List, Optional, Union
@@ -65,7 +66,7 @@ class TaskComponent(Component, ABC):
65
  undefined_vars = prompt_vars - vars_in_scope.keys()
66
  if len(undefined_vars) > 0:
67
  raise KeyError(
68
- f"The variables :: {undefined_vars} are being used before being defined."
69
  )
70
  return input.format(**vars_in_scope)
71
 
@@ -127,7 +128,7 @@ class CodeTask(TaskComponent):
127
  with gr.Row():
128
  with gr.Column():
129
  self.code_prompt = gr.Textbox(
130
- label="What would you like to do?",
131
  interactive=True,
132
  value=self._initial_code_value,
133
  lines=3,
@@ -159,7 +160,7 @@ class CodeTask(TaskComponent):
159
  with gr.Column():
160
  self.output = gr.Textbox(
161
  label=f"Output: {{{self.vname}{self._id}}}",
162
- lines=13,
163
  interactive=True,
164
  )
165
 
@@ -179,8 +180,6 @@ class CodeTask(TaskComponent):
179
 
180
  @staticmethod
181
  def generate_code(code_prompt: str):
182
- import traceback
183
-
184
  raw_output = ""
185
  packages = ""
186
  script = ""
@@ -202,9 +201,10 @@ class CodeTask(TaskComponent):
202
  print(f"Generating code.")
203
  try:
204
  raw_output = llm_call(
205
- f"""{code_prompt}
 
206
 
207
- Write one python function for the request above.
208
  Use pip packages where available.
209
  For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
210
  Include the necessary imports.
@@ -262,40 +262,46 @@ Extract it. Remove anything after the function definition.""",
262
  return [self.packages, self.script, self.input]
263
 
264
  def execute(
265
- self, packages: str, function: str, input: str, vars_in_scope: Dict[str, Any]
266
  ):
267
- if not function:
268
  return None
269
- function = function.strip()
 
 
270
 
271
  import inspect
272
  import subprocess
273
  import sys
274
 
 
 
 
 
 
 
 
 
 
 
275
  for p in eval(packages):
276
  subprocess.check_call([sys.executable, "-m", "pip", "install", p])
277
 
278
- function = f"import os\nos.environ = {{}}\n\n{function}"
279
- exec(function, locals())
280
 
281
- for var in reversed(list(locals().values())):
 
 
 
 
282
  # Try to run all local functions
283
  if callable(var):
284
- print(var)
285
- _toolkit_func = var
286
  try:
287
- if len(inspect.getfullargspec(_toolkit_func)[0]) > 0:
288
- formatted_input = self.format_input(input, vars_in_scope)
289
- if formatted_input:
290
- try:
291
- return _toolkit_func(eval(formatted_input))
292
- except:
293
- return _toolkit_func(formatted_input)
294
- return None # No input, so it doesn't run
295
- return _toolkit_func()
296
  except:
297
- pass
298
- raise RuntimeError("Unable to run the code")
299
 
300
 
301
  class Task(Component):
 
1
  import json
2
  import re
3
+ import traceback
4
  from concurrent.futures import ThreadPoolExecutor
5
  from abc import ABC, abstractmethod
6
  from typing import Any, Dict, List, Optional, Union
 
66
  undefined_vars = prompt_vars - vars_in_scope.keys()
67
  if len(undefined_vars) > 0:
68
  raise KeyError(
69
+ f"The variables :: {undefined_vars} in task :: {self._id} are being used before being defined."
70
  )
71
  return input.format(**vars_in_scope)
72
 
 
128
  with gr.Row():
129
  with gr.Column():
130
  self.code_prompt = gr.Textbox(
131
+ label="What would you like the code to do?",
132
  interactive=True,
133
  value=self._initial_code_value,
134
  lines=3,
 
160
  with gr.Column():
161
  self.output = gr.Textbox(
162
  label=f"Output: {{{self.vname}{self._id}}}",
163
+ lines=14,
164
  interactive=True,
165
  )
166
 
 
180
 
181
  @staticmethod
182
  def generate_code(code_prompt: str):
 
 
183
  raw_output = ""
184
  packages = ""
185
  script = ""
 
201
  print(f"Generating code.")
202
  try:
203
  raw_output = llm_call(
204
+ f"""I want you to write a python function to:
205
+ {code_prompt}
206
 
207
+ Name the function toolkit.
208
  Use pip packages where available.
209
  For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
210
  Include the necessary imports.
 
262
  return [self.packages, self.script, self.input]
263
 
264
  def execute(
265
+ self, packages: str, script: str, input: str, vars_in_scope: Dict[str, Any]
266
  ):
267
+ if not script:
268
  return None
269
+ script = script.strip()
270
+
271
+ formatted_input = self.format_input(input, vars_in_scope)
272
 
273
  import inspect
274
  import subprocess
275
  import sys
276
 
277
+ def run(toolkit_func):
278
+ if len(inspect.getfullargspec(toolkit_func)[0]) > 0:
279
+ if formatted_input:
280
+ try:
281
+ return toolkit_func(eval(formatted_input))
282
+ except:
283
+ return toolkit_func(formatted_input)
284
+ raise ValueError(f"Code for task :: {self._id} needs an input.")
285
+ return toolkit_func()
286
+
287
  for p in eval(packages):
288
  subprocess.check_call([sys.executable, "-m", "pip", "install", p])
289
 
290
+ script = f"import os\nos.environ = {{}}\n\n{script}"
291
+ exec(script, locals())
292
 
293
+ locals_ = locals()
294
+ if "toolkit" in locals_:
295
+ toolkit_func = locals_["toolkit"]
296
+ return run(toolkit_func)
297
+ for var in reversed(locals_.values()):
298
  # Try to run all local functions
299
  if callable(var):
 
 
300
  try:
301
+ return run(var)
 
 
 
 
 
 
 
 
302
  except:
303
+ continue
304
+ raise RuntimeError(f"Unable to run the code for task :: {self._id}")
305
 
306
 
307
  class Task(Component):