import re import evaluate as hf_evaluate compute_ = hf_evaluate.load("code_eval") try: test_cases = ["assert add(2, 3)==5"] candidates = [["def add(a,b): return a*b"]] results = compute_.compute(references=test_cases, predictions=candidates, k=[1]) except Exception as e: raise e def pass_at_k_process(doc, results): global compute_ result = {"pass@1": 0} # We support a single prediction currently prediction = results[0][0] if prediction.strip() == "" or prediction is None: return result assert doc["test_code"] is not None # Compute res = compute_.compute( references=[doc["test_code"]], predictions=[[prediction]], k=[1], ) result["pass@1"] = res[0]['pass@1'] return result def build_predictions_chat_ds1000( resps: list[list[str]], docs: list[dict] ) -> list[list[str]]: out = list() for resp, _ in zip(resps, docs): out.append(list()) for r in resp: filtered = extract_longest_def_solve_code_block(r) if filtered is not None: out[-1].append(filtered['content']) else: out[-1].append(r) return out def extract_longest_def_solve_code_block(text: str) -> dict | None: """ Extracts the longest code block from text. A code block starts with ```language (e.g., ```python) and ends with ```. Returns dict with 'language', 'content', and 'length', or None if none found. """ # Matches ```language followed by content until ``` pattern = r'```([a-zA-Z0-9]+)\s*(.*?)\s*```' matches = re.findall(pattern, text, re.DOTALL) if not matches: return None # Filter only those including the solve definition matches = [(lang, content) for lang, content in matches if 'def solve' in content] if len(matches) == 0: return None # Select longest by content length longest = max(matches, key=lambda x: len(x[1].strip())) language, content = longest return { 'language': language, 'content': content, 'length': len(content) }