Skip to content

Commit 6bf31db

Browse files
committed
Made it easy to create a project
1 parent de22b8b commit 6bf31db

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

amadeusgpt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from amadeusgpt.integration_modules import *
1111
from amadeusgpt.main import AMADEUS
1212
from amadeusgpt.version import VERSION, __version__
13-
13+
from amadeusgpt.project import create_project
1414
params = {
1515
"axes.labelsize": 10,
1616
"legend.fontsize": 10,

amadeusgpt/main.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from amadeusgpt.config import Config
44
from amadeusgpt.programs.sandbox import Sandbox
5+
import yaml
56
##########
67
# all these are providing the customized classes for the code execution
78
##########
@@ -20,7 +21,7 @@
2021

2122

2223
class AMADEUS:
23-
def __init__(self, config: Config, use_vlm=True):
24+
def __init__(self, config: Config | dict, use_vlm=True):
2425
self.config = config
2526
### fields that decide the behavior of the application
2627
self.use_self_debug = True
@@ -42,6 +43,9 @@ def __init__(self, config: Config, use_vlm=True):
4243
video_suffix = data_info["video_suffix"]
4344
video_file_paths = glob.glob(str(data_folder / f"*{video_suffix}"))
4445

46+
if len(video_file_paths) == 0:
47+
print (f"No video files found in the data folder {data_folder}. Please check the data folder and the video suffix")
48+
return
4549
# optionally get the corresponding keypoint files
4650
keypoint_file_paths = self.get_DLC_keypoint_files(video_file_paths)
4751

@@ -114,7 +118,13 @@ def step(self, user_query: str) -> QA_Message:
114118
return qa_message
115119

116120
def get_video_file_paths(self) -> list[str]:
117-
return self.sandbox.video_file_paths
121+
data_info = self.config["data_info"]
122+
data_folder = data_info['data_folder']
123+
video_suffix = data_info['video_suffix']
124+
video_file_paths = glob.glob(os.path.join(data_folder, f"*{video_suffix}"))
125+
126+
return video_file_paths
127+
118128

119129
def get_keypoint_file_paths(self) -> list[str]:
120130
return self.sandbox.keypoint_file_paths
@@ -143,12 +153,11 @@ def register_task_program(self, task_program, creator="human"):
143153
TaskProgramLibrary.register_task_program(creator=creator)(task_program)
144154

145155
def get_messages(self):
146-
147156
return self.sandbox.message_cache
148157

149158
def get_task_programs(self):
150159
return TaskProgramLibrary.get_task_programs()
151-
160+
152161

153162
if __name__ == "__main__":
154163
from amadeusgpt.analysis_objects.llm import VisualLLM

amadeusgpt/programs/sandbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def wrapper(*args, **kwargs):
165165
class Sandbox(SandboxBase):
166166
def __init__(
167167
self,
168-
config: Config,
168+
config: Config | dict,
169169
video_file_paths: list[str],
170170
keypoint_file_paths: list[str],
171171
):

amadeusgpt/project.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from amadeusgpt.config import Config
2+
import os
3+
import pprint
4+
import yaml
5+
6+
def create_project(data_folder,
7+
result_folder,
8+
video_suffix=".mp4"):
9+
"""
10+
Create a project config file. Save the config file to the result folder
11+
"""
12+
config = {
13+
"data_info": {
14+
"data_folder": data_folder,
15+
"result_folder": result_folder,
16+
"video_suffix": video_suffix,
17+
},
18+
"llm_info": {
19+
"max_tokens": 4096,
20+
"temperature": 0.0,
21+
"keep_last_n_messages": 2
22+
},
23+
}
24+
# save the dictionary config to yaml
25+
26+
os.makedirs(result_folder, exist_ok=True)
27+
28+
file_path = os.path.join(result_folder, "config.yaml")
29+
30+
with open(file_path, "w") as f:
31+
yaml.dump(config, f)
32+
33+
print (f"Project created at {result_folder}. Results will be saved to {result_folder}")
34+
print (f"The project will load video files (*.{video_suffix}) and optionally keypoint files from {data_folder}")
35+
print (f"A copy of the project config file is saved at {file_path}")
36+
pprint.pprint(config)
37+
38+
return config

notebooks/custom_mouse_video.ipynb

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"from amadeusgpt.config import Config\n",
2525
"from amadeusgpt.utils import parse_result\n",
2626
"import amadeusgpt\n",
27-
"from pathlib import Path\n",
27+
"from amadeusgpt import create_project\n",
2828
"import matplotlib.pyplot as plt\n",
2929
"import cv2"
3030
]
@@ -41,33 +41,48 @@
4141
},
4242
{
4343
"cell_type": "code",
44-
"execution_count": 4,
44+
"execution_count": 3,
4545
"id": "be76dc87-fbe8-452f-b85c-2af3e95a03bf",
4646
"metadata": {},
4747
"outputs": [
4848
{
49-
"name": "stderr",
49+
"name": "stdout",
5050
"output_type": "stream",
5151
"text": [
52-
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
52+
"Project created at temp_result_folder. Results will be saved to temp_result_folder\n",
53+
"The project will load video files (*..mp4) and optionally keypoint files from temp_data_folder\n",
54+
"A copy of the project config file is saved at temp_result_folder/config.yaml\n",
55+
"{'data_info': {'data_folder': 'temp_data_folder',\n",
56+
" 'result_folder': 'temp_result_folder',\n",
57+
" 'video_suffix': '.mp4'},\n",
58+
" 'llm_info': {'max_tokens': 4096, 'temperature': 1.0}}\n",
59+
"No video files found in the data folder temp_data_folder. Please check the data folder and the video suffix\n"
5360
]
5461
},
5562
{
56-
"name": "stdout",
57-
"output_type": "stream",
58-
"text": [
59-
"current total cost 0.004 $\n",
60-
"current input tokens 25667\n",
61-
"current accumulated tokens 25843\n",
62-
"['/Users/shaokaiye/OFT/OFT_43_short.mp4']\n"
63+
"ename": "AttributeError",
64+
"evalue": "'AMADEUS' object has no attribute 'sandbox'",
65+
"output_type": "error",
66+
"traceback": [
67+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
68+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
69+
"Cell \u001b[0;32mIn[3], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mscene_frame_number\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m scene_frame_number\n\u001b[1;32m 12\u001b[0m amadeus \u001b[38;5;241m=\u001b[39m AMADEUS(config)\n\u001b[0;32m---> 13\u001b[0m video_file_paths \u001b[38;5;241m=\u001b[39m \u001b[43mamadeus\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_video_file_paths\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m (video_file_paths)\n",
70+
"File \u001b[0;32m~/AmadeusGPT-dev/amadeusgpt/main.py:121\u001b[0m, in \u001b[0;36mAMADEUS.get_video_file_paths\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 120\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_video_file_paths\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mstr\u001b[39m]:\n\u001b[0;32m--> 121\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msandbox\u001b[49m\u001b[38;5;241m.\u001b[39mvideo_file_paths\n",
71+
"\u001b[0;31mAttributeError\u001b[0m: 'AMADEUS' object has no attribute 'sandbox'"
6372
]
6473
}
6574
],
6675
"source": [
6776
"scene_frame_number = 400\n",
68-
"amadeus_root = Path(amadeusgpt.__file__).parent.parent\n",
69-
"config = Config(amadeus_root / \"amadeusgpt/configs/template.yaml\")\n",
70-
"config['data_info']['data_folder'] = \"/Users/shaokaiye/OFT\"\n",
77+
"\n",
78+
"# where you store you video and (optionally) keypoint files\n",
79+
"data_folder = \"temp_data_folder\"\n",
80+
"result_folder = \"temp_result_folder\"\n",
81+
"video_suffix = \".mp4\"\n",
82+
"\n",
83+
"config = create_project(data_folder, result_folder, video_suffix = video_suffix)\n",
84+
"\n",
85+
"config[\"scene_frame_number\"] = scene_frame_number\n",
7186
"\n",
7287
"amadeus = AMADEUS(config)\n",
7388
"video_file_paths = amadeus.get_video_file_paths()\n",

tests/test_project_creation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
if 'OPENAI_API_KEY' not in os.environ:
3+
os.environ['OPENAI_API_KEY'] = 'your key'
4+
5+
from amadeusgpt import create_project
6+
from amadeusgpt import AMADEUS
7+
# Create a project
8+
9+
data_folder = "temp_data_folder"
10+
result_folder = "temp_result_folder"
11+
12+
config = create_project(data_folder, result_folder)
13+
14+
# Create an AMADEUS instance
15+
amadeus = AMADEUS(config)

0 commit comments

Comments
 (0)