{
 "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're all set! Please **proceed to Section 1**.\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.ipynb)"
   ],
   "metadata": {
    "id": "pjnUTfUOGMX3"
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "---\n",
    "# **Section 1: Welcome to Ethics at Play**"
   ],
   "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 **Ethics at Play**. 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 complex, real-world trade-offs in technology is to experience them for yourself.\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\u2014and your sense of justice in the age of AI.\n",
    "\n",
    "---\n",
    "\n",
    "## \ud83c\udfaf **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\u2019ll use an AI\u2019s 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\u2019ll begin to discover how data, design, and human judgment intertwine to produce real-world consequences.\n",
    "\n",
    "---\n",
    "\n",
    "## \ud83e\udded **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",
    "## \ud83d\udca1 **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": [
    "## \ud83d\ude80 **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 \u25b6 **Play Button** next to it.\n",
    "    * **Click the \u25b6 Play Button** and wait for a moment.\n",
    "\n",
    "2.  **Install the Software**:\n",
    "    * The next step will ask you to install the necessary software.\n",
    "    * **Click the \u25b6 Play Button** on the installation cell and wait for it to complete.\n",
    "\n",
    "3.  **Launch Your First Activity**:\n",
    "    * Find the final \u25b6  **Play Button** in the tutorial section.\n",
    "    * **Click the \u25b6 Play Button** and wait for the learning 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 \u25b6 Play Button** and then wait for the learning activity 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": [
    "## \ud83d\uded1 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. |\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 numbered bracket next to each play button, like `[1]`). |\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  \u25b6 **Play Button** again to use the new information. |\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 cell!\n",
    "\n",
    "Your only job is **to place your cursor above the below code cell**, then to click the \u25b6 **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 (circular arrow) to the left of this cell.\n",
    "print(\"\u2705 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 to practice the second quickstart step below!\")"
   ],
   "metadata": {
    "id": "HyNxkyH9SWOl"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2. **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 \u25b6 **Play Button** on the code cell below.\n",
    "* Wait for it to finish. You will see a message \"\u2705 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 or so)...\")\n",
    "!pip install aimodelshare --upgrade -q --no-warn-script-location > /dev/null 2>&1\n",
    "print(\"\u2705 Installation complete! You can now launch the app in the next step.\")"
   ],
   "metadata": {
    "id": "CXmOjTGphi1z"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3. **Launch Your First Activity - The Tutorial App**\n",
    "\n",
    "Great! Now that the software is installed, you can launch the tutorial app.\n",
    "\n",
    "**Your Task:**\n",
    "* Click the \u25b6 **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 30 seconds or so...)\")\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",
    "# **Section 3: The Justice and Equity Challenge**"
   ],
   "metadata": {
    "id": "ln0kAp0V-6uZ"
   }
  },
  {
   "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": [
    "## **Activity 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 \u25b6 **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 30 seconds or so...)\")\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": [
    "## **Activity 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 \u25b6 **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 30 seconds or so...)\")\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": [
    "## **Activity 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 \u2192 Model \u2192 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 \u25b6 **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 30 seconds or so...)\")\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",
   "source": [
    "## **Activity 4: The Technical Challenge \u2014 Step Into the AI Lead Engineer Role**\n",
    "\n",
    "Get ready\u2014next, **you\u2019ll build your own AI models!**\n",
    "  \n",
    "You\u2019re about to step into a *new builder role*: **AI Lead Engineer**\u2014the person who gets to design, tweak, and improve an AI system.\n",
    "\n",
    "Your mission: **build a smarter AI risk-prediction model** that can help a judge make more accurate decisions.\n",
    "\n",
    "Just like real AI teams, you\u2019ll **experiment**, **test ideas**, and **compare results**.\n",
    "\n",
    "There\u2019s also a **leaderboard** to help you track how your models perform alongside your peers in a model improvement game.\n",
    "\n",
    "Ready to try to build something better? Let\u2019s go.\n",
    "\n",
    "### **What You\u2019ll Learn in This Section**\n",
    "- How different model choices affect prediction accuracy\n",
    "- How experimentation leads to better AI performance  \n",
    "- How to think like an engineer when improving a prediction system  \n",
    "- How leaderboard feedback helps you iterate strategically  \n",
    "\n",
    "### **Your Task**\n",
    "- Click the \u25b6 **Play Button** below to launch the interactive lesson  \n",
    "- Try out the interactive model building game\n",
    "- Explore how different design choices affect predictive performance.  \n",
    "- Get ready\u2014next, **you\u2019ll build your own AI models!**"
   ],
   "metadata": {
    "id": "jc5a6l-PCTFn"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Launch the the Model Building Game app\n",
    "print(\"Loading the Model Building Game lesson (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.model_building_game import create_model_building_game_app\n",
    "model_building_game_app = create_model_building_game_app()\n",
    "model_building_game_app.launch(inline=True, share=False, debug=False, height=1100, quiet=True)"
   ],
   "metadata": {
    "id": "ZunO1ff5EtK-"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Activity 5: The Ethical Revelation \u2014 Real-World Impact**\n",
    "\n",
    "Congratulations on building high-performing AI models! You've competed, climbed the leaderboard, and mastered the technical process.\n",
    "\n",
    "But before we celebrate, there's something crucial you need to know. A model similar to yours was deployed in the real world\u2014and it had serious, unintended consequences.\n",
    "\n",
    "In this activity, you'll discover:\n",
    "- The ProPublica \"Machine Bias\" investigation findings\n",
    "- How high accuracy doesn't guarantee fair outcomes\n",
    "- The real-world impact of biased AI systems\n",
    "\n",
    "**Your Task:** Click the \u25b6 **Play Button** below to launch the Ethical Revelation app."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Launch the Ethical Revelation app\n",
    "print(\"Loading the Ethical Revelation app (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.ethical_revelation import create_ethical_revelation_app\n",
    "ethical_revelation_app = create_ethical_revelation_app()\n",
    "ethical_revelation_app.launch(inline=True, share=False, debug=False, height=1000, quiet=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Activity 6: The Moral Compass Challenge \u2014 A New Way to Win**\n",
    "\n",
    "You've learned that high accuracy alone isn't enough\u2014we need a higher standard that prioritizes both performance and ethics.\n",
    "\n",
    "Welcome to the **Moral Compass Challenge**, where you'll shift from purely technical optimization to ethical AI development.\n",
    "\n",
    "In this activity, you'll experience:\n",
    "- A dramatic paradigm shift (watch your score reset!)\n",
    "- Introduction to the **Moral Compass Score**\n",
    "- The path forward to building fair and ethical AI\n",
    "\n",
    "**Your Task:** Click the \u25b6 **Play Button** below to launch the Moral Compass Challenge app."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Launch the Moral Compass Challenge app\n",
    "print(\"Loading the Moral Compass Challenge app (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.moral_compass_challenge import create_moral_compass_challenge_app\n",
    "moral_compass_challenge_app = create_moral_compass_challenge_app()\n",
    "moral_compass_challenge_app.launch(inline=True, share=False, debug=False, height=1000, quiet=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **Activity 7: Bias Detective - (!!Activities from here forward in development.  Please ignore for now.!!)**\n",
    "\n",
    "**Objective:** Diagnose where and how bias appears in the AI model using expert fairness principles.\n",
    "\n",
    "**Your Role:** You've joined the **AI Ethics Task Force** as a **Bias Detective**.\n",
    "\n",
    "In this activity, you will:\n",
    "1. Learn the **OEIAC framework** for evaluating AI ethics\n",
    "2. Scan datasets for **demographic variables** that can encode bias\n",
    "3. Analyze **group-level bias** using fairness metrics\n",
    "4. Generate a comprehensive **Bias Diagnosis Report**\n",
    "\n",
    "**Estimated Time:** 8\u201312 minutes\n",
    "\n",
    "---"
   ],
   "metadata": {
    "id": "bias_detective_intro"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Launch the Bias Detective app\n",
    "print(\"Loading the Bias Detective app (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.bias_detective import create_bias_detective_app\n",
    "bias_detective_app = create_bias_detective_app()\n",
    "bias_detective_app.launch(share=True, debug=False)"
   ],
   "metadata": {
    "id": "launch_bias_detective"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **Activity 8: Fairness Fixer**\n",
    "\n",
    "**Objective:** Apply hands-on fairness fixes: remove biased features, eliminate proxy variables, and develop a representative and continuously improving data strategy.\n",
    "\n",
    "**Your Role:** You're now a **Fairness Engineer**.\n",
    "\n",
    "In this activity, you will:\n",
    "1. **Remove direct demographics** (race, sex, age) from the model\n",
    "2. **Identify and eliminate proxy variables** that replicate bias\n",
    "3. Develop **representative data guidelines**\n",
    "4. Build a **continuous improvement plan** for responsible AI\n",
    "\n",
    "**Estimated Time:** 12\u201315 minutes\n",
    "\n",
    "---"
   ],
   "metadata": {
    "id": "fairness_fixer_intro"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Launch the Fairness Fixer app\n",
    "print(\"Loading the Fairness Fixer app (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.fairness_fixer import create_fairness_fixer_app\n",
    "fairness_fixer_app = create_fairness_fixer_app()\n",
    "fairness_fixer_app.launch(share=True, debug=False)"
   ],
   "metadata": {
    "id": "launch_fairness_fixer"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **Activity 9: Justice & Equity Upgrade**\n",
    "\n",
    "**Objective:** Elevate fairness improvements by addressing inclusion, accessibility, stakeholder engagement, and structural justice.\n",
    "\n",
    "**Your Role:** You're now a **Justice Architect**.\n",
    "\n",
    "In this activity, you will:\n",
    "1. Apply **accessibility features** (multi-language, plain text, screen reader support)\n",
    "2. Introduce **diversity and inclusion** improvements\n",
    "3. Map and prioritize **stakeholders** (defendants, families, community advocates)\n",
    "4. Reveal your **final Moral Compass Score** and earn your **Justice & Equity Champion** badge\n",
    "5. Unlock your **completion certificate**\n",
    "\n",
    "**Estimated Time:** 8\u201310 minutes\n",
    "\n",
    "---"
   ],
   "metadata": {
    "id": "justice_equity_upgrade_intro"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Launch the Justice & Equity Upgrade app\n",
    "print(\"Loading the Justice & Equity Upgrade app (in 30 seconds or so...)\")\n",
    "from aimodelshare.moral_compass.apps.justice_equity_upgrade import create_justice_equity_upgrade_app\n",
    "justice_equity_app = create_justice_equity_upgrade_app()\n",
    "justice_equity_app.launch(share=True, debug=False)"
   ],
   "metadata": {
    "id": "launch_justice_equity_upgrade"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "---\n",
    "\n",
    "# **Section 10: Continue Your Journey**\n",
    "\n",
    "\ud83c\udf89 **Congratulations!** You've completed Activities 7, 8, and 9 of the Justice & Equity Challenge.\n",
    "\n",
    "You've learned:\n",
    "- How to **diagnose bias** using expert frameworks\n",
    "- How to apply **technical fairness interventions**\n",
    "- How to build **representative data strategies**\n",
    "- How to elevate systems through **accessibility and inclusion**\n",
    "- How to engage **stakeholders** in responsible AI development\n",
    "\n",
    "**Next steps:**\n",
    "- Share your certificate on social media\n",
    "- Explore additional modules in the Ethics at Play series\n",
    "- Apply these principles to your own AI projects\n",
    "\n",
    "Thank you for being part of this important work in AI ethics! \ud83c\udf1f\n"
   ],
   "metadata": {
    "id": "section_10"
   }
  }
 ]
}
