{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7776182,"sourceType":"datasetVersion","datasetId":4549979},{"sourceId":7776528,"sourceType":"datasetVersion","datasetId":4550247}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install  -U transformers\n!pip install  -U accelerate\n!pip install  -U bitsandbytes","metadata":{"execution":{"iopub.status.busy":"2024-03-06T13:15:28.188604Z","iopub.execute_input":"2024-03-06T13:15:28.189191Z","iopub.status.idle":"2024-03-06T13:16:19.274578Z","shell.execute_reply.started":"2024-03-06T13:15:28.189156Z","shell.execute_reply":"2024-03-06T13:16:19.273641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import BitsAndBytesConfig, AutoTokenizer, AutoModelForCausalLM\nimport torch\nimport accelerate\n\nuse_4bit = True\nbnb_4bit_compute_dtype = \"float16\"\nbnb_4bit_quant_type = \"nf4\"\nuse_double_nested_quant = True\ncompute_dtype = getattr(torch, bnb_4bit_compute_dtype)\n\n# BitsAndBytesConfig 4-bit config\nbnb_config = BitsAndBytesConfig(\n    load_in_4bit=use_4bit,\n    bnb_4bit_use_double_quant=use_double_nested_quant,\n    bnb_4bit_quant_type=bnb_4bit_quant_type,\n    bnb_4bit_compute_dtype=compute_dtype,\n    load_in_8bit_fp32_cpu_offload=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T13:18:20.853646Z","iopub.execute_input":"2024-03-06T13:18:20.854057Z","iopub.status.idle":"2024-03-06T13:18:27.246729Z","shell.execute_reply.started":"2024-03-06T13:18:20.854025Z","shell.execute_reply":"2024-03-06T13:18:27.245997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = AutoTokenizer.from_pretrained(\"AlfredPros/CodeLlama-7b-Instruct-Solidity\")\nmodel = AutoModelForCausalLM.from_pretrained(\"AlfredPros/CodeLlama-7b-Instruct-Solidity\", quantization_config=bnb_config, device_map=\"balanced_low_0\")","metadata":{"execution":{"iopub.status.busy":"2024-03-06T13:35:39.014517Z","iopub.execute_input":"2024-03-06T13:35:39.015419Z","iopub.status.idle":"2024-03-06T13:51:19.213038Z","shell.execute_reply.started":"2024-03-06T13:35:39.015389Z","shell.execute_reply":"2024-03-06T13:51:19.211891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make input\ninput='Make a smart contract to create a whitelist of approved wallets. The purpose of this contract is to allow the DAO (Decentralized Autonomous Organization) to approve or revoke certain wallets, and also set a checker address for additional validation if needed. The current owner address can be changed by the current owner.'\n\n# Make prompt template\nprompt = f\"\"\"### Instruction:\nUse the Task below and the Input given to write the Response, which is a programming code that can solve the following Task:\n\n### Task:\n{input}\n\n### Solution:\n\"\"\"\n\n# Tokenize the input\ninput_ids = tokenizer(prompt, return_tensors=\"pt\", truncation=True).input_ids.cuda()\n# Run the model to infere an output\noutputs = model.generate(input_ids=input_ids, max_new_tokens=1024, do_sample=True, top_p=0.9, temperature=0.001, pad_token_id=1)\n\n# Detokenize and display the generated output\nprint(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0][len(prompt):])","metadata":{"execution":{"iopub.status.busy":"2024-03-06T13:51:51.593693Z","iopub.execute_input":"2024-03-06T13:51:51.594250Z","iopub.status.idle":"2024-03-06T13:52:12.954861Z","shell.execute_reply.started":"2024-03-06T13:51:51.594223Z","shell.execute_reply":"2024-03-06T13:52:12.953909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make input\ninput=\"\"\"Make a smart contract to fix the vulnerabilities present in the following smart contract. pragma solidity ^0.8.0;\n\ncontract VulnerableContract {\n    mapping(address => uint) balances;\n\n    function deposit() public payable {\n        balances[msg.sender] += msg.value;\n    }\n\n    function withdraw(uint amount) public {\n        require(balances[msg.sender] >= amount, \"Insufficient balance\");\n        \n        (bool success, ) = msg.sender.call{value: amount}(\"\");\n        require(success, \"Transfer failed\");\n        \n        balances[msg.sender] -= amount;\n    }\n}\n\"\"\"\n\n# Make prompt template\nprompt = f\"\"\"### Instruction:\nUse the Task below and the Input given to write the Response, which is a programming code that can solve the following Task:\n\n### Task:\n{input}\n\n### Solution:\n\"\"\"\n\n# Tokenize the input\ninput_ids = tokenizer(prompt, return_tensors=\"pt\", truncation=True).input_ids.cuda()\n# Run the model to infere an output\noutputs = model.generate(input_ids=input_ids, max_new_tokens=1024, do_sample=True, top_p=0.9, temperature=0.001, pad_token_id=1)\n\n# Detokenize and display the generated output\nprint(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0][len(prompt):])","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:07:41.614066Z","iopub.execute_input":"2024-03-06T14:07:41.614650Z","iopub.status.idle":"2024-03-06T14:07:54.386721Z","shell.execute_reply.started":"2024-03-06T14:07:41.614618Z","shell.execute_reply":"2024-03-06T14:07:54.385669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv(\"/kaggle/input/smartcontractdataset/file_change_exported_data_.csv\")\nprint(\"First 10 rows:\")\nprint(df.head(10))","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:16:42.716807Z","iopub.execute_input":"2024-03-06T14:16:42.717490Z","iopub.status.idle":"2024-03-06T14:16:43.225805Z","shell.execute_reply.started":"2024-03-06T14:16:42.717455Z","shell.execute_reply":"2024-03-06T14:16:43.224884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_rows = len(df)\nprint(\"\\nTotal number of rows:\", num_rows)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:17:00.860423Z","iopub.execute_input":"2024-03-06T14:17:00.861108Z","iopub.status.idle":"2024-03-06T14:17:00.865805Z","shell.execute_reply.started":"2024-03-06T14:17:00.861075Z","shell.execute_reply":"2024-03-06T14:17:00.864800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Original columns:\")\nprint(df.columns)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:17:08.316134Z","iopub.execute_input":"2024-03-06T14:17:08.316885Z","iopub.status.idle":"2024-03-06T14:17:08.323563Z","shell.execute_reply.started":"2024-03-06T14:17:08.316852Z","shell.execute_reply":"2024-03-06T14:17:08.322329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_to_keep = ['code_after', 'cod_befor']\ndf = df[columns_to_keep]\nprint(\"Removing columns that are not required:\")\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:17:16.616225Z","iopub.execute_input":"2024-03-06T14:17:16.616955Z","iopub.status.idle":"2024-03-06T14:17:16.633328Z","shell.execute_reply.started":"2024-03-06T14:17:16.616915Z","shell.execute_reply":"2024-03-06T14:17:16.632399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dropna(subset=['code_after', 'cod_befor'], inplace=True)\nprint(\"Removing rows with either of the values empty:\")\nprint(df)\nnum_rows = len(df)\nprint(\"\\nTotal number of rows:\", num_rows)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:17:28.179440Z","iopub.execute_input":"2024-03-06T14:17:28.179824Z","iopub.status.idle":"2024-03-06T14:17:28.193930Z","shell.execute_reply.started":"2024-03-06T14:17:28.179795Z","shell.execute_reply":"2024-03-06T14:17:28.192924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\ndef normalize_code(code):\n    # Remove leading and trailing whitespace\n    code = code.strip()\n    # Remove extra whitespace within lines\n    code = re.sub(r'\\s+', ' ', code)\n    # Remove empty lines\n    code = '\\n'.join(line for line in code.split('\\n') if line.strip())\n    return code\n\n# Function to remove '+' and '-' signs from the beginning of each line\ndef remove_signs(text):\n    lines = text.split('\\n')\n    cleaned_lines = []\n    for line in lines:\n        if line.startswith('+') or line.startswith('-'):\n            line = line[1:]\n        cleaned_lines.append(line)\n    return '\\n'.join(cleaned_lines)\n\n# Apply the function to each cell in both columns and normalize the code\ndf['code_after'] = df['code_after'].apply(remove_signs).apply(normalize_code)\ndf['cod_befor'] = df['cod_befor'].apply(remove_signs).apply(normalize_code)\n\n# Remove rows where codes in both columns are the same after normalization\ndf = df[df['code_after'] != df['cod_befor']]\n\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:18:41.930720Z","iopub.execute_input":"2024-03-06T14:18:41.931104Z","iopub.status.idle":"2024-03-06T14:18:42.780870Z","shell.execute_reply.started":"2024-03-06T14:18:41.931074Z","shell.execute_reply":"2024-03-06T14:18:42.779928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.iloc[3][0])\nprint(df.iloc[3][1])","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:19:02.825405Z","iopub.execute_input":"2024-03-06T14:19:02.825773Z","iopub.status.idle":"2024-03-06T14:19:02.831580Z","shell.execute_reply.started":"2024-03-06T14:19:02.825744Z","shell.execute_reply":"2024-03-06T14:19:02.830561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"dataset.csv\", index=False)\nfrom IPython.display import FileLink\nFileLink(\"dataset.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-06T14:20:59.436390Z","iopub.execute_input":"2024-03-06T14:20:59.437273Z","iopub.status.idle":"2024-03-06T14:20:59.962410Z","shell.execute_reply.started":"2024-03-06T14:20:59.437226Z","shell.execute_reply":"2024-03-06T14:20:59.961508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}