Update shared_vis_python_exe.py
Browse files- shared_vis_python_exe.py +138 -62
shared_vis_python_exe.py
CHANGED
@@ -213,74 +213,150 @@ class PythonExecutor:
|
|
213 |
return [g.split("\n") for g in gens]
|
214 |
|
215 |
# @staticmethod
|
216 |
-
def execute(
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
|
|
284 |
def apply(self, code, messages):
|
285 |
return self.batch_apply([code], messages)[0]
|
286 |
|
|
|
213 |
return [g.split("\n") for g in gens]
|
214 |
|
215 |
# @staticmethod
|
216 |
+
# def execute(
|
217 |
+
# self,
|
218 |
+
# code,
|
219 |
+
# messages,
|
220 |
+
# get_answer_from_stdout=True,
|
221 |
+
# runtime_class=None,
|
222 |
+
# # run_time_instance=None,
|
223 |
+
# answer_symbol=None,
|
224 |
+
# answer_expr=None,
|
225 |
+
# timeout_length=20,
|
226 |
+
# ) -> Tuple[Union[str, Dict[str, Any]], str]:
|
227 |
+
# # print("dome")
|
228 |
+
# # try:
|
229 |
+
# # 在每个进程中创建新的运行时实例
|
230 |
+
# print(f"################################################## I am excuting ! #############################################################")
|
231 |
+
# print(str(messages)[0:500])
|
232 |
+
# print(str(messages)[-500:])
|
233 |
+
# print(runtime_class)
|
234 |
+
# # runtime = runtime_class(messages)
|
235 |
+
# runtime = self.persistent_runtime
|
236 |
+
# print(f"################################################## I am excuting ! #############################################################")
|
237 |
|
238 |
+
# if get_answer_from_stdout:
|
239 |
+
# program_io = io.StringIO()
|
240 |
+
# with redirect_stdout(program_io):
|
241 |
+
# timeout(timeout_length)(runtime.exec_code)("\n".join(code))
|
242 |
+
# program_io.seek(0)
|
243 |
+
# result = program_io.read()
|
244 |
+
# elif answer_symbol:
|
245 |
+
# timeout(timeout_length)(runtime.exec_code)("\n".join(code))
|
246 |
+
# result = runtime._global_vars.get(answer_symbol, "")
|
247 |
+
# elif answer_expr:
|
248 |
+
# timeout(timeout_length)(runtime.exec_code)("\n".join(code))
|
249 |
+
# result = timeout(timeout_length)(runtime.eval_code)(answer_expr)
|
250 |
+
# else:
|
251 |
+
# if len(code) > 1:
|
252 |
+
# timeout(timeout_length)(runtime.exec_code)("\n".join(code[:-1]))
|
253 |
+
# result = timeout(timeout_length)(runtime.eval_code)(code[-1])
|
254 |
+
# else:
|
255 |
+
# timeout(timeout_length)(runtime.exec_code)("\n".join(code))
|
256 |
+
# result = ""
|
257 |
|
258 |
+
# # 检查是否有捕获的图像
|
259 |
+
# captured_figures = runtime._global_vars.get("_captured_figures", [])
|
260 |
+
# if captured_figures:
|
261 |
+
# # 如果有文本输出和图像,将它们组合
|
262 |
+
# if result:
|
263 |
+
# result = {
|
264 |
+
# 'text': result,
|
265 |
+
# 'images': captured_figures
|
266 |
+
# }
|
267 |
+
# else:
|
268 |
+
# result = {'images': captured_figures}
|
269 |
|
270 |
+
# report = "Done"
|
271 |
+
# # except Exception as e:
|
272 |
+
# # result = ""
|
273 |
+
# # report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
274 |
|
275 |
+
# # 确保结果可序列化
|
276 |
+
# try:
|
277 |
+
# pickle.dumps(result)
|
278 |
+
# except Exception as e:
|
279 |
+
# result = f"Result serialization error: {str(e)}"
|
280 |
+
# report = f"Serialization Error: {str(e)}"
|
281 |
|
282 |
+
# return result, report
|
283 |
+
|
284 |
+
def execute(
|
285 |
+
self,
|
286 |
+
code,
|
287 |
+
messages,
|
288 |
+
get_answer_from_stdout=True,
|
289 |
+
runtime_class=None,
|
290 |
+
# run_time_instance=None,
|
291 |
+
answer_symbol=None,
|
292 |
+
answer_expr=None,
|
293 |
+
# 移除 timeout_length 参数
|
294 |
+
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
295 |
+
# print("dome")
|
296 |
+
# try:
|
297 |
+
# 在每个进程中创建新的运行时实例
|
298 |
+
print(f"################################################## I am excuting ! #############################################################")
|
299 |
+
print(str(messages)[0:500])
|
300 |
+
print(str(messages)[-500:])
|
301 |
+
print(runtime_class)
|
302 |
+
# runtime = runtime_class(messages)
|
303 |
+
runtime = self.persistent_runtime
|
304 |
+
print(f"################################################## I am excuting ! #############################################################")
|
305 |
+
|
306 |
+
if get_answer_from_stdout:
|
307 |
+
program_io = io.StringIO()
|
308 |
+
with redirect_stdout(program_io):
|
309 |
+
# 移除 timeout 调用
|
310 |
+
runtime.exec_code("\n".join(code))
|
311 |
+
program_io.seek(0)
|
312 |
+
result = program_io.read()
|
313 |
+
elif answer_symbol:
|
314 |
+
# 移除 timeout 调用
|
315 |
+
runtime.exec_code("\n".join(code))
|
316 |
+
result = runtime._global_vars.get(answer_symbol, "")
|
317 |
+
elif answer_expr:
|
318 |
+
# 移除 timeout 调用
|
319 |
+
runtime.exec_code("\n".join(code))
|
320 |
+
# 移除 timeout 调用
|
321 |
+
result = runtime.eval_code(answer_expr)
|
322 |
+
else:
|
323 |
+
if len(code) > 1:
|
324 |
+
# 移除 timeout 调用
|
325 |
+
runtime.exec_code("\n".join(code[:-1]))
|
326 |
+
# 移除 timeout 调用
|
327 |
+
result = runtime.eval_code(code[-1])
|
328 |
+
else:
|
329 |
+
# 移除 timeout 调用
|
330 |
+
runtime.exec_code("\n".join(code))
|
331 |
+
result = ""
|
332 |
+
|
333 |
+
# 检查是否有捕获的图像
|
334 |
+
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
335 |
+
if captured_figures:
|
336 |
+
# 如果有文本输出和图像,将它们组合
|
337 |
+
if result:
|
338 |
+
result = {
|
339 |
+
'text': result,
|
340 |
+
'images': captured_figures
|
341 |
+
}
|
342 |
+
else:
|
343 |
+
result = {'images': captured_figures}
|
344 |
+
|
345 |
+
report = "Done"
|
346 |
+
# except Exception as e:
|
347 |
+
# result = ""
|
348 |
+
# report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
349 |
+
|
350 |
+
# 确保结果可序列化
|
351 |
+
try:
|
352 |
+
pickle.dumps(result)
|
353 |
+
except Exception as e:
|
354 |
+
result = f"Result serialization error: {str(e)}"
|
355 |
+
report = f"Serialization Error: {str(e)}"
|
356 |
+
|
357 |
+
return result, report
|
358 |
|
359 |
+
|
360 |
def apply(self, code, messages):
|
361 |
return self.batch_apply([code], messages)[0]
|
362 |
|