Team Ai
Apppublic

bigcode/Reasoning-with-StarCoder

sourceHugging Faceupdated 3y agoView on Hugging Face
32likes
pal.py41 linesDownload Raw Back to root
1from prompt import prompt2import re3from utils import generate_response, run_code4 5 6def post_process_code(code, question):7    parameters = code.split("\n")[0].split("def solution")[-1][1:-2].split(",")8    if '' in parameters:9        parameters.remove('')10    values = re.findall(r"[-+]?\d*\.\d+|\d+", question)[:len(parameters)]11    values = [int(v) for v in values]12    return list(zip(parameters, values))13 14 15def solve_pal(question):16    question = question.strip()17    query = prompt.format(question=question).strip()18    code = generate_response(query, 0.9)19    code = code.split("def solution():")[-1].strip()20    code = "def solution():\n" + code21    # code preprocessing22 23    arguments = post_process_code(code, question)24 25    arg_string = ""26    for param, val in arguments:27        arg_string += f"{param}={val},"28    func_call = f"\nprint(solution({arg_string[:-1]}))"29    code += func_call30    # code running31    if "input(" in code:32        return None, code33    pred = None34    try:35        pred = run_code(code)36    except Exception as ex:37        return None, code38    return pred, code39 40 41