Test
Browse files- components.py +20 -15
components.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
|
2 |
import re
|
|
|
3 |
from abc import ABC, abstractmethod
|
4 |
from typing import Any, Dict, List, Optional, Union
|
5 |
|
@@ -55,14 +56,22 @@ class TaskComponent(Component, ABC):
|
|
55 |
self.input: gr.Textbox
|
56 |
|
57 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
@property
|
68 |
def n_inputs(self) -> int:
|
@@ -173,7 +182,6 @@ class CodeTask(TaskComponent):
|
|
173 |
|
174 |
@staticmethod
|
175 |
def generate_code(code_prompt: str):
|
176 |
-
import json
|
177 |
import traceback
|
178 |
|
179 |
raw_output = ""
|
@@ -202,7 +210,7 @@ class CodeTask(TaskComponent):
|
|
202 |
Write one python function for the request above.
|
203 |
Use pip packages where available.
|
204 |
For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
|
205 |
-
Include
|
206 |
Instead of printing or saving to disk, the function should return the data."""
|
207 |
)
|
208 |
with ThreadPoolExecutor() as executor:
|
@@ -215,6 +223,7 @@ Instead of printing or saving to disk, the function should return the data."""
|
|
215 |
|
216 |
Find the pip packages that need to be installed and get their corresponsing names in pip.
|
217 |
Package names in the imports and in pip might be different. Use the correct pip names.
|
|
|
218 |
|
219 |
Put them in a valid JSON:
|
220 |
```
|
@@ -280,10 +289,6 @@ Extract it. Remove anything after the function definition.""",
|
|
280 |
|
281 |
if len(inspect.getfullargspec(self._toolkit_func)[0]) > 0:
|
282 |
formatted_input = self.format_input(input, vars_in_scope)
|
283 |
-
try:
|
284 |
-
formatted_input = eval(formatted_input)
|
285 |
-
except:
|
286 |
-
pass
|
287 |
if formatted_input:
|
288 |
return self._toolkit_func(formatted_input)
|
289 |
return None
|
|
|
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
|
6 |
|
|
|
56 |
self.input: gr.Textbox
|
57 |
|
58 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
59 |
+
try:
|
60 |
+
return json.loads(input)
|
61 |
+
except:
|
62 |
+
input = input.strip()
|
63 |
+
prompt_vars = [v for v in re.findall("{(.*)}", input)]
|
64 |
+
undefined_vars = prompt_vars - vars_in_scope.keys()
|
65 |
+
if len(undefined_vars) > 0:
|
66 |
+
raise KeyError(
|
67 |
+
f"The variables :: {undefined_vars} are being used before being defined."
|
68 |
+
)
|
69 |
+
formatted_input = input.format(**vars_in_scope)
|
70 |
+
|
71 |
+
try:
|
72 |
+
return eval(formatted_input)
|
73 |
+
except:
|
74 |
+
return formatted_input
|
75 |
|
76 |
@property
|
77 |
def n_inputs(self) -> int:
|
|
|
182 |
|
183 |
@staticmethod
|
184 |
def generate_code(code_prompt: str):
|
|
|
185 |
import traceback
|
186 |
|
187 |
raw_output = ""
|
|
|
210 |
Write one python function for the request above.
|
211 |
Use pip packages where available.
|
212 |
For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
|
213 |
+
Include the necessary imports.
|
214 |
Instead of printing or saving to disk, the function should return the data."""
|
215 |
)
|
216 |
with ThreadPoolExecutor() as executor:
|
|
|
223 |
|
224 |
Find the pip packages that need to be installed and get their corresponsing names in pip.
|
225 |
Package names in the imports and in pip might be different. Use the correct pip names.
|
226 |
+
Include only the packages that need to be installed with pip.
|
227 |
|
228 |
Put them in a valid JSON:
|
229 |
```
|
|
|
289 |
|
290 |
if len(inspect.getfullargspec(self._toolkit_func)[0]) > 0:
|
291 |
formatted_input = self.format_input(input, vars_in_scope)
|
|
|
|
|
|
|
|
|
292 |
if formatted_input:
|
293 |
return self._toolkit_func(formatted_input)
|
294 |
return None
|