From 009a90b7ae7a9a7f7a7eb6824b2b3019f4139e33 Mon Sep 17 00:00:00 2001 From: Emads Date: Sun, 2 Feb 2025 15:18:14 +0200 Subject: [PATCH] Add contributions to week 4 community-contributions --- .../ems_week4_docupy.ipynb | 869 ++++++++++++++++++ .../ems_week4_trading.ipynb | 528 +++++++++++ 2 files changed, 1397 insertions(+) create mode 100644 week4/community-contributions/ems_week4_docupy.ipynb create mode 100644 week4/community-contributions/ems_week4_trading.ipynb diff --git a/week4/community-contributions/ems_week4_docupy.ipynb b/week4/community-contributions/ems_week4_docupy.ipynb new file mode 100644 index 0000000..88ea725 --- /dev/null +++ b/week4/community-contributions/ems_week4_docupy.ipynb @@ -0,0 +1,869 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ykDDGx1cjYlh" + }, + "source": [ + "# **DocuPy** \n", + "### _\"Automate Documentation, Comments, and Unit Tests for Python Code\"_ \n", + "\n", + "## Overview \n", + "DocuPy is a Gradio-powered tool designed to automate essential but time-consuming Python development tasks. It streamlines documentation, unit testing, and Python-to-C++ code conversion with AI-driven assistance. \n", + "\n", + "### Key Features \n", + "✅ **Auto-Generate Docstrings & Comments** – Instantly improve code clarity and maintainability. \n", + "✅ **Unit Test Generation** – Ensure reliability with AI-generated test cases. \n", + "✅ **Python to C++ Conversion** – Seamlessly translate Python code to C++ with execution support. \n", + "\n", + "With an intuitive tab-based UI, DocuPy enhances productivity for developers of all levels. Whether you're documenting functions, validating code with tests, or exploring C++ conversions, this tool lets you focus on coding while it handles the rest. \n", + "\n", + "🔗 **Check out the repo**: [GitHub Repo](https://github.com/emads22/DocuPy) \n", + "\n", + "💡 **Have insights, feedback, or ideas?** Feel free to reach out. \n", + "\n", + "[](https://github.com/emads22)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you're running this notebook on **`Google Colab`**, ensure you install the required libraries by running the following command:\n", + "\n", + "```bash\n", + "!pip install -q openai anthropic python-dotenv gradio huggingface_hub transformers\n", + "```\n", + "Otherwise, make sure to activate the Conda environment `docupy` that already includes these modules:\n", + "\n", + "```bash\n", + "conda activate docupy\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "6wIpBtNPjXc8" + }, + "outputs": [], + "source": [ + "# Uncomment the following command when running on Google Colab\n", + "# !pip install -q openai anthropic python-dotenv gradio huggingface_hub transformers " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T-cTBf9amBxf" + }, + "source": [ + "## Setup and Install Dependencies\n", + "\n", + "- Start by installing all necessary libraries." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "aIHWC7xpk87X" + }, + "outputs": [], + "source": [ + "# imports\n", + "import os\n", + "import io\n", + "import sys\n", + "import subprocess\n", + "import openai\n", + "import anthropic\n", + "import google.generativeai as google_genai\n", + "import gradio as gr\n", + "from openai import OpenAI\n", + "# from google.colab import userdata\n", + "from dotenv import load_dotenv\n", + "from pathlib import Path\n", + "from huggingface_hub import login, InferenceClient\n", + "from transformers import AutoTokenizer" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LZQbXR3dmZy4" + }, + "source": [ + "## Add Secrets to the Colab Notebook\n", + "\n", + "- Add the API keys for OpenAI, Claude, and Gemini to authenticate and access their respective models and services.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "AadABekBm4fV" + }, + "outputs": [], + "source": [ + "# # Log in to Hugging Face using the token and add it to git credentials\n", + "# hf_token = userdata.get('HF_TOKEN')\n", + "# login(token=hf_token, add_to_git_credential=True)\n", + "\n", + "# # Endpoint URL for accessing the Code Qwen model through Hugging Face\n", + "# CODE_QWEN_URL = userdata.get('CODE_QWEN_URL')\n", + "\n", + "# # Initialize inference clients with every model using API keys\n", + "# gpt = openai.OpenAI(api_key=userdata.get('OPENAI_API_KEY'))\n", + "# claude = anthropic.Anthropic(api_key=userdata.get('ANTHROPIC_API_KEY'))\n", + "# google_genai.configure(api_key=userdata.get('GOOGLE_API_KEY'))\n", + "# code_qwen = InferenceClient(CODE_QWEN_URL, token=hf_token)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ej3JNfh_wc0m" + }, + "source": [ + "## Alternatively, if not running on Google Colab, Load Environment Variables for API Keys\n", + "\n", + "- Use the `load_dotenv()` function to securely load API keys from a `.env` file.\n", + "- Ensure that the `.env` file is located in the same directory as your script or Jupyter Notebook.\n", + "- The `.env` file should include the required API keys for OpenAI, Claude, and Gemini." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "av9X9XpQw0Vd" + }, + "outputs": [], + "source": [ + "load_dotenv()\n", + "\n", + "# Log in to Hugging Face using the token and add it to git credentials\n", + "hf_token = os.getenv('HF_TOKEN')\n", + "login(token=hf_token, add_to_git_credential=True)\n", + "\n", + "# Endpoint URL for accessing the Code Qwen model through Hugging Face\n", + "CODE_QWEN_URL = os.getenv('CODE_QWEN_URL')\n", + "\n", + "# Initialize inference clients with every model using API keys\n", + "gpt = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))\n", + "claude = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))\n", + "google_genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))\n", + "code_qwen = InferenceClient(CODE_QWEN_URL, token=hf_token)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lvEhCuQjrTYu" + }, + "source": [ + "## Define Required Constants\n", + "\n", + "- Initialize the essential constants required for the application's functionality.\n", + "- Configure the system and user prompts specific to each task or feature.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "AKEBKKmAowt2" + }, + "outputs": [], + "source": [ + "# Models\n", + "OPENAI_MODEL = \"gpt-4o\"\n", + "CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n", + "GEMINI_MODEL = \"gemini-1.5-pro\"\n", + "CODE_QWEN_MODEL = \"Qwen/CodeQwen1.5-7B-Chat\"\n", + "\n", + "MODELS_IN_USE = [\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"]\n", + "\n", + "MAX_TOKENS = 2000\n", + "\n", + "ACTION_A = \"commenting\"\n", + "ACTION_B = \"testing\"\n", + "ACTION_C = \"converting\"\n", + "\n", + "# Define and create the path for the \"temp_files\" directory within the current script's directory\n", + "TEMP_DIR = Path.cwd() / \"temp_files\"\n", + "TEMP_DIR.mkdir(parents=True, exist_ok=True)\n", + "\n", + "PYTHON_SCRIPT_EASY = \"\"\"\n", + "import time\n", + "\n", + "def reverse_string(s):\n", + " return s[::-1]\n", + "\n", + "if __name__ == \"__main__\":\n", + " start_time = time.time()\n", + " text = \"Hello, World!\"\n", + " print(f\"- Original string: {text}\")\n", + " print(\"- Reversed string:\", reverse_string(text))\n", + " execution_time = time.time() - start_time \n", + " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n", + "\"\"\"\n", + "\n", + "PYTHON_SCRIPT_INTERMEDIATE = \"\"\"\n", + "import time\n", + "\n", + "def is_palindrome(s):\n", + " s = s.lower().replace(\" \", \"\") \n", + " return s == s[::-1]\n", + "\n", + "if __name__ == \"__main__\":\n", + " start_time = time.time()\n", + " text = \"Racecar\"\n", + " if is_palindrome(text):\n", + " print(f\"- '{text}' is a palindrome!\")\n", + " else:\n", + " print(f\"- '{text}' is Not a palindrome.\")\n", + " execution_time = time.time() - start_time \n", + " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n", + "\"\"\"\n", + "\n", + "PYTHON_SCRIPT_HARD = \"\"\"\n", + "import time\n", + "\n", + "def generate_primes(limit):\n", + " primes = []\n", + " for num in range(2, limit + 1):\n", + " if all(num % p != 0 for p in primes):\n", + " primes.append(num)\n", + " return primes\n", + "\n", + "if __name__ == \"__main__\":\n", + " start_time = time.time()\n", + " n = 20\n", + " print(f\"- Generating primes up to: {n}\")\n", + " print(\"- Prime numbers:\", generate_primes(n))\n", + " execution_time = time.time() - start_time \n", + " print(f\"\\\\n=> Execution Time: {execution_time:.6f} seconds\")\n", + "\"\"\"\n", + "\n", + "PYTHON_SCRIPTS = {\n", + " \"reverse_string\" : PYTHON_SCRIPT_EASY,\n", + " \"is_palindrome\" : PYTHON_SCRIPT_INTERMEDIATE,\n", + " \"generate_primes\" : PYTHON_SCRIPT_HARD,\n", + " \"custom\" : \"\"\"\n", + "# Write your custom Python script here\n", + "if __name__ == \"__main__\":\n", + " print(\"Hello, World!\")\n", + "\"\"\"\n", + "}\n", + "\n", + "# Relative system prompts\n", + "SYSTEM_PROMPT_COMMENTS = \"\"\"\n", + "You are an AI model specializing in enhancing Python code documentation.\n", + "Generate detailed and precise docstrings and inline comments for the provided Python code.\n", + "Ensure the docstrings clearly describe the purpose, parameters, and return values of each function.\n", + "Inline comments should explain complex or non-obvious code segments.\n", + "Do not include any introductions, explanations, conclusions, or additional context.\n", + "Return only the updated Python code enclosed within ```python ... ``` for proper formatting and syntax highlighting.\n", + "\"\"\"\n", + "\n", + "SYSTEM_PROMPT_TESTS = \"\"\"\n", + "You are an AI model specializing in generating comprehensive unit tests for Python code.\n", + "Create Python unit tests that thoroughly validate the functionality of the given code.\n", + "Use the `unittest` framework and ensure edge cases and error conditions are tested.\n", + "Do not include any comments, introductions, explanations, conclusions, or additional context.\n", + "Return only the unit test code enclosed within ```python ... ``` for proper formatting and syntax highlighting.\n", + "\"\"\"\n", + "\n", + "SYSTEM_PROMPT_CONVERT = \"\"\"\n", + "You are an AI model specializing in high-performance code translation.\n", + "Translate the given Python code into equivalent, optimized C++ code.\n", + "Focus on:\n", + "- Using efficient data structures and algorithms.\n", + "- Avoiding unnecessary memory allocations and computational overhead.\n", + "- Ensuring minimal risk of integer overflow by using appropriate data types.\n", + "- Leveraging the C++ Standard Library (e.g., ``, ``) for performance and readability.\n", + "Produce concise and efficient C++ code that matches the functionality of the original Python code.\n", + "Do not include any comments, introductions, explanations, conclusions, or additional context..\n", + "Return only the C++ code enclosed within ```cpp ... ``` for proper formatting and syntax highlighting.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "JJ1zttf7ANqD" + }, + "outputs": [], + "source": [ + "# Relative user prompts\n", + "def user_prompt_comments(python_code):\n", + " user_prompt = f\"\"\"\n", + "Add detailed docstrings and inline comments to the following Python code:\n", + "\n", + "```python\n", + "{python_code}\n", + "```\n", + "\"\"\"\n", + " return user_prompt\n", + "\n", + "def user_prompt_tests(python_code):\n", + " user_prompt = f\"\"\"\n", + "Generate unit tests for the following Python code using the `unittest` framework:\n", + "\n", + "```python\n", + "{python_code}\n", + "```\n", + "\"\"\"\n", + " return user_prompt\n", + "\n", + "def user_prompt_convert(python_code):\n", + " user_prompt = f\"\"\"\n", + "Convert the following Python code into C++:\n", + "\n", + "```python\n", + "{python_code}\n", + "``` \n", + "\"\"\"\n", + " return user_prompt" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tqrOO_qsCRkd" + }, + "source": [ + "### Define the Tab Functions\n", + "\n", + "- Develop dedicated functions for each service: documenting Python code, generating unit tests, and converting Python to C++.\n", + "- Structure each function to handle user input, process it using the selected AI model, and display the generated output seamlessly.\n", + "- Ensure the functionality of each tab aligns with its specific purpose, providing an intuitive and efficient user experience.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "HBsBrq3G94ul" + }, + "outputs": [], + "source": [ + "def stream_gpt(system_prompt, user_prompt):\n", + " stream = gpt.chat.completions.create(\n", + " model=OPENAI_MODEL,\n", + " messages=[\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + " ],\n", + " stream=True)\n", + " reply = \"\"\n", + " for chunk in stream:\n", + " reply += chunk.choices[0].delta.content or \"\"\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n", + "\n", + "def stream_claude(system_prompt, user_prompt):\n", + " response = claude.messages.stream(\n", + " model=CLAUDE_MODEL,\n", + " max_tokens=MAX_TOKENS,\n", + " system=system_prompt,\n", + " messages=[{\"role\": \"user\", \"content\": user_prompt}],\n", + " )\n", + " reply = \"\"\n", + " with response as stream:\n", + " for text in stream.text_stream:\n", + " reply += text\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n", + "\n", + "def stream_gemini(system_prompt, user_prompt):\n", + " gemini = google_genai.GenerativeModel(\n", + " model_name=GEMINI_MODEL,\n", + " system_instruction=system_prompt\n", + " )\n", + " stream = gemini.generate_content(\n", + " contents=user_prompt,\n", + " stream=True\n", + " )\n", + " reply = \"\"\n", + " for chunk in stream:\n", + " reply += chunk.text or \"\"\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n", + "\n", + "def stream_code_qwen(system_prompt, user_prompt):\n", + " tokenizer = AutoTokenizer.from_pretrained(CODE_QWEN_MODEL)\n", + " model_input = tokenizer.apply_chat_template(\n", + " conversation=[\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + " ],\n", + " tokenize=False,\n", + " add_generation_prompt=True\n", + " )\n", + " stream = code_qwen.text_generation(\n", + " prompt=model_input,\n", + " stream=True,\n", + " details=True,\n", + " max_new_tokens=MAX_TOKENS\n", + " )\n", + " reply = \"\"\n", + " for chunk in stream:\n", + " reply += chunk.token.text or \"\"\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```cpp\\n\", \"\").replace(\"```\", \"\")\n", + "\n", + "def set_prompts(user_input, action):\n", + " action = action.lower()\n", + "\n", + " if action == ACTION_A.lower():\n", + " system_prompt = SYSTEM_PROMPT_COMMENTS\n", + " user_prompt = user_prompt_comments(user_input)\n", + " elif action == ACTION_B.lower():\n", + " system_prompt = SYSTEM_PROMPT_TESTS\n", + " user_prompt = user_prompt_tests(user_input)\n", + " elif action == ACTION_C.lower():\n", + " system_prompt = SYSTEM_PROMPT_CONVERT\n", + " user_prompt = user_prompt_convert(user_input)\n", + " else:\n", + " return None, None\n", + " \n", + " return system_prompt, user_prompt\n", + "\n", + "def stream_response(user_input, model, action):\n", + " system_prompt, user_prompt = set_prompts(user_input, action)\n", + " if not all((system_prompt, user_prompt)):\n", + " raise ValueError(\"Unknown Action\")\n", + "\n", + " match model:\n", + " case \"GPT\":\n", + " yield from stream_gpt(system_prompt, user_prompt)\n", + "\n", + " case \"Claude\":\n", + " yield from stream_claude(system_prompt, user_prompt)\n", + "\n", + " case \"Gemini\":\n", + " yield from stream_gemini(system_prompt, user_prompt)\n", + "\n", + " case \"CodeQwen\":\n", + " yield from stream_code_qwen(system_prompt, user_prompt)\n", + " \n", + "def generate_comments(python_code, selected_model):\n", + " for model in MODELS_IN_USE:\n", + " if model == selected_model:\n", + " yield from stream_response(python_code, model, action=ACTION_A)\n", + " return # Exit the function immediately after exhausting the generator\n", + " raise ValueError(\"Unknown Model\")\n", + "\n", + "def generate_tests(python_code, selected_model):\n", + " for model in MODELS_IN_USE:\n", + " if model == selected_model:\n", + " yield from stream_response(python_code, model, action=ACTION_B)\n", + " return # Exit the function immediately after exhausting the generator\n", + " raise ValueError(\"Unknown Model\")\n", + "\n", + "def convert_code(python_code, selected_model):\n", + " for model in MODELS_IN_USE:\n", + " if model == selected_model:\n", + " yield from stream_response(python_code, model, action=ACTION_C)\n", + " return # Exit the function immediately after exhausting the generator\n", + " raise ValueError(\"Unknown Model\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Running Code Functions\n", + "\n", + "- Functions that dynamically execute Python or C++ code provided as a string and captures its output.\n", + "- This is useful for evaluating Python or C++ code snippets and returning their results programmatically.\n", + "\n", + "### IMPORTANT WARNING:\n", + "The functions that dynamically execute Python or C++ code provided as input.\n", + "While powerful, this is extremely dangerous if the input code is not trusted.\n", + "Any malicious code can be executed, including:\n", + " - Deleting files or directories\n", + " - Stealing sensitive data (e.g., accessing environment variables or credentials)\n", + " - Running arbitrary commands that compromise the system\n", + "\n", + "Sharing this notebook with this code snippet can allow attackers to exploit this functionality \n", + "by passing harmful code as input. \n", + "\n", + "If you share this notebook or use this function:\n", + " 1. Only accept input from trusted sources.\n", + " 2. Consider running the code in a sandboxed environment (e.g., virtual machine or container).\n", + " 3. Avoid using this function in publicly accessible applications or notebooks without strict validation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def run_python_exec(code):\n", + " try:\n", + " # Capture stdout using StringIO\n", + " output = io.StringIO()\n", + "\n", + " # Redirect stdout to StringIO\n", + " sys.stdout = output\n", + "\n", + " # Execute the provided Python code\n", + " exec(code)\n", + " finally:\n", + " # Restore original stdout\n", + " sys.stdout = sys.__stdout__\n", + "\n", + " # Return the captured output\n", + " return output.getvalue()\n", + "\n", + "# Improved running python function\n", + "def run_python(code):\n", + " # Save the Python code to a file\n", + " with open(TEMP_DIR / \"python_code.py\", \"w\") as python_file:\n", + " python_file.write(code)\n", + "\n", + " try:\n", + " # Execute the Python code\n", + " result = subprocess.run(\n", + " [\"python\", str(TEMP_DIR / \"python_code.py\")],\n", + " check=True, text=True, capture_output=True\n", + " )\n", + "\n", + " # Return the program's output\n", + " return result.stdout\n", + "\n", + " except subprocess.CalledProcessError as e:\n", + " # Handle compilation or execution errors\n", + " return f\"An error occurred during execution:\\n{e.stderr}\"\n", + "\n", + " finally:\n", + " # Clean up: Delete the Python code file and executable\n", + " file_path = TEMP_DIR / \"python_code.py\"\n", + " if file_path.exists():\n", + " file_path.unlink()\n", + "\n", + "def run_cpp(code):\n", + " # Save the C++ code to a file\n", + " with open(TEMP_DIR / \"cpp_code.cpp\", \"w\") as cpp_file:\n", + " cpp_file.write(code)\n", + "\n", + " try:\n", + " # Compile the C++ code\n", + " subprocess.run(\n", + " [\"g++\", \"-o\", str(TEMP_DIR / \"cpp_code\"), str(TEMP_DIR / \"cpp_code.cpp\")],\n", + " check=True, text=True, capture_output=True\n", + " )\n", + "\n", + " # Execute the compiled program\n", + " result = subprocess.run(\n", + " [str(TEMP_DIR / \"cpp_code\")],\n", + " check=True, text=True, capture_output=True\n", + " )\n", + "\n", + " # Return the program's output\n", + " return result.stdout\n", + "\n", + " except subprocess.CalledProcessError as e:\n", + " # Handle compilation or execution errors\n", + " error_context = \"during compilation\" if \"cpp_code.cpp\" in e.stderr else \"during execution\"\n", + " return f\"An error occurred {error_context}:\\n{e.stderr}\"\n", + "\n", + " finally:\n", + " # Clean up: Delete the C++ source file and executable\n", + " for filename in [\"cpp_code.cpp\", \"cpp_code\", \"cpp_code.exe\"]:\n", + " file_path = TEMP_DIR / filename\n", + " if file_path.exists():\n", + " file_path.unlink()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vude1jzPrgT2" + }, + "source": [ + "## Develop a User-Friendly Interface with Gradio\n", + "\n", + "- Design a clean, intuitive, and user-centric interface using Gradio.\n", + "- Ensure responsiveness and accessibility to provide a seamless and efficient user experience.\n", + "- Focus on simplicity while maintaining functionality to cater to diverse user needs.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Eh-sWFZVBb_y" + }, + "outputs": [], + "source": [ + "# CSS styles for customizing the appearance of the Gradio UI elements.\n", + "css = \"\"\"\n", + ".python { \n", + " background-color: #377ef0; \n", + " color: #ffffff; \n", + " padding: 0.5em; \n", + " border-radius: 5px; /* Slightly rounded corners */\n", + "}\n", + ".cpp { \n", + " background-color: #00549e; \n", + " color: #ffffff; \n", + " padding: 0.5em; \n", + " border-radius: 5px; \n", + "}\n", + ".model { \n", + " background-color: #17a2b8; /* Vibrant cyan color */\n", + " color: white; \n", + " font-size: 1.2em; \n", + " padding: 0.5em; \n", + " border: none; \n", + " border-radius: 5px; \n", + " cursor: pointer; \n", + "}\n", + ".button { \n", + " height: 4em; \n", + " font-size: 1.5em; \n", + " padding: 0.5em 1em; \n", + " background-color: #e67e22; /* Vibrant orange */\n", + " color: white; \n", + " border: none; \n", + " border-radius: 5px; \n", + " cursor: pointer; \n", + "}\n", + ".run-button { \n", + " height: 3em; \n", + " font-size: 1.5em; \n", + " padding: 0.5em 1em; \n", + " background-color: #16a085; /* Rich teal color */\n", + " color: white; \n", + " border: none; \n", + " border-radius: 5px; \n", + " cursor: pointer; \n", + "}\n", + ".button:hover, .run-button:hover {\n", + " background-color: #2c3e50; /* Dark navy for hover effect */\n", + " color: #fff; \n", + "}\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "M_v-j-B_sQHe" + }, + "outputs": [], + "source": [ + "# Tab to Document Code with Docstrings and Comments\n", + "def docs_comments_ui():\n", + " with gr.Tab(\"Docstrings & Comments\"):\n", + " gr.Markdown(\"\"\"\n", + " ## Document Code with Docstrings and Comments\n", + " This tab allows you to automatically generate docstrings and inline comments for your Python code.\n", + " - Paste your Python code into the **`Python Code`** textbox.\n", + " - Select your preferred model (GPT, Claude, Gemini, or CodeQwen) to process the code.\n", + " - Click the **`Add Docstrings & Comments`** button to generate well-documented Python code.\n", + " The generated code will appear in the **`Python Code with Docstrings and Comments`** textarea.\n", + " \"\"\")\n", + " with gr.Row():\n", + " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n", + " python_with_comments = gr.TextArea(label=\"Python Code with Docstrings and Comments:\", interactive=True, lines=20, elem_classes=[\"python\"])\n", + " with gr.Row():\n", + " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n", + " comments_btn = gr.Button(\"Add Docstrings & Comments\", elem_classes=[\"button\"])\n", + " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n", + " \n", + " python_script.change(\n", + " fn=lambda script: PYTHON_SCRIPTS[script],\n", + " inputs=[python_script],\n", + " outputs=[python]\n", + " )\n", + " \n", + " comments_btn.click(\n", + " fn=lambda: \"\",\n", + " inputs=None,\n", + " outputs=[python_with_comments]\n", + " ).then(\n", + " fn=generate_comments,\n", + " inputs=[python, model],\n", + " outputs=[python_with_comments]\n", + " )\n", + "\n", + " return python_with_comments" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "WDjJp1eXtQzY" + }, + "outputs": [], + "source": [ + "# Tab to Generate Comprehensive Unit Tests\n", + "def unit_tests_ui():\n", + " with gr.Tab(\"Unit Tests\"):\n", + " gr.Markdown(\"\"\"\n", + " ## Generate Comprehensive Unit Tests\n", + " This tab helps you create unit tests for your Python code automatically.\n", + " - Paste your Python code into the **`Python Code`** textbox.\n", + " - Choose a model (GPT, Claude, Gemini, or CodeQwen) to generate the unit tests.\n", + " - Click the **`Generate Unit Tests`** button, and the generated unit tests will appear in the **`Python Code with Unit Tests`** textarea.\n", + " Use these unit tests to ensure your code behaves as expected.\n", + " \"\"\")\n", + " with gr.Row():\n", + " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n", + " python_unit_tests = gr.TextArea(label=\"Python Code with Unit Tests:\", interactive=True, lines=20, elem_classes=[\"python\"])\n", + " with gr.Row():\n", + " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n", + " unit_tests_btn = gr.Button(\"Generate Unit Tests\", elem_classes=[\"button\"])\n", + " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n", + " \n", + " python_script.change(\n", + " fn=lambda script: PYTHON_SCRIPTS[script],\n", + " inputs=[python_script],\n", + " outputs=[python]\n", + " )\n", + " \n", + " unit_tests_btn.click(\n", + " fn=lambda: \"\",\n", + " inputs=None,\n", + " outputs=[python_unit_tests]\n", + " ).then(\n", + " fn=generate_tests,\n", + " inputs=[python, model],\n", + " outputs=[python_unit_tests]\n", + " )\n", + "\n", + " return python_unit_tests" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "x57SZeLi9NyV" + }, + "outputs": [], + "source": [ + "# Tab to Convert Python Code to C++\n", + "def python_to_cpp_ui():\n", + " with gr.Tab(\"Python to C++\"):\n", + " gr.Markdown(\"\"\"\n", + " ## Convert Python Code to C++\n", + " This tab facilitates the conversion of Python code into C++.\n", + " - Paste your Python code into the **`Python Code`** textbox.\n", + " - Select your preferred model (GPT, Claude, Gemini, or CodeQwen) to perform the conversion.\n", + " - Click **`Convert to C++`** to see the equivalent C++ code in the **`C++ Code`** textbox.\n", + " Additional Features:\n", + " - You can execute the Python or C++ code directly using the respective **`Run Python`** or **`Run C++`** buttons.\n", + " - The output will appear in the respective result text areas below.\n", + " \"\"\")\n", + " with gr.Row():\n", + " python = gr.Textbox(label=\"Python Code:\", lines=20, value=PYTHON_SCRIPTS[\"custom\"], elem_classes=[\"python\"])\n", + " cpp = gr.Textbox(label=\"C++ Code:\", interactive=True, lines=20, elem_classes=[\"cpp\"])\n", + " with gr.Row():\n", + " python_script = gr.Dropdown(choices=list(PYTHON_SCRIPTS.keys()), label=\"Select a Python script\", value=\"custom\", elem_classes=[\"model\"])\n", + " convert_btn = gr.Button(\"Convert to C++\", elem_classes=[\"button\"])\n", + " model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select Model\", value=\"GPT\", elem_classes=[\"model\"])\n", + " with gr.Row():\n", + " run_python_btn = gr.Button(\"Run Python\", elem_classes=[\"run-button\"])\n", + " run_cpp_btn = gr.Button(\"Run C++\", elem_classes=[\"run-button\"])\n", + " with gr.Row():\n", + " python_out = gr.TextArea(label=\"Python Result:\", lines=10, elem_classes=[\"python\"])\n", + " cpp_out = gr.TextArea(label=\"C++ Result:\", lines=10, elem_classes=[\"cpp\"])\n", + "\n", + " python_script.change(\n", + " fn=lambda script: PYTHON_SCRIPTS[script],\n", + " inputs=[python_script],\n", + " outputs=[python]\n", + " )\n", + " \n", + " convert_btn.click(\n", + " fn=lambda: \"\",\n", + " inputs=None,\n", + " outputs=[cpp]\n", + " ).then(\n", + " fn=convert_code,\n", + " inputs=[python, model],\n", + " outputs=[cpp]\n", + " )\n", + " run_python_btn.click(run_python, inputs=[python], outputs=[python_out])\n", + " run_cpp_btn.click(run_cpp, inputs=[cpp], outputs=[cpp_out])\n", + "\n", + " return cpp, python_out, cpp_out" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 645 + }, + "id": "n8ZdDrOrrbl-", + "outputId": "08350d69-569e-4947-8da1-d755e9a2678f" + }, + "outputs": [], + "source": [ + "# Combine the tabs into the main UI and handle tab switching\n", + "with gr.Blocks(css=css) as main_ui:\n", + " with gr.Tabs() as tabs:\n", + " comments_output = docs_comments_ui()\n", + " tests_output = unit_tests_ui()\n", + " cpp_output, python_out, cpp_out = python_to_cpp_ui()\n", + "\n", + " # Reset outputs on tab switch\n", + " tabs.select(\n", + " fn=lambda: [\"\", \"\", \"\", \"\", \"\"],\n", + " inputs=None,\n", + " outputs=[comments_output, \n", + " tests_output, \n", + " cpp_output, python_out, cpp_out]\n", + " )\n", + "\n", + "# Launch the app\n", + "main_ui.launch(inbrowser=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/week4/community-contributions/ems_week4_trading.ipynb b/week4/community-contributions/ems_week4_trading.ipynb new file mode 100644 index 0000000..a2460d3 --- /dev/null +++ b/week4/community-contributions/ems_week4_trading.ipynb @@ -0,0 +1,528 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9", + "metadata": {}, + "source": [ + "# Trading Decision Simulator\n", + "\n", + "## Description\n", + "This document provides Python functions to simulate trading decisions using a predefined API. The API includes stock tickers, historical prices, and a `Trade` class to represent buy or sell actions. Each function demonstrates a unique trading strategy, such as momentum-based trading, mean reversion, portfolio diversification, and more. These examples can serve as a foundation for developing or testing algorithmic trading systems.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "from huggingface_hub import login, InferenceClient\n", + "from transformers import AutoTokenizer\n", + "import google.generativeai as google_genai\n", + "import anthropic\n", + "import gradio as gr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f672e1c-87e9-4865-b760-370fa605e614", + "metadata": {}, + "outputs": [], + "source": [ + "# Setting up environment\n", + "load_dotenv()\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')\n", + "os.environ['CODE_QWEN_URL'] = os.getenv('CODE_QWEN_URL', 'your-url-if-not-using-env')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize\n", + "openai = OpenAI()\n", + "claude = anthropic.Anthropic()\n", + "google_genai.configure()\n", + "code_qwen = InferenceClient(CODE_QWEN_URL)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cbb4319c-870f-4c04-99e2-6f54c650537a", + "metadata": {}, + "outputs": [], + "source": [ + "# Constants \n", + "MODELS = {\n", + " \"GPT\": \"gpt-4o\", \n", + " \"Claude\": \"claude-3-5-sonnet-20240620\", \n", + " \"Gemini\": \"gemini-1.5-pro\", \n", + " \"CodeQwen\": \"Qwen/CodeQwen1.5-7B-Chat\"\n", + "}\n", + "\n", + "MAX_TOKENS = 2000\n", + "\n", + "SYSTEM_PROMPT = \"\"\"\n", + "You are an advanced code generation assistant capable of creating high-quality Python code for financial trading systems. \n", + "Your task is to generate Python functions that simulate trading decisions based on the following API:\n", + "\n", + "API DETAILS:\n", + "1. tickers: A list of stock tickers (strings) representing available stocks.\n", + "2. prices: A dictionary where the key is a stock ticker (string) and the value is a list of historical prices (floats). The list is ordered with the most recent price first.\n", + "3. Trade: A class used to represent trading actions.\n", + " - `Trade(ticker, quantity)` creates a trade object:\n", + " - Positive `quantity` (e.g., `100`) represents buying shares.\n", + " - Negative `quantity` (e.g., `-50`) represents selling/shorting shares.\n", + "\n", + "INSTRUCTIONS:\n", + "- You will be provided with an example Python function to demonstrate the API.\n", + "- Your job is to generate 5 additional Python functions, each implementing a unique trading strategy.\n", + "- Ensure the functions are named sequentially (e.g., `trade2()`, `trade3()`, etc.).\n", + "- Include clear comments explaining the logic behind each function.\n", + "- Return a list of `Trade` objects from each function.\n", + "- The output should only include the Python code. Do not include any introductions, conclusions, summaries, or additional context.\n", + "\n", + "CONSIDERATIONS FOR TRADING STRATEGIES:\n", + "- Momentum-based strategies (e.g., trading based on price trends).\n", + "- Mean reversion strategies (e.g., identifying overbought or oversold stocks).\n", + "- Randomized strategies (e.g., simulating stochastic decision-making).\n", + "- Portfolio diversification (e.g., distributing trades across multiple tickers).\n", + "- Risk management strategies (e.g., limiting losses or locking in profits).\n", + "\n", + "EXAMPLE FUNCTION:\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb", + "metadata": {}, + "outputs": [], + "source": [ + "def user_prompt(example_function):\n", + " \"\"\"\n", + " Returns a user prompt for the model by appending the provided example function.\n", + " \"\"\"\n", + " return f\"\"\"\n", + "{example_function}\n", + "\n", + "TASK:\n", + "Based on the provided example function and API, please write 5 additional trading functions named `trade2()`, `trade3()`, and so on. Each function should implement a unique trading strategy as outlined in the system prompt. Make sure each function has clear comments explaining the logic and returns a list of `Trade` objects.\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "id": "455728fd-be9b-4d7a-88f2-3afcf026303e", + "metadata": {}, + "source": [ + "# Trade Function Example: `trade1`\n", + "\n", + "This Python script demonstrates a simple trading strategy implemented using a provided API. The `trade1` function identifies the top-performing stock over the last 5 days based on its average price and creates a trade object to buy 100 shares of the selected stock. The function leverages the following components:\n", + "\n", + "- **`tickers`**: A list of available stock tickers.\n", + "- **`prices`**: A dictionary containing historical prices for each stock.\n", + "- **`Trade`**: A class used to represent trading actions (buy or sell).\n", + "- **`numpy`**: Used to calculate average prices efficiently.\n", + "\n", + "The example highlights a momentum-based strategy where the stock with the best recent performance is selected for trading.\n", + "\n", + "example:\n", + "```python\n", + "# Importing the required modules and classes for the trading simulation\n", + "\n", + "# `tickers` is a list of stock tickers (strings), representing available stocks to trade.\n", + "import tickers\n", + "\n", + "# `prices` is a dictionary where:\n", + "# - The key is a stock ticker (string).\n", + "# - The value is a list of historical prices (floats), ordered with the most recent price first.\n", + "import prices\n", + "\n", + "# `Trade` is a class that represents a trading decision. It takes two arguments:\n", + "# - `ticker`: A string representing the stock ticker.\n", + "# - `quantity`: An integer representing the number of shares to buy (positive) or sell/short (negative).\n", + "# Example usage:\n", + "# Trade(\"IBM\", 100) -> Buys 100 shares of IBM stock.\n", + "# Trade(\"IBM\", -50) -> Sells or shorts 50 shares of IBM stock.\n", + "import Trade\n", + "\n", + "# Additional modules for random number generation and numerical operations\n", + "import random\n", + "import numpy as np\n", + "\n", + "def trade1():\n", + " \"\"\"\n", + " Buys the top-performing stock based on its average price over the last 5 days.\n", + "\n", + " Strategy:\n", + " - Calculate the average price of the last 5 days for each stock in `tickers`.\n", + " - Identify the stock with the highest average price.\n", + " - Create a trade object to buy 100 shares of the identified stock.\n", + " \n", + " Returns:\n", + " list[Trade]: A list containing a single trade object for the chosen stock.\n", + " \"\"\"\n", + " # Calculate the 5-day average price for each stock\n", + " avg_prices = {ticker: np.mean(prices[ticker][:5]) for ticker in tickers}\n", + "\n", + " # Find the stock ticker with the highest 5-day average price\n", + " best_ticker = max(avg_prices, key=avg_prices.get)\n", + "\n", + " # Create a trade object to buy 100 shares of the top-performing stock\n", + " trade = Trade(best_ticker, 100)\n", + "\n", + " # Return the trade as a list\n", + " return [trade]\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce0fa282-4e07-4bf8-8b49-7cf7ef4a7572", + "metadata": {}, + "outputs": [], + "source": [ + "# A trading function example\n", + "TRADING_FUNCTION_EXAMPLE = \"\"\"\n", + "# tickers is a list of stock tickers (strings)\n", + "import tickers\n", + "\n", + "# prices is a dict; the key is a ticker and the value is a list of historic prices, today first\n", + "import prices\n", + "\n", + "# Trade represents a decision to buy or sell a quantity of a ticker\n", + "# Trade(\"IBM\", 100) for a trade object representing purchasing 100 shares of IBM stock\n", + "# Trade(\"IBM\", -50) for a trade object representing selling or shorting 50 shares of IBM stock\n", + "import Trade\n", + "\n", + "import random\n", + "import numpy as np\n", + "\n", + "def trade1():\n", + " # Buy top performing stock in the last 5 days\n", + " avg_prices = {ticker: np.mean(prices[ticker][:5]) for ticker in tickers}\n", + " best_ticker = max(avg_prices, key=avg_prices.get)\n", + " trade = Trade(best_ticker, 100)\n", + " return [trade]\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0", + "metadata": {}, + "outputs": [], + "source": [ + "# UI function to trade using GPT\n", + "def trade_gpt(function_example): \n", + " stream = openai.chat.completions.create(\n", + " model=MODELS[\"GPT\"], \n", + " messages=[\n", + " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n", + " {\"role\": \"user\", \"content\": user_prompt(function_example)}\n", + " ], \n", + " stream=True\n", + " )\n", + " reply = \"\"\n", + " for chunk in stream:\n", + " reply += chunk.choices[0].delta.content or \"\"\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8669f56b-8314-4582-a167-78842caea131", + "metadata": {}, + "outputs": [], + "source": [ + "# UI function to trade using Claude\n", + "def trade_claude(function_example):\n", + " result = claude.messages.stream(\n", + " model=MODELS[\"Claude\"],\n", + " max_tokens=MAX_TOKENS,\n", + " system=SYSTEM_PROMPT,\n", + " messages=[{\"role\": \"user\", \"content\": user_prompt(function_example)}],\n", + " )\n", + " reply = \"\"\n", + " with result as stream:\n", + " for text in stream.text_stream:\n", + " reply += text\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d27456d0-5cd3-4c2c-a12a-176d53142752", + "metadata": {}, + "outputs": [], + "source": [ + "# UI function to trade using Gemini\n", + "def trade_gemini(function_example):\n", + " gemini = google_genai.GenerativeModel(\n", + " model_name=MODELS[\"Gemini\"],\n", + " system_instruction=SYSTEM_PROMPT\n", + " )\n", + " stream = gemini.generate_content(\n", + " contents=user_prompt(function_example),\n", + " stream=True\n", + " )\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": "0a9fb676-83c3-452e-abeb-8712ebdee1d1", + "metadata": {}, + "outputs": [], + "source": [ + "# UI function to trade using CodeQwen\n", + "def trade_code_qwen(function_example):\n", + " tokenizer = AutoTokenizer.from_pretrained(MODELS[\"CodeQwen\"])\n", + " model_input = tokenizer.apply_chat_template(\n", + " conversation=[\n", + " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n", + " {\"role\": \"user\", \"content\": user_prompt(function_example)}\n", + " ],\n", + " tokenize=False,\n", + " add_generation_prompt=True\n", + " )\n", + " stream = code_qwen.text_generation(\n", + " prompt=model_input,\n", + " stream=True,\n", + " details=True,\n", + " max_new_tokens=MAX_TOKENS\n", + " )\n", + " reply = \"\"\n", + " for chunk in stream:\n", + " reply += chunk.token.text or \"\"\n", + " yield reply.replace(\"```python\\n\", \"\").replace(\"```\", \"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d", + "metadata": {}, + "outputs": [], + "source": [ + "# UI function to select model from dropdown \n", + "def trade(trading_function, model):\n", + " if model==\"GPT\":\n", + " yield from trade_gpt(trading_function)\n", + " elif model==\"Claude\":\n", + " yield from trade_claude(trading_function)\n", + " elif model==\"Gemini\":\n", + " yield from trade_gemini(trading_function)\n", + " elif model==\"CodeQwen\":\n", + " yield from trade_code_qwen(trading_function)\n", + " else:\n", + " raise ValueError(\"Unknown Model\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e6af1cd-f3d9-43f0-91d9-9800d9681a77", + "metadata": {}, + "outputs": [], + "source": [ + "# CSS styling for the UI\n", + "UI_CSS = \"\"\"\n", + "#title {\n", + " text-align: center;\n", + " font-size: 2.5em;\n", + " font-weight: bold;\n", + " margin-bottom: 10px;\n", + "}\n", + "\n", + "#description {\n", + " text-align: left;\n", + " font-size: 1.2em;\n", + " font-weight: bold;\n", + " margin-bottom: 20px;\n", + " color: #BBB;\n", + "}\n", + "\n", + "#simulate-btn {\n", + " height: 3em;\n", + " font-size: 2em !important;\n", + " padding: 12px 25px !important;\n", + " border-radius: 10px !important;\n", + " border: none !important;\n", + " cursor: pointer;\n", + " transition: background-color 0.3s, transform 0.2s; /* Smooth effects */\n", + "}\n", + "\n", + "#simulate-btn:hover {\n", + " background-color: #FFC107 !important; /* Bright golden-yellow on hover */\n", + " transform: scale(1.05); /* Slight zoom effect */\n", + " box-shadow: 0 6px 8px rgba(0, 0, 0, 0.25); /* Enhance shadow on hover */\n", + "}\n", + "\n", + "#simulate-btn:active {\n", + " background-color: #B8860B !important; /* Darker goldenrod on click */\n", + " transform: scale(0.95); /* Slight press effect */\n", + " box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); /* Reduce shadow on click */\n", + "}\n", + "\n", + "#simulate-btn,\n", + "#trading-decisions {\n", + " background-color: #DAA520 !important; /* Goldenrod color same as #simulate-btn */\n", + " color: #FFFFFF !important; /* White text for contrast */\n", + " box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3); /* Subtle shadow for depth */\n", + "}\n", + "\n", + "#trading-decisions {\n", + " border: 2px solid #B8860B; /* Darker goldenrod border */\n", + "}\n", + "\n", + "#trading-decisions:focus {\n", + " outline: none;\n", + " box-shadow: 0 0 8px #FFC107; /* Bright golden-yellow glow on focus */\n", + "}\n", + "\n", + "#example-function, \n", + "#model-dropdown {\n", + " background-color: #007965 !important; /* Darker and sharper teal for better contrast */\n", + " color: #FFFFFF !important; /* Pure white for strong visibility */\n", + " cursor: pointer;\n", + " border: 2px solid #00594D; /* Deep teal border for emphasis */\n", + " box-shadow: 0 4px 8px rgba(0, 0, 0, 0.9); /* Strong shadow for depth */\n", + "}\n", + "\n", + "#example-function:focus,\n", + "#model-dropdown:focus {\n", + " outline: none;\n", + " box-shadow: 0 0 10px #00A389; /* Vibrant teal glow on focus */\n", + "}\n", + "\n", + "#model-dropdown:hover {\n", + " background-color: #005F4A !important; /* Darker teal for hover effect */\n", + " box-shadow: 0 6px 10px rgba(0, 0, 0, 0.4); /* Enhanced shadow on hover */\n", + " border-color: #00A389; /* Change border color for hover */\n", + "}\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f733330f-6945-4be4-a2ab-9e68c94f70f0", + "metadata": {}, + "outputs": [], + "source": [ + "# Gradio UI\n", + "with gr.Blocks(css=UI_CSS) as ui:\n", + " # Title for the application\n", + " gr.Markdown(\"
🛠️ Trading Strategy Simulator

\")\n", + " \n", + " # Input and output section\n", + " with gr.Row():\n", + " trading_f = gr.Textbox(\n", + " label=\"📄 Trading Function Input\",\n", + " placeholder=\"Paste your trading function here...\",\n", + " lines=15,\n", + " value=TRADING_FUNCTION_EXAMPLE,\n", + " elem_id=\"example-function\"\n", + " )\n", + " decisions = gr.Textbox(\n", + " label=\"📊 Generated Trading Decisions\",\n", + " placeholder=\"Trading decisions will appear here...\",\n", + " lines=20,\n", + " interactive=False,\n", + " elem_id=\"trading-decisions\"\n", + " )\n", + " \n", + " with gr.Row():\n", + " # Dropdown scaled to take 1 part of the row\n", + " model = gr.Dropdown(\n", + " choices=MODELS,\n", + " label=\"🤖 Select AI Model\",\n", + " value=\"GPT\",\n", + " scale=1,\n", + " elem_id=\"model-dropdown\"\n", + " )\n", + " # Markdown for the description scaled to 2 parts of the row\n", + " with gr.Column(scale=2):\n", + " gr.Markdown(\n", + " \"\"\"\n", + "
\n", + " This interface allows you to test and simulate trading strategies using a predefined example function.\n", + " Simply input a trading function, select your preferred AI model, and see the generated trading decisions in action.
\n", + " Experiment with different strategies to refine your approach and analyze outcomes effectively.\n", + "
\n", + " \"\"\"\n", + " )\n", + " # Button scaled to take 1 part of the row\n", + " trade_btn = gr.Button(\n", + " \"💼 Simulate Trading\",\n", + " elem_id=\"simulate-btn\",\n", + " scale=1\n", + " )\n", + "\n", + " # Action button behavior\n", + " trade_btn.click(\n", + " fn=trade, \n", + " inputs=[trading_f, model], \n", + " outputs=[decisions]\n", + " )\n", + "\n", + "# Launch the UI in a browser\n", + "ui.launch(inbrowser=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06", + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}