Spaces:
Sleeping
Sleeping
Jeet Paul
commited on
Commit
·
c65e18e
1
Parent(s):
4ae5452
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
from matplotlib import pyplot as plt
|
5 |
+
|
6 |
+
def ReLU(Z):
|
7 |
+
return np.maximum(Z, 0)
|
8 |
+
|
9 |
+
def softmax(Z):
|
10 |
+
A = np.exp(Z) / sum(np.exp(Z))
|
11 |
+
return A
|
12 |
+
|
13 |
+
def init_params():
|
14 |
+
W1 = np.random.rand(10, 784) - 0.5
|
15 |
+
b1 = np.random.rand(10, 1) - 0.5
|
16 |
+
W2 = np.random.rand(10, 10) - 0.5
|
17 |
+
b2 = np.random.rand(10, 1) - 0.5
|
18 |
+
return W1, b1, W2, b2
|
19 |
+
|
20 |
+
def forward_prop(W1, b1, W2, b2, X):
|
21 |
+
Z1 = W1.dot(X) + b1
|
22 |
+
A1 = ReLU(Z1)
|
23 |
+
Z2 = W2.dot(A1) + b2
|
24 |
+
A2 = softmax(Z2)
|
25 |
+
return Z1, A1, Z2, A2
|
26 |
+
|
27 |
+
def get_predictions(A2):
|
28 |
+
return np.argmax(A2, 0)
|
29 |
+
|
30 |
+
def make_predictions(X, W1, b1, W2, b2):
|
31 |
+
_, _, _, A2 = forward_prop(W1, b1, W2, b2, X)
|
32 |
+
predictions = get_predictions(A2)
|
33 |
+
return predictions
|
34 |
+
|
35 |
+
def predict_digit(input_data):
|
36 |
+
# Load the trained parameters
|
37 |
+
params = np.load("trained_params.npz", allow_pickle=True)
|
38 |
+
W1, b1, W2, b2 = params["W1"], params["b1"], params["W2"], params["b2"]
|
39 |
+
|
40 |
+
|
41 |
+
# Reshape the input data to match the neural network architecture
|
42 |
+
X = input_data.reshape((784, 1)) / 255.
|
43 |
+
|
44 |
+
# Get the prediction
|
45 |
+
prediction = make_predictions(X, W1, b1, W2, b2)
|
46 |
+
|
47 |
+
return int(prediction)
|
48 |
+
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=predict_digit,
|
51 |
+
inputs=gr.inputs.Image(shape=(28, 28)),
|
52 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
53 |
+
live=True,
|
54 |
+
capture_session=True,
|
55 |
+
title="Handwritten Digit Recognizer",
|
56 |
+
description="Draw a digit using your mouse, and the model will try to recognize it.",
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
iface.launch()
|