{ "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": [ "