{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Define the core prediction function\n", "def predict_cat_dog(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 28x28 and converts it to gray scale\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", " # Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability\n", " # we could also change the output layer to dense(1, activation='sigmoid')\n", " prediction = np.round(float(tf.sigmoid(prediction)), 2)\n", " p_jolteon = prediction\n", " p_eevee = (1 - prediction)\n", " p_dratini = (2 - prediction)\n", " return {'jolteon': p_jolteon, 'eevee': p_eevee, 'dratini': p_dratini}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7862\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\queueing.py\", line 527, in process_events\n", " response = await route_utils.call_process_api(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\route_utils.py\", line 270, in call_process_api\n", " output = await app.get_blocks().process_api(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\blocks.py\", line 1847, in process_api\n", " result = await self.call_function(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\blocks.py\", line 1433, in call_function\n", " prediction = await anyio.to_thread.run_sync(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\to_thread.py\", line 56, in run_sync\n", " return await get_async_backend().run_sync_in_worker_thread(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2134, in run_sync_in_worker_thread\n", " return await future\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 851, in run\n", " result = context.run(func, *args)\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\utils.py\", line 805, in wrapper\n", " response = f(*args, **kwargs)\n", " File \"C:\\Users\\dom-k\\AppData\\Local\\Temp\\ipykernel_33600\\3230146116.py\", line 11, in predict_cat_dog\n", " prediction = model.predict(image)\n", "NameError: name 'model' is not defined\n" ] } ], "source": [ "# Create the Gradio interface\n", "input_image = gr.Image()\n", "iface = gr.Interface(\n", " fn=predict_cat_dog,\n", " inputs=input_image, \n", " outputs=gr.Label(),\n", " examples=[\"images_pokemon/Dratini1.png\", \"images_pokemon/Dratini2.png\", \"images_pokemon/Dratini3.png\", \"images_pokemon/Dratini4.png\", \"images_pokemon/Dratini5.png\",\n", " \"images_pokemon/Eevee1.png\", \"images_pokemon/Eevee2.png\", \"images_pokemon/Eevee3.png\", \"images_pokemon/Eevee4.png\", \"images_pokemon/Eevee5.png\",\n", " \"images_pokemon/Jolteon1.png\", \"images_pokemon/Jolteon2.png\", \"images_pokemon/Jolteon3.png\", \"images_pokemon/Jolteon4.png\", \"images_pokemon/Jolteon5.png\"], \n", " description=\"A simple mlp classification model for image classification using the mnist dataset.\")\n", "iface.launch()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7863\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Traceback (most recent call last):\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\queueing.py\", line 527, in process_events\n", " response = await route_utils.call_process_api(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\route_utils.py\", line 270, in call_process_api\n", " output = await app.get_blocks().process_api(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\blocks.py\", line 1847, in process_api\n", " result = await self.call_function(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\blocks.py\", line 1433, in call_function\n", " prediction = await anyio.to_thread.run_sync(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\to_thread.py\", line 56, in run_sync\n", " return await get_async_backend().run_sync_in_worker_thread(\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2134, in run_sync_in_worker_thread\n", " return await future\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 851, in run\n", " result = context.run(func, *args)\n", " File \"c:\\Users\\dom-k\\anaconda3\\envs\\KIA\\lib\\site-packages\\gradio\\utils.py\", line 805, in wrapper\n", " response = f(*args, **kwargs)\n", " File \"C:\\Users\\dom-k\\AppData\\Local\\Temp\\ipykernel_33600\\3230146116.py\", line 11, in predict_cat_dog\n", " prediction = model.predict(image)\n", "NameError: name 'model' is not defined\n" ] } ], "source": [ "import gradio as gr\n", "\n", "def greet(name, intensity):\n", " return \"Hello, \" + name + \"!\" * int(intensity)\n", "\n", "demo = gr.Interface(\n", " fn=greet,\n", " inputs=[\"text\", \"slider\"],\n", " outputs=[\"text\"],\n", ")\n", "\n", "demo.launch()\n" ] } ], "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.14" } }, "nbformat": 4, "nbformat_minor": 2 }