{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"colab":{"background_save":true,"base_uri":"https://localhost:8080/"},"id":"ezmJ9nlAWzQO"},"outputs":[{"name":"stdout","output_type":"stream","text":["STEP 1: Installing required libraries... This may take a minute.\n","✅ Libraries installed successfully!\n","\n","STEP 2: Setting up Kaggle API credentials...\n","✅ Kaggle API key already configured.\n","\n","STEP 3: Downloading and preparing the dataset...\n","403 Client Error: Forbidden for url: https://www.kaggle.com/api/v1/datasets/metadata/iamthedan/code-instructions-25k-python\n","An error occurred while downloading or loading the dataset: Unable to find '/content/code_instructions_25k_python.csv'\n","Please check that your Kaggle API key is correct and that the dataset path is valid.\n","Creating a dummy dataset to proceed...\n","\n","STEP 4: Preprocessing and tokenizing data...\n"]},{"name":"stderr","output_type":"stream","text":["/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n","The secret `HF_TOKEN` does not exist in your Colab secrets.\n","To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n","You will be able to reuse this secret in all of your notebooks.\n","Please note that authentication is recommended but still optional to access public models or datasets.\n"," warnings.warn(\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"c35ca28e6e9b44aa8a87e64c1fc58f71","version_major":2,"version_minor":0},"text/plain":["tokenizer_config.json: 0%| | 0.00/2.32k [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"916148936c224d13adeeb23ff01635b9","version_major":2,"version_minor":0},"text/plain":["spiece.model: 0%| | 0.00/792k [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"d89d4c915b2e43a182358c76b780a1fb","version_major":2,"version_minor":0},"text/plain":["tokenizer.json: 0%| | 0.00/1.39M [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"b5463c0837704435ae983ef3ce5dfd20","version_major":2,"version_minor":0},"text/plain":["Map: 0%| | 0/1 [00:00\u003c?, ? examples/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"d3d4ae9d5e714581adc07eff15af890b","version_major":2,"version_minor":0},"text/plain":["Map: 0%| | 0/1 [00:00\u003c?, ? examples/s]"]},"metadata":{},"output_type":"display_data"},{"name":"stdout","output_type":"stream","text":["✅ Data has been tokenized and is ready for training.\n","\n","STEP 5: Starting the model fine-tuning process...\n","⚠️ This will take approximately 15-25 minutes. Please ensure your Colab runtime is set to GPU.\n"]},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"b97bd6735f8a4c6c986bb99e9933bffb","version_major":2,"version_minor":0},"text/plain":["config.json: 0%| | 0.00/1.21k [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"6ed7b7a7201b4cf5a3041ca4d11d744c","version_major":2,"version_minor":0},"text/plain":["model.safetensors: 0%| | 0.00/242M [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"7fdfb01ad0e9492b99308baf0b373892","version_major":2,"version_minor":0},"text/plain":["generation_config.json: 0%| | 0.00/147 [00:00\u003c?, ?B/s]"]},"metadata":{},"output_type":"display_data"},{"ename":"TypeError","evalue":"TrainingArguments.__init__() got an unexpected keyword argument 'evaluation_strategy'","output_type":"error","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m/tmp/ipython-input-3906405876.py\u001b[0m in \u001b[0;36m\u003ccell line: 0\u003e\u001b[0;34m()\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 122\u001b[0m \u001b[0;31m# Define the training arguments\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--\u003e 123\u001b[0;31m training_args = TrainingArguments(\n\u001b[0m\u001b[1;32m 124\u001b[0m \u001b[0moutput_dir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"./results\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0mnum_train_epochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mTypeError\u001b[0m: TrainingArguments.__init__() got an unexpected keyword argument 'evaluation_strategy'"]}],"source":["# @title NL2Py v2: Fine-Tuned Transformer Model with Gradio UI\n","\n","# =================================================================================================\n","# STEP 1: INSTALL LIBRARIES\n","# =================================================================================================\n","# Install all necessary packages quietly to keep the output clean.\n","# transformers[torch]: For the core model and training.\n","# datasets: To handle the data loading and processing.\n","# accelerate: Speeds up training on modern hardware.\n","# sentencepiece: The tokenizer library required by T5.\n","# kaggle: To download the dataset.\n","# gradio: To build the web UI.\n","print(\"STEP 1: Installing required libraries... This may take a minute.\")\n","!pip install transformers[torch] datasets accelerate sentencepiece kaggle gradio -q\n","print(\"✅ Libraries installed successfully!\")\n","\n","\n","# =================================================================================================\n","# STEP 2: KAGGLE API SETUP\n","# =================================================================================================\n","# This section handles the secure setup of your Kaggle API credentials.\n","print(\"\\nSTEP 2: Setting up Kaggle API credentials...\")\n","import os\n","from google.colab import files\n","\n","# Check if kaggle.json already exists to avoid re-uploading\n","if not os.path.exists(\"/root/.kaggle/kaggle.json\"):\n"," print(\"Please upload your 'kaggle.json' file downloaded from your Kaggle account.\")\n"," # Prompt the user to upload the kaggle.json file\n"," try:\n"," uploaded = files.upload()\n"," # Create the .kaggle directory and move the file there\n"," os.makedirs(\"/root/.kaggle/\", exist_ok=True)\n"," os.rename(list(uploaded.keys())[0], \"/root/.kaggle/kaggle.json\")\n"," os.chmod(\"/root/.kaggle/kaggle.json\", 600)\n"," print(\"✅ Kaggle API key uploaded and configured.\")\n"," except Exception as e:\n"," print(f\"An error occurred during file upload: {e}\")\n"," print(\"Please ensure you upload the correct 'kaggle.json' file.\")\n","else:\n"," print(\"✅ Kaggle API key already configured.\")\n","\n","\n","# =================================================================================================\n","# STEP 3: DOWNLOAD AND PREPARE THE DATASET\n","# =================================================================================================\n","print(\"\\nSTEP 3: Downloading and preparing the dataset...\")\n","from datasets import load_dataset, Dataset\n","\n","try:\n"," # Download the public dataset from Kaggle using the API\n"," # Dataset: \"Code Instructions 25k Python\" by Dan (iamthedan)\n"," # This dataset is ideal as it contains pairs of instructions and corresponding code.\n"," !kaggle datasets download -d iamthedan/code-instructions-25k-python --unzip -q\n","\n"," # Load the dataset using the Hugging Face datasets library\n"," dataset = load_dataset('csv', data_files='code_instructions_25k_python.csv', split='train')\n","\n"," # Split the dataset into a training and a testing set for evaluation\n"," dataset = dataset.train_test_split(test_size=0.1, seed=42) # Use a seed for reproducibility\n","\n"," print(f\"✅ Dataset downloaded and loaded. {len(dataset['train'])} training examples, {len(dataset['test'])} testing examples.\")\n"," print(\"\\nHere's a sample from the dataset:\")\n"," print(dataset['train'][0])\n","\n","except Exception as e:\n"," print(f\"An error occurred while downloading or loading the dataset: {e}\")\n"," print(\"Please check that your Kaggle API key is correct and that the dataset path is valid.\")\n"," # Create a dummy dataset to allow the rest of the script to run for demonstration\n"," print(\"Creating a dummy dataset to proceed...\")\n"," dummy_data = {\n"," \"instruction\": [\"create a list of numbers from 1 to 5\", \"print the string 'hello'\"],\n"," \"response\": [\"my_list = [1, 2, 3, 4, 5]\", \"print('hello')\"]\n"," }\n"," dataset = Dataset.from_dict(dummy_data).train_test_split(test_size=0.1, seed=42)\n","\n","\n","# =================================================================================================\n","# STEP 4: PREPROCESS DATA AND TOKENIZE\n","# =================================================================================================\n","print(\"\\nSTEP 4: Preprocessing and tokenizing data...\")\n","from transformers import AutoTokenizer\n","\n","# Define the model we are going to use. t5-small is efficient and powerful.\n","MODEL_NAME = \"t5-small\"\n","tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n","\n","# We must format the data into a prompt that T5 understands.\n","# The prefix tells the model what task to perform.\n","PREFIX = \"translate English to Python: \"\n","\n","def preprocess_function(examples):\n"," \"\"\"\n"," This function takes the raw text from the dataset and formats it for the T5 model.\n"," It adds the task-specific prefix and tokenizes both the input (instruction)\n"," and the output (code).\n"," \"\"\"\n"," inputs = [PREFIX + doc for doc in examples[\"instruction\"]]\n"," model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding=\"max_length\")\n","\n"," # Tokenize the target code snippets\n"," labels = tokenizer(text_target=examples[\"response\"], max_length=512, truncation=True, padding=\"max_length\")\n","\n"," model_inputs[\"labels\"] = labels[\"input_ids\"]\n"," return model_inputs\n","\n","# Apply the preprocessing function to the entire dataset\n","tokenized_dataset = dataset.map(preprocess_function, batched=True)\n","print(\"✅ Data has been tokenized and is ready for training.\")\n","\n","\n","# =================================================================================================\n","# STEP 5: FINE-TUNE THE T5 MODEL\n","# =================================================================================================\n","print(\"\\nSTEP 5: Starting the model fine-tuning process...\")\n","print(\"⚠️ This will take approximately 15-25 minutes. Please ensure your Colab runtime is set to GPU.\")\n","from transformers import T5ForConditionalGeneration, Trainer, TrainingArguments\n","\n","# Load the pre-trained T5 model\n","model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)\n","\n","# Define the training arguments\n","training_args = TrainingArguments(\n"," output_dir=\"./results\",\n"," num_train_epochs=3,\n"," per_device_train_batch_size=8,\n"," per_device_eval_batch_size=8,\n"," warmup_steps=500,\n"," weight_decay=0.01,\n"," logging_dir=\"./logs\",\n"," logging_steps=200,\n"," evaluation_strategy=\"epoch\",\n"," save_strategy=\"epoch\",\n"," load_best_model_at_end=True,\n"," report_to=\"none\", # Disables wandb/tensorboard logging for simplicity\n",")\n","\n","# Create the Trainer instance\n","trainer = Trainer(\n"," model=model,\n"," args=training_args,\n"," train_dataset=tokenized_dataset[\"train\"],\n"," eval_dataset=tokenized_dataset[\"test\"],\n",")\n","\n","# Start the training\n","trainer.train()\n","\n","print(\"✅ Model fine-tuning complete!\")\n","\n","# Save the final, fine-tuned model and tokenizer for later use\n","FINE_TUNED_MODEL_PATH = \"./fine-tuned-nl2py-model\"\n","trainer.save_model(FINE_TUNED_MODEL_PATH)\n","tokenizer.save_pretrained(FINE_TUNED_MODEL_PATH)\n","print(f\"✅ Final model saved to '{FINE_TUNED_MODEL_PATH}'\")\n","\n","\n","# =================================================================================================\n","# STEP 6: CREATE AND LAUNCH THE GRADIO UI\n","# =================================================================================================\n","print(\"\\nSTEP 6: Building and launching the Gradio web interface...\")\n","import gradio as gr\n","from transformers import pipeline\n","\n","# Load the fine-tuned model using the pipeline for easy inference\n","translator_pipeline = pipeline(\"text2text-generation\", model=FINE_TUNED_MODEL_PATH, tokenizer=FINE_TUNED_MODEL_PATH)\n","\n","def generate_python_code(english_command):\n"," \"\"\"\n"," Takes an English command, formats it with the prefix, and uses the\n"," fine-tuned model to generate the corresponding Python code.\n"," \"\"\"\n"," if not english_command:\n"," return \"\"\n"," try:\n"," # The pipeline handles tokenization and decoding automatically\n"," result = translator_pipeline(PREFIX + english_command, max_length=512)\n"," return result[0]['generated_text']\n"," except Exception as e:\n"," return f\"# An error occurred during generation: {e}\"\n","\n","# Define the Gradio interface\n","description = \"\"\"\n","**NL2Py v2: AI-Powered Code Generation.**\n","This interface is powered by a `t5-small` model fine-tuned on a public dataset of English-to-Python pairs.\n","It understands more complex and nuanced requests than the v1 rule-based prototype.\n","\"\"\"\n","\n","examples = [\n"," [\"create a list of numbers from 1 to 10\"],\n"," [\"write a function that takes two numbers and returns their sum\"],\n"," [\"read a csv file named 'data.csv' into a pandas dataframe\"],\n"," [\"sort the list `my_list` in descending order\"],\n"," [\"print the current date and time\"],\n","]\n","\n","# Create and launch the web UI\n","demo = gr.Interface(\n"," fn=generate_python_code,\n"," inputs=gr.Textbox(\n"," lines=3,\n"," label=\"Enter your command in English:\",\n"," placeholder=\"e.g., write a Python regular expression to find all email addresses in a string\"\n"," ),\n"," outputs=gr.Code(\n"," language=\"python\",\n"," label=\"Generated Python Code\"\n"," ),\n"," title=\"Natural Language to Python Converter v2\",\n"," description=description,\n"," examples=examples,\n"," theme=gr.themes.Soft(primary_hue=\"emerald\", secondary_hue=\"blue\"),\n"," allow_flagging=\"never\"\n",")\n","\n","# The launch() command will create an interactive UI in the output and a public link.\n","demo.launch(debug=True)"]}],"metadata":{"accelerator":"GPU","colab":{"authorship_tag":"ABX9TyNNqHMPUYbybD6rh9PxuoiB","gpuType":"T4","name":"","version":""},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}