mdreyer5 commited on
Commit
360c3af
1 Parent(s): 751203b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 6.1: Define different input components
2
+ import gradio as gr
3
+
4
+ # g. define Dropdown data type
5
+ input_module1 = gr.inputs.Dropdown(choices=["KNN", "Softmax", "Deep Neural", "SVM", "Decision Tree","Random Forest","CNN"], label = "Model Selection")
6
+ # b. define image data type
7
+ input_module2 = gr.inputs.Dropdown(choices=["image1", "image2", "image3", "image4","image5"], label = "Sample Image")
8
+
9
+ # b. define image data type
10
+ output_module1 = gr.outputs.Textbox(label = "Predicted Class")
11
+
12
+ # b. define image data type
13
+ output_module2 = gr.outputs.Image(label = "Image")
14
+
15
+ # Step 6.3: Define a new function that accommodates the input modules.
16
+ def multi_inputs(input1, input2):
17
+ import numpy as np
18
+ import pickle
19
+ import matplotlib as mpl
20
+ import matplotlib.pyplot as plt
21
+ import tensorflow as tf
22
+ import scipy.ndimage.interpolation
23
+ (X_train_full, y_train_full), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
24
+
25
+ X_valid, X_train = X_train_full[:5000], X_train_full[5000:]
26
+ y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
27
+
28
+ if input2 == "image1":
29
+ img = X_test[1]
30
+ if input2 == "image2":
31
+ img = X_test[2]
32
+ if input2 == "image3":
33
+ img = X_test[3]
34
+ if input2 == "image4":
35
+ img = X_test[4]
36
+ if input2 == "image5":
37
+ img = X_test[5]
38
+
39
+ import matplotlib.pyplot as plt
40
+ import matplotlib.image as mpimg
41
+ #img = mpimg.imread(img)
42
+ #imag = scipy.ndimage.rotate(input2, 0, reshape=False, cval=0)
43
+
44
+ #class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
45
+ #"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
46
+ test_shape = img.shape
47
+ test_width = test_shape[0]
48
+ test_height = test_shape[1]
49
+
50
+ image =img.reshape(test_width*test_height)
51
+
52
+ if input1 == "KNN":
53
+ loaded_model = pickle.load(open('KNN_Model.sav', 'rb'))
54
+ image = img.reshape((1, -1))
55
+ y_test_pred = loaded_model.predict(image)
56
+
57
+
58
+ if input1 == "Softmax":
59
+ loaded_model = pickle.load(open('Softmax_Model.sav', 'rb'))
60
+ image = img.reshape((1, -1))
61
+ y_test_pred = loaded_model.predict(image)
62
+
63
+
64
+ if input1 == "SVM":
65
+ loaded_model = pickle.load(open('svm_model.sav', 'rb'))
66
+ image = img.reshape((1, -1))
67
+ y_test_pred = loaded_model.predict(image)
68
+
69
+ if input1 == "Deep Neural":
70
+ loaded_model = pickle.load(open('dnn_model.sav', 'rb'))
71
+ image = img.reshape((1, -1))
72
+ y_test_pred = loaded_model.predict(image)
73
+ y_test_pred = np.argmax(y_test_pred,axis=1)
74
+
75
+ if input1 == "Decision Tree":
76
+ loaded_model = pickle.load(open('Decision_Tree_model.sav', 'rb'))
77
+ image = img.reshape((1, -1))
78
+ y_test_pred = loaded_model.predict(image)
79
+
80
+ if input1 == "Random Forest":
81
+ loaded_model = pickle.load(open('randomforest.sav', 'rb'))
82
+ image = img.reshape((1, -1))
83
+ y_test_pred = loaded_model.predict(image)
84
+
85
+ if input1 == "CNN":
86
+ loaded_model = pickle.load(open('CNN.sav', 'rb'))
87
+ img = img.reshape((1, -1))
88
+ y_test_pred = loaded_model.predict(img)
89
+ y_test_pred = np.argmax(y_test_pred,axis=1)
90
+
91
+ result = y_test_pred[0]
92
+
93
+ return result, img
94
+
95
+ # Step 6.4: Put all three component together into the gradio's interface function
96
+ gr.Interface(fn=multi_inputs,
97
+ inputs=[input_module1, input_module2],
98
+ outputs=[output_module1,output_module2]
99
+ ).launch( debug = True)