Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,41 @@ import matplotlib as mpl
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
#step 1
|
12 |
-
|
13 |
|
14 |
-
input_module1 = gr.inputs.Image(label = "test_image")
|
15 |
|
16 |
input_module2 = gr.inputs.Slider(1, 10, step=1, label = "k")
|
17 |
|
18 |
-
output_module1 = gr.outputs.
|
19 |
|
20 |
output_module2 = gr.outputs.Image(label = "Predicted Probability per class")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
+
import gradio as gr
|
7 |
+
import tensorflow as tf
|
8 |
+
from sklearn.metrics import accuracy_score
|
9 |
+
from sklearn.neighbors import KNeighborsClassifier
|
10 |
|
11 |
#step 1
|
12 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
13 |
|
14 |
+
input_module1 = gr.inputs.Image(label = "test_image", image_mode='L', shape = (28,28))
|
15 |
|
16 |
input_module2 = gr.inputs.Slider(1, 10, step=1, label = "k")
|
17 |
|
18 |
+
output_module1 = gr.outputs.Textbox(label = "Predicted Digit")
|
19 |
|
20 |
output_module2 = gr.outputs.Image(label = "Predicted Probability per class")
|
21 |
|
22 |
+
def image_classification(input1, input2):
|
23 |
+
print(input1.shape)
|
24 |
+
print(input2)
|
25 |
+
|
26 |
+
image = input1.reshape(1, 28 *28)
|
27 |
+
X_train = x_train.reshape(60000, 28*28)
|
28 |
+
X_test = x_test.reshape(10000, 28*28)
|
29 |
+
|
30 |
+
kNN_classifier = KNeighborsClassifier(n_neighbors=input2)
|
31 |
+
kNN_classifier.fit(X_train, y_train)
|
32 |
+
|
33 |
+
y_test_predicted_label = kNN_classifier.predict(image)
|
34 |
+
|
35 |
+
output1 = y_test_predicted_label# text output example
|
36 |
+
output2 = np.random.rand(2,2) # image-like array output example
|
37 |
+
return output1,output2
|
38 |
+
|
39 |
+
# Step 6.4: Put all three component together into the gradio's interface function
|
40 |
+
gr.Interface(fn=image_classification,
|
41 |
+
inputs=[input_module1, input_module2],
|
42 |
+
outputs=[output_module1, output_module2]
|
43 |
+
).launch(debug = True)
|