stzhao commited on
Commit
25aad26
·
verified ·
1 Parent(s): 28ad5ef

Update shared_vis_python_exe.py

Browse files
Files changed (1) hide show
  1. shared_vis_python_exe.py +0 -148
shared_vis_python_exe.py CHANGED
@@ -104,7 +104,6 @@ plt.close()
104
 
105
  exec(modified_code, self._global_vars)
106
  else:
107
- print("###################################### I am excuting code. ##############################################")
108
  exec(code_piece, self._global_vars)
109
 
110
  def eval_code(self, expr: str) -> Any:
@@ -140,7 +139,6 @@ class ImageRuntime(GenericRuntime):
140
  ]
141
 
142
  def __init__(self, messages):
143
- print("############################### I am initing image runtime ################################")
144
  super().__init__()
145
  # pdb.set_trace()
146
 
@@ -153,7 +151,6 @@ class ImageRuntime(GenericRuntime):
153
 
154
  image_var_dict = {}
155
  image_var_idx = 0
156
- print("############################### I am initing image runtime ################################")
157
  for message_item in messages:
158
  content = message_item['content'] # {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
159
  for item in content:
@@ -165,7 +162,6 @@ class ImageRuntime(GenericRuntime):
165
  image_var_idx += 1
166
 
167
  self.inject(image_var_dict)
168
- print("##################### Initialize ImageRuntime. ##########################")
169
 
170
 
171
  class DateRuntime(GenericRuntime):
@@ -195,7 +191,6 @@ class PythonExecutor:
195
  get_answer_from_stdout: bool = True,
196
  timeout_length: int = 20,
197
  ) -> None:
198
- print(f"#################### When Init PythonExcutor, RunTime typel:, TimeOut Length: {timeout_length} #############################")
199
  self.runtime_class = runtime_class if runtime_class else ImageRuntime
200
  print(self.runtime_class)
201
  self.answer_symbol = get_answer_symbol
@@ -205,81 +200,10 @@ class PythonExecutor:
205
 
206
  # Create a persistent runtime instance if messages are provided
207
  self.persistent_runtime = None
208
- # if messages:
209
- # self.persistent_runtime = self.runtime_class(messages)
210
 
211
  def process_generation_to_code(self, gens: str):
212
  return [g.split("\n") for g in gens]
213
 
214
- # @staticmethod
215
- # def execute(
216
- # self,
217
- # code,
218
- # messages,
219
- # get_answer_from_stdout=True,
220
- # runtime_class=None,
221
- # # run_time_instance=None,
222
- # answer_symbol=None,
223
- # answer_expr=None,
224
- # timeout_length=20,
225
- # ) -> Tuple[Union[str, Dict[str, Any]], str]:
226
- # # print("dome")
227
- # # try:
228
- # # 在每个进程中创建新的运行时实例
229
- # print(f"################################################## I am excuting ! #############################################################")
230
- # print(str(messages)[0:500])
231
- # print(str(messages)[-500:])
232
- # print(runtime_class)
233
- # # runtime = runtime_class(messages)
234
- # runtime = self.persistent_runtime
235
- # print(f"################################################## I am excuting ! #############################################################")
236
-
237
- # if get_answer_from_stdout:
238
- # program_io = io.StringIO()
239
- # with redirect_stdout(program_io):
240
- # timeout(timeout_length)(runtime.exec_code)("\n".join(code))
241
- # program_io.seek(0)
242
- # result = program_io.read()
243
- # elif answer_symbol:
244
- # timeout(timeout_length)(runtime.exec_code)("\n".join(code))
245
- # result = runtime._global_vars.get(answer_symbol, "")
246
- # elif answer_expr:
247
- # timeout(timeout_length)(runtime.exec_code)("\n".join(code))
248
- # result = timeout(timeout_length)(runtime.eval_code)(answer_expr)
249
- # else:
250
- # if len(code) > 1:
251
- # timeout(timeout_length)(runtime.exec_code)("\n".join(code[:-1]))
252
- # result = timeout(timeout_length)(runtime.eval_code)(code[-1])
253
- # else:
254
- # timeout(timeout_length)(runtime.exec_code)("\n".join(code))
255
- # result = ""
256
-
257
- # # 检查是否有捕获的图像
258
- # captured_figures = runtime._global_vars.get("_captured_figures", [])
259
- # if captured_figures:
260
- # # 如果有文本输出和图像,将它们组合
261
- # if result:
262
- # result = {
263
- # 'text': result,
264
- # 'images': captured_figures
265
- # }
266
- # else:
267
- # result = {'images': captured_figures}
268
-
269
- # report = "Done"
270
- # # except Exception as e:
271
- # # result = ""
272
- # # report = f"Error: {str(e)}\n{traceback.format_exc()}"
273
-
274
- # # 确保结果可序列化
275
- # try:
276
- # pickle.dumps(result)
277
- # except Exception as e:
278
- # result = f"Result serialization error: {str(e)}"
279
- # report = f"Serialization Error: {str(e)}"
280
-
281
- # return result, report
282
-
283
  def execute(
284
  self,
285
  code,
@@ -294,13 +218,9 @@ class PythonExecutor:
294
  # print("dome")
295
  # try:
296
  # 在每个进程中创建新的运行时实例
297
- # print(f"################################################## I am excuting ! #############################################################")
298
- # print(str(messages)[0:500])
299
- # print(str(messages)[-500:])
300
  print(runtime_class)
301
  # runtime = runtime_class(messages)
302
  runtime = self.persistent_runtime
303
- # print(f"################################################## I am excuting ! #############################################################")
304
 
305
  if get_answer_from_stdout:
306
  program_io = io.StringIO()
@@ -386,72 +306,6 @@ class PythonExecutor:
386
  def get_persistent_runtime(self):
387
  return self.persistent_runtime
388
 
389
- # def batch_apply(self, batch_code, messages):
390
- # if not self.persistent_runtime and messages:
391
- # self.persistent_runtime = self.runtime_class(messages)
392
- # all_code_snippets = self.process_generation_to_code(batch_code)
393
-
394
- # timeout_cnt = 0
395
- # all_exec_results = []
396
- # print(f"################################### num of cpu: {os.cpu_count()} ; len of code: {len(all_code_snippets)} ######################################")
397
- # with ProcessPool(
398
- # max_workers=min(len(all_code_snippets), os.cpu_count())
399
- # ) as pool:
400
- # executor = partial(
401
- # self.execute,
402
- # get_answer_from_stdout=self.get_answer_from_stdout,
403
- # runtime_class=self.runtime_class,
404
- # # run_time_instance=self.persistent_runtime,
405
- # answer_symbol=self.answer_symbol,
406
- # answer_expr=self.answer_expr,
407
- # timeout_length=self.timeout_length,
408
- # )
409
- # future = pool.map(executor, all_code_snippets, [messages], timeout=self.timeout_length)
410
- # iterator = future.result()
411
-
412
- # if len(all_code_snippets) > 100:
413
- # progress_bar = tqdm(total=len(all_code_snippets), desc="Execute")
414
- # else:
415
- # progress_bar = None
416
-
417
- # while True:
418
- # try:
419
- # result = next(iterator)
420
- # all_exec_results.append(result)
421
- # except StopIteration:
422
- # break
423
- # except TimeoutError as error:
424
- # print(error)
425
- # all_exec_results.append(("", "Timeout Error"))
426
- # timeout_cnt += 1
427
- # except Exception as error:
428
- # print(f"Error in batch_apply: {error}")
429
- # all_exec_results.append(("", f"Error: {str(error)}"))
430
- # if progress_bar is not None:
431
- # progress_bar.update(1)
432
-
433
- # if progress_bar is not None:
434
- # progress_bar.close()
435
-
436
- # batch_results = []
437
- # for code, (res, report) in zip(all_code_snippets, all_exec_results):
438
- # # 处理结果
439
- # if isinstance(res, dict):
440
- # # 如果结果包含图像,特殊处理
441
- # if 'text' in res:
442
- # res['text'] = str(res['text']).strip()
443
- # res['text'] = self.truncate(res['text'])
444
- # report = str(report).strip()
445
- # report = self.truncate(report)
446
- # else:
447
- # # 普通文本结果
448
- # res = str(res).strip()
449
- # res = self.truncate(res)
450
- # report = str(report).strip()
451
- # report = self.truncate(report)
452
- # batch_results.append((res, report))
453
- # return batch_results
454
-
455
  def batch_apply(self, batch_code, messages):
456
  if not self.persistent_runtime and messages:
457
  self.persistent_runtime = self.runtime_class(messages)
@@ -459,8 +313,6 @@ class PythonExecutor:
459
 
460
  timeout_cnt = 0
461
  all_exec_results = []
462
-
463
- print(f"################################### num of cpu: {os.cpu_count()} ; len of code: {len(all_code_snippets)} ######################################")
464
 
465
  # 去掉 ProcessPool,改为单进程顺序执行
466
  if len(all_code_snippets) > 100:
 
104
 
105
  exec(modified_code, self._global_vars)
106
  else:
 
107
  exec(code_piece, self._global_vars)
108
 
109
  def eval_code(self, expr: str) -> Any:
 
139
  ]
140
 
141
  def __init__(self, messages):
 
142
  super().__init__()
143
  # pdb.set_trace()
144
 
 
151
 
152
  image_var_dict = {}
153
  image_var_idx = 0
 
154
  for message_item in messages:
155
  content = message_item['content'] # {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
156
  for item in content:
 
162
  image_var_idx += 1
163
 
164
  self.inject(image_var_dict)
 
165
 
166
 
167
  class DateRuntime(GenericRuntime):
 
191
  get_answer_from_stdout: bool = True,
192
  timeout_length: int = 20,
193
  ) -> None:
 
194
  self.runtime_class = runtime_class if runtime_class else ImageRuntime
195
  print(self.runtime_class)
196
  self.answer_symbol = get_answer_symbol
 
200
 
201
  # Create a persistent runtime instance if messages are provided
202
  self.persistent_runtime = None
 
 
203
 
204
  def process_generation_to_code(self, gens: str):
205
  return [g.split("\n") for g in gens]
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  def execute(
208
  self,
209
  code,
 
218
  # print("dome")
219
  # try:
220
  # 在每个进程中创建新的运行时实例
 
 
 
221
  print(runtime_class)
222
  # runtime = runtime_class(messages)
223
  runtime = self.persistent_runtime
 
224
 
225
  if get_answer_from_stdout:
226
  program_io = io.StringIO()
 
306
  def get_persistent_runtime(self):
307
  return self.persistent_runtime
308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  def batch_apply(self, batch_code, messages):
310
  if not self.persistent_runtime and messages:
311
  self.persistent_runtime = self.runtime_class(messages)
 
313
 
314
  timeout_cnt = 0
315
  all_exec_results = []
 
 
316
 
317
  # 去掉 ProcessPool,改为单进程顺序执行
318
  if len(all_code_snippets) > 100: