{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.14"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":84795,"databundleVersionId":10462807,"sourceType":"competition"},{"sourceId":212677085,"sourceType":"kernelVersion"},{"sourceId":4690,"sourceType":"modelInstanceVersion","modelInstanceId":3480,"modelId":1437}],"dockerImageVersionId":30805,"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":"markdown","source":"\"I added two functions: one to display the total number of instances being handled, and another to save the total instance count to a specified file.\"","metadata":{}},{"cell_type":"code","source":"import os\nimport io\nimport pandas as pd\nimport polars as pl\n\n# Placeholder for Kaggle evaluation server import\ntry:\n    import kaggle_evaluation.konwinski_prize_inference_server as kprize_server\nexcept ImportError:\n    print(\"Kaggle evaluation server library not available. Ensure it's accessible in your environment.\")\n\n# Uncomment and modify if dependencies need to be installed\n# !pip install -q --requirement /kaggle/input/kprize-transformers/requirements.txt \\\n# --no-index --find-links file:/kaggle/input/kprize-transformers/\n\n# Uncomment to load a model from the transformers library\n# from transformers import AutoTokenizer, AutoModel\n# tokenizer = AutoTokenizer.from_pretrained(\"/kaggle/input/chatglm2/pytorch/6b/1\", trust_remote_code=True)\n# model = AutoModel.from_pretrained(\"/kaggle/input/chatglm2/pytorch/6b/1\", trust_remote_code=True).half().cuda()\n# model.eval()\n\n# Variable to store the total number of instances\ntotal_instances = None\n\ndef set_instance_count(num: int) -> None:\n    \"\"\"\n    Stores the total number of instances to process.\n    \n    Args:\n        num (int): Total number of instances to handle.\n    \"\"\"\n    global total_instances\n    total_instances = num\n\ndef generate_response(query: str, archive: io.BytesIO) -> str:\n    \"\"\"\n    Processes the input query and returns a response.\n\n    Args:\n        query (str): Input problem description.\n        archive (io.BytesIO): Compressed repository archive.\n\n    Returns:\n        str: Generated response or None if not available.\n    \"\"\"\n    # Uncomment to use the model for generating predictions\n    # formatted_query = f\"Answer this question:\\n{query[:768]}\\n\"\n    # formatted_query += \"If you don’t know, respond with 'None'.\"\n    # response, context = model.chat(tokenizer, formatted_query, max_length=1024, history=[])\n    # print(f'Response: {response}')\n    # if response != 'None':\n    #     return response\n\n    # Default response\n    return None\n\ndef display_instance_count() -> None:\n    \"\"\"\n    Displays the total number of instances being handled.\n    \"\"\"\n    if total_instances is not None:\n        print(f\"Total instances to process: {total_instances}\")\n    else:\n        print(\"Instance count has not been set.\")\n\ndef save_instance_count_to_file(file_path: str) -> None:\n    \"\"\"\n    Saves the total instance count to a specified file.\n\n    Args:\n        file_path (str): Path to the file where the count will be saved.\n    \"\"\"\n    if total_instances is not None:\n        with open(file_path, \"w\") as file:\n            file.write(f\"Total instances: {total_instances}\\n\")\n        print(f\"Instance count saved to {file_path}\")\n    else:\n        print(\"Instance count has not been set. Nothing to save.\")\n\n# Placeholder for instance count and prediction function\ndef get_number_of_instances() -> int:\n    \"\"\"\n    Placeholder for retrieving the number of instances.\n    Update with the actual logic as per the requirements.\n    \"\"\"\n    if total_instances is not None:\n        return total_instances\n    else:\n        raise ValueError(\"Instance count has not been set.\")\n\ndef predict(instance_data: dict) -> str:\n    \"\"\"\n    Placeholder for prediction logic.\n    Update this function based on the model's prediction requirements.\n    \"\"\"\n    return \"Prediction placeholder\"\n\n# Initialize the Kaggle evaluation server\ntry:\n    inference_server = kprize_server.KPrizeInferenceServer(\n        get_number_of_instances,\n        predict\n    )\n\n    if os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n        inference_server.serve()\n    else:\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.\n            )\n        )\nexcept NameError as e:\n    print(f\"Error initializing inference server: {e}\")\nexcept Exception as e:\n    print(f\"Unexpected error: {e}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T13:36:24.221063Z","iopub.execute_input":"2025-01-06T13:36:24.221779Z","iopub.status.idle":"2025-01-06T13:36:25.863599Z","shell.execute_reply.started":"2025-01-06T13:36:24.221745Z","shell.execute_reply":"2025-01-06T13:36:25.862713Z"}},"outputs":[],"execution_count":null}]}