{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "8de6eb89-7d92-4e9d-ab20-8c71ed062072", "metadata": {}, "outputs": [], "source": [ "import keras\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D\n", "from keras.datasets import mnist\n", "from keras.preprocessing.image import ImageDataGenerator\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "id": "1caec746-026a-4649-952d-98ff1ac69e97", "metadata": {}, "outputs": [], "source": [ "# Intentionally including deprecated library\n", "import imp" ] }, { "cell_type": "code", "execution_count": null, "id": "6e7fb59e-6d40-4be1-a51e-0162ddd02c80", "metadata": {}, "outputs": [], "source": [ " # added this vulnerable library (safety might use updated safe library version for this)\n", "import urllib3 \n", "print(urllib3.__version__)" ] }, { "cell_type": "code", "execution_count": null, "id": "cf17cb05-cc6f-4ef0-a27d-fb6a5af33eb9", "metadata": {}, "outputs": [], "source": [ "#using vulnerable library forcefully for safety to detect\n", "!pip install urllib3==1.24.1" ] }, { "cell_type": "code", "execution_count": null, "id": "ca7abfef-f88c-4766-8db4-b1f0909c8e83", "metadata": {}, "outputs": [], "source": [ "!pip install scikit-learn==0.19.0\n", "import sklearn\n", "print(sklearn.__version__)" ] }, { "cell_type": "code", "execution_count": null, "id": "a78b2239-8abd-44fb-b337-9c9f0830ecaf", "metadata": {}, "outputs": [], "source": [ "!pip install numpy==1.16.0\n", "import numpy as np\n", "print(np.__version__)" ] }, { "cell_type": "code", "execution_count": null, "id": "b3fb3af1-200d-4088-b2a8-5fa445e5d0ac", "metadata": {}, "outputs": [], "source": [ "!pip install tensorflow==1.15.5\n", "import tensorflow as tf\n", "print(tf.__version__)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "95b8762f-bd02-4f4d-9fa2-e511a2b4a326", "metadata": {}, "outputs": [], "source": [ "# A real example of a library with a non-permissive license\n", "import gmpy2" ] }, { "cell_type": "code", "execution_count": null, "id": "88872ca5-c939-4b25-b37e-4351fd6ef336", "metadata": {}, "outputs": [], "source": [ "# GNU Octave, an interpreted high-level programming language for numerical computations\n", "# Licensed under GPL\n", "import oct2py" ] }, { "cell_type": "code", "execution_count": null, "id": "5e844eab-9caa-467e-b1c4-2c7aac5a31a9", "metadata": {}, "outputs": [], "source": [ "# Mock secret keys\n", "SECRET_KEY = \"ABCDEFG\"\n", "\n", "aws_secret_key_1 = \"A3TABCDEFGH1234567890\" \n", "\n", "AWS_SECRET_ACCESS_KEY_0 = \"AKIAIOSFODNN7EXAMPLE\"\n", "\n", "AWS_SECRET_ACCESS_KEY = \"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY\"" ] }, { "cell_type": "code", "execution_count": null, "id": "901dbdc1-76b2-47d3-9428-2ddb4c043653", "metadata": {}, "outputs": [], "source": [ "AWS_ACCOUNT_ID = \"1234-5678-9012\"" ] }, { "cell_type": "code", "execution_count": null, "id": "631f40be-470b-4bf1-b645-a0b8429f0dfb", "metadata": {}, "outputs": [], "source": [ "# PII Information (Just for demonstration, do not use real PII)\n", "user_data = {\n", " 'name': 'John Doe',\n", " 'email': 'johndoe@example.com',\n", " 'address': '123 Main St, Anytown, USA'\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "2056314b-486e-4b2f-923a-4194c8a955fe", "metadata": {}, "outputs": [], "source": [ "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "# normalize to range 0-1\n", "x_train = x_train / 255.0\n", "x_test = x_test / 255.0\n", "\n", "# reshape\n", "x_train = x_train.reshape(-1, 28, 28, 1)\n", "x_test = x_test.reshape(-1, 28, 28, 1)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e9766dd8-260d-4184-ac37-9e768f780d8e", "metadata": {}, "outputs": [], "source": [ "## Define the model\n", "\n", "# %%\n", "model = Sequential()\n", "model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Flatten())\n", "model.add(Dense(128, activation='relu'))\n", "model.add(Dense(10, activation='softmax'))" ] }, { "cell_type": "code", "execution_count": null, "id": "e2e6cd12-349b-4088-a189-3037da3191ab", "metadata": {}, "outputs": [], "source": [ "# ## Compile the model\n", "\n", "# %%\n", "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": null, "id": "156520be-06f9-45a6-8c75-5fcfa567d3de", "metadata": {}, "outputs": [], "source": [ "# ## Train the model\n", "\n", "# %%\n", "history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8bd92538-5655-444c-aa79-92c614f890d8", "metadata": {}, "outputs": [], "source": [ "test_loss, test_accuracy = model.evaluate(x_test, y_test)\n", "print(f'Test loss: {test_loss}, Test accuracy: {test_accuracy}')" ] } ], "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }