Delete vis_python_exe.py
Browse files- vis_python_exe.py +0 -783
vis_python_exe.py
DELETED
@@ -1,783 +0,0 @@
|
|
1 |
-
import sys
|
2 |
-
import os
|
3 |
-
import re
|
4 |
-
import json
|
5 |
-
import base64
|
6 |
-
from io import BytesIO
|
7 |
-
from PIL import Image
|
8 |
-
import argparse
|
9 |
-
from inference_engine.safe_persis_shared_vis_python_exe import PythonExecutor, ImageRuntime
|
10 |
-
from openai import OpenAI
|
11 |
-
import anthropic
|
12 |
-
|
13 |
-
def encode_image(image):
|
14 |
-
"""
|
15 |
-
Convert a PIL.Image object or image file path to base64-encoded string, and get resolution info.
|
16 |
-
|
17 |
-
Args:
|
18 |
-
image: Can be a PIL.Image object or image file path.
|
19 |
-
Returns:
|
20 |
-
dict with keys:
|
21 |
-
- 'base64': base64-encoded string
|
22 |
-
- 'width': width in pixels
|
23 |
-
- 'height': height in pixels
|
24 |
-
- 'resolution': string "widthxheight"
|
25 |
-
"""
|
26 |
-
img_obj = None
|
27 |
-
|
28 |
-
if isinstance(image, str):
|
29 |
-
# Handle file path
|
30 |
-
img_obj = Image.open(image)
|
31 |
-
with open(image, "rb") as image_file:
|
32 |
-
base64_str = base64.b64encode(image_file.read()).decode('utf-8')
|
33 |
-
else:
|
34 |
-
# Handle PIL.Image object
|
35 |
-
img_obj = image
|
36 |
-
buffered = BytesIO()
|
37 |
-
image.save(buffered, format='PNG')
|
38 |
-
base64_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
39 |
-
|
40 |
-
width, height = img_obj.size
|
41 |
-
|
42 |
-
return {
|
43 |
-
'base64': base64_str,
|
44 |
-
'width': width,
|
45 |
-
'height': height
|
46 |
-
}
|
47 |
-
|
48 |
-
def encode_image_with_resize(image):
|
49 |
-
"""
|
50 |
-
Convert a PIL.Image object or image file path to base64-encoded string, get resolution info.
|
51 |
-
If resolution > 1024x1024, resize to half.
|
52 |
-
|
53 |
-
Args:
|
54 |
-
image: Can be a PIL.Image object or image file path
|
55 |
-
Returns:
|
56 |
-
dict with keys:
|
57 |
-
- 'base64': base64-encoded string
|
58 |
-
- 'width': width in pixels
|
59 |
-
- 'height': height in pixels
|
60 |
-
- 'resolution': string "widthxheight"
|
61 |
-
"""
|
62 |
-
img_obj = None
|
63 |
-
|
64 |
-
if isinstance(image, str):
|
65 |
-
img_obj = Image.open(image)
|
66 |
-
else:
|
67 |
-
img_obj = image
|
68 |
-
|
69 |
-
# Resize if larger than 1024x1024
|
70 |
-
width, height = img_obj.size
|
71 |
-
if width > 1024 or height > 1024:
|
72 |
-
new_size = (width // 2, height // 2)
|
73 |
-
img_obj = img_obj.resize(new_size, Image.LANCZOS)
|
74 |
-
width, height = img_obj.size
|
75 |
-
|
76 |
-
buffered = BytesIO()
|
77 |
-
img_obj.save(buffered, format='PNG')
|
78 |
-
base64_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
79 |
-
|
80 |
-
return {
|
81 |
-
'base64': base64_str,
|
82 |
-
'width': width,
|
83 |
-
'height': height,
|
84 |
-
'resolution': f"{width}x{height}"
|
85 |
-
}
|
86 |
-
|
87 |
-
def check(evaluator, pred_ans, real_ans):
|
88 |
-
if len(pred_ans) == 0:
|
89 |
-
return []
|
90 |
-
correctness = evaluator.score(pred_ans, real_ans)
|
91 |
-
return correctness
|
92 |
-
|
93 |
-
def execute_codes(codes, messages, executor: PythonExecutor):
|
94 |
-
no_code_idx = []
|
95 |
-
codes_use = []
|
96 |
-
for i, code in enumerate(codes):
|
97 |
-
if code == "":
|
98 |
-
no_code_idx.append(i)
|
99 |
-
else:
|
100 |
-
codes_use.append(code)
|
101 |
-
batch_results = executor.batch_apply(codes_use, messages)
|
102 |
-
return batch_results, no_code_idx
|
103 |
-
|
104 |
-
def process_prompt_init(question, image_path_list, prompt_template, prompt_type, api_name):
|
105 |
-
with open(prompt_template, "r") as fin:
|
106 |
-
sys = json.load(fin)
|
107 |
-
prompt_prefix = sys[prompt_type]
|
108 |
-
|
109 |
-
image_path = image_path_list[0]
|
110 |
-
|
111 |
-
if "<IMAGE_PLACE_HOLDER_0>" in question:
|
112 |
-
if "no_tool" in prompt_type:
|
113 |
-
|
114 |
-
if "claude" in api_name:
|
115 |
-
img_result = encode_image_with_resize(image_path)
|
116 |
-
else:
|
117 |
-
img_result = encode_image(image_path)
|
118 |
-
image_base64 = img_result['base64']
|
119 |
-
question_with_options = question
|
120 |
-
question = prompt_prefix.format(query=question_with_options)
|
121 |
-
|
122 |
-
parts = question.split("<IMAGE_PLACE_HOLDER_0>")
|
123 |
-
content = []
|
124 |
-
|
125 |
-
# Add text before image (if any)
|
126 |
-
if parts[0].strip():
|
127 |
-
content.append({"type": "text", "text": parts[0].strip()})
|
128 |
-
# Add image
|
129 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}})
|
130 |
-
|
131 |
-
# Add text after image (if any)
|
132 |
-
if len(parts) > 1 and parts[1].strip():
|
133 |
-
content.append({"type": "text", "text": parts[1].strip()})
|
134 |
-
|
135 |
-
messages = [
|
136 |
-
{
|
137 |
-
"role": "user",
|
138 |
-
"content": content
|
139 |
-
}
|
140 |
-
]
|
141 |
-
|
142 |
-
return messages
|
143 |
-
|
144 |
-
else:
|
145 |
-
if "claude" in api_name:
|
146 |
-
img_result = encode_image_with_resize(image_path)
|
147 |
-
else:
|
148 |
-
img_result = encode_image(image_path)
|
149 |
-
image_base64 = img_result['base64']
|
150 |
-
width = img_result['width']
|
151 |
-
height = img_result['height']
|
152 |
-
question_with_options = question
|
153 |
-
question = prompt_prefix.format(query=question_with_options, width=str(width), height=str(height))
|
154 |
-
|
155 |
-
# Split question into parts
|
156 |
-
parts = question.split("<IMAGE_PLACE_HOLDER_0>")
|
157 |
-
# Build message with image_clue tags
|
158 |
-
content = []
|
159 |
-
|
160 |
-
# Add text before image (if any)
|
161 |
-
if parts[0].strip():
|
162 |
-
content.append({"type": "text", "text": parts[0].strip()})
|
163 |
-
|
164 |
-
# Add image with tags
|
165 |
-
content.append({"type": "text", "text": "<image_clue_0>"})
|
166 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}})
|
167 |
-
content.append({"type": "text", "text": "</image_clue_0>\n\n"})
|
168 |
-
|
169 |
-
# Add text after image (if any)
|
170 |
-
if len(parts) > 1 and parts[1].strip():
|
171 |
-
content.append({"type": "text", "text": parts[1].strip()})
|
172 |
-
|
173 |
-
messages = [
|
174 |
-
{
|
175 |
-
"role": "user",
|
176 |
-
"content": content
|
177 |
-
}
|
178 |
-
]
|
179 |
-
|
180 |
-
return messages
|
181 |
-
|
182 |
-
else:
|
183 |
-
if "no_tool" in prompt_type:
|
184 |
-
|
185 |
-
if "claude" in api_name:
|
186 |
-
img_result = encode_image_with_resize(image_path)
|
187 |
-
else:
|
188 |
-
img_result = encode_image(image_path)
|
189 |
-
image_base64 = img_result['base64']
|
190 |
-
question_with_options = question
|
191 |
-
|
192 |
-
messages = [
|
193 |
-
{
|
194 |
-
"role": "user",
|
195 |
-
"content": [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}] + [{"type": "text", "text": prompt_prefix.format(query=question_with_options)}]
|
196 |
-
}
|
197 |
-
]
|
198 |
-
|
199 |
-
return messages
|
200 |
-
|
201 |
-
else:
|
202 |
-
if "claude" in api_name:
|
203 |
-
img_result = encode_image_with_resize(image_path)
|
204 |
-
else:
|
205 |
-
img_result = encode_image(image_path)
|
206 |
-
image_base64 = img_result['base64']
|
207 |
-
width = img_result['width']
|
208 |
-
height = img_result['height']
|
209 |
-
question_with_options = question
|
210 |
-
|
211 |
-
messages = [
|
212 |
-
{
|
213 |
-
"role": "user",
|
214 |
-
"content": [{"type": "text", "text": "<image_clue_0>"}] + [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}] + [{"type": "text", "text": "</image_clue_0>\n\n"}] + [{"type": "text", "text": prompt_prefix.format(query=question_with_options, width=str(width), height=str(height))}]
|
215 |
-
}
|
216 |
-
]
|
217 |
-
|
218 |
-
return messages
|
219 |
-
|
220 |
-
def process_prompt_init_multi_images(question, image_path_list, prompt_template, prompt_type, api_name):
|
221 |
-
with open(prompt_template, "r") as fin:
|
222 |
-
sys = json.load(fin)
|
223 |
-
prompt_prefix = sys[prompt_type]
|
224 |
-
|
225 |
-
# Prepare image data
|
226 |
-
image_data = []
|
227 |
-
image_information = ""
|
228 |
-
|
229 |
-
for i, image_path in enumerate(image_path_list):
|
230 |
-
if "claude" in api_name:
|
231 |
-
img_result = encode_image_with_resize(image_path)
|
232 |
-
else:
|
233 |
-
img_result = encode_image(image_path)
|
234 |
-
image_base64 = img_result['base64']
|
235 |
-
width = img_result['width']
|
236 |
-
height = img_result['height']
|
237 |
-
|
238 |
-
image_data.append({
|
239 |
-
"index": i,
|
240 |
-
"base64": image_base64,
|
241 |
-
"width": width,
|
242 |
-
"height": height,
|
243 |
-
"placeholder": f"<IMAGE_PLACE_HOLDER_{i}>"
|
244 |
-
})
|
245 |
-
|
246 |
-
image_information += f"width of image_clue_{i}: {width}, height of image_clue_{i}: {height}\n"
|
247 |
-
|
248 |
-
# Format question
|
249 |
-
formatted_question = prompt_prefix.format(query=question, image_information=image_information)
|
250 |
-
|
251 |
-
# Check if placeholder exists
|
252 |
-
has_placeholders = any(f"<IMAGE_PLACE_HOLDER_{i}>" in formatted_question for i in range(len(image_path_list)))
|
253 |
-
|
254 |
-
if has_placeholders:
|
255 |
-
# Insert images at placeholder positions
|
256 |
-
if "no_tool" in prompt_type:
|
257 |
-
content = []
|
258 |
-
remaining_text = formatted_question
|
259 |
-
|
260 |
-
for img_data in image_data:
|
261 |
-
placeholder = img_data["placeholder"]
|
262 |
-
if placeholder in remaining_text:
|
263 |
-
parts = remaining_text.split(placeholder, 1)
|
264 |
-
|
265 |
-
if parts[0]:
|
266 |
-
content.append({"type": "text", "text": parts[0]})
|
267 |
-
|
268 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_data['base64']}"}})
|
269 |
-
|
270 |
-
remaining_text = parts[1]
|
271 |
-
|
272 |
-
if remaining_text:
|
273 |
-
content.append({"type": "text", "text": remaining_text})
|
274 |
-
|
275 |
-
messages = [{"role": "user", "content": content}]
|
276 |
-
return messages
|
277 |
-
else:
|
278 |
-
content = []
|
279 |
-
remaining_text = formatted_question
|
280 |
-
|
281 |
-
for img_data in image_data:
|
282 |
-
placeholder = img_data["placeholder"]
|
283 |
-
if placeholder in remaining_text:
|
284 |
-
parts = remaining_text.split(placeholder, 1)
|
285 |
-
|
286 |
-
if parts[0]:
|
287 |
-
content.append({"type": "text", "text": parts[0]})
|
288 |
-
|
289 |
-
i = img_data["index"]
|
290 |
-
content.append({"type": "text", "text": f"<image_clue_{i}>"})
|
291 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_data['base64']}"}})
|
292 |
-
content.append({"type": "text", "text": f"</image_clue_{i}>\n\n"})
|
293 |
-
|
294 |
-
remaining_text = parts[1]
|
295 |
-
|
296 |
-
if remaining_text:
|
297 |
-
content.append({"type": "text", "text": remaining_text})
|
298 |
-
|
299 |
-
messages = [{"role": "user", "content": content}]
|
300 |
-
return messages
|
301 |
-
else:
|
302 |
-
# Handle as usual if no placeholder
|
303 |
-
if "no_tool" in prompt_type:
|
304 |
-
content = []
|
305 |
-
|
306 |
-
for i, img_data in enumerate(image_data):
|
307 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_data['base64']}"}})
|
308 |
-
|
309 |
-
content.append({"type": "text", "text": formatted_question})
|
310 |
-
|
311 |
-
messages = [{"role": "user", "content": content}]
|
312 |
-
return messages
|
313 |
-
else:
|
314 |
-
content = []
|
315 |
-
|
316 |
-
for i, img_data in enumerate(image_data):
|
317 |
-
content.append({"type": "text", "text": f"<image_clue_{i}>"})
|
318 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_data['base64']}"}})
|
319 |
-
content.append({"type": "text", "text": f"</image_clue_{i}>\n\n"})
|
320 |
-
|
321 |
-
content.append({"type": "text", "text": formatted_question})
|
322 |
-
|
323 |
-
messages = [{"role": "user", "content": content}]
|
324 |
-
return messages
|
325 |
-
|
326 |
-
|
327 |
-
def update_messages_with_execute_content(image_nums_in_input, messages, images_result, text_result, error_result, image_clue_idx):
|
328 |
-
if error_result is None:
|
329 |
-
new_messages = []
|
330 |
-
image_content = []
|
331 |
-
for message_item in messages[:-1]:
|
332 |
-
new_messages.append(message_item)
|
333 |
-
|
334 |
-
assistant_message_item = messages[-1]['content']
|
335 |
-
interpreter_message_text_prefix = [{"type": "text", "text": f"<interpreter>\nText Result:\n{text_result}\nImage Result:\n"}]
|
336 |
-
if images_result is not None:
|
337 |
-
print(f"#### image_clue_index: {image_clue_idx},Image_nums_in_input: {image_nums_in_input}, len of images_result: {len(images_result)}")
|
338 |
-
# for image_base64_item in images_result[image_clue_idx-image_nums_in_input:]:
|
339 |
-
for image_base64_item in images_result:
|
340 |
-
interpreter_message_images = [{"type": "text", "text": f"<image_clue_{image_clue_idx}>"}] + [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64_item}"}}] + [{"type": "text", "text": f"</image_clue_{image_clue_idx}>"}]
|
341 |
-
image_content += interpreter_message_images
|
342 |
-
image_clue_idx += 1
|
343 |
-
else:
|
344 |
-
image_content = [{"type": "text", "text": "None"}]
|
345 |
-
interpreter_message_text_profill = [{"type": "text", "text": "</interpreter>\n"}]
|
346 |
-
|
347 |
-
interpreter_message_item = interpreter_message_text_prefix + image_content + interpreter_message_text_profill
|
348 |
-
new_messages.append({"role": "assistant", "content": assistant_message_item})
|
349 |
-
new_messages.append({"role": "user", "content": interpreter_message_item})
|
350 |
-
else:
|
351 |
-
new_messages = []
|
352 |
-
for message_item in messages[:-1]:
|
353 |
-
new_messages.append(message_item)
|
354 |
-
|
355 |
-
assistant_message_item = messages[-1]['content']
|
356 |
-
interpreter_message_text_prefix = [{"type": "text", "text": f"<interpreter>{error_result}"}]
|
357 |
-
interpreter_message_text_profill = [{"type": "text", "text": "</interpreter>\n"}]
|
358 |
-
|
359 |
-
interpreter_message_item = interpreter_message_text_prefix + interpreter_message_text_profill
|
360 |
-
new_messages.append({"role": "assistant", "content": assistant_message_item})
|
361 |
-
new_messages.append({"role": "user", "content": interpreter_message_item})
|
362 |
-
|
363 |
-
return new_messages, image_clue_idx
|
364 |
-
|
365 |
-
def update_messages_with_code(messages, generated_content):
|
366 |
-
message_item = {
|
367 |
-
"role": "assistant",
|
368 |
-
"content": [{"type": "text", "text": f"{generated_content}</code>\n"}]
|
369 |
-
}
|
370 |
-
|
371 |
-
messages.append(message_item)
|
372 |
-
return messages
|
373 |
-
|
374 |
-
def update_messages_with_text(messages, generated_content):
|
375 |
-
message_item = {
|
376 |
-
"role": "assistant",
|
377 |
-
"content": [{"type": "text", "text": f"{generated_content}"}]
|
378 |
-
}
|
379 |
-
|
380 |
-
messages.append(message_item)
|
381 |
-
return messages
|
382 |
-
|
383 |
-
def call_chatgpt_api(args, messages, client, max_tokens=10000, stop=None, temperature=0.6):
|
384 |
-
"""Call ChatGPT API with the given messages"""
|
385 |
-
try:
|
386 |
-
client_type = args.client_type
|
387 |
-
api_name = args.api_name
|
388 |
-
except:
|
389 |
-
client_type = args['client_type']
|
390 |
-
api_name = args['api_name']
|
391 |
-
|
392 |
-
if client_type == "openai" or client_type == "azure":
|
393 |
-
response = client.chat.completions.create(
|
394 |
-
model=api_name,
|
395 |
-
messages=messages,
|
396 |
-
max_tokens=max_tokens,
|
397 |
-
temperature=temperature,
|
398 |
-
top_p=1.0,
|
399 |
-
stop=stop,
|
400 |
-
timeout=300
|
401 |
-
)
|
402 |
-
response_text = response.choices[0].message.content
|
403 |
-
elif client_type == "anthropic":
|
404 |
-
message = client.messages.create(
|
405 |
-
model=api_name,
|
406 |
-
max_tokens=max_tokens,
|
407 |
-
messages=messages,
|
408 |
-
temperature=temperature,
|
409 |
-
top_p=1.0,
|
410 |
-
stop_sequences=stop
|
411 |
-
)
|
412 |
-
response_text = message.content[0].text if isinstance(message.content, list) else message.content
|
413 |
-
elif client_type == "vllm":
|
414 |
-
response = client.chat.completions.create(
|
415 |
-
model=api_name,
|
416 |
-
messages=messages,
|
417 |
-
max_tokens=max_tokens,
|
418 |
-
temperature=temperature,
|
419 |
-
top_p=1.0,
|
420 |
-
stop=stop
|
421 |
-
)
|
422 |
-
response_text = response.choices[0].message.content
|
423 |
-
else:
|
424 |
-
print("Your args.client_type must be one of openai, azure, anthropic and vllm.")
|
425 |
-
return None, None
|
426 |
-
|
427 |
-
# Check if stop sequence is encountered
|
428 |
-
stop_reason = None
|
429 |
-
if stop and any(s in response_text for s in stop):
|
430 |
-
for s in stop:
|
431 |
-
if s in response_text:
|
432 |
-
stop_reason = s
|
433 |
-
break
|
434 |
-
else:
|
435 |
-
if client_type in ["openai", "azure", "vllm"]:
|
436 |
-
stop_reason = response.choices[0].finish_reason
|
437 |
-
else:
|
438 |
-
stop_reason = "stop"
|
439 |
-
|
440 |
-
if "<code>" in response_text:
|
441 |
-
stop_reason = "</code>"
|
442 |
-
|
443 |
-
return response_text, stop_reason
|
444 |
-
|
445 |
-
def evaluate_single_data(args, data, client, executor):
|
446 |
-
try:
|
447 |
-
prompt_template = args.prompt_template
|
448 |
-
prompt = args.prompt
|
449 |
-
exe_code = args.exe_code
|
450 |
-
max_tokens = args.max_tokens
|
451 |
-
temperature = args.temperature
|
452 |
-
api_name = args.api_name
|
453 |
-
except:
|
454 |
-
prompt_template = args['prompt_template']
|
455 |
-
prompt = args['prompt']
|
456 |
-
exe_code = args['exe_code']
|
457 |
-
max_tokens = args['max_tokens']
|
458 |
-
temperature = args['temperature']
|
459 |
-
api_name = args['api_name']
|
460 |
-
|
461 |
-
image_path_list = data['image_path_list']
|
462 |
-
|
463 |
-
if "no_tool" in prompt:
|
464 |
-
if len(image_path_list) == 1:
|
465 |
-
messages = process_prompt_init(data["question"], image_path_list, prompt_template, prompt, api_name)
|
466 |
-
elif len(image_path_list) >= 2:
|
467 |
-
messages = process_prompt_init_multi_images(data["question"], image_path_list, prompt_template, prompt, api_name)
|
468 |
-
else:
|
469 |
-
if len(image_path_list) == 1:
|
470 |
-
prompt = "vistool_with_img_info_v2"
|
471 |
-
messages = process_prompt_init(data["question"], image_path_list, prompt_template, prompt, api_name)
|
472 |
-
elif len(image_path_list) >= 2:
|
473 |
-
prompt = "vistool_with_img_info_multi_image"
|
474 |
-
messages = process_prompt_init_multi_images(data["question"], image_path_list, prompt_template, prompt, api_name)
|
475 |
-
|
476 |
-
# Generate initial response
|
477 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
478 |
-
args,
|
479 |
-
messages,
|
480 |
-
client,
|
481 |
-
max_tokens=max_tokens,
|
482 |
-
stop=["</code>"] if exe_code else None,
|
483 |
-
temperature=temperature
|
484 |
-
)
|
485 |
-
|
486 |
-
# Handle response
|
487 |
-
final_response = response_text
|
488 |
-
code_execution_count = 0
|
489 |
-
image_clue_idx = len(image_path_list)
|
490 |
-
|
491 |
-
while True:
|
492 |
-
# Check if code execution is needed
|
493 |
-
if exe_code and pred_stop_reason == "</code>":
|
494 |
-
# Extract code to execute
|
495 |
-
messages = update_messages_with_code(messages, response_text)
|
496 |
-
code_to_execute = response_text.split("```python")[-1].split("```")[0].strip()
|
497 |
-
|
498 |
-
# Execute code
|
499 |
-
exe_result = execute_codes([code_to_execute], messages, executor)[0][0]
|
500 |
-
if exe_result is None:
|
501 |
-
text_result = "None"
|
502 |
-
images_result = None
|
503 |
-
else:
|
504 |
-
output, report = exe_result
|
505 |
-
if report == "Done":
|
506 |
-
error_result = None
|
507 |
-
try:
|
508 |
-
text_result = exe_result[0]['text']
|
509 |
-
except:
|
510 |
-
text_result = None
|
511 |
-
print("text result is none.")
|
512 |
-
try:
|
513 |
-
images_result = exe_result[0]['images']
|
514 |
-
except:
|
515 |
-
images_result = None
|
516 |
-
print("image result is none.")
|
517 |
-
else:
|
518 |
-
error_result = report
|
519 |
-
text_result = None
|
520 |
-
images_result = None
|
521 |
-
|
522 |
-
messages, new_image_clue_idx = update_messages_with_execute_content(len(image_path_list), messages, images_result, text_result, error_result, image_clue_idx)
|
523 |
-
image_clue_idx = new_image_clue_idx
|
524 |
-
|
525 |
-
code_execution_count += 1
|
526 |
-
|
527 |
-
# Generate next response part
|
528 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
529 |
-
args,
|
530 |
-
messages,
|
531 |
-
client,
|
532 |
-
max_tokens=max_tokens,
|
533 |
-
stop=["</code>"] if exe_code else None,
|
534 |
-
temperature=temperature
|
535 |
-
)
|
536 |
-
|
537 |
-
else:
|
538 |
-
final_response = response_text
|
539 |
-
messages = update_messages_with_text(messages, response_text)
|
540 |
-
break
|
541 |
-
|
542 |
-
return messages, final_response
|
543 |
-
|
544 |
-
|
545 |
-
def evaluate_single_data_multi_images(args, data, client, executor):
|
546 |
-
try:
|
547 |
-
prompt_template = args.prompt_template
|
548 |
-
prompt = args.prompt
|
549 |
-
exe_code = args.exe_code
|
550 |
-
max_tokens = args.max_tokens
|
551 |
-
except:
|
552 |
-
prompt_template = args['prompt_template']
|
553 |
-
prompt = args['prompt']
|
554 |
-
exe_code = args['exe_code']
|
555 |
-
max_tokens = args['max_tokens']
|
556 |
-
|
557 |
-
messages = process_prompt_init_multi_images(data["question"], data['image_path_list'], prompt_template, prompt)
|
558 |
-
|
559 |
-
# Generate initial response
|
560 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
561 |
-
args,
|
562 |
-
messages,
|
563 |
-
client,
|
564 |
-
max_tokens=max_tokens,
|
565 |
-
stop=["</code>"] if exe_code else None
|
566 |
-
)
|
567 |
-
|
568 |
-
# Handle response
|
569 |
-
final_response = response_text
|
570 |
-
code_execution_count = 0
|
571 |
-
image_clue_idx = data['image_nums_in_input']
|
572 |
-
|
573 |
-
while True:
|
574 |
-
# Check if code execution is needed
|
575 |
-
if exe_code and pred_stop_reason == "</code>":
|
576 |
-
# Extract code to execute
|
577 |
-
messages = update_messages_with_code(messages, response_text)
|
578 |
-
code_to_execute = response_text.split("```python")[-1].split("```")[0].strip()
|
579 |
-
|
580 |
-
# Execute code
|
581 |
-
exe_result = execute_codes([code_to_execute], messages, executor)[0][0]
|
582 |
-
if exe_result is None:
|
583 |
-
text_result = "None"
|
584 |
-
images_result = None
|
585 |
-
else:
|
586 |
-
output, report = exe_result
|
587 |
-
if report == "Done":
|
588 |
-
error_result = None
|
589 |
-
try:
|
590 |
-
text_result = exe_result[0]['text']
|
591 |
-
except:
|
592 |
-
text_result = None
|
593 |
-
print("text result is none.")
|
594 |
-
try:
|
595 |
-
images_result = exe_result[0]['images']
|
596 |
-
except:
|
597 |
-
images_result = None
|
598 |
-
print("image result is none.")
|
599 |
-
else:
|
600 |
-
error_result = report
|
601 |
-
text_result = None
|
602 |
-
images_result = None
|
603 |
-
|
604 |
-
messages, new_image_clue_idx = update_messages_with_execute_content(data['image_nums_in_input'], messages, images_result, text_result, error_result, image_clue_idx)
|
605 |
-
image_clue_idx = new_image_clue_idx
|
606 |
-
|
607 |
-
code_execution_count += 1
|
608 |
-
|
609 |
-
# Generate next response part
|
610 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
611 |
-
args,
|
612 |
-
messages,
|
613 |
-
client,
|
614 |
-
max_tokens=max_tokens,
|
615 |
-
stop=["</code>"] if exe_code else None
|
616 |
-
)
|
617 |
-
|
618 |
-
else:
|
619 |
-
final_response = response_text
|
620 |
-
messages = update_messages_with_text(messages, response_text)
|
621 |
-
break
|
622 |
-
|
623 |
-
return messages, final_response
|
624 |
-
|
625 |
-
def evaluate_single_data_video(args, data, client, executor):
|
626 |
-
try:
|
627 |
-
prompt_template = args.prompt_template
|
628 |
-
prompt = args.prompt
|
629 |
-
exe_code = args.exe_code
|
630 |
-
max_tokens = args.max_tokens
|
631 |
-
except:
|
632 |
-
prompt_template = args['prompt_template']
|
633 |
-
prompt = args['prompt']
|
634 |
-
exe_code = args['exe_code']
|
635 |
-
max_tokens = args['max_tokens']
|
636 |
-
|
637 |
-
messages = process_prompt_init_multi_images(data["question"], data['image_path_list'], prompt_template, prompt)
|
638 |
-
|
639 |
-
# Generate initial response
|
640 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
641 |
-
args,
|
642 |
-
messages,
|
643 |
-
client,
|
644 |
-
max_tokens=max_tokens,
|
645 |
-
stop=["</code>"] if exe_code else None
|
646 |
-
)
|
647 |
-
|
648 |
-
# Handle response
|
649 |
-
final_response = response_text
|
650 |
-
code_execution_count = 0
|
651 |
-
image_clue_idx = data['image_nums_in_input']
|
652 |
-
|
653 |
-
while True:
|
654 |
-
# Check if code execution is needed
|
655 |
-
if exe_code and pred_stop_reason == "</code>":
|
656 |
-
# Extract code to execute
|
657 |
-
messages = update_messages_with_code(messages, response_text)
|
658 |
-
code_to_execute = response_text.split("```python")[-1].split("```")[0].strip()
|
659 |
-
|
660 |
-
# Execute code
|
661 |
-
exe_result = execute_codes([code_to_execute], messages, executor)[0][0]
|
662 |
-
if exe_result is None:
|
663 |
-
text_result = "None"
|
664 |
-
images_result = None
|
665 |
-
else:
|
666 |
-
output, report = exe_result
|
667 |
-
if report == "Done":
|
668 |
-
error_result = None
|
669 |
-
try:
|
670 |
-
text_result = exe_result[0]['text']
|
671 |
-
except:
|
672 |
-
text_result = None
|
673 |
-
print("text result is none.")
|
674 |
-
try:
|
675 |
-
images_result = exe_result[0]['images']
|
676 |
-
except:
|
677 |
-
images_result = None
|
678 |
-
print("image result is none.")
|
679 |
-
else:
|
680 |
-
error_result = report
|
681 |
-
text_result = None
|
682 |
-
images_result = None
|
683 |
-
|
684 |
-
messages, new_image_clue_idx = update_messages_with_execute_content(data['image_nums_in_input'], messages, images_result, text_result, error_result, image_clue_idx)
|
685 |
-
image_clue_idx = new_image_clue_idx
|
686 |
-
|
687 |
-
code_execution_count += 1
|
688 |
-
|
689 |
-
# Generate next response part
|
690 |
-
response_text, pred_stop_reason = call_chatgpt_api(
|
691 |
-
args,
|
692 |
-
messages,
|
693 |
-
client,
|
694 |
-
max_tokens=max_tokens,
|
695 |
-
stop=["</code>"] if exe_code else None
|
696 |
-
)
|
697 |
-
|
698 |
-
else:
|
699 |
-
final_response = response_text
|
700 |
-
messages = update_messages_with_text(messages, response_text)
|
701 |
-
break
|
702 |
-
|
703 |
-
return messages, final_response
|
704 |
-
|
705 |
-
|
706 |
-
# New wrapper functions for safe execution with cleanup
|
707 |
-
def evaluate_batch_with_cleanup(args, data_list, client):
|
708 |
-
"""Wrapper function to ensure proper cleanup of resources when processing multiple items"""
|
709 |
-
# Initialize executor with process isolation
|
710 |
-
executor = PythonExecutor(use_process_isolation=True)
|
711 |
-
|
712 |
-
try:
|
713 |
-
results = []
|
714 |
-
for data in data_list:
|
715 |
-
try:
|
716 |
-
result = evaluate_single_data(args, data, client, executor)
|
717 |
-
results.append(result)
|
718 |
-
except Exception as e:
|
719 |
-
print(f"Error processing data item: {str(e)}")
|
720 |
-
results.append((None, f"Error: {str(e)}"))
|
721 |
-
# Reset the executor for the next item
|
722 |
-
executor.reset()
|
723 |
-
|
724 |
-
return results
|
725 |
-
finally:
|
726 |
-
# Ensure cleanup of persistent worker
|
727 |
-
del executor
|
728 |
-
|
729 |
-
def evaluate_single_with_cleanup(args, data, client):
|
730 |
-
"""Wrapper function for evaluating a single item with proper cleanup"""
|
731 |
-
# Initialize executor with process isolation
|
732 |
-
executor = PythonExecutor(use_process_isolation=True)
|
733 |
-
|
734 |
-
try:
|
735 |
-
result = evaluate_single_data(args, data, client, executor)
|
736 |
-
return result
|
737 |
-
finally:
|
738 |
-
# Ensure cleanup of persistent worker
|
739 |
-
del executor
|
740 |
-
|
741 |
-
def evaluate_multi_images_with_cleanup(args, data_list, client):
|
742 |
-
"""Wrapper function for multi-image evaluation with proper cleanup"""
|
743 |
-
# Initialize executor with process isolation
|
744 |
-
executor = PythonExecutor(use_process_isolation=True)
|
745 |
-
|
746 |
-
try:
|
747 |
-
results = []
|
748 |
-
for data in data_list:
|
749 |
-
try:
|
750 |
-
result = evaluate_single_data_multi_images(args, data, client, executor)
|
751 |
-
results.append(result)
|
752 |
-
except Exception as e:
|
753 |
-
print(f"Error processing multi-image data: {str(e)}")
|
754 |
-
results.append((None, f"Error: {str(e)}"))
|
755 |
-
# Reset the executor for the next item
|
756 |
-
executor.reset()
|
757 |
-
|
758 |
-
return results
|
759 |
-
finally:
|
760 |
-
# Ensure cleanup of persistent worker
|
761 |
-
del executor
|
762 |
-
|
763 |
-
def evaluate_video_with_cleanup(args, data_list, client):
|
764 |
-
"""Wrapper function for video evaluation with proper cleanup"""
|
765 |
-
# Initialize executor with process isolation
|
766 |
-
executor = PythonExecutor(use_process_isolation=True)
|
767 |
-
|
768 |
-
try:
|
769 |
-
results = []
|
770 |
-
for data in data_list:
|
771 |
-
try:
|
772 |
-
result = evaluate_single_data_video(args, data, client, executor)
|
773 |
-
results.append(result)
|
774 |
-
except Exception as e:
|
775 |
-
print(f"Error processing video data: {str(e)}")
|
776 |
-
results.append((None, f"Error: {str(e)}"))
|
777 |
-
# Reset the executor for the next item
|
778 |
-
executor.reset()
|
779 |
-
|
780 |
-
return results
|
781 |
-
finally:
|
782 |
-
# Ensure cleanup of persistent worker
|
783 |
-
del executor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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