{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: gradio in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (4.27.0)\n", "Requirement already satisfied: huggingface-hub>=0.19.3 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.22.2)\n", "Requirement already satisfied: pydantic>=2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (2.7.0)\n", "Requirement already satisfied: pillow<11.0,>=8.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (10.1.0)\n", "Requirement already satisfied: aiofiles<24.0,>=22.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (23.2.1)\n", "Requirement already satisfied: httpx>=0.24.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.27.0)\n", "Requirement already satisfied: typing-extensions~=4.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (4.11.0)\n", "Requirement already satisfied: altair<6.0,>=4.2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (5.3.0)\n", "Requirement already satisfied: markupsafe~=2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (2.1.3)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (2.0.3)\n", "Requirement already satisfied: typer<1.0,>=0.12 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.12.3)\n", "Requirement already satisfied: orjson~=3.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (3.10.1)\n", "Requirement already satisfied: jinja2<4.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (3.1.2)\n", "Requirement already satisfied: ruff>=0.2.2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.4.1)\n", "Requirement already satisfied: gradio-client==0.15.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.15.1)\n", "Requirement already satisfied: uvicorn>=0.14.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.29.0)\n", "Requirement already satisfied: python-multipart>=0.0.9 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.0.9)\n", "Requirement already satisfied: numpy~=1.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (1.24.3)\n", "Requirement already satisfied: pydub in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: tomlkit==0.12.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.12.0)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (6.0.1)\n", "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (6.4.0)\n", "Requirement already satisfied: fastapi in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.110.2)\n", "Requirement already satisfied: packaging in /Users/matis/Library/Python/3.10/lib/python/site-packages (from gradio) (23.2)\n", "Requirement already satisfied: ffmpy in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (0.3.2)\n", "Requirement already satisfied: urllib3~=2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (2.0.7)\n", "Requirement already satisfied: matplotlib~=3.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (3.7.2)\n", "Requirement already satisfied: semantic-version~=2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio) (2.10.0)\n", "Requirement already satisfied: websockets<12.0,>=10.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio-client==0.15.1->gradio) (11.0.3)\n", "Requirement already satisfied: fsspec in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from gradio-client==0.15.1->gradio) (2024.3.1)\n", "Requirement already satisfied: jsonschema>=3.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from altair<6.0,>=4.2.0->gradio) (4.19.1)\n", "Requirement already satisfied: toolz in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from altair<6.0,>=4.2.0->gradio) (0.12.1)\n", "Requirement already satisfied: idna in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (3.4)\n", "Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (2023.7.22)\n", "Requirement already satisfied: httpcore==1.* in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (1.0.5)\n", "Requirement already satisfied: sniffio in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (1.3.0)\n", "Requirement already satisfied: anyio in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (4.0.0)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n", "Requirement already satisfied: filelock in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from huggingface-hub>=0.19.3->gradio) (3.13.4)\n", "Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from huggingface-hub>=0.19.3->gradio) (4.66.1)\n", "Requirement already satisfied: contourpy>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib~=3.0->gradio) (1.1.1)\n", "Requirement already satisfied: python-dateutil>=2.7 in /Users/matis/Library/Python/3.10/lib/python/site-packages (from matplotlib~=3.0->gradio) (2.8.2)\n", "Requirement already satisfied: fonttools>=4.22.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib~=3.0->gradio) (4.43.1)\n", "Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib~=3.0->gradio) (3.0.9)\n", "Requirement already satisfied: cycler>=0.10 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib~=3.0->gradio) (0.12.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib~=3.0->gradio) (1.4.5)\n", "Requirement already satisfied: tzdata>=2022.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n", "Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from pydantic>=2.0->gradio) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.18.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from pydantic>=2.0->gradio) (2.18.1)\n", "Requirement already satisfied: rich>=10.11.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n", "Requirement already satisfied: click>=8.0.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n", "Requirement already satisfied: shellingham>=1.3.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n", "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from fastapi->gradio) (0.37.2)\n", "Requirement already satisfied: rpds-py>=0.7.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.10.6)\n", "Requirement already satisfied: attrs>=22.2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.1.0)\n", "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.7.1)\n", "Requirement already satisfied: referencing>=0.28.4 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.30.2)\n", "Requirement already satisfied: six>=1.5 in /Users/matis/Library/Python/3.10/lib/python/site-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/matis/Library/Python/3.10/lib/python/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n", "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/matis/Library/Python/3.10/lib/python/site-packages (from anyio->httpx>=0.24.1->gradio) (1.1.3)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.0)\n", "Requirement already satisfied: mdurl~=0.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n", "--- Logging error ---\n", "Traceback (most recent call last):\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/utils/logging.py\", line 177, in emit\n", " self.console.print(renderable, overflow=\"ignore\", crop=False, style=style)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_vendor/rich/console.py\", line 1673, in print\n", " extend(render(renderable, render_options))\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_vendor/rich/console.py\", line 1305, in render\n", " for render_output in iter_render:\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/utils/logging.py\", line 134, in __rich_console__\n", " for line in lines:\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_vendor/rich/segment.py\", line 249, in split_lines\n", " for segment in segments:\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_vendor/rich/console.py\", line 1283, in render\n", " renderable = rich_cast(renderable)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_vendor/rich/protocol.py\", line 36, in rich_cast\n", " renderable = cast_method()\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/self_outdated_check.py\", line 130, in __rich__\n", " pip_cmd = get_best_invocation_for_this_pip()\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/utils/entrypoints.py\", line 58, in get_best_invocation_for_this_pip\n", " if found_executable and os.path.samefile(\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/genericpath.py\", line 101, in samefile\n", " s2 = os.stat(f2)\n", "FileNotFoundError: [Errno 2] No such file or directory: '/Library/Frameworks/Python.framework/Versions/3.10/bin/pip'\n", "Call stack:\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py\", line 196, in _run_module_as_main\n", " return _run_code(code, main_globals, None,\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py\", line 86, in _run_code\n", " exec(code, run_globals)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/__main__.py\", line 31, in \n", " sys.exit(_main())\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/cli/main.py\", line 70, in main\n", " return command.main(cmd_args)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/cli/base_command.py\", line 101, in main\n", " return self._main(args)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/cli/base_command.py\", line 223, in _main\n", " self.handle_pip_version_check(options)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/cli/req_command.py\", line 190, in handle_pip_version_check\n", " pip_self_version_check(session, options)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/self_outdated_check.py\", line 236, in pip_self_version_check\n", " logger.warning(\"[present-rich] %s\", upgrade_prompt)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/logging/__init__.py\", line 1489, in warning\n", " self._log(WARNING, msg, args, **kwargs)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/logging/__init__.py\", line 1624, in _log\n", " self.handle(record)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/logging/__init__.py\", line 1634, in handle\n", " self.callHandlers(record)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/logging/__init__.py\", line 1696, in callHandlers\n", " hdlr.handle(record)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/logging/__init__.py\", line 968, in handle\n", " self.emit(record)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip/_internal/utils/logging.py\", line 179, in emit\n", " self.handleError(record)\n", "Message: '[present-rich] %s'\n", "Arguments: (UpgradePrompt(old='22.2.2', new='24.0'),)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install gradio\n", "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model_path = \"pokemon-predict-model_transferlearning.keras\"\n", "model = tf.keras.models.load_model(model_path)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Define the core prediction function\n", "def predict_pokemon(image):\n", " # Preprocess image\n", " print(type(image))\n", " image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n", " image = image.resize((150, 150)) #resize the image to 150x150\n", " image = np.array(image)\n", " image = np.expand_dims(image, axis=0) # same as image[None, ...]\n", " \n", " # Predict\n", " prediction = model.predict(image)\n", " \n", " # Apply softmax to get probabilities for each class\n", " prediction = tf.nn.softmax(prediction)\n", " \n", " # Create a dictionary with the probabilities for each Pokemon\n", " evee = np.round(float(prediction[0][0]), 2)\n", " farfetched = np.round(float(prediction[0][1]), 2)\n", " graveler = np.round(float(prediction[0][2]), 2)\n", " venonta = np.round(float(prediction[0][3]), 2)\n", " \n", " return {'Evee': evee, 'Farfetched': farfetched, 'Graveler': graveler, 'Venonta': venonta}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7864\n", "Running on public URL: https://625988e4933385a0d9.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create the Gradio interface\n", "input_image = gr.Image()\n", "iface = gr.Interface(\n", " fn=predict_pokemon,\n", " inputs=input_image, \n", " outputs=gr.Label(),\n", " description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n", "iface.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "venv_new", "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }