ishaqezaz commited on
Commit
5e173b1
·
1 Parent(s): 235ed91

mnist-model

Browse files
Files changed (2) hide show
  1. app.py +29 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import gradio as gr
3
+ import numpy as np
4
+ from PIL import Image, ImageOps
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ model_path = hf_hub_download(repo_id="ishaqezaz/mnist-cnn", filename="mnist_v4.keras")
8
+ model = tf.keras.models.load_model(model_path)
9
+
10
+ def preprocess_image(img):
11
+ if isinstance(img, np.ndarray):
12
+ img = Image.fromarray(img)
13
+
14
+ img = img.convert("L")
15
+ img = ImageOps.invert(img)
16
+ img = img.resize((28, 28))
17
+
18
+ img = np.array(img) / 255.0
19
+ img = img.reshape(1, 28, 28, 1)
20
+ return img
21
+
22
+ def predict(img):
23
+ img = preprocess_image(img)
24
+ prediction = model.predict(img)
25
+ predicted_digit = np.argmax(prediction)
26
+ return f"Predicted Digit: {predicted_digit}"
27
+
28
+ interface = gr.Interface(fn=predict, inputs="image", outputs="text")
29
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ pillow
4
+ numpy
5
+ huggingface_hub