# -*- coding: utf-8 -*- """gradio_deploy.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/13X2E9v7GxryXyT39R5CzxrNwxfA6KMFJ """ !pip install gradio import gradio as gr from PIL import Image from timeit import default_timer as timer from tensorflow import keras import numpy as np MODEL = keras.models.load_model( "convnet_from_scratch_with_augmentation.keras") def predict(img): # Start the timer start_time = timer() # Reading the image and size transformation features = Image.open(img) features = features.resize((180, 180)) features = np.array(features).reshape(1, 180,180,3) # Create a prediction label and prediction probability dictionary for each prediction class # This is the required format for Gradio's output parameter pred_labels_and_probs = {'dog' if MODEL.predict(features)> 0.5 else 'cat':float(MODEL.predict(features))} # Calculate the prediction time pred_time = round(timer() - start_time, 5) # Return the prediction dictionary and prediction time return pred_labels_and_probs, pred_time predict('/content/cat.1505.jpg') # Create title, description and article strings title = "Classification Demo" description = "Cat/Dog classification Tensorflow model with Augmentted small dataset" # Create the Gradio demo demo = gr.Interface(fn=predict, # mapping function from input to output inputs=gr.Image(type='filepath'), # what are the inputs? outputs=[gr.Label(label="Predictions"), # what are the outputs? gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs title=title, description=description,) # Launch the demo! demo.launch(debug=False, # print errors locally? share=True) # generate a publically shareable URL? pip install tensorflow import PIL import tensorflow as tf import timeit print(gr.__version__) print(np.__version__) print(tf.__version__) print(PIL.__version__)