diff --git a/community-contributions/Jonas Thamane Week 4 PR.ipynb b/community-contributions/Jonas Thamane Week 4 PR.ipynb new file mode 100644 index 0000000000..6c50ac1323 --- /dev/null +++ b/community-contributions/Jonas Thamane Week 4 PR.ipynb @@ -0,0 +1,328 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "intro", + "metadata": {}, + "source": [ + "# AI Coding Assistant\n", + "\n", + "Help you with writing code comments, unit tests, code fixing, and **converting Python to C++**.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "imports", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✓ Imports loaded\n" + ] + } + ], + "source": [ + "\n", + "import os\n", + "import anthropic\n", + "import gradio as gr\n", + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv(override=True)\n", + "\n", + "\n", + "MODEL = \"claude-sonnet-4-20250514\"\n", + "MAX_TOKENS = 4096\n", + "\n", + "SYSTEM_PROMPT = (\n", + " \"You are a senior software engineer fluent in both Python and C++. \"\n", + " \"When converting to C++, produce clean, compilable C++17 code with proper \"\n", + " \"memory management and idiomatic style.\"\n", + ")\n", + "\n", + "print(\"✓ Imports loaded\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "client", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✓ Client helper ready\n" + ] + } + ], + "source": [ + "\n", + "def get_client():\n", + " key = os.environ.get(\"ANTHROPIC_API_KEY\", \"\")\n", + " if not key:\n", + " raise ValueError(\n", + " \"ANTHROPIC_API_KEY not set. \"\n", + " \"Add it to your .env file or set it as an environment variable.\"\n", + " )\n", + " return anthropic.Anthropic(api_key=key)\n", + "\n", + "print(\"✓ Client helper ready\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "tasks", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✓ Task builder ready\n" + ] + } + ], + "source": [ + "\n", + "TASK_OPTIONS = [\n", + " \"Add Comments\",\n", + " \"Explain Code\",\n", + " \"Write Unit Tests\",\n", + " \"Fix Code\",\n", + " \"Convert to C++\",\n", + "]\n", + "\n", + "\n", + "def build_prompt(tasks, code):\n", + " instructions = []\n", + "\n", + " if \"Add Comments\" in tasks:\n", + " instructions.append(\"Add clear, helpful inline comments to the code.\")\n", + " if \"Explain Code\" in tasks:\n", + " instructions.append(\"Explain what the code does in plain English.\")\n", + " if \"Write Unit Tests\" in tasks:\n", + " instructions.append(\"Write comprehensive unit tests using pytest.\")\n", + " if \"Fix Code\" in tasks:\n", + " instructions.append(\"Identify and fix any bugs or issues in the code.\")\n", + " if \"Convert to C++\" in tasks:\n", + " instructions.append(\n", + " \"Convert the Python code to idiomatic, modern C++17. \"\n", + " \"Include all necessary headers, use appropriate STL containers and algorithms, \"\n", + " \"add a main() function with a usage example, and add comments explaining \"\n", + " \"key differences from the Python version.\"\n", + " )\n", + "\n", + " return f\"Tasks:\\n- {'\\n- '.join(instructions)}\\n\\nPython Code:\\n```python\\n{code}\\n```\"\n", + "\n", + "\n", + "print(\"✓ Task builder ready\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "generation", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✓ Generation function ready\n" + ] + } + ], + "source": [ + "\n", + "def run_code_assistant(model, tasks, code):\n", + " if not code.strip():\n", + " return \"Please provide some Python code.\"\n", + " if not tasks:\n", + " return \"Please select at least one task.\"\n", + "\n", + " try:\n", + " client = get_client()\n", + " user_prompt = build_prompt(tasks, code)\n", + "\n", + " response = client.messages.create(\n", + " model=model,\n", + " max_tokens=MAX_TOKENS,\n", + " system=SYSTEM_PROMPT,\n", + " messages=[{\"role\": \"user\", \"content\": user_prompt}],\n", + " )\n", + " return response.content[0].text\n", + "\n", + " except anthropic.AuthenticationError:\n", + " return \"⚠ Invalid API key. Check your ANTHROPIC_API_KEY.\"\n", + " except Exception as e:\n", + " return f\"⚠ Error: {e}\"\n", + "\n", + "\n", + "print(\"✓ Generation function ready\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "sample_code", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "SAMPLE_PYTHON = \"\"\"\\\n", + "class Stack:\n", + " def __init__(self):\n", + " self.items = []\n", + "\n", + " def push(self, item):\n", + " self.items.append(item)\n", + "\n", + " def pop(self):\n", + " if self.is_empty():\n", + " raise IndexError(\"pop from empty stack\")\n", + " return self.items.pop()\n", + "\n", + " def peek(self):\n", + " if self.is_empty():\n", + " raise IndexError(\"peek at empty stack\")\n", + " return self.items[-1]\n", + "\n", + " def is_empty(self):\n", + " return len(self.items) == 0\n", + "\n", + " def size(self):\n", + " return len(self.items)\n", + "\n", + "\n", + "def is_balanced(expression):\n", + " stack = Stack()\n", + " pairs = {\")\": \"(\", \"]\": \"[\", \"}\": \"{\"}\n", + " for char in expression:\n", + " if char in \"([{\":\n", + " stack.push(char)\n", + " elif char in \")]}\":\n", + " if stack.is_empty() or stack.pop() != pairs[char]:\n", + " return False\n", + " return stack.is_empty()\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ui", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7863\n", + "* To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "