Merge remote changes with local updates
This commit is contained in:
400
week4/community-contributions/Week4-Comments-Generator-DP.ipynb
Normal file
400
week4/community-contributions/Week4-Comments-Generator-DP.ipynb
Normal file
@@ -0,0 +1,400 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3e473bbd-a0c2-43bd-bf99-c749784d00c3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import gradio as gr\n",
|
||||
"import openai\n",
|
||||
"import anthropic\n",
|
||||
"import google.generativeai as genai\n",
|
||||
"import requests\n",
|
||||
"import json\n",
|
||||
"import os\n",
|
||||
"from typing import Dict, Any, Optional\n",
|
||||
"import asyncio\n",
|
||||
"from dotenv import load_dotenv"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "16210512-41f1-4de3-8348-2cd7129e023f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# load API\n",
|
||||
"load_dotenv(override=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6747e275-91eb-4d2b-90b6-805f2bd9b6b7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class CodeCommenter:\n",
|
||||
" def __init__(self):\n",
|
||||
" # Initialize API clients\n",
|
||||
" self.openai_client = None\n",
|
||||
" self.anthropic_client = None\n",
|
||||
" self.gemini_client = None\n",
|
||||
" \n",
|
||||
" # Load API keys from environment variables\n",
|
||||
" self.setup_clients()\n",
|
||||
" \n",
|
||||
" def setup_clients(self):\n",
|
||||
" \"\"\"Initialize API clients with keys from environment variables\"\"\"\n",
|
||||
" try:\n",
|
||||
" # OpenAI\n",
|
||||
" openai_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
" if openai_key:\n",
|
||||
" self.openai_client = openai.OpenAI(api_key=openai_key)\n",
|
||||
" \n",
|
||||
" # Anthropic\n",
|
||||
" anthropic_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
" if anthropic_key:\n",
|
||||
" self.anthropic_client = anthropic.Anthropic(api_key=anthropic_key)\n",
|
||||
" \n",
|
||||
" # Google Gemini\n",
|
||||
" gemini_key = os.getenv('GOOGLE_API_KEY')\n",
|
||||
" if gemini_key:\n",
|
||||
" genai.configure(api_key=gemini_key)\n",
|
||||
" self.gemini_client = genai.GenerativeModel('gemini-2.0-flash-exp')\n",
|
||||
" \n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Warning: Error setting up API clients: {e}\")\n",
|
||||
" \n",
|
||||
" def create_prompt(self, code: str, language: str) -> str:\n",
|
||||
" \"\"\"Create a prompt for the LLM to add comments and docstrings\"\"\"\n",
|
||||
" return f\"\"\"Please add detailed and helpful comments and docstrings to the following {language} code. \n",
|
||||
" \n",
|
||||
"Guidelines:\n",
|
||||
"1. Add comprehensive docstrings for functions, classes, and modules\n",
|
||||
"2. Add inline comments explaining complex logic\n",
|
||||
"3. Follow the commenting conventions for {language}\n",
|
||||
"4. Maintain the original code structure and functionality\n",
|
||||
"5. Make comments clear and professional\n",
|
||||
"6. Don't change the actual code logic, only add comments\n",
|
||||
"7. Do not add code markdown delimiters like ```python\n",
|
||||
"\n",
|
||||
"Here's the code to comment:\n",
|
||||
"\n",
|
||||
"{code}\n",
|
||||
"\n",
|
||||
"Please return only the commented code without any additional explanation or markdown formatting.\"\"\"\n",
|
||||
"\n",
|
||||
" def call_openai(self, prompt: str, model: str = \"gpt-4o-mini\") -> str:\n",
|
||||
" \"\"\"Make API call to OpenAI\"\"\"\n",
|
||||
" if not self.openai_client:\n",
|
||||
" return \"Error: OpenAI API key not configured. Please set OPENAI_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.openai_client.chat.completions.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"system\", \"content\": \"You are a helpful coding assistant that adds detailed comments and docstrings to code.\"},\n",
|
||||
" {\"role\": \"user\", \"content\": prompt}\n",
|
||||
" ],\n",
|
||||
" max_tokens=4000,\n",
|
||||
" temperature=0.1\n",
|
||||
" )\n",
|
||||
" return response.choices[0].message.content.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling OpenAI API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_anthropic(self, prompt: str, model: str = \"claude-3-5-haiku-20241022\") -> str:\n",
|
||||
" \"\"\"Make API call to Anthropic Claude\"\"\"\n",
|
||||
" if not self.anthropic_client:\n",
|
||||
" return \"Error: Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.anthropic_client.messages.create(\n",
|
||||
" model=model,\n",
|
||||
" max_tokens=4000,\n",
|
||||
" temperature=0.1,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"user\", \"content\": prompt}\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
" return response.content[0].text.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Anthropic API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_gemini(self, prompt: str) -> str:\n",
|
||||
" \"\"\"Make API call to Google Gemini\"\"\"\n",
|
||||
" if not self.gemini_client:\n",
|
||||
" return \"Error: Google API key not configured. Please set GOOGLE_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.gemini_client.generate_content(\n",
|
||||
" prompt,\n",
|
||||
" generation_config=genai.types.GenerationConfig(\n",
|
||||
" max_output_tokens=4000,\n",
|
||||
" temperature=0.1,\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" return response.text.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Gemini API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_ollama(self, prompt: str, model: str = \"llama3.2:latest\") -> str:\n",
|
||||
" \"\"\"Make API call to Ollama (local)\"\"\"\n",
|
||||
" try:\n",
|
||||
" url = \"http://localhost:11434/api/generate\"\n",
|
||||
" data = {\n",
|
||||
" \"model\": model,\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"stream\": False,\n",
|
||||
" \"options\": {\n",
|
||||
" \"temperature\": 0.1,\n",
|
||||
" \"num_predict\": 4000\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" response = requests.post(url, json=data, timeout=60)\n",
|
||||
" if response.status_code == 200:\n",
|
||||
" result = response.json()\n",
|
||||
" return result.get('response', '').strip()\n",
|
||||
" else:\n",
|
||||
" return f\"Error calling Ollama API: HTTP {response.status_code}\"\n",
|
||||
" except requests.exceptions.ConnectionError:\n",
|
||||
" return \"Error: Could not connect to Ollama. Make sure Ollama is running locally on port 11434.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Ollama API: {str(e)}\"\n",
|
||||
"\n",
|
||||
" def generate_comments(self, language: str, code: str, llm: str) -> str:\n",
|
||||
" \"\"\"Generate comments for the given code using the specified LLM\"\"\"\n",
|
||||
" if not code.strip():\n",
|
||||
" return \"Error: Please provide code to comment.\"\n",
|
||||
" \n",
|
||||
" prompt = self.create_prompt(code, language)\n",
|
||||
" \n",
|
||||
" # Route to appropriate LLM\n",
|
||||
" if llm == \"gpt-4o-mini\":\n",
|
||||
" return self.call_openai(prompt, \"gpt-4o-mini\")\n",
|
||||
" elif llm == \"claude-3-5-haiku-20241022\":\n",
|
||||
" return self.call_anthropic(prompt, \"claude-3-5-haiku-20241022\")\n",
|
||||
" elif llm == \"gemini-2.0-flash\":\n",
|
||||
" return self.call_gemini(prompt)\n",
|
||||
" elif llm == \"ollama:llama3.2:latest\":\n",
|
||||
" return self.call_ollama(prompt, \"llama3.2:latest\")\n",
|
||||
" else:\n",
|
||||
" return f\"Error: Unsupported LLM: {llm}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "813f0911-d53f-4887-9341-656712e32d8f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def create_gradio_interface():\n",
|
||||
" \"\"\"Create and configure the Gradio interface\"\"\"\n",
|
||||
" commenter = CodeCommenter()\n",
|
||||
" \n",
|
||||
" # Define the main function for the interface\n",
|
||||
" def process_code(language, code, llm):\n",
|
||||
" \"\"\"Process the code and return commented version\"\"\"\n",
|
||||
" if not code.strip():\n",
|
||||
" return \"Please enter some code to comment.\"\n",
|
||||
" \n",
|
||||
" # Show processing message\n",
|
||||
" processing_msg = f\"Processing {language} code with {llm}...\"\n",
|
||||
" print(processing_msg)\n",
|
||||
" \n",
|
||||
" # Generate comments\n",
|
||||
" result = commenter.generate_comments(language, code, llm)\n",
|
||||
" return result\n",
|
||||
" \n",
|
||||
" # Define default code\n",
|
||||
" default_code = \"\"\"import pyodbc\n",
|
||||
"from tabulate import tabulate\n",
|
||||
"def connect_to_sql_server(server_name, database, username=None, password=None):\n",
|
||||
" try:\n",
|
||||
" if username and password:\n",
|
||||
" connection_string = f\"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server_name};DATABASE={database};UID={username};PWD={password}\"\n",
|
||||
" else:\n",
|
||||
" connection_string = f\"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server_name};DATABASE={database};Trusted_Connection=yes\"\n",
|
||||
" connection = pyodbc.connect(connection_string)\n",
|
||||
" print(f\"Successfully connected to {server_name}/{database}\")\n",
|
||||
" return connection\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to connect to {server_name}/{database}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"def get_record_count(connection, table_name):\n",
|
||||
" try:\n",
|
||||
" cursor = connection.cursor()\n",
|
||||
" query = f\"SELECT COUNT(*) FROM {table_name}\"\n",
|
||||
" cursor.execute(query)\n",
|
||||
" count = cursor.fetchone()[0]\n",
|
||||
" cursor.close()\n",
|
||||
" print(f\"Record count for {table_name}: {count}\")\n",
|
||||
" return count\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to get record count for {table_name}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"def select_top_records(connection, table_name, n):\n",
|
||||
" try:\n",
|
||||
" cursor = connection.cursor()\n",
|
||||
" query = f\"SELECT TOP {n} * FROM {table_name}\"\n",
|
||||
" cursor.execute(query)\n",
|
||||
" records = cursor.fetchall()\n",
|
||||
" columns = [column[0] for column in cursor.description]\n",
|
||||
" cursor.close()\n",
|
||||
" print(f\"Top {n} records from {table_name}\")\n",
|
||||
" if records:\n",
|
||||
" print(tabulate(records, headers=columns, tablefmt=\"grid\"))\n",
|
||||
" return records\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to retrieve top {n} records from {table_name}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"conn = connect_to_sql_server(\"localhost\", \"AdventureWorks_lite\")\n",
|
||||
"if conn:\n",
|
||||
" total_records = get_record_count(conn, \"Sales.SalesOrderDetail\")\n",
|
||||
" top_records = select_top_records(conn, \"Production.Product\", 10)\n",
|
||||
" conn.close()\n",
|
||||
" print(\"Connection closed successfully\")\"\"\"\n",
|
||||
"\n",
|
||||
" css = \"\"\"\n",
|
||||
"textarea[rows]:not([rows=\"1\"]) {\n",
|
||||
" overflow-y: auto !important;\n",
|
||||
" scrollbar-width: thin !important;\n",
|
||||
"}\n",
|
||||
"textarea[rows]:not([rows=\"1\"])::-webkit-scrollbar {\n",
|
||||
" all: initial !important;\n",
|
||||
" background: #f1f1f1 !important;\n",
|
||||
"}\n",
|
||||
"textarea[rows]:not([rows=\"1\"])::-webkit-scrollbar-thumb {\n",
|
||||
" all: initial !important;\n",
|
||||
" background: #a8a8a8 !important;\n",
|
||||
"}\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
" # Create the interface\n",
|
||||
" with gr.Blocks(title=\"Code Commenter\", theme=gr.themes.Base(), css=css) as interface:\n",
|
||||
" gr.Markdown(\"# 🔧 Code Commenter\")\n",
|
||||
" gr.Markdown(\"Add detailed comments and docstrings to your code using various LLM models.\")\n",
|
||||
" \n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" code_input = gr.Textbox(\n",
|
||||
" label=\"Input Code\",\n",
|
||||
" value=default_code,\n",
|
||||
" lines=15,\n",
|
||||
" max_lines=20,\n",
|
||||
" info=\"Enter the code you want to add comments to\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with gr.Column():\n",
|
||||
" code_output = gr.Textbox(\n",
|
||||
" label=\"Commented Code\",\n",
|
||||
" lines=20,\n",
|
||||
" max_lines=20,\n",
|
||||
" info=\"Your code with added comments and docstrings\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column(scale=1):\n",
|
||||
" language_dropdown = gr.Dropdown(\n",
|
||||
" choices=[\"Python\", \"Ruby\", \"Rust\", \"C++\", \"Java\"],\n",
|
||||
" value=\"Python\",\n",
|
||||
" label=\"Programming Language\",\n",
|
||||
" info=\"Select the programming language of your code\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" llm_dropdown = gr.Dropdown(\n",
|
||||
" choices=[\n",
|
||||
" \"gpt-4o-mini\",\n",
|
||||
" \"claude-3-5-haiku-20241022\", \n",
|
||||
" \"gemini-2.0-flash\",\n",
|
||||
" \"ollama:llama3.2:latest\"\n",
|
||||
" ],\n",
|
||||
" value=\"gpt-4o-mini\",\n",
|
||||
" label=\"LLM Model\",\n",
|
||||
" info=\"Choose the language model to use\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" generate_btn = gr.Button(\n",
|
||||
" \"🚀 Generate Comments\", \n",
|
||||
" variant=\"primary\",\n",
|
||||
" size=\"lg\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" # Add some API setup information\n",
|
||||
" gr.Markdown(\"## 📝 API Setup Instructions\")\n",
|
||||
" gr.Markdown(\"\"\"\n",
|
||||
" To use this tool, you need to set up API keys as environment variables:\n",
|
||||
" \n",
|
||||
" - **OpenAI**: Set `OPENAI_API_KEY`\n",
|
||||
" - **Anthropic**: Set `ANTHROPIC_API_KEY` \n",
|
||||
" - **Google Gemini**: Set `GOOGLE_API_KEY`\n",
|
||||
" - **Ollama**: Make sure Ollama is running locally on port 11434\n",
|
||||
" \"\"\")\n",
|
||||
" \n",
|
||||
" # Connect the button to the processing function\n",
|
||||
" generate_btn.click(\n",
|
||||
" fn=process_code,\n",
|
||||
" inputs=[language_dropdown, code_input, llm_dropdown],\n",
|
||||
" outputs=code_output,\n",
|
||||
" show_progress=True\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" return interface"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ef461e08-c1d5-406d-b7d2-a4329f16486e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"🚀 Starting Code Commenter...\")\n",
|
||||
"print(\"📋 Setting up Gradio interface...\")\n",
|
||||
"\n",
|
||||
"# Create and launch the interface\n",
|
||||
"interface = create_gradio_interface()\n",
|
||||
"\n",
|
||||
"print(\"🌐 Launching interface...\")\n",
|
||||
"print(\"💡 The interface will open in your default browser\")\n",
|
||||
"print(\"🔧 Make sure to set up your API keys as environment variables\")\n",
|
||||
"\n",
|
||||
"# Launch with auto-opening in browser\n",
|
||||
"interface.launch(\n",
|
||||
" server_name=\"127.0.0.1\",\n",
|
||||
" server_port=7860,\n",
|
||||
" share=False,\n",
|
||||
" inbrowser=True,\n",
|
||||
" show_error=True\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,841 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Power Coder\n",
|
||||
"\n",
|
||||
"1. Convert code between two programming language; supporting languages are Python, Java, JavaScript, TypeScript, C, C++, C#, Go, Rust, Kotlin, Swift, PHP, Julia\n",
|
||||
"2. Automatically add docstring/comments based on selected comment style\n",
|
||||
"3. Automatically generate unit tests based on selected unit test style\n",
|
||||
"4. Supporting models: gpt-4o, claude-3-5-sonnet-20240620, gemini-2.5-flash\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# imports\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import io\n",
|
||||
"import sys\n",
|
||||
"import json\n",
|
||||
"import requests\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import google.generativeai\n",
|
||||
"import anthropic\n",
|
||||
"from IPython.display import Markdown, display, update_display\n",
|
||||
"import gradio as gr\n",
|
||||
"import subprocess"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# environment\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
|
||||
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
|
||||
"os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')\n",
|
||||
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# initialize\n",
|
||||
"\n",
|
||||
"openai = OpenAI()\n",
|
||||
"claude = anthropic.Anthropic()\n",
|
||||
"gemini_via_openai_client = OpenAI(\n",
|
||||
" api_key=os.environ['GOOGLE_API_KEY'], \n",
|
||||
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
|
||||
")\n",
|
||||
"OPENAI_MODEL = \"gpt-4o\"\n",
|
||||
"CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
|
||||
"GEMINI_MODEL = \"gemini-2.5-flash\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "37b204dd-f770-41d9-9b19-7e1baa5273cd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1. Convesion Part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_system_prompt_for(in_lang, out_lang):\n",
|
||||
" convert_system_message = f\"You are an assistant that reimplements {in_lang} code in high performance {out_lang}. \"\n",
|
||||
" convert_system_message += f\"Respond only with {out_lang} code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
|
||||
" convert_system_message += f\"The {out_lang} response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\"\n",
|
||||
" return convert_system_message"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_user_prompt_for(in_lang, out_lang, input_instruct, in_code):\n",
|
||||
" convert_user_prompt = f\"Rewrite this {in_lang} code in {out_lang} with the fastest possible implementation that produces identical output in the least time. \"\n",
|
||||
" convert_user_prompt += f\"Respond only with {out_lang} code; do not explain your work other than a few comments. \"\n",
|
||||
" convert_user_prompt += f\"Pay attention to number types to ensure no int overflows. Remember to include all necessary {out_lang} packages or modules, for example, iomanip for C++.\\n\\n\"\n",
|
||||
" if input_instruct:\n",
|
||||
" convert_user_prompt += \"Addtional instruction is: \" + input_instruct\n",
|
||||
" convert_user_prompt += in_code\n",
|
||||
" return convert_user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_messages_for(in_lang, out_lang, input_instruct, in_code):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": convert_system_prompt_for(in_lang, out_lang)},\n",
|
||||
" {\"role\": \"user\", \"content\": convert_user_prompt_for(in_lang, out_lang, input_instruct, in_code)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"python_hard = \"\"\"# Be careful to support large number sizes\n",
|
||||
"\n",
|
||||
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
|
||||
" value = seed\n",
|
||||
" while True:\n",
|
||||
" value = (a * value + c) % m\n",
|
||||
" yield value\n",
|
||||
" \n",
|
||||
"def max_subarray_sum(n, seed, min_val, max_val):\n",
|
||||
" lcg_gen = lcg(seed)\n",
|
||||
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
|
||||
" max_sum = float('-inf')\n",
|
||||
" for i in range(n):\n",
|
||||
" current_sum = 0\n",
|
||||
" for j in range(i, n):\n",
|
||||
" current_sum += random_numbers[j]\n",
|
||||
" if current_sum > max_sum:\n",
|
||||
" max_sum = current_sum\n",
|
||||
" return max_sum\n",
|
||||
"\n",
|
||||
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
|
||||
" total_sum = 0\n",
|
||||
" lcg_gen = lcg(initial_seed)\n",
|
||||
" for _ in range(20):\n",
|
||||
" seed = next(lcg_gen)\n",
|
||||
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
|
||||
" return total_sum\n",
|
||||
"\n",
|
||||
"# Parameters\n",
|
||||
"n = 10000 # Number of random numbers\n",
|
||||
"initial_seed = 42 # Initial seed for the LCG\n",
|
||||
"min_val = -10 # Minimum value of random numbers\n",
|
||||
"max_val = 10 # Maximum value of random numbers\n",
|
||||
"\n",
|
||||
"# Timing the function\n",
|
||||
"import time\n",
|
||||
"start_time = time.time()\n",
|
||||
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
|
||||
"end_time = time.time()\n",
|
||||
"\n",
|
||||
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
|
||||
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_stream_gpt(in_lang, out_lang, input_instruct, in_code): \n",
|
||||
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=convert_messages_for(in_lang, out_lang, input_instruct, in_code), temperature=0.0, stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8669f56b-8314-4582-a167-78842caea131",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_stream_claude(in_lang, out_lang, input_instruct, in_code):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=2000,\n",
|
||||
" temperature=0.0,\n",
|
||||
" system=convert_system_prompt_for(in_lang, out_lang),\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": convert_user_prompt_for(in_lang, out_lang, input_instruct, in_code)}],\n",
|
||||
" )\n",
|
||||
" reply = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" reply += text\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "01d3cd4f-c100-4e25-8670-0663513f6136",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def convert_stream_gemini(in_lang, out_lang, input_instruct, in_code): \n",
|
||||
" stream = gemini_via_openai_client.chat.completions.create(model=GEMINI_MODEL, messages=convert_messages_for(in_lang, out_lang, input_instruct, in_code), temperature=0.0, stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def optimize(in_lang, out_lang, in_code, input_instruct, convert_model):\n",
|
||||
" if \"gpt\" in convert_model.lower():\n",
|
||||
" result = convert_stream_gpt(in_lang, out_lang, input_instruct, in_code)\n",
|
||||
" elif \"claude\" in convert_model.lower():\n",
|
||||
" result = convert_stream_claude(in_lang, out_lang, input_instruct, in_code)\n",
|
||||
" elif \"gemini\" in convert_model.lower():\n",
|
||||
" result = convert_stream_gemini(in_lang, out_lang, input_instruct, in_code)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown convert model\")\n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "07383878-f887-464f-8bc7-527c669d3edd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2. Comment part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d254038c-fdd6-4ef8-8b7a-a074f1e7405d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_system_prompt_for(lang, comment_style):\n",
|
||||
" comment_system_message = f\"You are an assistant that generate necessary, concise and clear comment/docstring for the {lang} code by applying {comment_style} comment style. \"\n",
|
||||
" comment_system_message += f\"Respond only with added comments, and do not provide any redundant explanation. \"\n",
|
||||
" return comment_system_message"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e95cee4f-f229-4c9f-8e67-8a68cc9534c3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_user_prompt_for(lang, code, comment_style):\n",
|
||||
" comment_user_prompt = f\"Add the comments/docstring on the given code for the {lang} programming language in {comment_style} comment style. \"\n",
|
||||
" comment_user_prompt += f\"Respond only with added comments, and do not provide any redundant explanation.\\n\\n\"\n",
|
||||
" comment_user_prompt += f\"The given code is as follows: \"\n",
|
||||
" comment_user_prompt += code\n",
|
||||
" return comment_user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "507426c2-cf5a-4041-b904-b18a5afe83b6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_messages_for(lang, code, comment_style):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": comment_system_prompt_for(lang, comment_style)},\n",
|
||||
" {\"role\": \"user\", \"content\": comment_user_prompt_for(lang, code, comment_style)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7e1c8cf6-7a15-4e79-82f6-6bb2a0b85773",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_stream_gpt(lang, code, comment_style): \n",
|
||||
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=comment_messages_for(lang, code, comment_style), temperature=0.0, stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26f27781-4a3e-4e5f-a8ab-9a25944a9879",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_stream_claude(lang, code, comment_style):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=2000,\n",
|
||||
" temperature=0.0,\n",
|
||||
" system=comment_system_prompt_for(lang, comment_style),\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": comment_user_prompt_for(lang, code, comment_style)}],\n",
|
||||
" )\n",
|
||||
" reply = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" reply += text\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8e6719e7-f2f3-40ea-8fed-01d84a641306",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_stream_gemini(lang, code, comment_style): \n",
|
||||
" stream = gemini_via_openai_client.chat.completions.create(model=GEMINI_MODEL, messages=comment_messages_for(lang, code, comment_style), temperature=0.0, stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2b98acc4-23d8-4671-8f19-92d72631b55d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_comments_via_model(lang, code, comment_style, comment_model):\n",
|
||||
" if \"gpt\" in comment_model.lower():\n",
|
||||
" result = comment_stream_gpt(lang, code, comment_style)\n",
|
||||
" elif \"claude\" in comment_model.lower():\n",
|
||||
" result = comment_stream_claude(lang, code, comment_style)\n",
|
||||
" elif \"gemini\" in comment_model.lower():\n",
|
||||
" result = comment_stream_gemini(lang, code, comment_style)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown comment model\")\n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "282c75ae-d8c3-4866-a024-f7ecf87b3cde",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_comments_fn(comment_option, in_lang, out_lang, in_code, out_code, in_comment_style, out_comment_style, comment_model):\n",
|
||||
" if 'input' in comment_option:\n",
|
||||
" in_gen = generate_comments_via_model(in_lang, in_code, in_comment_style, comment_model)\n",
|
||||
" for in_output in in_gen:\n",
|
||||
" yield in_output, \"\"\n",
|
||||
" elif 'output' in comment_option:\n",
|
||||
" out_gen = generate_comments_via_model(out_lang, out_code, out_comment_style, comment_model)\n",
|
||||
" for out_output in out_gen:\n",
|
||||
" yield \"\", out_output\n",
|
||||
" elif 'both' in comment_option:\n",
|
||||
" in_gen = generate_comments_via_model(in_lang, in_code, in_comment_style, comment_model)\n",
|
||||
" out_gen = generate_comments_via_model(out_lang, out_code, out_comment_style, comment_model)\n",
|
||||
" for in_output, out_output in zip(in_gen, out_gen):\n",
|
||||
" yield in_output, out_output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce2c178c-d03c-49c0-b0e9-c57c699bca08",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 3. Unit test part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5a4743e-e1a8-42c7-8f1f-a73d49c0895d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_system_prompt_for(lang, unit_test_style):\n",
|
||||
" unit_test_system_message = f\"You are an assistant that generate necessary, concise, clear and executable unit tests for the {lang} code by applying {unit_test_style} unit test style. \"\n",
|
||||
" unit_test_system_message += f\"Respond only with generated unit tests; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
|
||||
" return unit_test_system_message"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "334d5e40-71ff-4d24-8cef-b6c81c188e4d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_user_prompt_for(lang, code, unit_test_style):\n",
|
||||
" unit_test_user_prompt = f\"Add the unit tests on the given code for the {lang} programming language in {unit_test_style} unit test style. \"\n",
|
||||
" unit_test_user_prompt += f\"Respond only with generated unit tests; use comments sparingly and do not provide any explanation other than occasional comments.\\n\\n\"\n",
|
||||
" unit_test_user_prompt += f\"The given code is as follows: \"\n",
|
||||
" unit_test_user_prompt += code\n",
|
||||
" return unit_test_user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8a8e061f-3993-4746-9425-d938d2537f65",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_messages_for(lang, code, unit_test_style):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": unit_test_system_prompt_for(lang, unit_test_style)},\n",
|
||||
" {\"role\": \"user\", \"content\": unit_test_user_prompt_for(lang, code, unit_test_style)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "71c1613b-7a16-4443-acec-d0a2d9bed192",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_stream_gpt(lang, code, unit_test_style): \n",
|
||||
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=unit_test_messages_for(lang, code, unit_test_style), stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8a6e3502-f7ff-42b8-8fc5-2697b2d1f36e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_stream_claude(lang, code, unit_test_style):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=2000,\n",
|
||||
" system=unit_test_system_prompt_for(lang, unit_test_style),\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": unit_test_user_prompt_for(lang, code, unit_test_style)}],\n",
|
||||
" )\n",
|
||||
" reply = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" reply += text\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8d7f694f-a276-4bdc-9cfb-755483fd4380",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_stream_gemini(lang, code, unit_test_style): \n",
|
||||
" stream = gemini_via_openai_client.chat.completions.create(model=GEMINI_MODEL, messages=unit_test_messages_for(lang, code, unit_test_style), stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c824429a-b18a-4320-8258-0141037a6531",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_unit_test_via_model(lang, code, unit_test_style, unit_test_model):\n",
|
||||
" if \"gpt\" in unit_test_model.lower():\n",
|
||||
" result = unit_test_stream_gpt(lang, code, unit_test_style)\n",
|
||||
" elif \"claude\" in unit_test_model.lower():\n",
|
||||
" result = unit_test_stream_claude(lang, code, unit_test_style)\n",
|
||||
" elif \"gemini\" in unit_test_model.lower():\n",
|
||||
" result = unit_test_stream_gemini(lang, code, unit_test_style)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown unit test model\")\n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c3e59e26-37c0-4429-b69c-deb581423dd0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_unit_test_fn(unit_test_option, in_lang, out_lang, in_code, out_code, in_unit_test_style, out_unit_test_style, unit_test_model):\n",
|
||||
" if 'input' in unit_test_option:\n",
|
||||
" in_gen = generate_unit_test_via_model(in_lang, in_code, in_unit_test_style, unit_test_model)\n",
|
||||
" for in_output in in_gen:\n",
|
||||
" yield in_output, \"\"\n",
|
||||
" elif 'output' in unit_test_option:\n",
|
||||
" out_gen = generate_unit_test_via_model(out_lang, out_code, out_unit_test_style, unit_test_model)\n",
|
||||
" for out_output in out_gen:\n",
|
||||
" yield \"\", out_output\n",
|
||||
" elif 'both' in unit_test_option:\n",
|
||||
" in_gen = generate_unit_test_via_model(in_lang, in_code, in_unit_test_style, unit_test_model)\n",
|
||||
" out_gen = generate_unit_test_via_model(out_lang, out_code, out_unit_test_style, unit_test_model)\n",
|
||||
" for in_output, out_output in zip(in_gen, out_gen):\n",
|
||||
" yield in_output, out_output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2a1f4d0c-f417-4de4-be9f-441cbe5a6db3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 4. Gradio UI part"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"LANGUAGE_INFO = {\n",
|
||||
" \"Python\": {\n",
|
||||
" \"doc_style\": [\"Google-style\", \"NumPy-style\", \"reST\", \"Doxygen\"],\n",
|
||||
" \"unit_test_style\": [\"unittest\", \"pytest\", \"doctest\"]\n",
|
||||
" },\n",
|
||||
" \"Java\": {\n",
|
||||
" \"doc_style\": [\"Javadoc\"],\n",
|
||||
" \"unit_test_style\": [\"JUnit4\", \"JUnit5\", \"TestNG\"]\n",
|
||||
" },\n",
|
||||
" \"JavaScript\": {\n",
|
||||
" \"doc_style\": [\"JSDoc\"],\n",
|
||||
" \"unit_test_style\": [\"Jest\", \"Mocha + Chai\", \"Jasmine\"]\n",
|
||||
" },\n",
|
||||
" \"TypeScript\": {\n",
|
||||
" \"doc_style\": [\"JSDoc\", \"TSDoc\"],\n",
|
||||
" \"unit_test_style\": [\"Jest\", \"Mocha + Chai\", \"Vitest\"]\n",
|
||||
" },\n",
|
||||
" \"C\": {\n",
|
||||
" \"doc_style\": [\"Doxygen\"],\n",
|
||||
" \"unit_test_style\": [\"Google Test (gtest)\", \"CppUnit\", \"Catch2\"]\n",
|
||||
" },\n",
|
||||
" \"C++\": {\n",
|
||||
" \"doc_style\": [\"Doxygen\"],\n",
|
||||
" \"unit_test_style\": [\"Google Test (gtest)\", \"CppUnit\", \"Catch2\"]\n",
|
||||
" },\n",
|
||||
" \"C#\": {\n",
|
||||
" \"doc_style\": [\"XML comments\"],\n",
|
||||
" \"unit_test_style\": [\"xUnit\", \"NUnit\", \"MSTest\"]\n",
|
||||
" },\n",
|
||||
" \"Go\": {\n",
|
||||
" \"doc_style\": [\"Godoc\"],\n",
|
||||
" \"unit_test_style\": [\"Built-in testing package\"]\n",
|
||||
" },\n",
|
||||
" \"Rust\": {\n",
|
||||
" \"doc_style\": [\"Rustdoc\", \"Markdown\"],\n",
|
||||
" \"unit_test_style\": [\"Built-in #[test] annotation\"]\n",
|
||||
" },\n",
|
||||
" \"Kotlin\": {\n",
|
||||
" \"doc_style\": [\"KDoc\"],\n",
|
||||
" \"unit_test_style\": [\"JUnit\", \"Kotest\", \"Spek\"]\n",
|
||||
" },\n",
|
||||
" \"Swift\": {\n",
|
||||
" \"doc_style\": [\"Mark-style comments\"],\n",
|
||||
" \"unit_test_style\": [\"XCTest\"]\n",
|
||||
" },\n",
|
||||
" \"PHP\": {\n",
|
||||
" \"doc_style\": [\"PHPDoc\"],\n",
|
||||
" \"unit_test_style\": [\"PHPUnit\"]\n",
|
||||
" },\n",
|
||||
" \"Julia\": {\n",
|
||||
" \"doc_style\": [\"Markdown\"],\n",
|
||||
" \"unit_test_style\": [\"Built-in Test standard library\"]\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"LANGUAGES = list(LANGUAGE_INFO.keys())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b50e7833-8f6f-407e-8174-37af9cec2030",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks(title=\"Power Coder\", theme=gr.themes.Citrus(), css=\"\"\"\n",
|
||||
".selected {\n",
|
||||
" background-color: orange !important;\n",
|
||||
" box-shadow: 0 4px 12px rgba(255, 140, 0, 0.5) !important;\n",
|
||||
" color: black;\n",
|
||||
"}\n",
|
||||
".unselected {\n",
|
||||
" background-color: gray !important;\n",
|
||||
" box-shadow: 0 4px 12px rgba(128, 128, 128, 0.4);\n",
|
||||
" color: white;\n",
|
||||
"}\n",
|
||||
"\"\"\") as ui:\n",
|
||||
" current_selected = gr.State(\"\")\n",
|
||||
" initial_in_lang = \"Python\"\n",
|
||||
" initial_out_lang = \"Java\"\n",
|
||||
" in_comment_style_choices = [\"Standard\"] + LANGUAGE_INFO[initial_in_lang][\"doc_style\"]\n",
|
||||
" out_comment_style_choices = [\"Standard\"] + LANGUAGE_INFO[initial_out_lang][\"doc_style\"]\n",
|
||||
" in_unit_test_style_choices = [\"Standard\"] + LANGUAGE_INFO[initial_in_lang][\"unit_test_style\"]\n",
|
||||
" out_unit_test_style_choices = [\"Standard\"] + LANGUAGE_INFO[initial_out_lang][\"unit_test_style\"]\n",
|
||||
" in_code_file_name = gr.State(\"in_code.txt\")\n",
|
||||
" out_code_file_name = gr.State(\"out_code.txt\")\n",
|
||||
" in_comments_file_name = gr.State(\"in_comments.txt\")\n",
|
||||
" out_comments_file_name = gr.State(\"out_comments.txt\")\n",
|
||||
" in_unit_test_file_name = gr.State(\"in_unit_tests.txt\")\n",
|
||||
" out_unit_test_file_name = gr.State(\"out_unit_tests.txt\")\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" gr.Markdown(\"## Code Helper\")\n",
|
||||
"\n",
|
||||
" def load_file_content(file):\n",
|
||||
" if file is None:\n",
|
||||
" return \"\"\n",
|
||||
" with open(file.name, \"r\", encoding=\"utf-8\") as f:\n",
|
||||
" return f.read()\n",
|
||||
"\n",
|
||||
" def change_lang(lang):\n",
|
||||
" comment_style_choices = [\"Standard\"] + LANGUAGE_INFO[lang][\"doc_style\"]\n",
|
||||
" unit_test_style_choices = [\"Standard\"] + LANGUAGE_INFO[lang][\"unit_test_style\"]\n",
|
||||
" return (\n",
|
||||
" gr.update(choices=comment_style_choices, value=str(comment_style_choices[0])), \n",
|
||||
" gr.update(choices=unit_test_style_choices, value=str(unit_test_style_choices[0]))\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def download_fn(in_text, out_text, in_file_name, out_file_name):\n",
|
||||
" if in_text:\n",
|
||||
" with open(in_file_name, \"w\") as f:\n",
|
||||
" f.write(in_text)\n",
|
||||
" if out_text:\n",
|
||||
" with open(out_file_name, \"w\") as f:\n",
|
||||
" f.write(out_text)\n",
|
||||
" \n",
|
||||
" # Conversion part\n",
|
||||
" with gr.Row():\n",
|
||||
" in_lang = gr.Dropdown(choices=LANGUAGES, label=\"Select input language\", value=initial_in_lang, interactive=True)\n",
|
||||
" out_lang = gr.Dropdown(choices=LANGUAGES, label=\"Select output language\", value=initial_out_lang, interactive=True)\n",
|
||||
" with gr.Row():\n",
|
||||
" input_file = gr.File(label=\"Upload a source code file or input below\")\n",
|
||||
" input_instruct = gr.Textbox(\n",
|
||||
" label=\"Additional instruction(optional)\",\n",
|
||||
" placeholder=\"Enter the instruction you want the ouput code to follow...\\n\\nFor example: Define the variable using snake_case style.\",\n",
|
||||
" lines=8\n",
|
||||
" )\n",
|
||||
" with gr.Row():\n",
|
||||
" in_code = gr.Textbox(label=\"Input Code:\", value=python_hard, lines=10)\n",
|
||||
" out_code = gr.Textbox(label=\"Output Code:\", lines=10)\n",
|
||||
" with gr.Row():\n",
|
||||
" convert_model = gr.Dropdown([\"Claude\", \"GPT\", \"Gemini\"], label=\"Select model\", value=\"Claude\")\n",
|
||||
" with gr.Row():\n",
|
||||
" convert = gr.Button(\"Convert code\")\n",
|
||||
" download_code = gr.Button(\"Download code\")\n",
|
||||
"\n",
|
||||
" gr.HTML(\"<hr style='border: none; height: 1px; background-color: #333;'>\")\n",
|
||||
"\n",
|
||||
" def show_comment(current_selected):\n",
|
||||
" if current_selected == \"comment\":\n",
|
||||
" return (\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" \"\"\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" return (\n",
|
||||
" gr.update(visible=True),\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(elem_classes=[\"selected\"]),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" \"comment\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def show_unit_test(current_selected):\n",
|
||||
" if current_selected == \"unit_test\":\n",
|
||||
" return (\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" \"\"\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" return (\n",
|
||||
" gr.update(visible=False),\n",
|
||||
" gr.update(visible=True),\n",
|
||||
" gr.update(elem_classes=[\"unselected\"]),\n",
|
||||
" gr.update(elem_classes=[\"selected\"]),\n",
|
||||
" \"unit_test\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with gr.Blocks() as demo:\n",
|
||||
" with gr.Row():\n",
|
||||
" comment_show_up = gr.Button(\"Comment\", elem_id=\"comment-btn\", elem_classes=[\"unselected\"])\n",
|
||||
" unit_test_show_up = gr.Button(\"Unit Test\", elem_id=\"unit-test-btn\", elem_classes=[\"unselected\"])\n",
|
||||
" \n",
|
||||
" comment_section = gr.Column(visible=False)\n",
|
||||
" unit_test_section = gr.Column(visible=False)\n",
|
||||
" \n",
|
||||
" with comment_section:\n",
|
||||
" # Comment section\n",
|
||||
" with gr.Row():\n",
|
||||
" comment_option = gr.Radio(\n",
|
||||
" choices=[\n",
|
||||
" \"Comment input code\",\n",
|
||||
" \"Comment output code\",\n",
|
||||
" \"Comment both\"\n",
|
||||
" ],\n",
|
||||
" label=\"Commenting Options\",\n",
|
||||
" value=\"Comment input code\",\n",
|
||||
" interactive=True\n",
|
||||
" )\n",
|
||||
" with gr.Row():\n",
|
||||
" in_comment_style = gr.Dropdown(choices=in_comment_style_choices, label=\"Select comment style for input code\", value=in_comment_style_choices[0], interactive=True)\n",
|
||||
" out_comment_style = gr.Dropdown(choices=out_comment_style_choices, label=\"Select comment style for oupt code\", value=out_comment_style_choices[0], interactive=True)\n",
|
||||
" with gr.Row():\n",
|
||||
" comment_model = gr.Dropdown([\"Claude\", \"GPT\", \"Gemini\"], label=\"Select model\", value=\"Claude\")\n",
|
||||
" with gr.Row():\n",
|
||||
" generate_comments = gr.Button(\"Generate comments\")\n",
|
||||
" download_comments = gr.Button(\"Download comments\")\n",
|
||||
" with gr.Row():\n",
|
||||
" in_comments = gr.Textbox(label=\"Comments for Input Code:\", lines=10)\n",
|
||||
" out_comments = gr.Textbox(label=\"Comments for Output Code:\", lines=10)\n",
|
||||
" \n",
|
||||
" with unit_test_section:\n",
|
||||
" # Unit test part\n",
|
||||
" with gr.Row():\n",
|
||||
" unit_test_option = gr.Radio(\n",
|
||||
" choices=[\n",
|
||||
" \"Add unit test for input code\",\n",
|
||||
" \"Add unit test for output code\",\n",
|
||||
" \"Add unit test for both\"\n",
|
||||
" ],\n",
|
||||
" label=\"Unit Test Options\",\n",
|
||||
" value=\"Add unit test for input code\",\n",
|
||||
" interactive=True\n",
|
||||
" )\n",
|
||||
" with gr.Row():\n",
|
||||
" in_unit_test_style = gr.Dropdown(choices=in_unit_test_style_choices, label=\"Select unit test style for input code\", value=in_unit_test_style_choices[0], interactive=True)\n",
|
||||
" out_unit_test_style = gr.Dropdown(choices=out_unit_test_style_choices, label=\"Select unit test style for oupt code\", value=out_unit_test_style_choices[0], interactive=True)\n",
|
||||
" with gr.Row():\n",
|
||||
" unit_test_model = gr.Dropdown([\"Claude\", \"GPT\", \"Gemini\"], label=\"Select model\", value=\"Claude\")\n",
|
||||
" with gr.Row():\n",
|
||||
" generate_unit_test = gr.Button(\"Generate unit test\")\n",
|
||||
" download_unit_test = gr.Button(\"Download unit text\")\n",
|
||||
" with gr.Row():\n",
|
||||
" in_unit_test = gr.Textbox(label=\"Unit Test for Input Code:\", lines=10)\n",
|
||||
" out_unit_test = gr.Textbox(label=\"Unit Test for Output Code:\", lines=10)\n",
|
||||
"\n",
|
||||
" in_lang.change(fn=change_lang, inputs=in_lang, outputs=[in_comment_style, in_unit_test_style])\n",
|
||||
" out_lang.change(fn=change_lang, inputs=out_lang, outputs=[out_comment_style, out_unit_test_style])\n",
|
||||
" input_file.change(fn=load_file_content, inputs=input_file, outputs=in_code)\n",
|
||||
" \n",
|
||||
" convert.click(optimize, inputs=[in_lang, out_lang, in_code, input_instruct, convert_model], outputs=[out_code])\n",
|
||||
" download_code.click(download_fn, inputs=[in_code, out_code, in_code_file_name, out_code_file_name])\n",
|
||||
" \n",
|
||||
" comment_show_up.click(fn=show_comment, inputs=current_selected, outputs=[comment_section, unit_test_section, comment_show_up, unit_test_show_up, current_selected])\n",
|
||||
" unit_test_show_up.click(fn=show_unit_test, inputs=current_selected, outputs=[comment_section, unit_test_section, comment_show_up, unit_test_show_up, current_selected])\n",
|
||||
"\n",
|
||||
" generate_comments.click(generate_comments_fn, inputs=[comment_option, in_lang, out_lang, in_code, out_code, in_comment_style, out_comment_style, comment_model], outputs=[in_comments, out_comments])\n",
|
||||
" download_comments.click(download_fn, inputs=[in_comments, out_comments, in_comments_file_name, out_comments_file_name])\n",
|
||||
" generate_unit_test.click(generate_unit_test_fn, inputs=[unit_test_option, in_lang, out_lang, in_code, out_code, in_unit_test_style, out_unit_test_style, unit_test_model], outputs=[in_unit_test, out_unit_test])\n",
|
||||
" download_unit_test.click(download_fn, inputs=[in_unit_test, out_unit_test, in_unit_test_file_name, out_unit_test_file_name])\n",
|
||||
" \n",
|
||||
"ui.launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0266734c-0bee-46c0-9b17-9fd2ae86cc3a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.11.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
643
week4/community-contributions/Week4_day3_Gemini_Codestral.ipynb
Normal file
643
week4/community-contributions/Week4_day3_Gemini_Codestral.ipynb
Normal file
@@ -0,0 +1,643 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ac833f26-d429-4fd2-8f83-92174f1c951a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Code conversion using Gemini and Codestral in Windows 11"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c230178c-6f31-4c5a-a888-16b7037ffbf9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import io\n",
|
||||
"import sys\n",
|
||||
"import gradio as gr\n",
|
||||
"import subprocess\n",
|
||||
"from google import genai\n",
|
||||
"from google.genai import types\n",
|
||||
"from mistralai import Mistral\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from IPython.display import Markdown, display, update_display"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6d824484-eaaa-456a-b7dc-7e3277fec34a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load Gemini and Mistral API Keys\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"gemini_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
|
||||
"mistral_api_key = os.getenv(\"MISTRAL_API_KEY\")\n",
|
||||
"\n",
|
||||
"if not mistral_api_key or not gemini_api_key:\n",
|
||||
" print(\"API Key not found!\")\n",
|
||||
"else:\n",
|
||||
" print(\"API Key loaded in memory\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "86f3633e-81f9-4c13-b7b5-793ddc4f886f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Models to be used\n",
|
||||
"\n",
|
||||
"MODEL_GEMINI = 'gemini-2.5-flash'\n",
|
||||
"MODEL_CODESTRAL = 'codestral-latest'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3f3a6d53-70f9-46b8-a490-a50f3a1adf9e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load Gemini client\n",
|
||||
"try:\n",
|
||||
" gemini_client = genai.Client(api_key=gemini_api_key)\n",
|
||||
" print(\"Google GenAI Client initialized successfully!\")\n",
|
||||
"\n",
|
||||
" codestral_client = Mistral(api_key=mistral_api_key)\n",
|
||||
" print(\"Mistral Client initialized successfully!\")\n",
|
||||
"except Exception as e:\n",
|
||||
" print(f\"Error initializing Client: {e}\")\n",
|
||||
" exit() "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f816fbe8-e094-499f-98a5-588ebecf8c72",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Gemini System prompt\n",
|
||||
"\n",
|
||||
"system_message = \"You are an assistant that reimplements Python code in high-performance C++ optimized for a Windows PC. \"\n",
|
||||
"system_message += \"Use Windows-specific optimizations where applicable (e.g., multithreading with std::thread, SIMD, or WinAPI if necessary). \"\n",
|
||||
"system_message += \"Respond only with the equivalent C++ code; include comments only where absolutely necessary. \"\n",
|
||||
"system_message += \"Avoid any explanation or text outside the code. \"\n",
|
||||
"system_message += \"The C++ output must produce identical functionality with the fastest possible execution time on Windows.\"\n",
|
||||
"\n",
|
||||
"generate_content_config = types.GenerateContentConfig(system_instruction=system_message)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "01227835-15d2-40bd-a9dd-2ef35ad371dc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt_for(python):\n",
|
||||
" user_prompt = (\n",
|
||||
" \"Convert the following Python code into high-performance C++ optimized for Windows. \"\n",
|
||||
" \"Use standard C++20 or newer with Windows-compatible libraries and best practices. \"\n",
|
||||
" \"Ensure the implementation runs as fast as possible and produces identical output. \"\n",
|
||||
" \"Use appropriate numeric types to avoid overflow or precision loss. \"\n",
|
||||
" \"Avoid unnecessary abstraction; prefer direct computation and memory-efficient structures. \"\n",
|
||||
" \"Respond only with C++ code, include all required headers (like <iomanip>, <vector>, etc.), and limit comments to only what's essential.\\n\\n\"\n",
|
||||
" )\n",
|
||||
" user_prompt += python\n",
|
||||
" return user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8d9fc8e2-acf0-4122-a8a9-5aadadf982ab",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_message_gemini(python): \n",
|
||||
" return types.Content(role=\"user\", parts=[types.Part.from_text(text=user_prompt_for(python))]) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "334c8b84-6e37-40fc-97ac-40a1b3aa29fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def messages_for(python):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": system_message},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4aca87ac-6330-4ed4-a36f-1726fd0ada1a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def write_output(cpp):\n",
|
||||
" code = cpp.replace(\"```cpp\", \"\").replace(\"```c++\", \"\").replace(\"```\", \"\").strip()\n",
|
||||
" \n",
|
||||
" if not \"#include\" in code:\n",
|
||||
" raise ValueError(\"C++ code appears invalid: missing #include directives.\")\n",
|
||||
"\n",
|
||||
" with open(\"optimized.cpp\", \"w\", encoding=\"utf-8\", newline=\"\\n\") as f:\n",
|
||||
" f.write(code) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "fcf42642-1a55-4556-8738-0c8c02effa9c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Generate CPP code using Gemini\n",
|
||||
"\n",
|
||||
"def optimize_gemini(python):\n",
|
||||
" stream = gemini_client.models.generate_content_stream(\n",
|
||||
" model = MODEL_GEMINI,\n",
|
||||
" config=generate_content_config,\n",
|
||||
" contents=user_message_gemini(python)\n",
|
||||
" )\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.text\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" print(chunk_text, end=\"\", flush=True) \n",
|
||||
" write_output(cpp_code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3f06a301-4397-4d63-9226-657bb2ddb792",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Generate CPP code using Codestral\n",
|
||||
"\n",
|
||||
"def optimize_codestral(python):\n",
|
||||
" stream = codestral_client.chat.stream(\n",
|
||||
" model = MODEL_CODESTRAL,\n",
|
||||
" messages = messages_for(python), \n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.data.choices[0].delta.content\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" print(chunk_text, end=\"\", flush=True) \n",
|
||||
" write_output(cpp_code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8bd51601-7c1d-478d-b043-6f92739e5c4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Actual code to convert\n",
|
||||
"\n",
|
||||
"pi = \"\"\"\n",
|
||||
"import time\n",
|
||||
"\n",
|
||||
"def calculate(iterations, param1, param2):\n",
|
||||
" result = 1.0\n",
|
||||
" for i in range(1, iterations+1):\n",
|
||||
" j = i * param1 - param2\n",
|
||||
" result -= (1/j)\n",
|
||||
" j = i * param1 + param2\n",
|
||||
" result += (1/j)\n",
|
||||
" return result\n",
|
||||
"\n",
|
||||
"start_time = time.time()\n",
|
||||
"result = calculate(100_000_000, 4, 1) * 4\n",
|
||||
"end_time = time.time()\n",
|
||||
"\n",
|
||||
"print(f\"Result: {result:.12f}\")\n",
|
||||
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "db9ea24e-d381-48ac-9196-853d2527dcca",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"exec(pi)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f3e26708-8475-474d-8e96-e602c3d5ef9f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimize_gemini(pi)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2cc23ea7-6062-4354-92bc-730baa52a50b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# CPP Compilation\n",
|
||||
"\n",
|
||||
"!g++ -O3 -std=c++20 -o optimized.exe optimized.cpp"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9b14704d-95fe-4ed2-861f-af591bf3090e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!.\\optimized.exe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5d756d1a-1d49-4cfb-bed7-8748d848b083",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimize_codestral(pi)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6e286dc8-9532-48b1-b748-a7950972e7df",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!g++ -O3 -std=c++20 -o optimized.exe optimized.cpp"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "61fe0044-7679-4245-9e59-50642f3d80c6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!.\\optimized.exe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f0c0392c-d2a7-4619-82a2-f7b9fa7c43f9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Hard Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9ca53eb4-46cd-435b-a950-0e2a8f845535",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"python_hard = \"\"\"# Be careful to support large number sizes\n",
|
||||
"\n",
|
||||
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
|
||||
" value = seed\n",
|
||||
" while True:\n",
|
||||
" value = (a * value + c) % m\n",
|
||||
" yield value\n",
|
||||
" \n",
|
||||
"def max_subarray_sum(n, seed, min_val, max_val):\n",
|
||||
" lcg_gen = lcg(seed)\n",
|
||||
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
|
||||
" max_sum = float('-inf')\n",
|
||||
" for i in range(n):\n",
|
||||
" current_sum = 0\n",
|
||||
" for j in range(i, n):\n",
|
||||
" current_sum += random_numbers[j]\n",
|
||||
" if current_sum > max_sum:\n",
|
||||
" max_sum = current_sum\n",
|
||||
" return max_sum\n",
|
||||
"\n",
|
||||
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
|
||||
" total_sum = 0\n",
|
||||
" lcg_gen = lcg(initial_seed)\n",
|
||||
" for _ in range(20):\n",
|
||||
" seed = next(lcg_gen)\n",
|
||||
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
|
||||
" return total_sum\n",
|
||||
"\n",
|
||||
"# Parameters\n",
|
||||
"n = 10000 # Number of random numbers\n",
|
||||
"initial_seed = 42 # Initial seed for the LCG\n",
|
||||
"min_val = -10 # Minimum value of random numbers\n",
|
||||
"max_val = 10 # Maximum value of random numbers\n",
|
||||
"\n",
|
||||
"# Timing the function\n",
|
||||
"import time\n",
|
||||
"start_time = time.time()\n",
|
||||
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
|
||||
"end_time = time.time()\n",
|
||||
"\n",
|
||||
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
|
||||
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "697cc9fe-efdb-40b7-8e43-871bd2df940e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"exec(python_hard)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "17ed6329-6c5f-45af-91ff-06d73830dd0d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimize_gemini(python_hard)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0b57f0e7-46c9-4235-86eb-389faf37b7bb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# CPP Compilation\n",
|
||||
"\n",
|
||||
"!g++ -O3 -std=c++20 -o optimized.exe optimized.cpp"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b8ce8d01-fda8-400d-b3d4-6f1ad3008d28",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!.\\optimized.exe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "adbcdac7-8656-41c9-8707-d8a71998d393",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optimize_codestral(python_hard)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9f9fc9b1-29cf-4510-83f8-1484d26e871e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# CPP Compilation\n",
|
||||
"\n",
|
||||
"!g++ -O3 -std=c++20 -o optimized.exe optimized.cpp"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "52170458-c4a1-4920-8d83-8c5ba7250759",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!.\\optimized.exe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "da6aee85-2792-487b-bef3-fec5dcf12623",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Accommodating the entire code in Gradio"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f2a90c4f-c289-4658-a6ce-51b80e20f91f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gemini(python):\n",
|
||||
" stream = gemini_client.models.generate_content_stream(\n",
|
||||
" model = MODEL_GEMINI,\n",
|
||||
" config=generate_content_config,\n",
|
||||
" contents=user_message_gemini(python)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.text or \"\"\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" yield cpp_code.replace('```cpp\\n','').replace('```','')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6e872171-96d8-4041-8cb0-0c632c5e957f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_codestral(python):\n",
|
||||
" stream = codestral_client.chat.stream(\n",
|
||||
" model = MODEL_CODESTRAL,\n",
|
||||
" messages = messages_for(python), \n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.data.choices[0].delta.content or \"\"\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" yield cpp_code.replace('```cpp\\n','').replace('```','') "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3340b36b-1241-4b0f-9e69-d4e5cc215a27",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def optimize(python, model):\n",
|
||||
" if model.lower() == 'gemini':\n",
|
||||
" result = stream_gemini(python)\n",
|
||||
" elif model.lower() == 'codestral':\n",
|
||||
" result = stream_codestral(python)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown model\")\n",
|
||||
" \n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "277ddd6c-e71e-4512-965a-57fca341487a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Gradio Implementation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "222a9eae-236e-4ba3-8f23-3d9b879ec2d0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"custom_css = \"\"\"\n",
|
||||
".scrollable-box textarea {\n",
|
||||
" overflow: auto !important;\n",
|
||||
" height: 400px;\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
".python {background-color: #306998;}\n",
|
||||
".cpp {background-color: #050;}\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"theme = gr.themes.Soft()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b4bd6ed1-ff8c-42d4-8da6-24b9cfd134db",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_python(code):\n",
|
||||
" try:\n",
|
||||
" result = subprocess.run(\n",
|
||||
" [\"python\", \"-c\", code],\n",
|
||||
" capture_output=True,\n",
|
||||
" text=True,\n",
|
||||
" timeout=60\n",
|
||||
" )\n",
|
||||
" if result.returncode == 0:\n",
|
||||
" return result.stdout or \"[No output]\"\n",
|
||||
" else:\n",
|
||||
" return f\"[Error]\\n{result.stderr}\"\n",
|
||||
" except subprocess.TimeoutExpired:\n",
|
||||
" return \"[Error] Execution timed out.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"[Exception] {str(e)}\" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1507c973-8699-48b2-80cd-45900c97a867",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_cpp(code):\n",
|
||||
" write_output(code)\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" compile_cmd = [\"g++\", \"-O3\", \"-std=c++20\", \"-o\", \"optimized.exe\", \"optimized.cpp\"]\n",
|
||||
" compile_result = subprocess.run(compile_cmd, capture_output=True, text=True, check=True)\n",
|
||||
" \n",
|
||||
" run_cmd = [\"optimized.exe\"]\n",
|
||||
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True, timeout=60)\n",
|
||||
" \n",
|
||||
" return run_result.stdout or \"[No output]\"\n",
|
||||
" \n",
|
||||
" except subprocess.CalledProcessError as e:\n",
|
||||
" return f\"[Compile/Runtime Error]\\n{e.stderr}\"\n",
|
||||
" except subprocess.TimeoutExpired:\n",
|
||||
" return \"[Error] Execution timed out.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"[Exception] {str(e)}\" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "374f00f3-8fcf-4ae9-bf54-c5a44dd74844",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks(css=custom_css, theme=theme) as ui:\n",
|
||||
" gr.Markdown(\"## Convert code from Python to C++\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard, elem_classes=[\"scrollable-box\"])\n",
|
||||
" cpp = gr.Textbox(label=\"C++ code:\", lines=10, elem_classes=[\"scrollable-box\"])\n",
|
||||
" with gr.Row():\n",
|
||||
" model = gr.Dropdown([\"Gemini\", \"Codestral\"], label=\"Select model\", value=\"Gemini\")\n",
|
||||
" convert = gr.Button(\"Convert code\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python_run = gr.Button(\"Run Python\")\n",
|
||||
" cpp_run = gr.Button(\"Run C++\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
|
||||
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
|
||||
"\n",
|
||||
" convert.click(optimize, inputs=[python,model], outputs=[cpp])\n",
|
||||
" python_run.click(execute_python,inputs=[python], outputs=[python_out])\n",
|
||||
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
|
||||
"\n",
|
||||
"ui.launch(inbrowser=True) "
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.12.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,476 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4c07cdc9-bce0-49ad-85c7-14f1872b8519",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Python to CPP using Qwen2.5-Coder-32B-Instruct with Hyperbolic Inference Endpoint in Windows"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f051c517-c4fd-4248-98aa-b808fae76cf6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import io\n",
|
||||
"import sys\n",
|
||||
"import gradio as gr\n",
|
||||
"import subprocess\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from huggingface_hub import InferenceClient\n",
|
||||
"from google import genai\n",
|
||||
"from google.genai import types\n",
|
||||
"from mistralai import Mistral"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c6c8777b-57bc-436a-978f-21a37ea310ae",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load Api Keys from env\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"\n",
|
||||
"hf_api_key = os.getenv(\"HF_TOKEN\")\n",
|
||||
"gemini_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
|
||||
"mistral_api_key = os.getenv(\"MISTRAL_API_KEY\")\n",
|
||||
"\n",
|
||||
"if not mistral_api_key or not gemini_api_key or not hf_api_key:\n",
|
||||
" print(\"API Key not found!\")\n",
|
||||
"else:\n",
|
||||
" print(\"API Key loaded in memory\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5cf6f93-7e07-40e0-98b8-d4e74ea18402",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# MODELs \n",
|
||||
"\n",
|
||||
"MODEL_QWEN = \"Qwen/Qwen2.5-Coder-32B-Instruct\"\n",
|
||||
"MODEL_GEMINI = 'gemini-2.5-flash'\n",
|
||||
"MODEL_CODESTRAL = 'codestral-latest'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "689547c3-aaa5-4800-86a2-da52765997d8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load Clients\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" gemini_client = genai.Client(api_key=gemini_api_key)\n",
|
||||
" print(\"Google GenAI Client initialized successfully!\")\n",
|
||||
"\n",
|
||||
" codestral_client = Mistral(api_key=mistral_api_key)\n",
|
||||
" print(\"Mistral Client initialized successfully!\")\n",
|
||||
" \n",
|
||||
" hf_client = InferenceClient(provider=\"hyperbolic\",api_key=hf_api_key)\n",
|
||||
" print(\"Hyperbolic Inference Client initialized successfully!\")\n",
|
||||
"except Exception as e:\n",
|
||||
" print(f\"Error initializing Client: {e}\")\n",
|
||||
" exit() "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1c3a81f4-99c3-463a-ae30-4656a7a246d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_message = \"You are an assistant that reimplements Python code in high-performance C++ optimized for a Windows PC. \"\n",
|
||||
"system_message += \"Use Windows-specific optimizations where applicable (e.g., multithreading with std::thread, SIMD, or WinAPI if necessary). \"\n",
|
||||
"system_message += \"Respond only with the equivalent C++ code; include comments only where absolutely necessary. \"\n",
|
||||
"system_message += \"Avoid any explanation or text outside the code. \"\n",
|
||||
"system_message += \"The C++ output must produce identical functionality with the fastest possible execution time on Windows.\"\n",
|
||||
"\n",
|
||||
"generate_content_config = types.GenerateContentConfig(system_instruction=system_message)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0fde9514-1005-4539-b01b-0372730ce67b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt_for(python):\n",
|
||||
" user_prompt = (\n",
|
||||
" \"Convert the following Python code into high-performance C++ optimized for Windows. \"\n",
|
||||
" \"Use standard C++20 or newer with Windows-compatible libraries and best practices. \"\n",
|
||||
" \"Ensure the implementation runs as fast as possible and produces identical output. \"\n",
|
||||
" \"Use appropriate numeric types to avoid overflow or precision loss. \"\n",
|
||||
" \"Avoid unnecessary abstraction; prefer direct computation and memory-efficient structures. \"\n",
|
||||
" \"Respond only with C++ code, include all required headers (like <iomanip>, <vector>, etc.), and limit comments to only what's essential.\\n\\n\"\n",
|
||||
" )\n",
|
||||
" user_prompt += python\n",
|
||||
" return user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "89c8b010-08dd-4695-a784-65162d82a24b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_message_gemini(python): \n",
|
||||
" return types.Content(role=\"user\", parts=[types.Part.from_text(text=user_prompt_for(python))]) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66923158-983d-46f7-ab19-f216fb1f6a87",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def messages_for(python):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": system_message},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9ab59a54-b28a-4d07-b04f-b568e6e25dfb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def write_output(cpp):\n",
|
||||
" code = cpp.replace(\"```cpp\", \"\").replace(\"```c++\", \"\").replace(\"```\", \"\").strip()\n",
|
||||
" \n",
|
||||
" if not \"#include\" in code:\n",
|
||||
" raise ValueError(\"C++ code appears invalid: missing #include directives.\")\n",
|
||||
"\n",
|
||||
" with open(\"qwenOptimized.cpp\", \"w\", encoding=\"utf-8\", newline=\"\\n\") as f:\n",
|
||||
" f.write(code) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e05ea9f0-6ade-4699-b5fa-fb8ef9f16bcb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Python Codes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c515ce2c-1f8d-4484-8d34-9ffe1372dad4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"python_easy = \"\"\"\n",
|
||||
"import time\n",
|
||||
"\n",
|
||||
"def calculate(iterations, param1, param2):\n",
|
||||
" result = 1.0\n",
|
||||
" for i in range(1, iterations+1):\n",
|
||||
" j = i * param1 - param2\n",
|
||||
" result -= (1/j)\n",
|
||||
" j = i * param1 + param2\n",
|
||||
" result += (1/j)\n",
|
||||
" return result\n",
|
||||
"\n",
|
||||
"start_time = time.time()\n",
|
||||
"result = calculate(100_000_000, 4, 1) * 4\n",
|
||||
"end_time = time.time()\n",
|
||||
"\n",
|
||||
"print(f\"Result: {result:.12f}\")\n",
|
||||
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "83ab4080-71ae-45e6-970b-030dc462f571",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"python_hard = \"\"\"# Be careful to support large number sizes\n",
|
||||
"\n",
|
||||
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
|
||||
" value = seed\n",
|
||||
" while True:\n",
|
||||
" value = (a * value + c) % m\n",
|
||||
" yield value\n",
|
||||
" \n",
|
||||
"def max_subarray_sum(n, seed, min_val, max_val):\n",
|
||||
" lcg_gen = lcg(seed)\n",
|
||||
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
|
||||
" max_sum = float('-inf')\n",
|
||||
" for i in range(n):\n",
|
||||
" current_sum = 0\n",
|
||||
" for j in range(i, n):\n",
|
||||
" current_sum += random_numbers[j]\n",
|
||||
" if current_sum > max_sum:\n",
|
||||
" max_sum = current_sum\n",
|
||||
" return max_sum\n",
|
||||
"\n",
|
||||
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
|
||||
" total_sum = 0\n",
|
||||
" lcg_gen = lcg(initial_seed)\n",
|
||||
" for _ in range(20):\n",
|
||||
" seed = next(lcg_gen)\n",
|
||||
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
|
||||
" return total_sum\n",
|
||||
"\n",
|
||||
"# Parameters\n",
|
||||
"n = 10000 # Number of random numbers\n",
|
||||
"initial_seed = 42 # Initial seed for the LCG\n",
|
||||
"min_val = -10 # Minimum value of random numbers\n",
|
||||
"max_val = 10 # Maximum value of random numbers\n",
|
||||
"\n",
|
||||
"# Timing the function\n",
|
||||
"import time\n",
|
||||
"start_time = time.time()\n",
|
||||
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
|
||||
"end_time = time.time()\n",
|
||||
"\n",
|
||||
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
|
||||
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "31498c5c-ecdd-4ed7-9607-4d09af893b98",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code Implementation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ea4a4968-e04f-4939-8c42-32c960699354",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gemini(python):\n",
|
||||
" stream = gemini_client.models.generate_content_stream(\n",
|
||||
" model = MODEL_GEMINI,\n",
|
||||
" config=generate_content_config,\n",
|
||||
" contents=user_message_gemini(python)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.text or \"\"\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" yield cpp_code.replace('```cpp\\n','').replace('```','')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "69601eee-520f-4813-b796-aee9118e8a72",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_codestral(python):\n",
|
||||
" stream = codestral_client.chat.stream(\n",
|
||||
" model = MODEL_CODESTRAL,\n",
|
||||
" messages = messages_for(python), \n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.data.choices[0].delta.content or \"\"\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" yield cpp_code.replace('```cpp\\n','').replace('```','') "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cb8899cf-54c0-4d2d-8772-42925c2e1d13",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_qwen(python):\n",
|
||||
" stream = hf_client.chat.completions.create(\n",
|
||||
" model = MODEL_QWEN,\n",
|
||||
" messages = messages_for(python),\n",
|
||||
" stream=True\n",
|
||||
" )\n",
|
||||
" cpp_code = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" chunk_text = chunk.choices[0].delta.content\n",
|
||||
" cpp_code += chunk_text\n",
|
||||
" yield cpp_code.replace('```cpp\\n','').replace('```','')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "98862fef-905c-4b50-bc7a-4c0462495b5c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def optimize(python, model):\n",
|
||||
" if model.lower() == 'gemini':\n",
|
||||
" result = stream_gemini(python)\n",
|
||||
" elif model.lower() == 'codestral':\n",
|
||||
" result = stream_codestral(python)\n",
|
||||
" elif model.lower() == 'qwen_coder':\n",
|
||||
" result = stream_qwen(python)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown model\")\n",
|
||||
" \n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aa9372df-db01-41d0-842c-4857b20f93f0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"custom_css = \"\"\"\n",
|
||||
".scrollable-box textarea {\n",
|
||||
" overflow: auto !important;\n",
|
||||
" height: 400px;\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
".python {background-color: #306998;}\n",
|
||||
".cpp {background-color: #050;}\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"theme = gr.themes.Soft()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dbcf9fe9-c3da-466b-8478-83dcdbe7d48e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_python(code):\n",
|
||||
" try:\n",
|
||||
" result = subprocess.run(\n",
|
||||
" [\"python\", \"-c\", code],\n",
|
||||
" capture_output=True,\n",
|
||||
" text=True,\n",
|
||||
" timeout=60\n",
|
||||
" )\n",
|
||||
" if result.returncode == 0:\n",
|
||||
" return result.stdout or \"[No output]\"\n",
|
||||
" else:\n",
|
||||
" return f\"[Error]\\n{result.stderr}\"\n",
|
||||
" except subprocess.TimeoutExpired:\n",
|
||||
" return \"[Error] Execution timed out.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"[Exception] {str(e)}\" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8029e00d-1ee8-43d1-8c87-2aa0544cf94c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_cpp(code):\n",
|
||||
" write_output(code)\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" compile_cmd = [\"g++\", \"-O3\", \"-std=c++20\", \"-o\", \"optimized.exe\", \"optimized.cpp\"]\n",
|
||||
" compile_result = subprocess.run(compile_cmd, capture_output=True, text=True, check=True)\n",
|
||||
" \n",
|
||||
" run_cmd = [\"optimized.exe\"]\n",
|
||||
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True, timeout=60)\n",
|
||||
" \n",
|
||||
" return run_result.stdout or \"[No output]\"\n",
|
||||
" \n",
|
||||
" except subprocess.CalledProcessError as e:\n",
|
||||
" return f\"[Compile/Runtime Error]\\n{e.stderr}\"\n",
|
||||
" except subprocess.TimeoutExpired:\n",
|
||||
" return \"[Error] Execution timed out.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"[Exception] {str(e)}\" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d5f4e88c-be15-4870-9f99-82b6273ee739",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks(css=custom_css, theme=theme) as ui:\n",
|
||||
" gr.Markdown(\"## Convert code from Python to C++\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard, elem_classes=[\"scrollable-box\"])\n",
|
||||
" cpp = gr.Textbox(label=\"C++ code:\", lines=10, elem_classes=[\"scrollable-box\"])\n",
|
||||
" with gr.Row():\n",
|
||||
" model = gr.Dropdown([\"Gemini\", \"Codestral\", \"QWEN_Coder\"], label=\"Select model\", value=\"Gemini\")\n",
|
||||
" convert = gr.Button(\"Convert code\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python_run = gr.Button(\"Run Python\")\n",
|
||||
" cpp_run = gr.Button(\"Run C++\")\n",
|
||||
" with gr.Row():\n",
|
||||
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
|
||||
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
|
||||
"\n",
|
||||
" convert.click(optimize, inputs=[python,model], outputs=[cpp])\n",
|
||||
" python_run.click(execute_python,inputs=[python], outputs=[python_out])\n",
|
||||
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
|
||||
"\n",
|
||||
"ui.launch(inbrowser=True) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aa1a231e-2743-4cee-afe2-783d2b9513e5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.12.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,538 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "3e473bbd-a0c2-43bd-bf99-c749784d00c3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import gradio as gr\n",
|
||||
"import openai\n",
|
||||
"import anthropic\n",
|
||||
"import google.generativeai as genai\n",
|
||||
"import requests\n",
|
||||
"import json\n",
|
||||
"import os\n",
|
||||
"from typing import Dict, Any, Optional\n",
|
||||
"import asyncio\n",
|
||||
"from dotenv import load_dotenv"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "16210512-41f1-4de3-8348-2cd7129e023f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# load API\n",
|
||||
"load_dotenv(override=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "6747e275-91eb-4d2b-90b6-805f2bd9b6b7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class CodeCommenter:\n",
|
||||
" def __init__(self):\n",
|
||||
" # Initialize API clients\n",
|
||||
" self.openai_client = None\n",
|
||||
" self.anthropic_client = None\n",
|
||||
" self.gemini_client = None\n",
|
||||
" \n",
|
||||
" # Load API keys from environment variables\n",
|
||||
" self.setup_clients()\n",
|
||||
" \n",
|
||||
" def setup_clients(self):\n",
|
||||
" \"\"\"Initialize API clients with keys from environment variables\"\"\"\n",
|
||||
" try:\n",
|
||||
" # OpenAI\n",
|
||||
" openai_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
" if openai_key:\n",
|
||||
" self.openai_client = openai.OpenAI(api_key=openai_key)\n",
|
||||
" \n",
|
||||
" # Anthropic\n",
|
||||
" anthropic_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
" if anthropic_key:\n",
|
||||
" self.anthropic_client = anthropic.Anthropic(api_key=anthropic_key)\n",
|
||||
" \n",
|
||||
" # Google Gemini\n",
|
||||
" gemini_key = os.getenv('GOOGLE_API_KEY')\n",
|
||||
" if gemini_key:\n",
|
||||
" genai.configure(api_key=gemini_key)\n",
|
||||
" self.gemini_client = genai.GenerativeModel('gemini-2.0-flash-exp')\n",
|
||||
" \n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Warning: Error setting up API clients: {e}\")\n",
|
||||
" \n",
|
||||
" def create_comments_prompt(self, code: str, language: str) -> str:\n",
|
||||
" \"\"\"Create a prompt for the LLM to add comments and docstrings\"\"\"\n",
|
||||
" return f\"\"\"Please add detailed and helpful comments and docstrings to the following {language} code. \n",
|
||||
" \n",
|
||||
"Guidelines:\n",
|
||||
"1. Add comprehensive docstrings for functions, classes, and modules\n",
|
||||
"2. Add inline comments explaining complex logic\n",
|
||||
"3. Follow the commenting conventions for {language}\n",
|
||||
"4. Maintain the original code structure and functionality\n",
|
||||
"5. Make comments clear and professional\n",
|
||||
"6. Don't change the actual code logic, only add comments\n",
|
||||
"7. Do not add code markdown delimiters like ```python\n",
|
||||
"\n",
|
||||
"Here's the code to comment:\n",
|
||||
"\n",
|
||||
"{code}\n",
|
||||
"\n",
|
||||
"Please return only the commented code without any additional explanation or markdown formatting.\"\"\"\n",
|
||||
"\n",
|
||||
" def create_tests_prompt(self, code: str, language: str) -> str:\n",
|
||||
" \"\"\"Create a prompt for the LLM to generate unit tests\"\"\"\n",
|
||||
" return f\"\"\"Please generate comprehensive unit tests for the following {language} code.\n",
|
||||
" \n",
|
||||
"Guidelines:\n",
|
||||
"1. Use appropriate testing framework for {language} (pytest for Python, JUnit for Java, etc.)\n",
|
||||
"2. Create tests for all functions and methods\n",
|
||||
"3. Include both positive and negative test cases\n",
|
||||
"4. Test edge cases and error conditions\n",
|
||||
"5. Use meaningful test names that describe what is being tested\n",
|
||||
"6. Include setup and teardown methods if needed\n",
|
||||
"7. Add mock objects for external dependencies (like database connections)\n",
|
||||
"8. Do not add code markdown delimiters like ```python\n",
|
||||
"9. Follow testing best practices for {language}\n",
|
||||
"\n",
|
||||
"Here's the code to test:\n",
|
||||
"\n",
|
||||
"{code}\n",
|
||||
"\n",
|
||||
"Please return only the unit test code without any additional explanation or markdown formatting.\"\"\"\n",
|
||||
"\n",
|
||||
" def create_combined_prompt(self, code: str, language: str) -> str:\n",
|
||||
" \"\"\"Create a prompt for the LLM to add both comments and unit tests\"\"\"\n",
|
||||
" return f\"\"\"Please add detailed comments and docstrings to the following {language} code AND generate comprehensive unit tests for it.\n",
|
||||
" \n",
|
||||
"For Comments:\n",
|
||||
"1. Add comprehensive docstrings for functions, classes, and modules\n",
|
||||
"2. Add inline comments explaining complex logic\n",
|
||||
"3. Follow the commenting conventions for {language}\n",
|
||||
"4. Don't change the actual code logic, only add comments\n",
|
||||
"\n",
|
||||
"For Unit Tests:\n",
|
||||
"1. Use appropriate testing framework for {language} (pytest for Python, JUnit for Java, etc.)\n",
|
||||
"2. Create tests for all functions and methods\n",
|
||||
"3. Include both positive and negative test cases\n",
|
||||
"4. Test edge cases and error conditions\n",
|
||||
"5. Add mock objects for external dependencies (like database connections)\n",
|
||||
"6. Follow testing best practices for {language}\n",
|
||||
"\n",
|
||||
"Structure your response as:\n",
|
||||
"1. First, provide the original code with added comments and docstrings \n",
|
||||
"2. Then, provide the unit tests as a separate section\n",
|
||||
"3. Do not add code markdown delimiters like ```python\n",
|
||||
"4. The 2 separated portions of code, comments and unit test should be clearly demarcated by comments specifying the following section purpose\n",
|
||||
"\n",
|
||||
"Here's the code:\n",
|
||||
"\n",
|
||||
"{code}\n",
|
||||
"\n",
|
||||
"Please return the commented code followed by the unit tests, clearly separated.\"\"\"\n",
|
||||
"\n",
|
||||
" def call_openai(self, prompt: str, model: str = \"gpt-4o-mini\") -> str:\n",
|
||||
" \"\"\"Make API call to OpenAI\"\"\"\n",
|
||||
" if not self.openai_client:\n",
|
||||
" return \"Error: OpenAI API key not configured. Please set OPENAI_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.openai_client.chat.completions.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"system\", \"content\": \"You are a helpful coding assistant that adds detailed comments, docstrings, and generates unit tests for code.\"},\n",
|
||||
" {\"role\": \"user\", \"content\": prompt}\n",
|
||||
" ],\n",
|
||||
" max_tokens=4000,\n",
|
||||
" temperature=0.1\n",
|
||||
" )\n",
|
||||
" return response.choices[0].message.content.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling OpenAI API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_anthropic(self, prompt: str, model: str = \"claude-3-5-haiku-20241022\") -> str:\n",
|
||||
" \"\"\"Make API call to Anthropic Claude\"\"\"\n",
|
||||
" if not self.anthropic_client:\n",
|
||||
" return \"Error: Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.anthropic_client.messages.create(\n",
|
||||
" model=model,\n",
|
||||
" max_tokens=4000,\n",
|
||||
" temperature=0.1,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"user\", \"content\": prompt}\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
" return response.content[0].text.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Anthropic API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_gemini(self, prompt: str) -> str:\n",
|
||||
" \"\"\"Make API call to Google Gemini\"\"\"\n",
|
||||
" if not self.gemini_client:\n",
|
||||
" return \"Error: Google API key not configured. Please set GOOGLE_API_KEY environment variable.\"\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" response = self.gemini_client.generate_content(\n",
|
||||
" prompt,\n",
|
||||
" generation_config=genai.types.GenerationConfig(\n",
|
||||
" max_output_tokens=4000,\n",
|
||||
" temperature=0.1,\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" return response.text.strip()\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Gemini API: {str(e)}\"\n",
|
||||
" \n",
|
||||
" def call_ollama(self, prompt: str, model: str = \"llama3.2:latest\") -> str:\n",
|
||||
" \"\"\"Make API call to Ollama (local)\"\"\"\n",
|
||||
" try:\n",
|
||||
" url = \"http://localhost:11434/api/generate\"\n",
|
||||
" data = {\n",
|
||||
" \"model\": model,\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"stream\": False,\n",
|
||||
" \"options\": {\n",
|
||||
" \"temperature\": 0.1,\n",
|
||||
" \"num_predict\": 4000\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" response = requests.post(url, json=data, timeout=60)\n",
|
||||
" if response.status_code == 200:\n",
|
||||
" result = response.json()\n",
|
||||
" return result.get('response', '').strip()\n",
|
||||
" else:\n",
|
||||
" return f\"Error calling Ollama API: HTTP {response.status_code}\"\n",
|
||||
" except requests.exceptions.ConnectionError:\n",
|
||||
" return \"Error: Could not connect to Ollama. Make sure Ollama is running locally on port 11434.\"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error calling Ollama API: {str(e)}\"\n",
|
||||
"\n",
|
||||
" def process_code(self, language: str, code: str, llm: str, generate_comments: bool, generate_tests: bool) -> str:\n",
|
||||
" \"\"\"Process the given code based on selected options\"\"\"\n",
|
||||
" if not code.strip():\n",
|
||||
" return \"Error: Please provide code to process.\"\n",
|
||||
" \n",
|
||||
" if not generate_comments and not generate_tests:\n",
|
||||
" return \"Error: Please select at least one option (Generate comments or Generate test units).\"\n",
|
||||
" \n",
|
||||
" # Determine which prompt to use\n",
|
||||
" if generate_comments and generate_tests:\n",
|
||||
" prompt = self.create_combined_prompt(code, language)\n",
|
||||
" elif generate_comments:\n",
|
||||
" prompt = self.create_comments_prompt(code, language)\n",
|
||||
" else: # generate_tests only\n",
|
||||
" prompt = self.create_tests_prompt(code, language)\n",
|
||||
" \n",
|
||||
" # Route to appropriate LLM\n",
|
||||
" if llm == \"gpt-4o-mini\":\n",
|
||||
" return self.call_openai(prompt, \"gpt-4o-mini\")\n",
|
||||
" elif llm == \"claude-3-5-haiku-20241022\":\n",
|
||||
" return self.call_anthropic(prompt, \"claude-3-5-haiku-20241022\")\n",
|
||||
" elif llm == \"gemini-2.0-flash\":\n",
|
||||
" return self.call_gemini(prompt)\n",
|
||||
" elif llm == \"ollama:llama3.2:latest\":\n",
|
||||
" return self.call_ollama(prompt, \"llama3.2:latest\")\n",
|
||||
" else:\n",
|
||||
" return f\"Error: Unsupported LLM: {llm}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "813f0911-d53f-4887-9341-656712e32d8f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def create_gradio_interface():\n",
|
||||
" \"\"\"Create and configure the Gradio interface\"\"\"\n",
|
||||
" commenter = CodeCommenter()\n",
|
||||
" \n",
|
||||
" # Define the main function for the interface\n",
|
||||
" def process_code_interface(language, code, llm, generate_comments, generate_tests):\n",
|
||||
" \"\"\"Process the code and return processed version based on selected options\"\"\"\n",
|
||||
" if not code.strip():\n",
|
||||
" return \"Please enter some code to process.\"\n",
|
||||
" \n",
|
||||
" if not generate_comments and not generate_tests:\n",
|
||||
" return \"Please select at least one option: Generate comments or Generate test units.\"\n",
|
||||
" \n",
|
||||
" # Show processing message\n",
|
||||
" options = []\n",
|
||||
" if generate_comments:\n",
|
||||
" options.append(\"comments\")\n",
|
||||
" if generate_tests:\n",
|
||||
" options.append(\"unit tests\")\n",
|
||||
" \n",
|
||||
" processing_msg = f\"Processing {language} code with {llm} to generate {' and '.join(options)}...\"\n",
|
||||
" print(processing_msg)\n",
|
||||
" \n",
|
||||
" # Process the code\n",
|
||||
" result = commenter.process_code(language, code, llm, generate_comments, generate_tests)\n",
|
||||
" return result\n",
|
||||
" \n",
|
||||
" # Define default code\n",
|
||||
" default_code = \"\"\"import pyodbc\n",
|
||||
"from tabulate import tabulate\n",
|
||||
"def connect_to_sql_server(server_name, database, username=None, password=None):\n",
|
||||
" try:\n",
|
||||
" if username and password:\n",
|
||||
" connection_string = f\"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server_name};DATABASE={database};UID={username};PWD={password}\"\n",
|
||||
" else:\n",
|
||||
" connection_string = f\"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server_name};DATABASE={database};Trusted_Connection=yes\"\n",
|
||||
" connection = pyodbc.connect(connection_string)\n",
|
||||
" print(f\"Successfully connected to {server_name}/{database}\")\n",
|
||||
" return connection\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to connect to {server_name}/{database}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"def get_record_count(connection, table_name):\n",
|
||||
" try:\n",
|
||||
" cursor = connection.cursor()\n",
|
||||
" query = f\"SELECT COUNT(*) FROM {table_name}\"\n",
|
||||
" cursor.execute(query)\n",
|
||||
" count = cursor.fetchone()[0]\n",
|
||||
" cursor.close()\n",
|
||||
" print(f\"Record count for {table_name}: {count}\")\n",
|
||||
" return count\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to get record count for {table_name}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"def select_top_records(connection, table_name, n):\n",
|
||||
" try:\n",
|
||||
" cursor = connection.cursor()\n",
|
||||
" query = f\"SELECT TOP {n} * FROM {table_name}\"\n",
|
||||
" cursor.execute(query)\n",
|
||||
" records = cursor.fetchall()\n",
|
||||
" columns = [column[0] for column in cursor.description]\n",
|
||||
" cursor.close()\n",
|
||||
" print(f\"Top {n} records from {table_name}\")\n",
|
||||
" if records:\n",
|
||||
" print(tabulate(records, headers=columns, tablefmt=\"grid\"))\n",
|
||||
" return records\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to retrieve top {n} records from {table_name}: {str(e)}\")\n",
|
||||
" return None\n",
|
||||
"conn = connect_to_sql_server(\"localhost\", \"AdventureWorks_lite\")\n",
|
||||
"if conn:\n",
|
||||
" total_records = get_record_count(conn, \"Sales.SalesOrderDetail\")\n",
|
||||
" top_records = select_top_records(conn, \"Production.Product\", 10)\n",
|
||||
" conn.close()\n",
|
||||
" print(\"Connection closed successfully\")\"\"\"\n",
|
||||
"\n",
|
||||
" css = \"\"\"\n",
|
||||
"textarea[rows]:not([rows=\"1\"]) {\n",
|
||||
" overflow-y: auto !important;\n",
|
||||
" scrollbar-width: thin !important;\n",
|
||||
"}\n",
|
||||
"textarea[rows]:not([rows=\"1\"])::-webkit-scrollbar {\n",
|
||||
" all: initial !important;\n",
|
||||
" background: #f1f1f1 !important;\n",
|
||||
"}\n",
|
||||
"textarea[rows]:not([rows=\"1\"])::-webkit-scrollbar-thumb {\n",
|
||||
" all: initial !important;\n",
|
||||
" background: #a8a8a8 !important;\n",
|
||||
"}\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
" # Create the interface\n",
|
||||
" with gr.Blocks(title=\"Code Commenter & Test Generator\", theme=gr.themes.Base(), css=css) as interface:\n",
|
||||
" gr.Markdown(\"# 🔧 Code Commenter & Test Generator\")\n",
|
||||
" gr.Markdown(\"Add detailed comments, docstrings, and/or generate unit tests for your code using various LLM models.\")\n",
|
||||
" \n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" code_input = gr.Textbox(\n",
|
||||
" label=\"Input Code\",\n",
|
||||
" value=default_code,\n",
|
||||
" lines=15,\n",
|
||||
" max_lines=20,\n",
|
||||
" info=\"Enter the code you want to process\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with gr.Column():\n",
|
||||
" code_output = gr.Textbox(\n",
|
||||
" label=\"Processed Code\",\n",
|
||||
" lines=20,\n",
|
||||
" max_lines=20,\n",
|
||||
" info=\"Your code with added comments, docstrings, and/or unit tests\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" # Add checkboxes below the textboxes\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" generate_comments_checkbox = gr.Checkbox(\n",
|
||||
" label=\"Generate comments\",\n",
|
||||
" value=True,\n",
|
||||
" info=\"Add detailed comments and docstrings to the code\"\n",
|
||||
" )\n",
|
||||
" generate_tests_checkbox = gr.Checkbox(\n",
|
||||
" label=\"Generate test units\",\n",
|
||||
" value=False,\n",
|
||||
" info=\"Generate comprehensive unit tests for the code\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column(scale=1):\n",
|
||||
" language_dropdown = gr.Dropdown(\n",
|
||||
" choices=[\"Python\", \"Ruby\", \"Rust\", \"C++\", \"Java\"],\n",
|
||||
" value=\"Python\",\n",
|
||||
" label=\"Programming Language\",\n",
|
||||
" info=\"Select the programming language of your code\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" llm_dropdown = gr.Dropdown(\n",
|
||||
" choices=[\n",
|
||||
" \"gpt-4o-mini\",\n",
|
||||
" \"claude-3-5-haiku-20241022\", \n",
|
||||
" \"gemini-2.0-flash\",\n",
|
||||
" \"ollama:llama3.2:latest\"\n",
|
||||
" ],\n",
|
||||
" value=\"gpt-4o-mini\",\n",
|
||||
" label=\"LLM Model\",\n",
|
||||
" info=\"Choose the language model to use\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" generate_btn = gr.Button(\n",
|
||||
" \"🚀 Process Code\", \n",
|
||||
" variant=\"primary\",\n",
|
||||
" size=\"lg\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" # Add some API setup information\n",
|
||||
" gr.Markdown(\"## 📝 API Setup Instructions\")\n",
|
||||
" gr.Markdown(\"\"\"\n",
|
||||
" To use this tool, you need to set up API keys as environment variables:\n",
|
||||
" \n",
|
||||
" - **OpenAI**: Set `OPENAI_API_KEY`\n",
|
||||
" - **Anthropic**: Set `ANTHROPIC_API_KEY` \n",
|
||||
" - **Google Gemini**: Set `GOOGLE_API_KEY`\n",
|
||||
" - **Ollama**: Make sure Ollama is running locally on port 11434\n",
|
||||
" \"\"\")\n",
|
||||
" \n",
|
||||
" gr.Markdown(\"## ✨ Features\")\n",
|
||||
" gr.Markdown(\"\"\"\n",
|
||||
" - **Generate Comments**: Add detailed docstrings and inline comments\n",
|
||||
" - **Generate Unit Tests**: Create comprehensive test suites with mocking for external dependencies\n",
|
||||
" - **Combined Mode**: Generate both comments and unit tests in one go\n",
|
||||
" - **Multiple LLMs**: Choose from OpenAI, Anthropic, Google Gemini, or local Ollama models\n",
|
||||
" - **Multiple Languages**: Support for Python, Ruby, Rust, C++, and Java\n",
|
||||
" \"\"\")\n",
|
||||
" \n",
|
||||
" # Connect the button to the processing function\n",
|
||||
" generate_btn.click(\n",
|
||||
" fn=process_code_interface,\n",
|
||||
" inputs=[language_dropdown, code_input, llm_dropdown, generate_comments_checkbox, generate_tests_checkbox],\n",
|
||||
" outputs=code_output,\n",
|
||||
" show_progress=True\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" return interface"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "ef461e08-c1d5-406d-b7d2-a4329f16486e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"🚀 Starting Code Commenter & Test Generator...\n",
|
||||
"📋 Setting up Gradio interface...\n",
|
||||
"🌐 Launching interface...\n",
|
||||
"💡 The interface will open in your default browser\n",
|
||||
"🔧 Make sure to set up your API keys as environment variables\n",
|
||||
"* Running on local URL: http://127.0.0.1:7860\n",
|
||||
"\n",
|
||||
"To create a public link, set `share=True` in `launch()`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": []
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(\"🚀 Starting Code Commenter & Test Generator...\")\n",
|
||||
"print(\"📋 Setting up Gradio interface...\")\n",
|
||||
"\n",
|
||||
"# Create and launch the interface\n",
|
||||
"interface = create_gradio_interface()\n",
|
||||
"\n",
|
||||
"print(\"🌐 Launching interface...\")\n",
|
||||
"print(\"💡 The interface will open in your default browser\")\n",
|
||||
"print(\"🔧 Make sure to set up your API keys as environment variables\")\n",
|
||||
"\n",
|
||||
"# Launch with auto-opening in browser\n",
|
||||
"interface.launch(\n",
|
||||
" server_name=\"127.0.0.1\",\n",
|
||||
" server_port=7860,\n",
|
||||
" share=False,\n",
|
||||
" inbrowser=True,\n",
|
||||
" show_error=True\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
335
week4/community-contributions/code_commentor.ipynb
Normal file
335
week4/community-contributions/code_commentor.ipynb
Normal file
@@ -0,0 +1,335 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "07bb451d-2b91-425f-b8ea-6f35ced780b0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# AI Code Commenting Assistant \n",
|
||||
"\n",
|
||||
"## Project Summary \n",
|
||||
"\n",
|
||||
"**Purpose**: \n",
|
||||
"An AI-powered assistant that automatically generates **clear, concise code comments** to improve code readability and maintainability. \n",
|
||||
"\n",
|
||||
"**Key Features**: \n",
|
||||
"- **Language-Agnostic**: Auto-detects programming languages or allows manual specification \n",
|
||||
"- **Smart Commenting**: Focuses on explaining **complex logic, algorithms, and edge cases** (not obvious syntax) \n",
|
||||
"- **Customizable**: Optional focus areas let users prioritize specific parts (e.g., database queries, recursion) \n",
|
||||
"- **Efficient Workflow**: Processes code in chunks and preserves original formatting \n",
|
||||
"\n",
|
||||
"**Benefits**: \n",
|
||||
"✔ Saves time writing documentation \n",
|
||||
"✔ Helps developers understand unfamiliar code \n",
|
||||
"✔ Supports multiple languages (Python, JavaScript, C++, SQL, etc.) \n",
|
||||
"✔ Avoids redundant comments on trivial operations \n",
|
||||
"\n",
|
||||
"**Example Use Case**: \n",
|
||||
"```python \n",
|
||||
"# Before AI: \n",
|
||||
"def fib(n): \n",
|
||||
" if n <= 1: return n \n",
|
||||
" else: return fib(n-1) + fib(n-2) \n",
|
||||
"\n",
|
||||
"# After AI: \n",
|
||||
"def fib(n): \n",
|
||||
" # Recursively computes nth Fibonacci number (O(2^n) time) \n",
|
||||
" if n <= 1: return n # Base case \n",
|
||||
" else: return fib(n-1) + fib(n-2) # Recursive case "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a0413ae1-0348-4884-ba95-384c4c8f841c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install --upgrade huggingface_hub"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b22da766-042b-402f-9e05-78aa8f45ddd4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import io\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from google import genai\n",
|
||||
"from google.genai import types\n",
|
||||
"from openai import OpenAI\n",
|
||||
"from anthropic import Anthropic\n",
|
||||
"from huggingface_hub import InferenceClient\n",
|
||||
"import gradio as gr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5af6d3de-bab6-475e-b2f3-7b788bb2e529",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# load environments\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"os.environ['ANTHROPIC_API_KEY'] = os.getenv(\"CLAUDE_API_KEY\")\n",
|
||||
"os.environ[\"HF_TOKEN\"] = os.getenv(\"HF_TOKEN\")\n",
|
||||
"gemini_api_key= os.getenv(\"GEMINI_API_KEY\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "cad0755e-4174-4fbc-84e6-15cc54bc609a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#initialize remote models\n",
|
||||
"claude= Anthropic()\n",
|
||||
"gemini = genai.Client(api_key=gemini_api_key)\n",
|
||||
"\n",
|
||||
"#opensource models\n",
|
||||
"qwen = InferenceClient(provider=\"featherless-ai\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "31d75812-1cd3-4512-8446-022c3357c354",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#initialize local model\n",
|
||||
"llama = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "31316379-2a56-4707-b207-ea60b490f536",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#models\n",
|
||||
"claude_model = \"claude-3-5-haiku-latest\"\n",
|
||||
"gemini_model = \"gemini-2.5-pro\"\n",
|
||||
"qwen_model= \"Qwen/Qwen2.5-Coder-32B-Instruct\"\n",
|
||||
"llama_model = \"llama3:8b\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b7d9c4bf-0955-4406-8717-ffa7bdd0bec9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_message=\"\"\"\n",
|
||||
"You are an expert AI specialized in code documentation. Your task is to generate concise, meaningful comments that explain the purpose and logic of provided code. Follow these rules:\n",
|
||||
"\n",
|
||||
"1. **Infer language**: Auto-detect programming language and use appropriate comment syntax\n",
|
||||
"2. **Explain why, not what**: Focus on purpose, edge cases, and non-obvious logic\n",
|
||||
"3. **Be concise**: Maximum 1-2 sentences per comment block\n",
|
||||
"4. **Prioritize key sections**: Only comment complex logic, algorithms, or critical operations\n",
|
||||
"5. **Maintain structure**: Preserve original code formatting and indentation\n",
|
||||
"6. **Output format**: Return ONLY commented code with no additional text\n",
|
||||
"\n",
|
||||
"Commenting guidelines by language:\n",
|
||||
"- Python: `# Inline comments` and `\"\"Docstrings\"\"`\n",
|
||||
"- JavaScript/Java: `// Line comments` and `/* Block comments */`\n",
|
||||
"- C/C++: `//` and `/* */`\n",
|
||||
"- SQL: `-- Line comments`\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "79dfe110-1523-40c7-ad90-2787ed22fd8d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt(code):\n",
|
||||
" prompt = f\"\"\"\n",
|
||||
" i want to document my code for better understanding. Please generate meaningful necessary comments\n",
|
||||
" here is my code:\n",
|
||||
" {code}\n",
|
||||
"\n",
|
||||
" Return ONLY commented code with no additional text\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" return prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c7bcf29e-ec78-4cfd-9b41-f2dc86400435",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def conversation_template(code):\n",
|
||||
" messages = [\n",
|
||||
" {\"role\":\"system\", \"content\":system_message},\n",
|
||||
" {\"role\":\"user\",\"content\":user_prompt(code)}\n",
|
||||
" ]\n",
|
||||
" return messages"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a36fec0f-7eba-4ccd-8fc4-cbf5ade76fa2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gemini(code):\n",
|
||||
" message = user_prompt(code)\n",
|
||||
" response = gemini.models.generate_content_stream(\n",
|
||||
" model=gemini_model,\n",
|
||||
" config= types.GenerateContentConfig(\n",
|
||||
" system_instruction = system_message,\n",
|
||||
" temperature = 0.8,\n",
|
||||
" ),\n",
|
||||
" contents = [message]\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in response:\n",
|
||||
" result += chunk.text or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5d1e0c0-dc88-43ee-8698-82ad9ce7c51b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_claude(code):\n",
|
||||
" messages = [{\"role\":\"user\",\"content\":user_prompt(code)}]\n",
|
||||
" response = claude.messages.stream(\n",
|
||||
" model= claude_model,\n",
|
||||
" temperature=0.8,\n",
|
||||
" messages = messages,\n",
|
||||
" max_tokens=5000\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" result = \"\"\n",
|
||||
" with response as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" result += text or \"\"\n",
|
||||
" yield result\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "903c97e5-9170-449e-8a0f-9f906351ec45",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_opensource(code,model):\n",
|
||||
" model = model.lower()\n",
|
||||
" client = globals()[model]\n",
|
||||
" model = globals()[f\"{model}_model\"]\n",
|
||||
" stream = client.chat.completions.create(\n",
|
||||
" model = model,\n",
|
||||
" messages= conversation_template(code),\n",
|
||||
" temperature = 0.7,\n",
|
||||
" stream = True\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" result += chunk.choices[0].delta.content or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ff051c22-a2f8-4153-b970-f8a466a4cf5a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def commentor(code, model):\n",
|
||||
" model =model.lower()\n",
|
||||
" if model == \"claude\":\n",
|
||||
" result = stream_claude(code)\n",
|
||||
" elif model == \"gemini\":\n",
|
||||
" result = stream_gemini(code)\n",
|
||||
" elif model == \"qwen\" or model == \"llama\":\n",
|
||||
" result = stream_opensource(code, model)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" for code in result:\n",
|
||||
" yield code.replace(\"```cpp\\n\",\"\").replace(\"```python\\n\",\"\").replace(\"```javascript\\n\",\"\").replace(\"```typescript\\n\",\"\").replace(\"```\",\"\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "10daf070-3546-4073-a2a0-3f5f8fc156f0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks() as ui:\n",
|
||||
" gr.Markdown(\"# Genarate comment\")\n",
|
||||
" with gr.Row():\n",
|
||||
" raw_code = gr.Textbox(label=\"Raw Code:\", lines=10)\n",
|
||||
" commented_code = gr.Textbox(label=\"Commented_code\",lines=10)\n",
|
||||
" with gr.Row():\n",
|
||||
" models = gr.Dropdown([\"Gemini\",\"Claude\",\"Llama\",\"Qwen\"], value=\"Gemini\")\n",
|
||||
" with gr.Row():\n",
|
||||
" generate_comment = gr.Button(\"Generate Comment\")\n",
|
||||
"\n",
|
||||
" generate_comment.click(commentor, inputs=[raw_code, models], outputs=[commented_code])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "afb87f32-f25e-40c5-844a-d2b7af748192",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ui.launch(inbrowser=True,debug=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "96bc48ad-10ad-4821-b58e-ea1b22cdcdc9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.11.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
300
week4/community-contributions/day5_java_code_commenter.ipynb
Normal file
300
week4/community-contributions/day5_java_code_commenter.ipynb
Normal file
@@ -0,0 +1,300 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "45ca91c2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# AI tool to add comments to the provided Java code\n",
|
||||
"\n",
|
||||
"Here we build a Gradio App that uses the frontier models to add comments to a java code. For testing purposes I have used the *cheaper* versions of the models, not the ones the leaderboards indicate as the best ones."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f44901f5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# imports\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import google.generativeai as genai\n",
|
||||
"import anthropic\n",
|
||||
"import gradio as gr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c47706b3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# environment\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
"google_api_key = os.getenv('GOOGLE_API_KEY')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "35446b9a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"openai = OpenAI()\n",
|
||||
"claude = anthropic.Anthropic()\n",
|
||||
"genai.configure()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0e899efd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"OPENAI_MODEL = \"gpt-4o-mini\"\n",
|
||||
"CLAUDE_MODEL = \"claude-3-haiku-20240307\"\n",
|
||||
"GEMINI_MODEL = 'gemini-2.0-flash-lite'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "47640f53",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_message = \"You are an assistant that adds comments to java code. \"\n",
|
||||
"system_message += \"Do not make any changes to the code itself.\"\n",
|
||||
"system_message += \"Use comments sparingly. Only add them in places where they help to undestand how the code works. Do not comment every single line of the code.\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f41ccbf0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt_for(code):\n",
|
||||
" user_prompt = \"Add helpful comments to this java code. \"\n",
|
||||
" user_prompt += \"Do not change the code itself.\\n\\n\"\n",
|
||||
" user_prompt += code\n",
|
||||
" return user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c57c0000",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test_code = \"\"\"\n",
|
||||
"package com.hma.kafkaproducertest.producer;\n",
|
||||
"\n",
|
||||
"import com.hma.kafkaproducertest.model.TestDTO;\n",
|
||||
"import org.springframework.cloud.stream.function.StreamBridge;\n",
|
||||
"import org.springframework.messaging.Message;\n",
|
||||
"import org.springframework.messaging.support.MessageBuilder;\n",
|
||||
"import org.springframework.stereotype.Component;\n",
|
||||
"\n",
|
||||
"import java.util.Arrays;\n",
|
||||
"import java.util.Comparator;\n",
|
||||
"import java.util.StringJoiner;\n",
|
||||
"import java.util.stream.Collectors;\n",
|
||||
"import java.util.stream.IntStream;\n",
|
||||
"\n",
|
||||
"@Component\n",
|
||||
"public class TestProducer {\n",
|
||||
"\n",
|
||||
" public static final String EVENT_TYPE_HEADER = \"event-type\";\n",
|
||||
" private static final String BINDING_NAME = \"testProducer-out-0\";\n",
|
||||
"\n",
|
||||
" private final StreamBridge streamBridge;\n",
|
||||
"\n",
|
||||
" public TestProducer(StreamBridge streamBridge) {\n",
|
||||
" this.streamBridge = streamBridge;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" public void sendMessage(TestDTO payload, String eventType){\n",
|
||||
" Message<TestDTO> message = MessageBuilder\n",
|
||||
" .withPayload(payload)\n",
|
||||
" .setHeader(EVENT_TYPE_HEADER, eventType)\n",
|
||||
" .build();\n",
|
||||
"\n",
|
||||
" streamBridge.send(BINDING_NAME, message);\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" public void test(String t1, String t2) {\n",
|
||||
" var s = t1.length() > t2.length() ? t2 : t1;\n",
|
||||
" var l = t1.length() > t2.length() ? t1 : t2;\n",
|
||||
" var res = true;\n",
|
||||
" for (int i = 0; i < s.length(); i++) {\n",
|
||||
" if (s.charAt(i) == l.charAt(i)) {\n",
|
||||
" res = false;\n",
|
||||
" break;\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" System.out.println(res);\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "00c71128",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gpt(code):\n",
|
||||
" messages = [\n",
|
||||
" {\"role\": \"system\", \"content\": system_message},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(code)}\n",
|
||||
" ]\n",
|
||||
" stream = openai.chat.completions.create(\n",
|
||||
" model=OPENAI_MODEL,\n",
|
||||
" messages=messages,\n",
|
||||
" stream=True\n",
|
||||
" )\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" result += chunk.choices[0].delta.content or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ca92f8a8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_claude(code):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=2000,\n",
|
||||
" system=system_message,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(code)},\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" response = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" response += text or \"\"\n",
|
||||
" yield response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9dffed4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gemini(code):\n",
|
||||
" gemini = genai.GenerativeModel(\n",
|
||||
" model_name=GEMINI_MODEL,\n",
|
||||
" system_instruction=system_message\n",
|
||||
" )\n",
|
||||
" stream = gemini.generate_content(user_prompt_for(code), stream=True)\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" result += chunk.text or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "31f9c267",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def comment_code(code, model):\n",
|
||||
" if model==\"GPT\":\n",
|
||||
" result = stream_gpt(code)\n",
|
||||
" elif model==\"Claude\":\n",
|
||||
" result = stream_claude(code)\n",
|
||||
" elif model==\"Gemini\":\n",
|
||||
" result = stream_gemini(code)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown model\")\n",
|
||||
" yield from result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c04c0a1b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks() as ui:\n",
|
||||
" with gr.Row():\n",
|
||||
" original_code = gr.Textbox(label=\"Java code:\", lines=10, value=test_code)\n",
|
||||
" commented_code = gr.Markdown(label=\"Commented code:\")\n",
|
||||
" with gr.Row():\n",
|
||||
" model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
|
||||
" comment = gr.Button(\"Comment code\")\n",
|
||||
"\n",
|
||||
" comment.click(comment_code, inputs=[original_code, model], outputs=[commented_code])\n",
|
||||
"\n",
|
||||
"ui.launch(inbrowser=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "84d33a5f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ui.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bbd50bf7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Conclusion\n",
|
||||
"\n",
|
||||
"In my personal opinion, at least when using these *cheaper* versions of the models, the result provided by Claude is the best. ChatGPT adds way too many comments even if the system message discourages that. Gemini provides a good result also, but maybe adds a tad too few comments -- although that certainly depends on your personal preferences."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "llms",
|
||||
"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.11.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "45ca91c2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# AI tool to generate unit tests for the provided Java code\n",
|
||||
"\n",
|
||||
"Here we build a Gradio App that uses the frontier models to generate unit tests for a java code. For testing purposes I have used the *cheaper* versions of the models, not the ones the leaderboards indicate as the best ones."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f44901f5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# imports\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import google.generativeai as genai\n",
|
||||
"import anthropic\n",
|
||||
"import gradio as gr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c47706b3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# environment\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
"google_api_key = os.getenv('GOOGLE_API_KEY')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "35446b9a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"openai = OpenAI()\n",
|
||||
"claude = anthropic.Anthropic()\n",
|
||||
"genai.configure()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0e899efd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"OPENAI_MODEL = \"gpt-4o-mini\"\n",
|
||||
"CLAUDE_MODEL = \"claude-3-haiku-20240307\"\n",
|
||||
"GEMINI_MODEL = 'gemini-2.0-flash-lite'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "47640f53",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_message = \"You are an assistant that generates unit test for java code. \"\n",
|
||||
"system_message += \"Generate one JUnit5 test class with all the relevant test cases in it.\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f41ccbf0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt_for(code):\n",
|
||||
" user_prompt = \"Generate unit tests for this java code.\\n\\n\"\n",
|
||||
" user_prompt += code\n",
|
||||
" return user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c57c0000",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test_code = \"\"\"\n",
|
||||
"package com.hma.kafkaproducertest.rest;\n",
|
||||
"\n",
|
||||
"import com.hma.kafkaproducertest.model.TestDTO;\n",
|
||||
"import com.hma.kafkaproducertest.producer.TestProducer;\n",
|
||||
"import org.springframework.web.bind.annotation.*;\n",
|
||||
"\n",
|
||||
"@RestController\n",
|
||||
"@RequestMapping(\"/api\")\n",
|
||||
"public class TestController {\n",
|
||||
"\n",
|
||||
" private final TestProducer producer;\n",
|
||||
"\n",
|
||||
" public TestController(TestProducer producer) {\n",
|
||||
" this.producer = producer;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" @PostMapping(\"/event\")\n",
|
||||
" public TestDTO triggerKafkaEvent(@RequestBody TestDTO payload) {\n",
|
||||
" producer.sendMessage(payload, \"test\");\n",
|
||||
" return payload;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "00c71128",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gpt(code):\n",
|
||||
" messages = [\n",
|
||||
" {\"role\": \"system\", \"content\": system_message},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(code)}\n",
|
||||
" ]\n",
|
||||
" stream = openai.chat.completions.create(\n",
|
||||
" model=OPENAI_MODEL,\n",
|
||||
" messages=messages,\n",
|
||||
" stream=True\n",
|
||||
" )\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" result += chunk.choices[0].delta.content or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ca92f8a8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_claude(code):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=2000,\n",
|
||||
" system=system_message,\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(code)},\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" response = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" response += text or \"\"\n",
|
||||
" yield response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9dffed4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def stream_gemini(code):\n",
|
||||
" gemini = genai.GenerativeModel(\n",
|
||||
" model_name=GEMINI_MODEL,\n",
|
||||
" system_instruction=system_message\n",
|
||||
" )\n",
|
||||
" stream = gemini.generate_content(user_prompt_for(code), stream=True)\n",
|
||||
" result = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" result += chunk.text or \"\"\n",
|
||||
" yield result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "31f9c267",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_tests(code, model):\n",
|
||||
" if model==\"GPT\":\n",
|
||||
" result = stream_gpt(code)\n",
|
||||
" elif model==\"Claude\":\n",
|
||||
" result = stream_claude(code)\n",
|
||||
" elif model==\"Gemini\":\n",
|
||||
" result = stream_gemini(code)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown model\")\n",
|
||||
" yield from result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c04c0a1b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks() as ui:\n",
|
||||
" with gr.Row():\n",
|
||||
" original_code = gr.Textbox(label=\"Java code:\", lines=10, value=test_code)\n",
|
||||
" generated_code = gr.Markdown(label=\"Unit tests:\")\n",
|
||||
" with gr.Row():\n",
|
||||
" model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
|
||||
" generate = gr.Button(\"Generate tests\")\n",
|
||||
"\n",
|
||||
" generate.click(generate_tests, inputs=[original_code, model], outputs=[generated_code])\n",
|
||||
"\n",
|
||||
"ui.launch(inbrowser=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "84d33a5f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ui.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bbd50bf7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Conclusion\n",
|
||||
"\n",
|
||||
"The models are missing some information as the `TestDTO` is not defined in the code provided as an input.\n",
|
||||
"\n",
|
||||
"Results:\n",
|
||||
"- Gemini: Generates a well constructed test class with multiple test cases covering scenarios with valid and invalid inputs. It makes assumptions about the content of `TestDTO` and adds a note about those as a comment.\n",
|
||||
"- Claude: Similar approach to unknown format of `TestDTO`, although no comment added about the assumptions made. The test cases are strutured differently, and they don't cover any case of invalid input, which in my opinion is an important test for a REST endpoint.\n",
|
||||
"- GPT: While the other two generated *real* unit tests using the mockito extension, GPT generated a *webMVC* test. The other two relied on the equality impelemntation of `TestDTO`, while GPT checks separately each field in the response. As this type of test spins up the application context, the test won't run without additional configuration. In addition, some imports are missing from the test file.\n",
|
||||
"\n",
|
||||
"It comes down to personal preferences, but I would give the point to Gemini for this one."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "llms",
|
||||
"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.11.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
337
week4/community-contributions/wk4-final-passwordgen.ipynb
Normal file
337
week4/community-contributions/wk4-final-passwordgen.ipynb
Normal file
@@ -0,0 +1,337 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cc7674a9-6164-4424-85a9-f669454cfd2a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"I used this project to play about with Gradio blocks a little bit as it had more inputs than the other projects I've done.\n",
|
||||
"Its a password generator which I have no doubt I will use!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "04c8d2dd-cb9a-4b18-b12d-48ed2f39679a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# imports\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import requests\n",
|
||||
"import google.generativeai\n",
|
||||
"import anthropic\n",
|
||||
"from IPython.display import Markdown, display, update_display\n",
|
||||
"import gradio as gr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "04521351-f220-42fe-9dc5-d0be80c95dd7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# keys\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
|
||||
"\n",
|
||||
"if openai_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"OpenAI key issue\")\n",
|
||||
"\n",
|
||||
"claude_api_key = os.getenv(\"ANTHROPIC_API_KEY\")\n",
|
||||
"\n",
|
||||
"if claude_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"Claude key issue\")\n",
|
||||
"\n",
|
||||
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
|
||||
"\n",
|
||||
"if google_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"Google key issue\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "70fd3748-e6b6-4ac2-89a5-ef65ed7e41a3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# initialise\n",
|
||||
"\n",
|
||||
"openai = OpenAI()\n",
|
||||
"claude = anthropic.Anthropic()\n",
|
||||
"google.generativeai.configure()\n",
|
||||
"\n",
|
||||
"OPENAI_MODEL = \"gpt-4o\"\n",
|
||||
"CLAUDE_MODEL = \"claude-sonnet-4-20250514\"\n",
|
||||
"GOOGLE_MODEL = \"gemini-2.0-flash\"\n",
|
||||
"\n",
|
||||
"max_tok = 500"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6a448651-e426-4c3c-96f7-d69975dc7b10",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Prompts\n",
|
||||
"\n",
|
||||
"def pass_system_prompt(required_len, spec_char=\"Y\",num_char=\"Y\",min_lowercase=1,min_uppercase=1):\n",
|
||||
"\n",
|
||||
" system_prompt = f\"\"\"You are a secure password generator. Your task is to create a single, cryptographically strong password that meets ALL specified requirements.\n",
|
||||
" \n",
|
||||
"CRITICAL REQUIREMENTS:\n",
|
||||
"- Length: EXACTLY {required_len} characters\n",
|
||||
"- Must include: At least {min_lowercase} lowercase letter(s) AND at least {min_uppercase} uppercase letter(s)\n",
|
||||
"- Special characters: {'REQUIRED - include at least 1 char' if spec_char else 'FORBIDDEN - do not include any'}\n",
|
||||
"- Numbers: {'REQUIRED - include at least 1 digit' if num_char else 'FORBIDDEN - do not include any digits'}\n",
|
||||
"\n",
|
||||
"SECURITY RULES:\n",
|
||||
"1. Generate truly random passwords - avoid patterns, dictionary words, or predictable sequences\n",
|
||||
"2. Distribute character types evenly throughout the password\n",
|
||||
"3. Do not use repeated characters excessively (max 2 of same character)\n",
|
||||
"4. Ensure password meets minimum complexity for each required character type\n",
|
||||
"\n",
|
||||
"OUTPUT FORMAT:\n",
|
||||
"- Respond with ONLY the generated password\n",
|
||||
"- No explanations, no additional text, just the password\n",
|
||||
"- Verify the password meets ALL requirements before responding\"\"\"\n",
|
||||
"\n",
|
||||
" return system_prompt\n",
|
||||
"\n",
|
||||
"def pass_user_prompt(required_len, spec_char=\"Y\",num_char=\"Y\",min_lowercase=1,min_uppercase=1):\n",
|
||||
" \n",
|
||||
" user_prompt = f\"\"\"Generate a secure password with these exact specifications:\n",
|
||||
" \n",
|
||||
"Length: {required_len} characters\n",
|
||||
"Lowercase letters: Required (minimum {min_lowercase})\n",
|
||||
"Uppercase letters: Required (minimum {min_uppercase})\n",
|
||||
"Numbers: {'Required (minimum 1)' if num_char else 'Not allowed'}\n",
|
||||
"Special characters: {'Required (minimum 1)' if spec_char else 'Not allowed'}\n",
|
||||
"\n",
|
||||
"Requirements verification checklist:\n",
|
||||
"✓ Exactly {required_len} characters total\n",
|
||||
"✓ Contains {min_lowercase}+ lowercase letters\n",
|
||||
"✓ Contains {min_uppercase}+ uppercase letters\n",
|
||||
"✓ {'Contains 1+ numbers' if num_char else 'Contains NO numbers'}\n",
|
||||
"✓ {'Contains 1+ special characters' if spec_char else 'Contains NO special characters'}\n",
|
||||
"✓ No obvious patterns or dictionary words\n",
|
||||
"✓ Good distribution of character types\n",
|
||||
"\n",
|
||||
"Generate the password now.\"\"\"\n",
|
||||
"\n",
|
||||
" return user_prompt\n",
|
||||
" \n",
|
||||
"def pass_messages(required_len, spec_char,num_char,min_lowercase,min_uppercase):\n",
|
||||
" messages = [\n",
|
||||
" {\"role\":\"system\",\"content\":pass_system_prompt(required_len, spec_char,num_char,min_lowercase,min_uppercase)},\n",
|
||||
" {\"role\":\"user\",\"content\":pass_user_prompt(required_len, spec_char,num_char,min_lowercase,min_uppercase)}\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" return messages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "857370b0-35a5-4b50-8715-86f8e781523b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#test\n",
|
||||
"\n",
|
||||
"messages1 = pass_messages(12, \"N\", \"Y\",1,1)\n",
|
||||
"print(messages1)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "59ab4279-90a8-4997-8e15-f07295856222",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def openai_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
|
||||
" response=openai.chat.completions.create(\n",
|
||||
" model=OPENAI_MODEL,\n",
|
||||
" max_tokens=max_tok,\n",
|
||||
" messages=pass_messages(required_len, spec_char,num_char,min_lowercase,min_uppercase)\n",
|
||||
" )\n",
|
||||
" return response.choices[0].message.content\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f5e1a41a-b03c-4408-a0f5-00529785f3d1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def claude_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
|
||||
" response = claude.messages.create(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=max_tok,\n",
|
||||
" system=pass_system_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase),\n",
|
||||
" messages = [{\"role\":\"user\",\"content\":pass_user_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase)}]\n",
|
||||
" )\n",
|
||||
" return response.content[0].text\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6a41a0a2-55a1-47e5-8fc0-5dd04ebd3573",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def google_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
|
||||
" message = google.generativeai.GenerativeModel(\n",
|
||||
" model_name=GOOGLE_MODEL,\n",
|
||||
" system_instruction=pass_system_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
|
||||
" )\n",
|
||||
" response = message.generate_content(pass_user_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase))\n",
|
||||
" return response.text"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dcd1ce50-6576-4594-8739-1d7daf602213",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#test\n",
|
||||
"messages1 = openai_password_gen(12, \"N\",\"Y\",1,1)\n",
|
||||
"messages2 = claude_password_gen(12,\"N\",\"Y\",1,1)\n",
|
||||
"messages3= google_password_gen(12,\"N\",\"Y\",1,1)\n",
|
||||
"print(\"OpenAI: \",messages1)\n",
|
||||
"print(\"Claude: \", messages2)\n",
|
||||
"print(\"Gemini: \", messages3)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9cec429a-2355-4941-8422-480b2614009c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# model select\n",
|
||||
"\n",
|
||||
"def select_model(required_len, spec_char, num_char,min_lowercase,min_uppercase,model):\n",
|
||||
" if model == \"OpenAI\":\n",
|
||||
" return openai_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
|
||||
" elif model == \"Claude\":\n",
|
||||
" return claude_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
|
||||
" elif model == \"Gemini\":\n",
|
||||
" return google_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
|
||||
" else:\n",
|
||||
" print(\"No model selected\")\n",
|
||||
" return None"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bef52e6d-dc50-4c91-9d56-624dfdd66276",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test = select_model(12, \"N\",\"Y\",1,1,\"OpenAI\")\n",
|
||||
"\n",
|
||||
"print(test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7b9d3685-a1b8-470c-8f4b-e63d68a0240d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"css = \"\"\"\n",
|
||||
"#password_box textarea {\n",
|
||||
" background-color: #306998;\n",
|
||||
" color: white;\n",
|
||||
"}\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "81c423ec-0ca7-4c96-a2fe-02ed2b5f3839",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"with gr.Blocks(css=css) as demo:\n",
|
||||
" gr.Markdown(\"Choose your password complexity requirements and run:\")\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column(min_width=150,scale=2):\n",
|
||||
" with gr.Row():\n",
|
||||
" required_len = gr.Number(label=\"Specify the required length\",value=12,minimum=1,maximum=30)\n",
|
||||
" min_lowercase = gr.Number(label=\"the minimum lowercase letters\", value=1,minimum=0)\n",
|
||||
" min_uppercase = gr.Number(label=\"the minimum uppercase letters\", value=1,minimum=0)\n",
|
||||
" with gr.Column():\n",
|
||||
" spec_char = gr.Checkbox(label=\"Include special characters?\",value=True)\n",
|
||||
" num_char = gr.Checkbox(label=\"Include numbers?\", value=True)\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" model = gr.Dropdown([\"OpenAI\",\"Claude\",\"Gemini\"])\n",
|
||||
" btn = gr.Button(\"Run\")\n",
|
||||
" with gr.Column():\n",
|
||||
" output = gr.Textbox(label=\"Password:\", elem_id=\"password_box\")\n",
|
||||
" \n",
|
||||
" btn.click(fn=select_model,inputs=[required_len,spec_char,num_char,min_lowercase,min_uppercase,model],outputs=output)\n",
|
||||
"\n",
|
||||
"demo.launch()\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d81a8318-57ef-46ae-91b7-ae63d661edd8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.11.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
420
week4/community-contributions/wk4-unittest-generator.ipynb
Normal file
420
week4/community-contributions/wk4-unittest-generator.ipynb
Normal file
@@ -0,0 +1,420 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "65b3aadc-c540-4cb2-a338-d523d3f22e5b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Unit test generator using GPT, Claude and Gemini.\n",
|
||||
"This will create unit test code from python and also run the code and provide the result (including any errors)\n",
|
||||
"Note:\n",
|
||||
"When I tried to use claude-sonnet-4-20250514 the results were too big and the python was cut-off (no matter how big I made the max tokens). This seemed to be the case for both examples. I've changed it to claude-3-5-sonnet-20240620 and it seems to be run better."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# imports\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import requests\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import google.generativeai\n",
|
||||
"import anthropic\n",
|
||||
"from IPython.display import Markdown, display, update_display\n",
|
||||
"import gradio as gr\n",
|
||||
"import sys\n",
|
||||
"import io\n",
|
||||
"import traceback\n",
|
||||
"import unittest\n",
|
||||
"import subprocess\n",
|
||||
"import tempfile"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# keys\n",
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
|
||||
"\n",
|
||||
"if openai_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"OpenAI key issue\")\n",
|
||||
"\n",
|
||||
"claude_api_key = os.getenv(\"ANTHROPIC_API_KEY\")\n",
|
||||
"\n",
|
||||
"if claude_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"Claude key issue\")\n",
|
||||
"\n",
|
||||
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
|
||||
"\n",
|
||||
"if google_api_key:\n",
|
||||
" print(\"All good\")\n",
|
||||
"else:\n",
|
||||
" print(\"Google key issue\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# initialise\n",
|
||||
"\n",
|
||||
"openai = OpenAI()\n",
|
||||
"claude = anthropic.Anthropic()\n",
|
||||
"google.generativeai.configure()\n",
|
||||
"\n",
|
||||
"OPENAI_MODEL = \"gpt-4o\"\n",
|
||||
"CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\" #\"claude-sonnet-4-20250514\"\n",
|
||||
"GOOGLE_MODEL = \"gemini-2.0-flash\"\n",
|
||||
"\n",
|
||||
"max_tok = 5000"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_message = \"You are an engineer with responsibility for unit testing python code.\"\n",
|
||||
"system_message += \"You review base python code and develop unit tests, also in python, which validate each unit of code.\"\n",
|
||||
"system_message += \"\"\" The output must be in Python with both the unit tests and comments explaining the purpose of each test.\n",
|
||||
"The output should not include any additional text at the start or end including \"```\". It should be possible to run the code without any updates including an execution statement.\n",
|
||||
"Include the base / original python code in the response.\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt_for(python):\n",
|
||||
" user_prompt = \"Review the Python code provided and develop unit tests which can be run in a jupyter lab.\"\n",
|
||||
" user_prompt += \"\"\" The output must be in Python with both the unit tests and comments explaining the purpose of each test.\n",
|
||||
"The output should not include any additional text at the start or end including \"```\". It should be possible to run the code without any updates (include an execution statement).\n",
|
||||
"Include the base / original python code in the response.\"\"\"\n",
|
||||
" user_prompt += python\n",
|
||||
" return user_prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def messages_for(python):\n",
|
||||
" return [\n",
|
||||
" {\"role\": \"system\", \"content\": system_message},\n",
|
||||
" {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
|
||||
" ]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0b327aa3-3277-44e1-972f-aa7158147ddd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# python example\n",
|
||||
"example = \"\"\"class BookNotAvailableError(Exception):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"class Library:\n",
|
||||
" def __init__(self):\n",
|
||||
" self.inventory = {} # book title -> quantity\n",
|
||||
" self.borrowed = {} # user -> list of borrowed book titles\n",
|
||||
"\n",
|
||||
" def add_book(self, title, quantity=1):\n",
|
||||
" if quantity <= 0:\n",
|
||||
" raise ValueError(\"Quantity must be positive\")\n",
|
||||
" self.inventory[title] = self.inventory.get(title, 0) + quantity\n",
|
||||
"\n",
|
||||
" def borrow_book(self, user, title):\n",
|
||||
" if self.inventory.get(title, 0) < 1:\n",
|
||||
" raise BookNotAvailableError(f\"'{title}' is not available\")\n",
|
||||
" self.inventory[title] -= 1\n",
|
||||
" self.borrowed.setdefault(user, []).append(title)\n",
|
||||
"\n",
|
||||
" def return_book(self, user, title):\n",
|
||||
" if user not in self.borrowed or title not in self.borrowed[user]:\n",
|
||||
" raise ValueError(f\"User '{user}' did not borrow '{title}'\")\n",
|
||||
" self.borrowed[user].remove(title)\n",
|
||||
" self.inventory[title] = self.inventory.get(title, 0) + 1\n",
|
||||
"\n",
|
||||
" def get_available_books(self):\n",
|
||||
" return {title: qty for title, qty in self.inventory.items() if qty > 0}\n",
|
||||
"\n",
|
||||
" def get_borrowed_books(self, user):\n",
|
||||
" return self.borrowed.get(user, [])\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ed6e624e-88a5-4f10-8ab5-f071f0ca3041",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# python example2\n",
|
||||
"example2 = \"\"\"class Calculator:\n",
|
||||
" def add(self, a, b):\n",
|
||||
" return a + b\n",
|
||||
"\n",
|
||||
" def subtract(self, a, b):\n",
|
||||
" return a - b\n",
|
||||
"\n",
|
||||
" def divide(self, a, b):\n",
|
||||
" if b == 0:\n",
|
||||
" raise ValueError(\"Cannot divide by zero\")\n",
|
||||
" return a / b\n",
|
||||
"\n",
|
||||
" def multiply(self, a, b):\n",
|
||||
" return a * b\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def is_prime(n):\n",
|
||||
" if n <= 1:\n",
|
||||
" return False\n",
|
||||
" if n <= 3:\n",
|
||||
" return True\n",
|
||||
" if n % 2 == 0 or n % 3 == 0:\n",
|
||||
" return False\n",
|
||||
" i = 5\n",
|
||||
" while i * i <= n:\n",
|
||||
" if n % i == 0 or n % (i + 2) == 0:\n",
|
||||
" return False\n",
|
||||
" i += 6\n",
|
||||
" return True\n",
|
||||
" \"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_gpt(python): \n",
|
||||
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" fragment = chunk.choices[0].delta.content or \"\"\n",
|
||||
" reply += fragment\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_claude(python):\n",
|
||||
" result = claude.messages.stream(\n",
|
||||
" model=CLAUDE_MODEL,\n",
|
||||
" max_tokens=max_tok,\n",
|
||||
" system=system_message,\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
|
||||
" )\n",
|
||||
" reply = \"\"\n",
|
||||
" with result as stream:\n",
|
||||
" for text in stream.text_stream:\n",
|
||||
" reply += text\n",
|
||||
" yield reply"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ad86f652-879a-489f-9891-bdc2d97c33b0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unit_test_google(python):\n",
|
||||
" model = google.generativeai.GenerativeModel(\n",
|
||||
" model_name=GOOGLE_MODEL,\n",
|
||||
" system_instruction=system_message\n",
|
||||
" )\n",
|
||||
" stream = model.generate_content(contents=user_prompt_for(python),stream=True)\n",
|
||||
" reply = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" reply += chunk.text or \"\"\n",
|
||||
" yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "105db6f9-343c-491d-8e44-3a5328b81719",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#unit_test_gpt(example)\n",
|
||||
"#unit_test_claude(example)\n",
|
||||
"#unit_test_google(example)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def select_model(python, model):\n",
|
||||
" if model==\"GPT\":\n",
|
||||
" result = unit_test_gpt(python)\n",
|
||||
" elif model==\"Claude\":\n",
|
||||
" result = unit_test_claude(python)\n",
|
||||
" elif model==\"Google\":\n",
|
||||
" result = unit_test_google(python)\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Unknown model\")\n",
|
||||
" for stream_so_far in result:\n",
|
||||
" yield stream_so_far "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# with gr.Blocks() as ui:\n",
|
||||
"# with gr.Row():\n",
|
||||
"# python = gr.Textbox(label=\"Python code:\", lines=10, value=example)\n",
|
||||
"# test = gr.Textbox(label=\"Unit tests\", lines=10)\n",
|
||||
"# with gr.Row():\n",
|
||||
"# model = gr.Dropdown([\"GPT\", \"Claude\",\"Google\"], label=\"Select model\", value=\"GPT\")\n",
|
||||
"# generate = gr.Button(\"Generate unit tests\")\n",
|
||||
"\n",
|
||||
"# generate.click(select_model, inputs=[python, model], outputs=[test])\n",
|
||||
"\n",
|
||||
"# ui.launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "389ae411-a4f6-44f2-8b26-d46a971687a7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def execute_python(code):\n",
|
||||
" # Capture stdout and stderr\n",
|
||||
" output = io.StringIO()\n",
|
||||
" sys_stdout = sys.stdout\n",
|
||||
" sys_stderr = sys.stderr\n",
|
||||
" sys.stdout = output\n",
|
||||
" sys.stderr = output\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" # Compile the code first\n",
|
||||
" compiled_code = compile(code, '<string>', 'exec')\n",
|
||||
"\n",
|
||||
" # Prepare a namespace dict for exec environment\n",
|
||||
" # Include __builtins__ so imports like 'import unittest' work\n",
|
||||
" namespace = {\"__builtins__\": __builtins__}\n",
|
||||
"\n",
|
||||
" # Run the user's code, but expect tests will be defined here\n",
|
||||
" exec(compiled_code, namespace)\n",
|
||||
"\n",
|
||||
" # Look for unittest.TestCase subclasses in the namespace\n",
|
||||
" loader = unittest.TestLoader()\n",
|
||||
" suite = unittest.TestSuite()\n",
|
||||
"\n",
|
||||
" for obj in namespace.values():\n",
|
||||
" if isinstance(obj, type) and issubclass(obj, unittest.TestCase):\n",
|
||||
" tests = loader.loadTestsFromTestCase(obj)\n",
|
||||
" suite.addTests(tests)\n",
|
||||
"\n",
|
||||
" # Run the tests\n",
|
||||
" runner = unittest.TextTestRunner(stream=output, verbosity=2)\n",
|
||||
" result = runner.run(suite)\n",
|
||||
"\n",
|
||||
" except SystemExit as e:\n",
|
||||
" # Catch sys.exit calls from unittest.main()\n",
|
||||
" output.write(f\"\\nSystemExit called with code {e.code}\\n\")\n",
|
||||
" except Exception as e:\n",
|
||||
" # Catch other errors\n",
|
||||
" output.write(f\"\\nException: {e}\\n\")\n",
|
||||
" finally:\n",
|
||||
" sys.stdout = sys_stdout\n",
|
||||
" sys.stderr = sys_stderr\n",
|
||||
"\n",
|
||||
" return output.getvalue()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "eca98de3-9e2f-4c23-8bb4-dbb2787a15a4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with gr.Blocks() as ui:\n",
|
||||
" with gr.Row():\n",
|
||||
" python = gr.Textbox(label=\"Python code:\", lines=10, value=example2)\n",
|
||||
" test = gr.Textbox(label=\"Unit tests\", lines=10)\n",
|
||||
" test_run = gr.Textbox(label=\"Test results\", lines=10)\n",
|
||||
" with gr.Row():\n",
|
||||
" model = gr.Dropdown([\"GPT\", \"Claude\",\"Google\"], label=\"Select model\", value=\"GPT\")\n",
|
||||
" generate = gr.Button(\"Generate unit tests\")\n",
|
||||
" run = gr.Button(\"Run unit tests\")\n",
|
||||
"\n",
|
||||
" generate.click(select_model, inputs=[python, model], outputs=[test])\n",
|
||||
" run.click(execute_python, inputs=[test],outputs=[test_run])\n",
|
||||
"\n",
|
||||
"ui.launch()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.11.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user