{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaL4","dataSources":[{"sourceId":84795,"databundleVersionId":11281725,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":221548458,"sourceType":"kernelVersion"},{"sourceId":225599010,"sourceType":"kernelVersion"}],"dockerImageVersionId":30918,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import io\nimport os\nimport shutil\nimport subprocess\n\nimport pandas as pd\nimport polars as pl\n\nimport kaggle_evaluation.konwinski_prize_inference_server","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-06T13:06:17.25005Z","iopub.execute_input":"2025-03-06T13:06:17.250333Z","iopub.status.idle":"2025-03-06T13:06:32.990139Z","shell.execute_reply.started":"2025-03-06T13:06:17.250309Z","shell.execute_reply":"2025-03-06T13:06:32.989526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"instance_count = None\n\ndef get_number_of_instances(num_instances: int) -> None:\n    \"\"\" The very first message from the gateway will be the total number of instances to be served.\n    You don't need to edit this function.\n    \"\"\"\n    global instance_count\n    instance_count = num_instances","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T13:06:32.991095Z","iopub.execute_input":"2025-03-06T13:06:32.991722Z","iopub.status.idle":"2025-03-06T13:06:32.994957Z","shell.execute_reply.started":"2025-03-06T13:06:32.991696Z","shell.execute_reply":"2025-03-06T13:06:32.994349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re \ndef extract_test_counts(text):\n    failed = 0\n    passed = 0\n    lines = text.split('\\n')\n    for line in lines:\n        if re.search(r'(failed|passed)', line, re.IGNORECASE):\n            # Extract failed count\n            failed_match = re.search(r'(\\d+)\\s+failed', line, re.IGNORECASE)\n            current_failed = int(failed_match.group(1)) if failed_match else 0\n            # Extract passed count\n            passed_match = re.search(r'(\\d+)\\s+passed', line, re.IGNORECASE)\n            current_passed = int(passed_match.group(1)) if passed_match else 0\n            # Update counts\n            failed = current_failed\n            passed = current_passed\n    return failed, passed","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T13:06:32.995968Z","iopub.execute_input":"2025-03-06T13:06:32.996166Z","iopub.status.idle":"2025-03-06T13:06:33.017992Z","shell.execute_reply.started":"2025-03-06T13:06:32.996148Z","shell.execute_reply":"2025-03-06T13:06:33.017415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"i = 0\nquestions_done=50\nfail_count=0\nallowed_fails=10\nimport time\ndef predict(problem_statement: str, repo_archive: io.BytesIO, pip_packages_archive: io.BytesIO, env_setup_cmds_templates: list[str]) -> str:\n    \"\"\" Replace this function with your inference code.\n    Args:\n        problem_statement: The text of the git issue.\n        repo_path: A BytesIO buffer path with a .tar containing the codebase that must be patched. The gateway will make this directory available immediately before this function runs.\n        pip_packages_archive: A BytesIO buffer path with a .tar containing the wheel files necessary for running unit tests.\n        env_setup_cmds_templates: Commands necessary for installing the pip_packages_archive.\n    \"\"\"\n    global i\n    global questions_done\n    global fail_count\n    global allowed_fails\n    if i > questions_done:\n        return None\n\n    # Unpack the codebase to be patched into a directory that won't be exported when\n    # the notebook is saved.\n    archive_path = '/tmp/repo_archive.tar'\n    with open(archive_path, 'wb') as f:\n        f.write(repo_archive.read())\n    repo_path = 'repo'\n    if os.path.exists(repo_path):\n        shutil.rmtree(repo_path)\n    shutil.unpack_archive(archive_path, extract_dir=repo_path)\n    os.remove(archive_path)\n\n    \"\"\"\n    Unpack pip_packages if you want to run unit tests on your patch.\n    Note that editing unit tests with your patch -- even to add valid tests -- can cause your submission to be flagged as a failure.\n    Most of the relevant repos use pytest for running tests. You will almost certainly need to run only a subset of the unit tests to avoid running out of inference time.\n    \"\"\"\n    pip_archive_dir = '/tmp/pip_packages_archive.tar'\n    with open(pip_archive_dir, 'wb') as f:\n        f.write(pip_packages_archive.read())\n    pip_packages_path = '/kaggle/working/repo/pip_packages_path'\n    if os.path.exists(pip_packages_path):\n        shutil.rmtree(pip_packages_path)\n    shutil.unpack_archive(pip_archive_dir, extract_dir=pip_packages_path)\n    os.remove(pip_archive_dir)\n\n    env_setup_cmds = [cmd.format(pip_packages_path=pip_packages_path) for cmd in env_setup_cmds_templates]\n    env_setup_cmds.insert(2, \"unset PYTHONPATH\")\n    env_setup_cmds.insert(3, f\"export PYTHONPATH={pip_packages_path}:$PYTHONPATH\")\n    env_setup_cmds[-1] += \" --no-build-isolation\"\n    print(env_setup_cmds)\n    \n    # Run env setup for the repo\n    setup_result = subprocess.run(\n        \"\\n\".join(env_setup_cmds),\n        shell=True,\n        executable=\"/bin/bash\",\n        cwd=repo_path,\n        capture_output=True,\n        text=True,\n    )\n    print(\"=== SETUP ===\")\n    !ls repo\n    print(\"STDOUT\\n\", setup_result.stdout)\n    print(\"STDERR\\n\", setup_result.stderr)\n\n    print(\"=== TEST RUN TEST ===\")\n    with open(\"repo/myspecialtest.py\", 'w') as ff:\n        ff.write(\"def test_assert_right_1():\\n\\tassert 1 == 2\\ndef test_assert_wrong_1():\\n\\tassert 2+4==1\\ndef test_assert_right_2():\\n\\tassert 1-1==2\\ndef test_assert_wrong_2():\\n\\tassert 5+5==3\")\n    with open(\"repo/myspecialtest.py\", 'r') as ff:\n        print(ff.read())\n    cmds = ['source .venv/bin/activate', 'unset PYTHONPATH', f'export PYTHONPATH={pip_packages_path}:$PYTHONPATH', 'pytest myspecialtest.py -ra -q --tb=no']\n    print (cmds)\n    output = subprocess.run(\n        \"\\n\".join(cmds),\n        shell=True,\n        executable=\"/bin/bash\",\n        cwd=repo_path,\n        capture_output=True,\n        text=True,\n    )\n    out = output.stdout\n    err = output.stderr\n    print(\"Stdout:\", out)\n    print(\"Stderr:\", err)\n    test_patch_failed = False\n    if err != None and err != \"\" and len(err) > 1:\n        print(\"*** Failed to run ***\")\n        print(\"\\n# WARNING: This test threw some random errors. Pytest might be broken\")\n        test_patch_failed = True\n    if \"ERROR collecting my_super_custom_special\" in out.lower():\n        print(\"*** Failed to compile ***\")\n        print(\"\\n# WARNING: This test failed to collect. It is most likely broken.\")\n        test_patch_failed = True\n    if \"error during collection\" in out.lower():\n        print(\"*** Failed to compile ***\")\n        print(\"\\n# WARNING: This test failed to collect. It is most likely broken.\")\n        test_patch_failed = True\n    if test_patch_failed:\n        print(\"THE TEST FAILED WHEN IT DEFINITELY SHOULDN'T?\")\n        fail_count += 1\n    else:\n        print(\"TEST RAN SUCCESSFULLY\")\n        print(extract_test_counts(out))\n    if fail_count > allowed_fails:\n        print(\"KILL NOTEBOOK\")\n        time.sleep(60*60*9)\n        raise Exception(\"...\")\n    return \"Hello World\"\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T13:06:33.018704Z","iopub.execute_input":"2025-03-06T13:06:33.018899Z","iopub.status.idle":"2025-03-06T13:06:33.0343Z","shell.execute_reply.started":"2025-03-06T13:06:33.018881Z","shell.execute_reply":"2025-03-06T13:06:33.03375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.konwinski_prize_inference_server.KPrizeInferenceServer(\n    get_number_of_instances,   \n    predict\n)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/konwinski-prize/',  # Path to the entire competition dataset\n            '/kaggle/tmp/konwinski-prize/',   # Path to a scratch directory for unpacking data.a_zip.\n        ),\n        use_concurrency=True,  # This can safely be disabled for purposes of local testing if necessary.\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-06T13:06:33.034871Z","iopub.execute_input":"2025-03-06T13:06:33.035057Z","execution_failed":"2025-03-06T13:08:44.268Z"}},"outputs":[],"execution_count":null}]}