{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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,"sourceType":"competition"},{"sourceId":10986730,"sourceType":"datasetVersion","datasetId":6430548},{"sourceId":162990,"sourceType":"modelInstanceVersion","modelInstanceId":138615,"modelId":161088}],"dockerImageVersionId":30887,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":22.341371,"end_time":"2024-12-11T03:22:13.479076","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-12-11T03:21:51.137705","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --no-index /kaggle/input/helper-packages/humanfriendly-10.0-py2.py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/coloredlogs-15.0.1-py2.py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/gekko-1.2.1-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/huggingface_hub-0.27.1-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/peft-0.14.0-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/rouge-1.0.1-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/auto_gptq-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/optimum-1.23.3-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/tree_sitter-0.21.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/tree_sitter_languages-1.10.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --quiet\n!pip install --no-index /kaggle/input/helper-packages/kowpy-1.0.1-py3-none-any.whl --quiet\n!pip install --no-index /kaggle/input/konwinski-prize/kprize_setup/pip_packages/kprize/unidiff-0.7.5-py2.py3-none-any.whl --quiet","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T01:27:45.071934Z","iopub.execute_input":"2025-02-10T01:27:45.07239Z","iopub.status.idle":"2025-02-10T01:28:43.36456Z","shell.execute_reply.started":"2025-02-10T01:27:45.072345Z","shell.execute_reply":"2025-02-10T01:28:43.362936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import io\nimport os\nimport shutil\nimport subprocess\nimport unidiff\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport torch\nimport gc\n\nimport kowpy\nfrom kaggle_evaluation.konwinski_prize_inference_server import KPrizeInferenceServer","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2025-02-10T01:28:43.366233Z","iopub.execute_input":"2025-02-10T01:28:43.366617Z","iopub.status.idle":"2025-02-10T01:28:43.373009Z","shell.execute_reply.started":"2025-02-10T01:28:43.366584Z","shell.execute_reply":"2025-02-10T01:28:43.371588Z"},"papermill":{"duration":14.873526,"end_time":"2024-12-11T03:22:08.818755","exception":false,"start_time":"2024-12-11T03:21:53.945229","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The evaluation API requires that you set up a server which will respond to inference requests. We have already defined the server; you just need write the predict function. When we evaluate your submission on the hidden test set the client defined in `konwinski_prize_gateway` will run in a different container with direct access to the hidden test set and hand off the data.\n\nYour code will always have access to the published copies of the files.","metadata":{"papermill":{"duration":0.002032,"end_time":"2024-12-11T03:22:08.823897","exception":false,"start_time":"2024-12-11T03:22:08.821865","status":"completed"},"tags":[]}},{"cell_type":"code","source":"instance_count = None\nfirst_prediction = True\n\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\n\n\ndef is_valid_patch_format(patch: str) -> bool:\n    \"\"\"\n    A quick check to confirm if a patch could be valid.\n    \"\"\"\n    if not(isinstance(patch, str)):\n        return False\n    try:\n        patch_set = unidiff.PatchSet(patch)\n        if len(patch_set) == 0:\n            return False\n    except Exception:\n        return False\n    return True\n\n\n# model_name = \"/kaggle/input/qwen2.5/transformers/7b-instruct-gptq-int4/1\"\n# search_model = kowpy.TextGenerator(model_name)\n\nmodel_name = \"/kaggle/input/qwen2.5-coder/transformers/14b-instruct-gptq-int4/1\"\n# fix_model = kowpy.TextGenerator(model_name)\nmodel = kowpy.TextGenerator(model_name)\n\n\ndef predict(\n    problem_statement: str, \n    repo_archive: io.BytesIO, \n    pip_packages_archive: io.BytesIO, \n    env_setup_cmds_templates: list[str],\n) -> 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. \n            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 first_prediction \n    \n    repo_path = \"repo\"\n    archive_path = \"/tmp/repo_archive.tar\"\n    with open(archive_path, \"wb\") as f:\n        f.write(repo_archive.read())\n\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    try:\n        mods = kowpy.run_pipeline(\n            repo_path=repo_path, \n            problem=problem_statement, \n            search_model=model,\n            search_skip=True,\n            search_fallback=False,\n            fix_model=model,\n            fix_tokens=2800,\n            tokens_per_second=2,\n            verbose=False,\n            print_list=[\"search_output\", \"ranked_matches\", \"time_check\"],\n        )\n        torch.cuda.empty_cache()\n        gc.collect()\n        print(\"*\" * 95)\n        print(mods)\n    except Exception as e:\n        print(\">>> Exception encountered...\")\n        print(str(e))\n        return None\n    finally:\n        if os.path.exists(repo_path):\n            shutil.rmtree(repo_path)\n\n    if is_valid_patch_format(mods):\n        print(\"!!! PATCH FORMAT VALIDATED\")\n        if kowpy.has_substantive_changes(mods):\n            return mods\n        else:\n            print(\"!!! PATCH IS USELESS\")\n    else:\n        print(\"!!! INVALID PATCH FORMAT\")\n        return None","metadata":{"execution":{"iopub.status.busy":"2025-02-10T01:28:43.375692Z","iopub.execute_input":"2025-02-10T01:28:43.37608Z","iopub.status.idle":"2025-02-10T01:28:43.394247Z","shell.execute_reply.started":"2025-02-10T01:28:43.376048Z","shell.execute_reply":"2025-02-10T01:28:43.392832Z"},"papermill":{"duration":0.011382,"end_time":"2024-12-11T03:22:08.852112","exception":false,"start_time":"2024-12-11T03:22:08.84073","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"When your notebook is run on the hidden test set, inference_server.serve must be called within 15 minutes of the notebook starting or the gateway will throw an error. If you need more than 15 minutes to load your model you can do so during the very first predict call, which does not have the usual 30 minute response deadline.","metadata":{"papermill":{"duration":0.001889,"end_time":"2024-12-11T03:22:08.856283","exception":false,"start_time":"2024-12-11T03:22:08.854394","status":"completed"},"tags":[]}},{"cell_type":"code","source":"inference_server = KPrizeInferenceServer(get_number_of_instances, predict)\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":{"execution":{"iopub.status.busy":"2025-02-10T01:28:43.395582Z","iopub.execute_input":"2025-02-10T01:28:43.396025Z","execution_failed":"2025-02-10T01:32:26.683Z"},"papermill":{"duration":3.790202,"end_time":"2024-12-11T03:22:12.648591","exception":false,"start_time":"2024-12-11T03:22:08.858389","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}