MCPcopy Create free account
hub / github.com/SalesforceAIResearch/perfcodegen / Extractor

Class Extractor

src/evaluate.py:22–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class Extractor(object):
23 def __init__(self, dataset, output_file) -> None:
24 self.dataset = Dataset(dataset, data_path = os.path.join("test_datasets/", dataset.replace("/", "_")))
25 self.output_file = output_file
26 self.outputs = json.load(open(self.output_file, "r"))
27
28 self.solutions = {}
29
30
31 def get_entrypoint(self, instance):
32 entry_point = ""
33 if self.dataset.name == "openai_humaneval":
34 entry_point += instance["entry_point"]
35
36 return entry_point
37
38 def process_solution(self, solution, instance):
39 if self.dataset.name == "openai_humaneval" and not solution.startswith("def"):
40 lines = solution.splitlines()
41 if len(lines) > 0:
42 lines[0] = " " + lines[0]
43 solution = instance["prompt"] + "\n".join(lines)
44 elif self.dataset.name == "openai_humaneval":
45 try:
46 ast.parse(solution)
47 except:
48 lines = solution.splitlines()
49 if len(lines) > 0:
50 lines[0] = " " + lines[0]
51 solution = instance["prompt"] + "\n".join(lines)
52
53 return solution
54
55
56 def get_solutions(self, codegen = False, chat = False):
57 for index, prompt in enumerate(self.outputs):
58 if prompt not in self.dataset.prompt2instance:
59 print('Cannot find the prompt of instance #{} in dataset, skipped.'.format(index))
60 continue
61 instance = self.dataset.prompt2instance[prompt]
62 solutions = []
63 if not self.outputs[prompt][1]:
64 continue
65 if not isinstance(self.outputs[prompt][0], list):
66 print(self.outputs[prompt][0])
67 continue
68 for code in self.outputs[prompt][0]:
69 solution = sanitize(code, self.get_entrypoint(instance), codegen = codegen, global_code = True if self.dataset.name in ["NTU-NLP-sg/xCodeEval", "codeparrot/apps"] else False, chat = chat)
70 solution = self.process_solution(solution, instance)
71 if solution not in solutions:
72 solutions.append(solution)
73
74 self.solutions[prompt] = solutions
75
76
77 def save_solutions(self):
78 filename = self.output_file.replace(".json", "_SOLUTIONS.json")
79 with open(filename, "w", encoding = "utf-8") as f:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected