AnonyCAD commited on
Commit
0d4e49d
·
verified ·
1 Parent(s): 649296a

Upload test.py

Browse files
Files changed (1) hide show
  1. test.py +183 -0
test.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import anthropic
2
+ import base64
3
+ import httpx
4
+ import os
5
+ import time
6
+ from mimetypes import guess_type
7
+
8
+ import random
9
+ # import numpy as np
10
+ def seed_everything(seed):
11
+ random.seed(seed)
12
+ # np.random.seed(seed)
13
+ os.environ['PYTHONHASHSEED'] = str(seed)
14
+ # torch.manual_seed(seed)
15
+ # torch.cuda.manual_seed(seed)
16
+ # torch.backends.cudnn.deterministic = True
17
+ # env.seed(seed)
18
+ seed_everything(1)
19
+
20
+ def local_image_to_data_url(image_path):
21
+ mime_type, _ = guess_type(image_path)
22
+ if mime_type is None:
23
+ mime_type = 'application/octet-stream'
24
+ with open(image_path, "rb") as image_file:
25
+ base64_encoded_data = base64.b64encode(image_file.read()).decode('utf-8')
26
+ return base64_encoded_data
27
+
28
+ client = anthropic.Anthropic(
29
+ api_key="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",
30
+ )
31
+
32
+
33
+ # Start test
34
+ levels = [3,4,5,6,7,8]
35
+ in_context_example_num = 0 # 0, 1, 2, 4, 8
36
+
37
+ if in_context_example_num > 0:
38
+ output_path = "output/output_img_%d/"%(in_context_example_num)
39
+ input_backup_path = "input/input_backup_img_%d/"%(in_context_example_num)
40
+ else:
41
+ output_path = "output/output_img/"
42
+ input_backup_path = "input/input_backup_img/"
43
+
44
+ os.makedirs(output_path, exist_ok=True)
45
+ os.makedirs(input_backup_path, exist_ok=True)
46
+
47
+ EXAMPLE_DICT = {
48
+ 3: [],
49
+ 4: [],
50
+ 5: [],
51
+ 6: [],
52
+ 7: [],
53
+ 8: [],
54
+ }
55
+ # for level in levels:
56
+ # for example_id in range(8):
57
+ # curr_example_pack = {}
58
+ # curr_example_pack["image_path"] = "../example/level%d/img/%d.png"%(level, example_id)
59
+ # with open("../example/level%d/answer/%d.txt"%(level, example_id), "r") as f:
60
+ # curr_example_pack["answer"] = f.read()
61
+
62
+ # curr_example_pack["pure_text"] = "../example/level%d/pure_text/%d.txt"%(level, example_id)
63
+ # curr_example_pack["table"] = "../example/level%d/table/%d.txt"%(level, example_id)
64
+
65
+ # curr_example_pack["start_image_path"] = "../example/level%d/begin/%d.jpg"%(level, example_id)
66
+ # curr_example_pack["end_image_path"] = "../example/level%d/end/%d.jpg"%(level, example_id)
67
+ # example_path = "../example/level%d/"%(level)
68
+ # curr_example_pack["question1"] = "\n\nPlease generate the moving plan. The beginning state is:"
69
+ # curr_example_pack["question2"] = "\nThe end state is:"
70
+ # with open(example_path + "sol_%d.txt"%(example_id), "r") as f:
71
+ # curr_example_pack["answer"] = f.read()
72
+ # EXAMPLE_DICT[level].append(curr_example_pack)
73
+
74
+
75
+ import ipdb; ipdb.set_trace()
76
+ for level in levels:
77
+ os.makedirs(output_path + "level%d"%(level), exist_ok=True)
78
+ os.makedirs(input_backup_path + "level%d"%(level), exist_ok=True)
79
+ start_idx = 0
80
+ end_idx = 100
81
+ runned_term = 0
82
+ map_path = "../maps/level%d/img/"%(level)
83
+ while True:
84
+ try:
85
+ curr_id = start_idx + runned_term
86
+ if curr_id >= end_idx:
87
+ break
88
+
89
+ prompt_input_1 = '''
90
+ In this task, you will analyze a maze to determine if there is a hole in a specific position.
91
+ The following figure illustrates the appearances of the player, holes, lands, and the goal within the maze. You will need to focus on the appearance of the hole.
92
+ '''
93
+ prompt_input_2 = '''
94
+ Here is an example to illustrate how to analyze and answer the question:
95
+ '''
96
+ prompt_input_3 = '''
97
+ Example question: Is there a hole in row 3, column 3?
98
+
99
+ In this example:
100
+ - We check the position in row 3, column 3.
101
+ - According to the image, it is a land square. It does not contain a hole.
102
+ - Therefore, you will output "<Output> No".
103
+
104
+ Your output should be: "<Output> No" or "<Output> Yes", depending on whether there is a hole at the specified position.
105
+ '''
106
+ # prompt_examples = []
107
+ # image_examples = []
108
+ # if in_context_example_num > 0:
109
+ # prompt_examples.append("## Example:\n")
110
+ # example_indices = random.sample(range(8), in_context_example_num)
111
+ # for example_index in example_indices:
112
+ # this_example = EXAMPLE_DICT[level][example_index]
113
+ # image_examples.append(local_image_to_data_url(this_example["image_path"]))
114
+ # prompt_examples.append(this_example["answer"])
115
+ prompt_input_4 = "\n\nNow you will analyze the following maze and answer the question: "
116
+ with open("../maps/level%d/question/%d.txt"%(level, curr_id), "r") as f:
117
+ prompt_input_5 = f.read()
118
+
119
+ # construct
120
+ content_input_seq = []
121
+ content_input_seq.append({
122
+ "type": "text",
123
+ "text": prompt_input_1,
124
+ })
125
+ content_input_seq.append({
126
+ "type": "image",
127
+ "source": {
128
+ "type": "base64",
129
+ "media_type": "image/png",
130
+ "data": local_image_to_data_url("../prompt-visual-images/system-figure-1.png"),
131
+ }
132
+ })
133
+ content_input_seq.append({
134
+ "type": "text",
135
+ "text": prompt_input_2,
136
+ })
137
+ content_input_seq.append({
138
+ "type": "image",
139
+ "source": {
140
+ "type": "base64",
141
+ "media_type": "image/png",
142
+ "data": local_image_to_data_url("../prompt-visual-images/system-figure-2.png"),
143
+ }
144
+ })
145
+ content_input_seq.append({
146
+ "type": "text",
147
+ "text": prompt_input_3,
148
+ })
149
+ content_input_seq.append({
150
+ "type": "text",
151
+ "text": prompt_input_4,
152
+ })
153
+ content_input_seq.append({
154
+ "type": "image",
155
+ "source": {
156
+ "type": "base64",
157
+ "media_type": "image/png",
158
+ "data": local_image_to_data_url(map_path + "%d.png"%(curr_id)),
159
+ }
160
+ })
161
+ content_input_seq.append({
162
+ "type": "text",
163
+ "text": prompt_input_5,
164
+ })
165
+ response = client.messages.create(
166
+ model="claude-3-sonnet-20240229",
167
+ max_tokens=1024,
168
+ system="You are a maze-solving agent playing a pixelated maze videogame.\nMazes are presented on grid maps, where each tile can be empty land, or contain a player, hole, or goal.",
169
+ messages=[
170
+ {
171
+ "role": "user",
172
+ "content": content_input_seq
173
+ },
174
+ ],
175
+ )
176
+ with open(output_path + "level%d/%d.txt"%(level, curr_id), "w") as f:
177
+ f.write(response.content[0].text)
178
+ time.sleep(2)
179
+ runned_term += 1
180
+ except:
181
+ time.sleep(2)
182
+ pass
183
+