File size: 8,767 Bytes
8a615d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
{
"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": [
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\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": [
"<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\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
}
|