Spaces:
Sleeping
Sleeping
Update code_efficiency_calculator.py
Browse files- code_efficiency_calculator.py +59 -12
code_efficiency_calculator.py
CHANGED
@@ -19,7 +19,32 @@ import io
|
|
19 |
import os
|
20 |
import signal
|
21 |
from tqdm import tqdm
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
class TimeoutException(Exception):
|
25 |
pass
|
@@ -388,7 +413,7 @@ def calculate_line_efficiency(completion_file,entry_point):
|
|
388 |
# print(report_content)
|
389 |
|
390 |
except subprocess.CalledProcessError as e:
|
391 |
-
|
392 |
report_content = f"Error during the execution: {e}"
|
393 |
|
394 |
# # 清理临时文件
|
@@ -417,14 +442,36 @@ def humaneval_add_string_to_py_file(data,evaluation_code=False, path="./tmp/"):
|
|
417 |
else:
|
418 |
code.append(string)
|
419 |
data["completion"] = "\n".join(code)
|
|
|
|
|
420 |
try:
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
# with open(f"./{path}/{problem_idx}.py", "w") as f:
|
429 |
# f.write(full_code)
|
430 |
# return_path = f"./{path}/{problem_idx}.py"
|
@@ -477,7 +524,7 @@ def mbpp_add_string_to_py_file(data,evaluation_code=False, path="./tmp/"):
|
|
477 |
f.write(full_code)
|
478 |
return_path = f"./{path}/{problem_idx}.py"
|
479 |
except Exception as e:
|
480 |
-
|
481 |
pass
|
482 |
# print(return_path,full_code)
|
483 |
return return_path,full_code
|
@@ -524,7 +571,7 @@ def add_string_to_py_file(data,evaluation_code=False, path="./tmp/"):
|
|
524 |
else:
|
525 |
return_path = None
|
526 |
except Exception as e:
|
527 |
-
|
528 |
pass
|
529 |
return return_path,full_code
|
530 |
|
@@ -554,7 +601,7 @@ def calculate_code_execution_efficiency(data,evaluation_code=False,path="./tmp/"
|
|
554 |
problem_idx = data["problem_idx"]
|
555 |
completion_file,full_code = add_string_to_py_file(data,evaluation_code=evaluation_code, path=path)
|
556 |
except Exception as e:
|
557 |
-
|
558 |
completion_file = None
|
559 |
if completion_file == None:
|
560 |
# print("test")
|
@@ -583,7 +630,7 @@ The total execution time is: {canonical_solution_execution_time} s.
|
|
583 |
The maximum memory peak requirement is: {canonical_solution_max_memory_usage} MB.
|
584 |
"""
|
585 |
except Exception as e:
|
586 |
-
|
587 |
overhead = f"""
|
588 |
The code execution failed.
|
589 |
"""
|
|
|
19 |
import os
|
20 |
import signal
|
21 |
from tqdm import tqdm
|
22 |
+
import ast
|
23 |
|
24 |
+
def is_string_expression(node):
|
25 |
+
"""检查节点是否为字符串表达式(Str或Constant节点且值为字符串)"""
|
26 |
+
if isinstance(node, ast.Str):
|
27 |
+
return True
|
28 |
+
elif isinstance(node, ast.Constant):
|
29 |
+
return isinstance(node.value, str)
|
30 |
+
return False
|
31 |
+
|
32 |
+
def is_only_imports_and_function_headers(code):
|
33 |
+
try:
|
34 |
+
tree = ast.parse(code)
|
35 |
+
except SyntaxError:
|
36 |
+
return False
|
37 |
+
|
38 |
+
for node in tree.body:
|
39 |
+
if isinstance(node, (ast.Import, ast.ImportFrom)):
|
40 |
+
continue
|
41 |
+
elif isinstance(node, ast.FunctionDef):
|
42 |
+
for stmt in node.body:
|
43 |
+
if not (isinstance(stmt, ast.Expr) and is_string_expression(stmt.value)):
|
44 |
+
return False
|
45 |
+
else:
|
46 |
+
return False
|
47 |
+
return True
|
48 |
|
49 |
class TimeoutException(Exception):
|
50 |
pass
|
|
|
413 |
# print(report_content)
|
414 |
|
415 |
except subprocess.CalledProcessError as e:
|
416 |
+
print(f"Error during the execution: {e}")
|
417 |
report_content = f"Error during the execution: {e}"
|
418 |
|
419 |
# # 清理临时文件
|
|
|
442 |
else:
|
443 |
code.append(string)
|
444 |
data["completion"] = "\n".join(code)
|
445 |
+
solution1 = data["completion"]
|
446 |
+
flagg = 0
|
447 |
try:
|
448 |
+
try:
|
449 |
+
while("```python" in solution1):
|
450 |
+
start_idx = solution1.find(f"```python")
|
451 |
+
solution1 = solution1[start_idx+len(f"```python"):]
|
452 |
+
if "```" in solution1:
|
453 |
+
end_idx = solution1.find("```")
|
454 |
+
if data["entry_point"] in solution1[:end_idx]:
|
455 |
+
if is_only_imports_and_function_headers(solution1[:end_idx]):
|
456 |
+
solution1 = solution1[end_idx+len(f"```"):]
|
457 |
+
else:
|
458 |
+
data["completion"] = solution1[:end_idx]
|
459 |
+
flagg = 1
|
460 |
+
break
|
461 |
+
else:
|
462 |
+
solution1 = solution1[end_idx+len(f"```"):]
|
463 |
+
else:
|
464 |
+
break
|
465 |
+
except:
|
466 |
+
pass
|
467 |
+
if flagg == 0:
|
468 |
+
if f"```python" in data["completion"]:
|
469 |
+
start_idx = data["completion"].find(f"```python")
|
470 |
+
data["completion"] = data["completion"][start_idx+len(f"```python"):]
|
471 |
+
if "```" in data["completion"]:
|
472 |
+
end_idx = data["completion"].find("```")
|
473 |
+
data["completion"] = data["completion"][:end_idx]
|
474 |
+
full_code = import_pkg+ "\n"+data["prompt"] + "\n"+data["completion"] + "\n" + test_case +"\n"+f"check({data['entry_point']})"
|
475 |
# with open(f"./{path}/{problem_idx}.py", "w") as f:
|
476 |
# f.write(full_code)
|
477 |
# return_path = f"./{path}/{problem_idx}.py"
|
|
|
524 |
f.write(full_code)
|
525 |
return_path = f"./{path}/{problem_idx}.py"
|
526 |
except Exception as e:
|
527 |
+
print(e)
|
528 |
pass
|
529 |
# print(return_path,full_code)
|
530 |
return return_path,full_code
|
|
|
571 |
else:
|
572 |
return_path = None
|
573 |
except Exception as e:
|
574 |
+
print(e)
|
575 |
pass
|
576 |
return return_path,full_code
|
577 |
|
|
|
601 |
problem_idx = data["problem_idx"]
|
602 |
completion_file,full_code = add_string_to_py_file(data,evaluation_code=evaluation_code, path=path)
|
603 |
except Exception as e:
|
604 |
+
print(e)
|
605 |
completion_file = None
|
606 |
if completion_file == None:
|
607 |
# print("test")
|
|
|
630 |
The maximum memory peak requirement is: {canonical_solution_max_memory_usage} MB.
|
631 |
"""
|
632 |
except Exception as e:
|
633 |
+
print(e)
|
634 |
overhead = f"""
|
635 |
The code execution failed.
|
636 |
"""
|