Update shared_vis_python_exe.py
Browse files- shared_vis_python_exe.py +114 -52
shared_vis_python_exe.py
CHANGED
@@ -204,82 +204,144 @@ class PythonExecutor:
|
|
204 |
def process_generation_to_code(self, gens: str):
|
205 |
return [g.split("\n") for g in gens]
|
206 |
|
207 |
-
def execute(
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
if get_answer_from_stdout:
|
226 |
program_io = io.StringIO()
|
227 |
with redirect_stdout(program_io):
|
228 |
-
# 移除 timeout 调用
|
229 |
runtime.exec_code("\n".join(code))
|
230 |
program_io.seek(0)
|
231 |
result = program_io.read()
|
232 |
elif answer_symbol:
|
233 |
-
# 移除 timeout 调用
|
234 |
runtime.exec_code("\n".join(code))
|
235 |
result = runtime._global_vars.get(answer_symbol, "")
|
236 |
elif answer_expr:
|
237 |
-
# 移除 timeout 调用
|
238 |
runtime.exec_code("\n".join(code))
|
239 |
-
# 移除 timeout 调用
|
240 |
result = runtime.eval_code(answer_expr)
|
241 |
else:
|
242 |
if len(code) > 1:
|
243 |
-
# 移除 timeout 调用
|
244 |
runtime.exec_code("\n".join(code[:-1]))
|
245 |
-
# 移除 timeout 调用
|
246 |
result = runtime.eval_code(code[-1])
|
247 |
else:
|
248 |
-
# 移除 timeout 调用
|
249 |
runtime.exec_code("\n".join(code))
|
250 |
result = ""
|
251 |
-
|
252 |
-
#
|
253 |
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
254 |
if captured_figures:
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
'images': captured_figures
|
260 |
-
}
|
261 |
-
else:
|
262 |
-
result = {'images': captured_figures}
|
263 |
else:
|
264 |
-
if result
|
265 |
-
|
266 |
-
'text': result,
|
267 |
-
}
|
268 |
-
|
269 |
-
|
270 |
report = "Done"
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
|
285 |
def apply(self, code, messages):
|
|
|
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,
|
210 |
+
# messages,
|
211 |
+
# get_answer_from_stdout=True,
|
212 |
+
# runtime_class=None,
|
213 |
+
# # run_time_instance=None,
|
214 |
+
# answer_symbol=None,
|
215 |
+
# answer_expr=None,
|
216 |
+
# # 移除 timeout_length 参数
|
217 |
+
# ) -> Tuple[Union[str, Dict[str, Any]], str]:
|
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()
|
227 |
+
# with redirect_stdout(program_io):
|
228 |
+
# # 移除 timeout 调用
|
229 |
+
# runtime.exec_code("\n".join(code))
|
230 |
+
# program_io.seek(0)
|
231 |
+
# result = program_io.read()
|
232 |
+
# elif answer_symbol:
|
233 |
+
# # 移除 timeout 调用
|
234 |
+
# runtime.exec_code("\n".join(code))
|
235 |
+
# result = runtime._global_vars.get(answer_symbol, "")
|
236 |
+
# elif answer_expr:
|
237 |
+
# # 移除 timeout 调用
|
238 |
+
# runtime.exec_code("\n".join(code))
|
239 |
+
# # 移除 timeout 调用
|
240 |
+
# result = runtime.eval_code(answer_expr)
|
241 |
+
# else:
|
242 |
+
# if len(code) > 1:
|
243 |
+
# # 移除 timeout 调用
|
244 |
+
# runtime.exec_code("\n".join(code[:-1]))
|
245 |
+
# # 移除 timeout 调用
|
246 |
+
# result = runtime.eval_code(code[-1])
|
247 |
+
# else:
|
248 |
+
# # 移除 timeout 调用
|
249 |
+
# runtime.exec_code("\n".join(code))
|
250 |
+
# result = ""
|
251 |
+
|
252 |
+
# # 检查是否有捕获的图像
|
253 |
+
# captured_figures = runtime._global_vars.get("_captured_figures", [])
|
254 |
+
# if captured_figures:
|
255 |
+
# # 如果有文本输出和图像,将它们组合
|
256 |
+
# if result:
|
257 |
+
# result = {
|
258 |
+
# 'text': result,
|
259 |
+
# 'images': captured_figures
|
260 |
+
# }
|
261 |
+
# else:
|
262 |
+
# result = {'images': captured_figures}
|
263 |
+
# else:
|
264 |
+
# if result:
|
265 |
+
# result = {
|
266 |
+
# 'text': result,
|
267 |
+
# }
|
268 |
+
|
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 |
+
answer_symbol=None,
|
291 |
+
answer_expr=None,
|
292 |
+
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
293 |
+
print(runtime_class)
|
294 |
+
runtime = self.persistent_runtime
|
295 |
+
|
296 |
+
try:
|
297 |
if get_answer_from_stdout:
|
298 |
program_io = io.StringIO()
|
299 |
with redirect_stdout(program_io):
|
|
|
300 |
runtime.exec_code("\n".join(code))
|
301 |
program_io.seek(0)
|
302 |
result = program_io.read()
|
303 |
elif answer_symbol:
|
|
|
304 |
runtime.exec_code("\n".join(code))
|
305 |
result = runtime._global_vars.get(answer_symbol, "")
|
306 |
elif answer_expr:
|
|
|
307 |
runtime.exec_code("\n".join(code))
|
|
|
308 |
result = runtime.eval_code(answer_expr)
|
309 |
else:
|
310 |
if len(code) > 1:
|
|
|
311 |
runtime.exec_code("\n".join(code[:-1]))
|
|
|
312 |
result = runtime.eval_code(code[-1])
|
313 |
else:
|
|
|
314 |
runtime.exec_code("\n".join(code))
|
315 |
result = ""
|
316 |
+
|
317 |
+
# Check for captured figures
|
318 |
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
319 |
if captured_figures:
|
320 |
+
result = {
|
321 |
+
'text': result,
|
322 |
+
'images': captured_figures
|
323 |
+
} if result else {'images': captured_figures}
|
|
|
|
|
|
|
|
|
324 |
else:
|
325 |
+
result = {'text': result} if result else {}
|
326 |
+
|
|
|
|
|
|
|
|
|
327 |
report = "Done"
|
328 |
+
|
329 |
+
except Exception as e:
|
330 |
+
result = {
|
331 |
+
'error': str(e),
|
332 |
+
'traceback': traceback.format_exc()
|
333 |
+
}
|
334 |
+
report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
335 |
+
|
336 |
+
# Ensure result is serializable
|
337 |
+
try:
|
338 |
+
pickle.dumps(result)
|
339 |
+
except Exception as e:
|
340 |
+
result = f"Result serialization error: {str(e)}"
|
341 |
+
report = f"Serialization Error: {str(e)}"
|
342 |
+
|
343 |
+
return result, report
|
344 |
+
|
345 |
|
346 |
|
347 |
def apply(self, code, messages):
|