{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "**Note:** This notebook is designed for **Google Colab**.\n",
        "\n",
        "If you see the Colab logo <span style='vertical-align:bottom;'><img src='https://colab.research.google.com/img/colab_favicon_256px.png' width='40' alt='Colab logo'></span> in the top-left corner, you are in the correct environment.\n",
        "\n",
        "If you don't see the logo (e.g., you are on GitHub), please click the button below to open it in the correct environment:\n",
        "\n",
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mparrott-at-wiris/aimodelshare/blob/master/notebooks/Etica_en_Joc_Justice_Challenge_nightly.ipynb)"
      ],
      "metadata": {
        "id": "pjnUTfUOGMX3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "# **Section 1: Welcome to Ètica en Joc**"
      ],
      "metadata": {
        "id": "T1yDpMQwYJ2i"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "We live in a world where unseen algorithms are making decisions about our lives. We trust them to be **objective**. We trust them to be **fair**.\n",
        "\n",
        "But what if they're not?\n",
        "\n",
        "Welcome to **Ètica en Joc**. This isn't a traditional lecture. This is a hands-on mission that puts you in control of a powerful AI system. We believe the ideal way to understand the complexities of AI ethics is to experience them directly.\n",
        "\n",
        "We have turned AI ethics into a game: earn badges, make high-impact decisions, and compete on a live leaderboard. Every choice shapes your score—and your sense of justice in the age of AI.\n",
        "\n",
        "---\n",
        "\n",
        "## 🎯 **Your Mission**\n",
        "\n",
        "In this module, **The Justice and Equity Challenge**, you will confront one of the biggest ethical risks in AI today: **bias and fairness**.\n",
        "\n",
        "Your journey will begin by taking on two real-world roles:\n",
        "\n",
        "1. **The Judge:** You’ll use an AI’s recommendations to decide who gets released from prison. But what happens if the algorithm is wrong?\n",
        "2. **The AI Engineer:** You'll compete with others to build AI models that are much better at identifying criminal risk. But better by whose definition?\n",
        "\n",
        "Through these roles, you’ll begin to discover how data, design, and human judgment intertwine to produce real-world consequences.\n",
        "\n",
        "---\n",
        "\n",
        "## 🧭 **The Moral Compass**\n",
        "\n",
        "Your goal will shift: rebuild your AI model, this time guided by the **Moral Compass Score**, which rewards **ethical improvement** over raw performance.\n",
        "\n",
        "Following expert guidance from the **UdG's OEIAC AI Ethics Center**, you will compete to detect bias, measure inequity, and redesign your system toward greater justice.\n",
        "\n",
        "---\n",
        "\n",
        "## 💡 **A Tool for Every Learner**\n",
        "\n",
        "This platform is engineered for a diverse audience:\n",
        "\n",
        "* **\"Low-Tech First\":** **No prior coding or AI knowledge is required.** All core interactions use simple buttons and sliders.\n",
        "* **Dual-Pathway:** Advanced students can use **optional, parallel code notebooks** to build and train the models directly.\n",
        "\n",
        "This program directly aligns with Catalan, Spanish, and EU education goals for fostering socially and ethically aware users of digital technology.\n",
        "\n",
        "---"
      ],
      "metadata": {
        "id": "p1TxuJaNn3rx"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 🚀 **Quick Start Guide**\n",
        "\n",
        "This guide summarizes the simple actions needed to successfully start the challenge.\n",
        "\n",
        "**Read these steps first** and then practice them in **Section 2** below.\n",
        "\n",
        "1. **Run the First Cell** (How?):\n",
        "   * Find the first block with the ▶ **Play Button** next to it.\n",
        "   * **Click the ▶ Play Button** and wait for a moment.\n",
        "\n",
        "2. **Change Your Name** (Practice a simple edit):\n",
        "   * Find the second block with the ▶ **Play Button**.\n",
        "   * **Carefully edit the text** inside the quotes (`\"\"`) to replace the default name with your own.\n",
        "   * **You MUST** click the ▶ **Play Button again** to save and run the cell with your new name.\n",
        "\n",
        "3. **Install the Software**:\n",
        "   * The next step will ask you to install the necessary software.\n",
        "   * **Click the ▶ Play Button** on the installation cell and wait for it to complete.\n",
        "\n",
        "4. **Launch Your First Application**:\n",
        "   * Find the final ▶ **Play Button** in the tutorial section.\n",
        "   * **Click the ▶ Play Button** and wait for the app to load.\n",
        "   * **Use the App:** Interact with the mini-website that appears to officially begin the challenge.\n",
        "\n",
        "---\n",
        "### Key Action to Remember:\n",
        "The single most important action for almost every step is to **click the ▶ Play Button** and then wait for the output to appear."
      ],
      "metadata": {
        "id": "NDsm4UGnXQsq"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "# **Section 2: Quick Start Tutorial**\n",
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "QfeCcEmEc5cX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 🛑 Quick Troubleshooting: When Things Go Wrong\n",
        "\n",
        "Don't panic! Here are the three most common issues and their solutions. Read this first so you know what to do if you get stuck.\n",
        "\n",
        "| Issue | Cause | Solution |\n",
        "| :--- | :--- | :--- |\n",
        "| **\"Cell is spinning indefinitely!\"** | The notebook disconnected from the computer's resources. | Look in the **upper-right corner**. If it says **\"Connect,\"** click that button to reconnect, then re-run the cell. |\n",
        "| **\"I see a red error message!\"** | A cell higher up in the notebook was skipped or not run. | **Scroll up** and check that *every* Code Cell above the error has been run (look for the number in brackets like `[3]`). Then re-run the failing cell. |\n",
        "| **\"I changed the text but nothing happened!\"** | You didn't re-run the code after making an edit. | After editing information in a Code Cell, you **must** click the ▶ Play Button again to apply the change. |\n",
        "\n",
        "-----"
      ],
      "metadata": {
        "id": "m1wbkGNuHEhA"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Now, let's practice each step.\n"
      ],
      "metadata": {
        "id": "kKXb9BBaHVZO"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 1. **Run** your first code cell!\n",
        "\n",
        "Your only job is **to place your cursor above the below code cell**, then to click the ▶ **Play Button** on the left side to run the code in the cell.  \n",
        "\n",
        "**Note:** *A warning message may pop up because the notebook was not authored by Google. Select \"RUN ANYWAY\".*"
      ],
      "metadata": {
        "id": "wcbQjwZ5SWCV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# CODE CELL 1: Click the Play button (triangle) to the left of this cell.\n",
        "print(\"✅ Congratulations! You ran your first cell successfully.\")\n",
        "print(\"The new text below the cell is the 'output' of the code. The play button now shows a number like [1] next to it.\")\n",
        "print(\"You are ready to practice the second quickstart step below!\")"
      ],
      "metadata": {
        "id": "HyNxkyH9SWOl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 2. **Change Your Name** (Practice a simple edit)\n",
        "\n",
        "The only time you will need to type inside a Code Cell is when we ask you to change a specific piece of information.\n",
        "\n",
        "**Your Goal:**\n",
        "\n",
        "1. Find the text that says `\"Your Name\"` in the cell below.\n",
        "2. **Delete** it and type your actual name, keeping the quotes (`\"\"`) around it.\n",
        "3. **Place your cursor above the below code cell**, then click the ▶ **Play Button** for the cell again to apply the change.\n"
      ],
      "metadata": {
        "id": "4PUkJND9Tf1z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# CODE CELL 2: Change the text inside the quotes below and then run the cell.\n",
        "user_name = \"Your Name\"\n",
        "\n",
        "print(\"Hello, \" + user_name + \"! Let's practice launching an application next.\")"
      ],
      "metadata": {
        "id": "yqMzAN5VTncG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 3. **Install the Application Software**\n",
        "\n",
        "Almost ready! The tutorial app is part of a special software package that we need to install first.\n",
        "\n",
        "**Your Task:**\n",
        "* Click the ▶ **Play Button** on the code cell below.\n",
        "* Wait for it to finish. You will see a message \"✅ Installation complete!\" when it's done."
      ],
      "metadata": {
        "id": "lSREqPa1hWYX"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# This cell installs the 'aimodelshare' library\n",
        "print(\"Installing required libraries (Give this process ~30 seconds)...\")\n",
        "!pip install aimodelshare --upgrade -q --no-warn-script-location > /dev/null 2>&1\n",
        "print(\"✅ Installation complete! You can now launch the app in the next step.\")"
      ],
      "metadata": {
        "id": "CXmOjTGphi1z"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 4. **Launch Your First Application**\n",
        "\n",
        "Great! Now that the software is installed, you can launch the tutorial app.\n",
        "\n",
        "**Your Task:**\n",
        "* Click the ▶ **Play Button** on the final code cell below.\n",
        "* A small, interactive website window will appear below the cell.\n",
        "* Complete the tasks inside that app to finish the tutorial."
      ],
      "metadata": {
        "id": "ITj7bTFqVbLn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# This cell imports and runs the tutorial app\n",
        "print(\"Loading tutorial app (in ~15 seconds)...\")\n",
        "from aimodelshare.moral_compass.apps.tutorial import create_tutorial_app\n",
        "tutorial_app = create_tutorial_app()\n",
        "tutorial_app.launch(inline=True, share=False, debug=False, height=750, quiet=True)"
      ],
      "metadata": {
        "id": "ZgyEhB5jqwfP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "# **Below content under active development please ignore!**"
      ],
      "metadata": {
        "id": "MgkNXHOuRhLq_dev"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "---\n",
        "# **Section 3: The Justice and Equity Challenge**"
      ],
      "metadata": {
        "id": "MgkNXHOuRhLq"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now you are ready to begin the challenge! The next few sections will guide you through an interactive experience where you'll make real ethical decisions."
      ],
      "metadata": {
        "id": "HLqleu3fIJoK"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Part 1: You Be the Judge**\n",
        "\n",
        "In this first part of the challenge, you will take on the role of a judge. You'll review defendant profiles and decide whether to release them from prison or keep them incarcerated.\n",
        "\n",
        "An AI system will provide risk predictions to help guide your decisions. But remember: these are just predictions.\n",
        "\n",
        "**Your Task:**\n",
        "* Click the ▶ **Play Button** below to launch the \"You Be the Judge\" app.\n",
        "* Make your decisions for each defendant.\n",
        "* When finished, scroll down to continue to the next section."
      ],
      "metadata": {
        "id": "judge_intro_md"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Launch the You Be the Judge app\n",
        "print(\"Loading the Judge Decision App (in ~15 seconds)...\")\n",
        "from aimodelshare.moral_compass.apps.judge import create_judge_app\n",
        "judge_app = create_judge_app()\n",
        "judge_app.launch(inline=True, share=False, debug=False, height=1200, quiet=True)"
      ],
      "metadata": {
        "id": "judge_app_code"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Part 2: What If the AI Was Wrong?**\n",
        "\n",
        "You just made several important decisions based on AI predictions. But what happens when those predictions are incorrect?\n",
        "\n",
        "In this section, you'll learn about:\n",
        "* **False Positives** – When AI incorrectly predicts high risk.\n",
        "* **False Negatives** – When AI incorrectly predicts low risk.\n",
        "* The real-world consequences of each type of error.\n",
        "\n",
        "**Your Task:**\n",
        "* Click the ▶ **Play Button** below to launch the interactive slideshow.\n",
        "* Read through each slide carefully.\n",
        "* When finished, scroll down to continue."
      ],
      "metadata": {
        "id": "consequences_intro_md"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Launch the AI Consequences app\n",
        "print(\"Loading the AI Consequences slideshow (in ~15 seconds)...\")\n",
        "from aimodelshare.moral_compass.apps.ai_consequences import create_ai_consequences_app\n",
        "consequences_app = create_ai_consequences_app()\n",
        "consequences_app.launch(inline=True, share=False, debug=False, height=1000, quiet=True)"
      ],
      "metadata": {
        "id": "consequences_app_code"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Part 3: So, What Is AI, Really?**\n",
        "\n",
        "Before you can build better AI systems, you need to understand what AI actually is and how it works.\n",
        "\n",
        "In this section, you'll learn:\n",
        "* A simple, non-technical definition of AI\n",
        "* How predictive models work (Input → Model → Output)\n",
        "* How this applies to the criminal justice scenario\n",
        "* Why understanding AI is crucial for building ethical systems\n",
        "\n",
        "**Your Task:**\n",
        "* Click the ▶ **Play Button** below to launch the interactive lesson.\n",
        "* Try out the interactive prediction demo.\n",
        "* When finished, you'll be ready to start building your own AI models!"
      ],
      "metadata": {
        "id": "what_is_ai_intro_md"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Launch the What Is AI app\n",
        "print(\"Loading the What Is AI lesson (in ~15 seconds)...\")\n",
        "from aimodelshare.moral_compass.apps.what_is_ai import create_what_is_ai_app\n",
        "what_is_ai_app = create_what_is_ai_app()\n",
        "what_is_ai_app.launch(inline=True, share=False, debug=False, height=1100, quiet=True)"
      ],
      "metadata": {
        "id": "what_is_ai_app_code"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": { "id": "section4_header" },
      "source": [
        "---\n",
        "# **Section 4: The Technical Challenge – You Be the AI Lead Engineer**\n",
        "\n",
        "You are now leaving the purely *conceptual* part of the experience and stepping into a *technical role*: **AI Lead Engineer**.\n",
        "\n",
        "In real teams, engineers **experiment** to improve model performance. Today, you'll do the same—without needing to write any code. Your mission: **Build and submit several prediction models** to learn how choices affect accuracy.\n",
        "\n",
        "> ⏱ *Estimated Time: 15 minutes*\n",
        "\n",
        "### 🎯 Why This Matters\n",
        "* The better the model predicts, the more people might trust its recommendations.\n",
        "* But today, we focus **only** on raw predictive accuracy—the ethical twist comes *next* in Section 5.\n",
        "\n",
        "---\n",
        "## 🧪 How Real AI Teams Improve Models (Plain Language)\n",
        "Engineering teams rarely build one model and stop. Instead, they:\n",
        "1. **Choose a modeling strategy** (a type of algorithm)\n",
        "2. **Control complexity** (simpler vs. more powerful patterns)\n",
        "3. **Select how much data to use** (small, medium, full)\n",
        "4. **Optionally include or exclude sensitive information** (later you'll see why this matters)\n",
        "5. **Train → Evaluate → Compare → Repeat**\n",
        "\n",
        "Every cycle teaches them something. Today you will run those cycles yourself.\n",
        "\n",
        "---\n",
        "## 🧩 The Parameter Puzzle (Simplified)\n",
        "Think of each setting you adjust like a *knob* on a machine:\n",
        "* **Model Type** – Different strategies for finding patterns\n",
        "* **Complexity Level** – Higher levels try to learn deeper relationships (but can “overlearn” noise)\n",
        "* **Data Size** – More data usually stabilizes learning but can slow training\n",
        "* **Sensitive / Additional Features** – May increase accuracy but can raise fairness concerns (later!)\n",
        "\n",
        "You’ll unlock more knobs (choices) as you submit more models.\n",
        "\n",
        "---\n",
        "## 🏆 The Game Mechanics\n",
        "* You are auto-assigned to a fun **team name** (e.g., *The Justice League*)\n",
        "* Each submission appears on the leaderboard\n",
        "* Your **rank** and available tools expand as you submit models (gamified progression)\n",
        "* Goal for this section: **Submit at least 3 different models** and try to *improve your accuracy*\n",
        "\n",
        "---\n",
        "## ✅ What You Need To Do (Before Launching the App)\n",
        "1. Scroll to the next Code Cell.\n",
        "2. Run the small setup cell (it links your earlier name to the leaderboard system).\n",
        "3. Run the app launch cell.\n",
        "4. Inside the app:\n",
        "   * Start at Step 1 → Read the briefing\n",
        "   * Move to the workspace → Choose parameters → Submit a model\n",
        "   * Watch your rank/accuracy change\n",
        "   * Submit at least 3 times, making *one change at a time* to learn cause and effect\n",
        "5. When finished, click the in-app button to move to the Section Complete screen.\n",
        "\n",
        "> 🔁 **Tip:** After each submission, change only ONE thing (e.g., complexity or data size) to build intuition.\n",
        "\n",
        "---\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": { "id": "section4_setup_title" },
      "source": [
        "### 🔧 Step 1: Link Your Name to Leaderboard\n",
        "Run the following code cell to store your chosen name (from earlier) so the Model Building Game can use it. If you skipped the earlier name change, feel free to edit `user_name` now before running."
      ]
    },
    {
      "cell_type": "code",
      "metadata": { "id": "section4_setup_code" },
      "source": [
        "# CODE CELL (Section 4 Setup)\n",
        "# This assumes you previously set: user_name = \"Your Name\" in Section 2.\n",
        "# If not, you can edit it here before running.\n",
        "try:\n",
        "    user_name  # Check if it exists\n",
        "except NameError:\n",
        "    user_name = \"Your Name\"  # Fallback if not defined earlier\n",
        "\n",
        "import os\n",
        "os.environ['username'] = user_name  # App reads this env var.\n",
        "print(f\"✅ Leaderboard identity set. Submissions will appear under: {os.environ['username']}\")\n",
        "print(\"Proceed to the next cell to launch the Model Building Game app.\")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": { "id": "section4_launch_title" },
      "source": [
        "### 🚀 Step 2: Launch the Model Building Game App\n",
        "**Action:** Run the cell below. It may take ~15–30 seconds the first time as code loads.\n",
        "\n",
        "Inside the app:\n",
        "* Read the Mission Briefing (Step 1)\n",
        "* Enter the Model Building Arena (Step 2)\n",
        "* Submit multiple models (at least 3)\n",
        "* Observe how accuracy changes\n",
        "* Finish & Reflect (Step 3)\n",
        "\n",
        "> If the app window doesn’t appear after a minute, re-run the installation cell from earlier, then retry."
      ]
    },
    {
      "cell_type": "code",
      "metadata": { "id": "section4_launch_code" },
      "source": [
        "# CODE CELL (Launch Model Building Game)\n",
        "print(\"Loading Model Building Game (Give this ~20 seconds if first run)...\")\n",
        "from aimodelshare.moral_compass.apps.model_building_game import create_model_building_game_app\n",
        "model_game_app = create_model_building_game_app()\n",
        "model_game_app.launch(inline=True, share=False, debug=False, height=1200)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": { "id": "section4_optional_beginner" },
      "source": [
        "### 🟢 (Optional) Beginner Mode Launch\n",
        "If you prefer a simpler, more scaffolded experience, you can instead (or additionally) launch **Beginner Mode**.\n",
        "\n",
        "Beginner Mode unlocks tools gradually and uses plainer language.\n",
        "\n",
        "Run the cell below ONLY if you want that version. (Advanced mode above already launched.)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": { "id": "section4_beginner_code" },
      "source": [
        "# OPTIONAL: Beginner Mode (uncomment to use)\n",
        "# print(\"Loading Beginner Mode (Model Building Game)...\")\n",
        "# from aimodelshare.moral_compass.apps.model_building_game_beginner import create_model_building_game_beginner_app\n",
        "# beginner_app = create_model_building_game_beginner_app()\n",
        "# beginner_app.launch(inline=True, share=False, debug=False, height=1100)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": { "id": "section4_reflection" },
      "source": [
        "### 🔍 After You Submit 3+ Models\n",
        "Ask yourself:\n",
        "* Which change produced the biggest accuracy jump?\n",
        "* Did more complexity always help? Why or why not?\n",
        "* Did adding more data help stabilize results?\n",
        "* What might be the *risk* of adding sensitive features just to chase accuracy?\n",
        "\n",
        "You’ve just reenacted a core loop used by real engineering teams.\n",
        "\n",
        "➡️ **Next (Section 5):** We reveal an ethical twist—why better accuracy alone can still lead to harmful outcomes. Scroll down to continue."
      ],
      "metadata": {
        "id": "section4_reflection"
      }
    }
  ]
}
