Spaces:
Runtime error
Runtime error
init!
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Web application (sample codes from student)
|
| 2 |
+
import matplotlib as mpl
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pickle
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
from tensorflow import keras
|
| 8 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 9 |
+
|
| 10 |
+
#Step 1
|
| 11 |
+
fashion_mnist = keras.datasets.fashion_mnist
|
| 12 |
+
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
|
| 13 |
+
|
| 14 |
+
class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
|
| 15 |
+
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
|
| 16 |
+
|
| 17 |
+
%matplotlib inline
|
| 18 |
+
import numpy as np
|
| 19 |
+
import matplotlib as mpl
|
| 20 |
+
import matplotlib.pyplot as plt
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# recommended way to save data into image
|
| 24 |
+
from PIL import Image
|
| 25 |
+
for i in range(6):
|
| 26 |
+
idx = np.random.randint(0,len(X_train_full))
|
| 27 |
+
some_image = X_train_full[idx] # select one image sample
|
| 28 |
+
some_image_label = class_names[y_train_full[idx]] # select one image sample
|
| 29 |
+
some_image = some_image.reshape(28, 28) # reshape from rank-1 tensor (784,) to rank-2 tensor (28,28)
|
| 30 |
+
im = Image.fromarray(some_image)
|
| 31 |
+
im.save('image'+str(i)+'.jpg')
|
| 32 |
+
|
| 33 |
+
input_module1 = gr.inputs.Image(label = "test_image", image_mode='L', shape = (28,28))
|
| 34 |
+
|
| 35 |
+
input_module2 = gr.inputs.Dropdown(choices=["Random Forest", "Decision Tree", "AdaBoost", "Gradient Tree Boosting", "KNN"], label = "Select Algorithm")
|
| 36 |
+
|
| 37 |
+
output_module1 = gr.outputs.Textbox(label = "Predicted Class")
|
| 38 |
+
|
| 39 |
+
output_module2 = gr.outputs.Label(label = "Predict Probability")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def fashion_images(input1, input2):
|
| 43 |
+
image = input1.reshape(1, 28*28)/255.0
|
| 44 |
+
|
| 45 |
+
import pickle
|
| 46 |
+
with open('knn_model_best.pkl', 'rb') as file:
|
| 47 |
+
best_knn_model = pickle.load(file)
|
| 48 |
+
|
| 49 |
+
with open('gradientboost_model_best.pkl', 'rb') as file:
|
| 50 |
+
best_gbt_model = pickle.load(file)
|
| 51 |
+
|
| 52 |
+
with open('adaboost_model_best.pkl', 'rb') as file:
|
| 53 |
+
best_adaboost_model = pickle.load(file)
|
| 54 |
+
|
| 55 |
+
with open('decision_tree_model_best.pkl', 'rb') as file:
|
| 56 |
+
best_tree_model = pickle.load(file)
|
| 57 |
+
|
| 58 |
+
with open('random_forest_model_best.pkl', 'rb') as file:
|
| 59 |
+
best_RF_model = pickle.load(file)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if input2 == 'Random Forest':
|
| 63 |
+
y_test_predicted_proba = best_RF_model.predict_proba(image)
|
| 64 |
+
y_test_predicted_label = best_RF_model.predict(image)
|
| 65 |
+
output = class_names[y_test_predicted_label[0]]
|
| 66 |
+
|
| 67 |
+
elif input2 == 'Gradient Tree Boosting':
|
| 68 |
+
y_test_predicted_proba = best_gbt_model.predict_proba(image)
|
| 69 |
+
y_test_predicted_label = best_gbt_model.predict(image)
|
| 70 |
+
output = class_names[y_test_predicted_label[0]]
|
| 71 |
+
|
| 72 |
+
elif input2 == 'AdaBoost':
|
| 73 |
+
y_test_predicted_proba = best_adaboost_model.predict_proba(image)
|
| 74 |
+
y_test_predicted_label = best_adaboost_model.predict(image)
|
| 75 |
+
output = class_names[y_test_predicted_label[0]]
|
| 76 |
+
|
| 77 |
+
elif input2 == 'Decision tree':
|
| 78 |
+
y_test_predicted_proba = best_tree_model.predict_proba(image)
|
| 79 |
+
y_test_predicted_label = best_tree_model.predict(image)
|
| 80 |
+
output = class_names[y_test_predicted_label[0]]
|
| 81 |
+
|
| 82 |
+
elif input2 == 'Random Forest':
|
| 83 |
+
y_test_predicted_proba = best_RF_model.predict_proba(image)
|
| 84 |
+
y_test_predicted_label = best_RF_model.predict(image)
|
| 85 |
+
output = class_names[y_test_predicted_label[0]]
|
| 86 |
+
|
| 87 |
+
elif input2 == 'KNN':
|
| 88 |
+
y_test_predicted_proba = best_knn_model.predict_proba(image)
|
| 89 |
+
y_test_predicted_label = best_knn_model.predict(image)
|
| 90 |
+
output = class_names[y_test_predicted_label[0]]
|
| 91 |
+
|
| 92 |
+
output_prob = {}
|
| 93 |
+
for name, prob in zip(class_names, y_test_predicted_proba[0]):
|
| 94 |
+
output_prob[name] = prob
|
| 95 |
+
|
| 96 |
+
return output, output_prob
|
| 97 |
+
|
| 98 |
+
# Step 6.4: Put all three component together into the gradio's interface function
|
| 99 |
+
interface = gr.Interface(fn=fashion_images,
|
| 100 |
+
inputs=[input_module1, input_module2],
|
| 101 |
+
outputs=[output_module1,output_module2],
|
| 102 |
+
examples=[["image0.jpg", "Random Forest"],
|
| 103 |
+
["image1.jpg", "Decision tree"],
|
| 104 |
+
["image2.jpg", "KNN"],
|
| 105 |
+
)
|
| 106 |
+
interface.launch()
|