Datasets:
Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,126 +0,0 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import re
|
| 3 |
-
from typing import Union
|
| 4 |
-
|
| 5 |
-
import datasets
|
| 6 |
-
import evaluate as hf_evaluate
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
eval_logger = logging.getLogger(__name__)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
try:
|
| 13 |
-
pass_at_k = hf_evaluate.load("code_eval")
|
| 14 |
-
|
| 15 |
-
# run simple test to check code execution is enabled before model generation
|
| 16 |
-
test_cases = ["assert add(2, 3)==5"]
|
| 17 |
-
candidates = [["def add(a,b): return a*b"]]
|
| 18 |
-
results = pass_at_k.compute(references=test_cases, predictions=candidates, k=[1])
|
| 19 |
-
except Exception as e:
|
| 20 |
-
raise e
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def load_dataset(**kwargs):
|
| 24 |
-
"""Load MBPP long-context dataset with specified context length."""
|
| 25 |
-
context_length = kwargs.get("context_length", "128k")
|
| 26 |
-
|
| 27 |
-
eval_logger.info(
|
| 28 |
-
f"Loading mbpp_longcontext dataset: context_length={context_length}"
|
| 29 |
-
)
|
| 30 |
-
dataset = datasets.load_dataset("jannalu/mbpp-longcontext", name=context_length)
|
| 31 |
-
return dataset
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def pass_at_1(
|
| 35 |
-
references: Union[str, list[str]], predictions: Union[str, list[list[str]]]
|
| 36 |
-
) -> float:
|
| 37 |
-
"""Compute pass@1 metric for code generation."""
|
| 38 |
-
if isinstance(references, str):
|
| 39 |
-
references = [references]
|
| 40 |
-
if isinstance(predictions[0], str):
|
| 41 |
-
predictions = [[p] for p in predictions]
|
| 42 |
-
return pass_at_k.compute(
|
| 43 |
-
references=references,
|
| 44 |
-
predictions=predictions,
|
| 45 |
-
k=[1],
|
| 46 |
-
)[0]["pass@1"]
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def extract_code_blocks(text: str) -> str:
|
| 50 |
-
"""Extract code from markdown code blocks in generated text."""
|
| 51 |
-
# Pattern to match ```...``` blocks
|
| 52 |
-
pattern = r"```(?:\w+)?\n?(.*?)\n?```"
|
| 53 |
-
# (+ ```) as we add the opening "```python" to the gen_prefix
|
| 54 |
-
matches = re.findall(pattern, r"```" + text, re.DOTALL)
|
| 55 |
-
# if no matches, try to match ```...``` blocks (after removing the language)
|
| 56 |
-
if not matches:
|
| 57 |
-
text_without_lang = re.sub(r"```python", "```", text)
|
| 58 |
-
matches = re.findall(pattern, text_without_lang, re.DOTALL)
|
| 59 |
-
if not matches:
|
| 60 |
-
return ""
|
| 61 |
-
else:
|
| 62 |
-
return matches[0]
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def build_predictions(resps: list[list[str]], docs: list[dict]) -> list[list[str]]:
|
| 66 |
-
"""Build predictions by extracting code blocks from model responses."""
|
| 67 |
-
return [[extract_code_blocks(r) for r in resp] for resp in resps]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def doc_to_metadata(doc: dict) -> dict:
|
| 71 |
-
"""
|
| 72 |
-
Extract metadata from a document for tracking and analysis.
|
| 73 |
-
|
| 74 |
-
This extracts the context_length_tokens field so results can be
|
| 75 |
-
grouped and analyzed by sequence length.
|
| 76 |
-
"""
|
| 77 |
-
return {
|
| 78 |
-
"seq_length": doc.get("context_length_tokens", 0),
|
| 79 |
-
"context_id": doc.get("context_id", ""),
|
| 80 |
-
"context_type": doc.get("context_type", "narrative"),
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def list_fewshot_samples():
|
| 85 |
-
"""
|
| 86 |
-
Return few-shot examples for MBPP long-context.
|
| 87 |
-
Note: These examples do NOT include the long context since they're used for few-shot.
|
| 88 |
-
"""
|
| 89 |
-
return [
|
| 90 |
-
{
|
| 91 |
-
"task_id": 2,
|
| 92 |
-
"text": "Write a function to find the similar elements from the given two tuple lists.",
|
| 93 |
-
"code": "def similar_elements(test_tup1, test_tup2):\r\n res = tuple(set(test_tup1) & set(test_tup2))\r\n return (res) ",
|
| 94 |
-
"test_list": [
|
| 95 |
-
"assert similar_elements((3, 4, 5, 6),(5, 7, 4, 10)) == (4, 5)",
|
| 96 |
-
"assert similar_elements((1, 2, 3, 4),(5, 4, 3, 7)) == (3, 4)",
|
| 97 |
-
"assert similar_elements((11, 12, 14, 13),(17, 15, 14, 13)) == (13, 14)",
|
| 98 |
-
],
|
| 99 |
-
"is_fewshot": True,
|
| 100 |
-
"prompt_with_context": "Here is your task: Write a function to find the similar elements from the given two tuple lists. Your code should pass these tests:\n\nassert similar_elements((3, 4, 5, 6),(5, 7, 4, 10)) == (4, 5)\nassert similar_elements((1, 2, 3, 4),(5, 4, 3, 7)) == (3, 4)\nassert similar_elements((11, 12, 14, 13),(17, 15, 14, 13)) == (13, 14)\n[BEGIN]\n",
|
| 101 |
-
},
|
| 102 |
-
{
|
| 103 |
-
"task_id": 3,
|
| 104 |
-
"text": "Write a python function to identify non-prime numbers.",
|
| 105 |
-
"code": "import math\r\ndef is_not_prime(n):\r\n result = False\r\n for i in range(2,int(math.sqrt(n)) + 1):\r\n if n % i == 0:\r\n result = True\r\n return result",
|
| 106 |
-
"test_list": [
|
| 107 |
-
"assert is_not_prime(2) == False",
|
| 108 |
-
"assert is_not_prime(10) == True",
|
| 109 |
-
"assert is_not_prime(35) == True",
|
| 110 |
-
],
|
| 111 |
-
"is_fewshot": True,
|
| 112 |
-
"prompt_with_context": "Here is your task: Write a python function to identify non-prime numbers. Your code should pass these tests:\n\nassert is_not_prime(2) == False\nassert is_not_prime(10) == True\nassert is_not_prime(35) == True\n[BEGIN]\n",
|
| 113 |
-
},
|
| 114 |
-
{
|
| 115 |
-
"task_id": 4,
|
| 116 |
-
"text": "Write a function to find the largest integers from a given list of numbers using heap queue algorithm.",
|
| 117 |
-
"code": "import heapq as hq\r\ndef heap_queue_largest(nums,n):\r\n largest_nums = hq.nlargest(n, nums)\r\n return largest_nums",
|
| 118 |
-
"test_list": [
|
| 119 |
-
"assert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],3)==[85, 75, 65] ",
|
| 120 |
-
"assert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],2)==[85, 75] ",
|
| 121 |
-
"assert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],5)==[85, 75, 65, 58, 35]",
|
| 122 |
-
],
|
| 123 |
-
"is_fewshot": True,
|
| 124 |
-
"prompt_with_context": "Here is your task: Write a function to find the largest integers from a given list of numbers using heap queue algorithm. Your code should pass these tests:\n\nassert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],3)==[85, 75, 65] \nassert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],2)==[85, 75] \nassert heap_queue_largest( [25, 35, 22, 85, 14, 65, 75, 22, 58],5)==[85, 75, 65, 58, 35]\n[BEGIN]\n",
|
| 125 |
-
},
|
| 126 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|