File size: 1,211 Bytes
2dcdf90
 
 
 
 
deaafde
 
 
 
2dcdf90
 
deaafde
2dcdf90
deaafde
2dcdf90
 
 
deaafde
2dcdf90
deaafde
 
 
 
 
 
 
 
 
 
9819aa3
f42d557
 
deaafde
 
 
f42d557
deaafde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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")

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
    
    return output1
# 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]
            ).launch(debug = True)