{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove-cell",
     "remove_cell"
    ]
   },
   "outputs": [],
   "source": [
    "my_notebook_text = \"\"\"\n",
    "# Midterm Exam Exercises\n",
    "\n",
    "\n",
    "## Practical Exercises for midterm exam preparation. \n",
    "\n",
    "\n",
    "1. By using command ```apt```, download package ```libqt5sql5t64``` into the home directory (on the desktop) without installing it.\n",
    "\n",
    "\n",
    "2. Create a directory called <TT>PKG</TT> in your home directory and extract the package files into directory <TT>PKG</TT>.\n",
    "\n",
    "\n",
    "3. By using command find, find the file with name ```libQt5Sql.so.5.15.15``` in directory <TT>PKG</TT>.\n",
    "\n",
    "\n",
    "4. By using command find find directory doc in <TT>PKG</TT>.\n",
    "\n",
    "\n",
    "5. Remove the file and the directory from <TT>PKG</TT>.\n",
    "\n",
    "\n",
    "6. Under <TT>PKG/usr/share</TT>, create directory tree <TT>D1/D2/D3</TT>.\n",
    "\n",
    "\n",
    "7. Copy directory <TT>/etc</TT> on you desktop into the directory tree.\n",
    "\n",
    "\n",
    "8. Archive directory <TT>PKG</TT> into file ```pkgfile-arc.tgz``` by using command ```tar```.\n",
    "\n",
    "\n",
    "9. Deploy a new VM, <TT>exvm1</TT>, by cloning <TT>kvm1</TT>.\n",
    "\n",
    "\n",
    "10. Find the IP address of <TT>exvm1</TT>.\n",
    "\n",
    "\n",
    "11. Create account mike on <TT>exvm1</TT>.\n",
    "\n",
    "\n",
    "12. Upload ```pkgfile-arc.tgz``` into the home directory of user mike on <TT>exvm1</TT>.\n",
    "\n",
    "\n",
    "13. By using command ```tar```, extract the archive on <TT>exvm1</TT>.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove-cell",
     "remove_cell"
    ]
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Midterm Exam Exercises\n",
      "\n",
      "## Practical Exercises for Midterm Exam Preparation\n",
      "\n",
      "1. Using the `apt` command, download the package `libqt5sql5t64` into the home directory (located on the desktop) without installing it.\n",
      "\n",
      "2. Create a directory named `PKG` in your home directory and extract the package files into the `PKG` directory.\n",
      "\n",
      "3. Using the `find` command, locate the file named `libQt5Sql.so.5.15.15` within the `PKG` directory.\n",
      "\n",
      "4. Using the `find` command, locate the `doc` directory within `PKG`.\n",
      "\n",
      "5. Remove the specified file and directory from `PKG`.\n",
      "\n",
      "6. Under the `PKG/usr/share` path, create the nested directory tree `D1/D2/D3`.\n",
      "\n",
      "7. Copy the `/etc` directory from your desktop into the newly created directory tree.\n",
      "\n",
      "8. Archive the `PKG` directory into a file named `pkgfile-arc.tgz` using the `tar` command.\n",
      "\n",
      "9. Deploy a new virtual machine, `exvm1`, by cloning `kvm1`.\n",
      "\n",
      "10. Determine the IP address of `exvm1`.\n",
      "\n",
      "11. Create a user account named `mike` on `exvm1`.\n",
      "\n",
      "12. Upload `pkgfile-arc.tgz` into the home directory of user `mike` on `exvm1`.\n",
      "\n",
      "13. Using the `tar` command, extract the archive on `exvm1`.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "import base64 \n",
    "from google import genai\n",
    "from google.genai.errors import APIError\n",
    "\n",
    "os.environ[\"GEMINI_API_KEY\"] = \"AQ.Ab8RN6K9G5S-h6IWgpUgfmHbIk7b8iZlWTuYptMFqOT4Ph6FnQ\"\n",
    "client = genai.Client()\n",
    "\n",
    "# Use triple quotes \"\" \" so you can paste long paragraphs easily!\n",
    "\n",
    "prompt_instructions_simple = f\"Rewrite this text for a university course. Make it clear and write WCAG 2.1 AA compliant alt text descriptions for any charts mentioned: {my_notebook_text}\"\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "prompt_instructions_detailed5 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate.Make it clear and write WCAG 2.1 AA compliant. Use the following strict sequence \"\n",
    "    \"when dealing with image placements:\\n\\n\"\n",
    "    \n",
    "    \"1. First, insert the NEW Text-Based Structured Alternative ABOVE the image:\\n\"\n",
    "    \"   - For numerical data charts or grid plots: Provide a clean Markdown Data Table.\\n\"\n",
    "    \"   - For hardware architecture, network layouts, or OS Process lifecycles (like fork(), process states, execution threads): \"\n",
    "    \"Provide a high-level text flowchart inside a preformatted ```text code block.\\n\"\n",
    "    \"   - For Terminal Commands or Pipelines (like stdin/stdout piping, shell interactions): \"\n",
    "    \"Provide a vertical box-and-arrow sequence map illustrating data flow from one utility to the next.\\n\"\n",
    "    \"   - Keep diagram characters perfectly aligned and compatible with static HTML page exports.\\n\\n\"\n",
    "    \"   - CRITICAL RULE FOR VIRTUAL DESKTOP LOGIN CONTEXT:\\n\"\n",
    "    \"     If the text or diagram is about logging into the Virtual Desktop or using Ravada, \"\n",
    "    \"the text diagram sequence MUST start exactly with this workflow path:\\n\"\n",
    "    \"     [Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\\n\\n\"\n",
    "    \n",
    "    \"2. Second, place the ORIGINAL untouched image link exactly as it was.\\n\\n\"\n",
    "    \n",
    "    \"3. Third, immediately BELOW the original image link, add the Comprehensive Detailed Description:\\n\"\n",
    "    \"Inside this caption block, you MUST start with the exact text '**Accessible Image Description:**'. Place it always under the image.\"\n",
    "    \"   - Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   - Setup: Explain the primary components, labels, axes, or code pathways without hyper-granular specs.\\n\"\n",
    "    \"   - Key Takeaways: Core conceptual trends or execution results shown.\\n\\n\"\n",
    "\n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "prompt_instructions = prompt_instructions_detailed5\n",
    "\n",
    "# Trying the 2.5-flash model which often has lower server congestion\n",
    "try:\n",
    "    response = client.models.generate_content(\n",
    "        model='models/gemini-3.5-flash-lite',\n",
    "        contents= prompt_instructions,\n",
    "        )\n",
    "\n",
    "    #print(response.text)\n",
    "\n",
    "    output_text = response.text\n",
    "    print(output_text)\n",
    "    \n",
    "    # --- THE MAGIC TRICK ---\n",
    "    # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "    #get_ipython().set_next_input(response.text)\n",
    "    \n",
    "except APIError as e:\n",
    "    if e.code == 503:\n",
    "        print(\"Servers are still busy. Waiting 5 seconds before a quick automatic retry...\")\n",
    "        time.sleep(5)\n",
    "        # Automatic second attempt\n",
    "        response = client.models.generate_content(\n",
    "            model='models/gemini-3.5-flash-lite',\n",
    "            contents= prompt_instructions,\n",
    "            )\n",
    "        print(response.text)\n",
    "        # --- THE MAGIC TRICK ---\n",
    "        # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "        #get_ipython().set_next_input(response.text)\n",
    "    else:\n",
    "        raise e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Midterm Exam Exercises\n",
    "\n",
    "## Practical Exercises for Midterm Exam Preparation\n",
    "\n",
    "1. Using the `apt` command, download the package `libqt5sql5t64` into the home directory (located on the desktop) without installing it.\n",
    "\n",
    "2. Create a directory named `PKG` in your home directory and extract the package files into the `PKG` directory.\n",
    "\n",
    "3. Using the `find` command, locate the file named `libQt5Sql.so.5.15.15` within the `PKG` directory.\n",
    "\n",
    "4. Using the `find` command, locate the `doc` directory within `PKG`.\n",
    "\n",
    "5. Remove the specified file and directory from `PKG`.\n",
    "\n",
    "6. Under the `PKG/usr/share` path, create the nested directory tree `D1/D2/D3`.\n",
    "\n",
    "7. Copy the `/etc` directory from your desktop into the newly created directory tree.\n",
    "\n",
    "8. Archive the `PKG` directory into a file named `pkgfile-arc.tgz` using the `tar` command.\n",
    "\n",
    "9. Deploy a new virtual machine, `exvm1`, by cloning `kvm1`.\n",
    "\n",
    "10. Determine the IP address of `exvm1`.\n",
    "\n",
    "11. Create a user account named `mike` on `exvm1`.\n",
    "\n",
    "12. Upload `pkgfile-arc.tgz` into the home directory of user `mike` on `exvm1`.\n",
    "\n",
    "13. Using the `tar` command, extract the archive on `exvm1`."
   ]
  }
 ],
 "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.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
