|
1 | 1 | import ast |
2 | 2 | import json |
3 | | -import os |
4 | | -import typing |
5 | 3 | from collections import defaultdict |
6 | | -from typing import Any, Callable, Dict, List |
7 | | - |
8 | | -from numpy import ndarray |
9 | | - |
10 | | -from amadeusgpt.config import Config |
11 | | -from amadeusgpt.implementation import AnimalBehaviorAnalysis |
| 4 | +from typing import Any, Callable |
12 | 5 | from amadeusgpt.utils import func2json |
13 | | - |
14 | | -required_classes = {"AnimalBehaviorAnalysis": AnimalBehaviorAnalysis, "Config": Config} |
15 | | -required_types = { |
16 | | - name: getattr(typing, name) for name in dir(typing) if not name.startswith("_") |
17 | | -} |
18 | | -required_types.update({"ndarray": ndarray}) |
19 | | - |
20 | | - |
21 | 6 | class TaskProgram: |
22 | 7 | """ |
23 | 8 | The task program in the system should be uniquely tracked by the id |
@@ -46,9 +31,7 @@ def task_program_name(config) -> List[BaseEvent]: |
46 | 31 | __call__(): should take the context and run the program in a sandbox. |
47 | 32 | In the future we use docker container to run it |
48 | 33 |
|
49 | | - """ |
50 | | - |
51 | | - exec_namespace = None |
| 34 | + """ |
52 | 35 | cache = defaultdict(dict) |
53 | 36 |
|
54 | 37 | def __init__( |
@@ -76,10 +59,17 @@ def __init__( |
76 | 59 | self.json_obj["parents"] = parents |
77 | 60 | self.json_obj["mutation_from"] = mutation_from |
78 | 61 | self.json_obj["generation"] = generation |
| 62 | + |
79 | 63 |
|
80 | 64 | def __setitem__(self, key, value): |
81 | 65 | self.json_obj[key] = value |
82 | 66 |
|
| 67 | + def display(self): |
| 68 | + print (self.json_obj['name'], |
| 69 | + self.json_obj['source_code'], |
| 70 | + self.json_obj['description']) |
| 71 | + |
| 72 | + |
83 | 73 | def __getitem__(self, key): |
84 | 74 | """ |
85 | 75 | { |
@@ -130,40 +120,15 @@ def deserialize(self): |
130 | 120 |
|
131 | 121 | def validate(self): |
132 | 122 | pass |
133 | | - |
134 | | - def __call__(self, config) -> Any: |
135 | | - namespace = TaskProgram.exec_namespace |
136 | | - function_name = self.json_obj["name"] |
137 | | - keypoint_file = config["keypoint_info"]["keypoint_file_path"] |
138 | | - keypoint_type = keypoint_file.split(".")[-1] |
139 | | - videoname = keypoint_file.split("/")[-1].replace(keypoint_type, "") |
140 | | - if self.json_obj["source_code"] is not None: |
141 | | - exec(self.json_obj["source_code"], namespace) |
142 | | - function = namespace[function_name] |
143 | | - else: |
144 | | - assert self.json_obj["func_pointer"] is not None |
145 | | - function = self.json_obj["func_pointer"] |
146 | | - namespace[function_name] = function |
147 | | - call_str = f"{function_name}(config)" |
148 | | - if videoname in TaskProgram.cache[function_name]: |
149 | | - return TaskProgram.cache[function_name][videoname] |
150 | | - else: |
151 | | - exec(f"result = {call_str}", namespace) |
152 | | - result = namespace["result"] |
153 | | - |
154 | | - return result |
155 | | - |
| 123 | + |
156 | 124 |
|
157 | 125 | class TaskProgramLibrary: |
158 | 126 | """ |
159 | 127 | Keep track of the task programs |
160 | 128 | There are following types of task programs: |
161 | 129 | 1) Custom task programs that are created by the user (can be loaded from disk) |
162 | 130 | 2) Task programs that are created by LLMs |
163 | | -
|
164 | | -
|
165 | 131 | """ |
166 | | - |
167 | 132 | LIBRARY = {} |
168 | 133 |
|
169 | 134 | @classmethod |
@@ -217,13 +182,6 @@ def get_task_programs(cls): |
217 | 182 | """ |
218 | 183 | return cls.LIBRARY |
219 | 184 |
|
220 | | - @classmethod |
221 | | - def bind_exec_namespace(cls, exec_namespace): |
222 | | - """ |
223 | | - For task programs to execute, we need to bind the namespace |
224 | | - """ |
225 | | - TaskProgram.exec_namespace = exec_namespace |
226 | | - |
227 | 185 | @classmethod |
228 | 186 | def save(cls, out_path): |
229 | 187 | ret = [] |
|
0 commit comments