From 7bd2814a4d81a73922aec53c48feffbb57178a34 Mon Sep 17 00:00:00 2001 From: Jonas Thamane <166150947+NathiJonas@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:40:31 +0200 Subject: [PATCH] Create Jonas Thamane Week 4 PR.ipynb --- .../Jonas Thamane Week 4 PR.ipynb | 328 ++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 community-contributions/Jonas Thamane Week 4 PR.ipynb 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": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "CLAUDE_MODELS = [\n", + " \"claude-sonnet-4-20250514\",\n", + " \"claude-opus-4-20250514\",\n", + " \"claude-haiku-4-5-20251001\",\n", + "]\n", + "\n", + "with gr.Blocks(title=\"AI Coding Assistant\") as ui:\n", + "\n", + " gr.Markdown(\"# AI Coding Assistant\")\n", + " gr.Markdown(\n", + " \"Add comments, explain, fix, write tests, or **convert Python → C++**. \"\n", + " \"Pick a Claude model, tick your tasks, paste code, and hit **Run**.\"\n", + " )\n", + "\n", + "\n", + " model = gr.Dropdown(\n", + " choices=CLAUDE_MODELS,\n", + " value=CLAUDE_MODELS[0],\n", + " label=\"Claude Model\",\n", + " )\n", + "\n", + "\n", + " tasks = gr.CheckboxGroup(\n", + " choices=TASK_OPTIONS,\n", + " value=[\"Convert to C++\"],\n", + " label=\"Tasks\",\n", + " )\n", + "\n", + " \n", + " code_input = gr.Code(\n", + " value=SAMPLE_PYTHON,\n", + " language=\"python\",\n", + " label=\"Python Code\",\n", + " lines=25,\n", + " )\n", + "\n", + " with gr.Row():\n", + " run_btn = gr.Button(\"▶ Run\", variant=\"primary\")\n", + " clear_btn = gr.Button(\"🗑 Clear output\")\n", + "\n", + " output = gr.Markdown(label=\"Output\")\n", + "\n", + " run_btn.click(\n", + " run_code_assistant,\n", + " inputs=[model, tasks, code_input],\n", + " outputs=output,\n", + " )\n", + " clear_btn.click(lambda: \"\", outputs=output)\n", + "\n", + "ui.launch()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}