Halima's picture
Update app.py
9d36a2a
raw
history blame
1.4 kB
#Part 1
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import gradio as gr
import tensorflow as tf
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
#step 1
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
input_module1 = gr.inputs.Image(label = "test_image", image_mode='L', shape = (28,28))
input_module2 = gr.inputs.Slider(1, 10, step=1, label = "k")
output_module1 = gr.outputs.Textbox(label = "Predicted Digit")
output_module2 = gr.outputs.Label(label = "Predicted Probability per class" num_top_classes=4)
def image_classification(input1, input2):
image = input1.reshape(1, 28 *28)
X_train = x_train.reshape(60000, 28*28)
X_test = x_test.reshape(10000, 28*28)
kNN_classifier = KNeighborsClassifier(n_neighbors=input2)
kNN_classifier.fit(X_train, y_train)
y_test_predicted_label = kNN_classifier.predict(image)
output1 = y_test_predicted_label[0]# text output example
output2 = y_test_predicted_label # image-like array output example
return output1,output2
# Step 6.4: Put all three component together into the gradio's interface function
gr.Interface(fn=image_classification,
inputs=[input_module1, input_module2],
outputs=[output_module1, output_module2]
).launch(debug = True)