M009 commited on
Commit
cee9005
1 Parent(s): c4f2465

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sklearn.tree import DecisionTreeClassifier
3
+ from sklearn.ensemble import RandomForestClassifier
4
+ import joblib
5
+ from sklearn.ensemble import AdaBoostClassifier
6
+ from sklearn.ensemble import GradientBoostingClassifier
7
+ from sklearn.neighbors import KNeighborsClassifier
8
+ import numpy as np
9
+
10
+ import gradio as gr
11
+ # define image data type
12
+ input_image = gr.inputs.Image(label = "Input Image")
13
+ select_algorithm = gr.inputs.Dropdown(choices=["Decision Tree", "Random Forest", "AdaBoost", "Gradient Tree Boosting", "KNN"], label = "Select Algorithm")
14
+
15
+ out_classify = gr.outputs.Textbox(label = "Predict Class")
16
+ out_prob = gr.outputs.Textbox(label = "Predict Probability")
17
+
18
+ """
19
+ gradio interface
20
+ """
21
+ def predict_interface(input_image, select_algorithm):
22
+ """
23
+ evaluate model
24
+ """
25
+ # Convert image to NumPy array
26
+ print(input_image.shape)
27
+ input_image2 = input_image.mean(axis=2)
28
+ print(input_image2.shape)
29
+ img_array = input_image2.reshape(1, 28*28)
30
+
31
+
32
+ model_dict = {"Decision Tree":"best_dt_model.joblib",
33
+ "Random Forest":"best_rf_model.joblib",
34
+ "AdaBoost":"best_ada_model.joblib",
35
+ "Gradient Tree Boosting":"best_gbc_model.joblib",
36
+ "KNN":"best_knn_model.joblib"}
37
+ # Reload the best trained model from disk using joblib
38
+ loaded_model = joblib.load(model_dict[select_algorithm])
39
+
40
+ # Use the reloaded model to make predictions on the validation data
41
+ out_classify = loaded_model.predict(img_array)
42
+ out_prob = loaded_model.predict_proba(img_array)
43
+ class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
44
+ "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
45
+ out_prob2 = '\n'.join([f"{name}\t\t\t{np.round(pro,2)}" for name, pro in zip(class_names, out_prob[0])])
46
+ return class_names[out_classify[0]], out_prob2
47
+
48
+ gr.Interface(fn=predict_interface, inputs=[input_image, select_algorithm],
49
+ outputs=[out_classify, out_prob],
50
+ examples=[["fashion_1.png", "Random Forest"],
51
+ ["fashion_2.png", "Random Forest"],
52
+ ["fashion_3.png", "Random Forest"]]
53
+ ).launch(debug=True)