Josephus67 commited on
Commit
c813ea4
·
verified ·
1 Parent(s): bd0d83c

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. README.md +32 -9
  3. app.py +47 -0
  4. mnist_model.keras +3 -0
  5. requirements.txt +4 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ mnist_model.keras filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,35 @@
1
  ---
2
- title: Mnist Model Joe
3
- emoji: 👀
4
- colorFrom: gray
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 6.2.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ library_name: keras
3
+ tags:
4
+ - image-classification
5
+ - tensorflow
6
+ datasets:
7
+ - mnist
 
 
8
  ---
9
 
10
+ # MNIST Digit Classifier
11
+
12
+ This is a Keras/TensorFlow model trained on the MNIST dataset to classify handwritten digits (0-9).
13
+
14
+ ## Model Details
15
+ - **Framework:** TensorFlow / Keras
16
+ - **Task:** Image Classification (Multi-class)
17
+ - **Dataset:** MNIST
18
+
19
+ ## Intended Use
20
+ This model is intended for educational purposes to demonstrate digit classification.
21
+
22
+ ## Usage
23
+
24
+ You can download and load this model in Python:
25
+
26
+ ```python
27
+ from huggingface_hub import hf_hub_download
28
+ import tensorflow as tf
29
+
30
+ # Download the model file
31
+ model_path = hf_hub_download(repo_id="your-username/your-model-name", filename="mnist_model.keras")
32
+
33
+ # Load the model
34
+ model = tf.keras.models.load_model(model_path)
35
+ ```
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image, ImageOps
5
+
6
+ # Load the model directly
7
+ # reliable for Spaces where you upload the model file alongside the app
8
+ model = tf.keras.models.load_model("mnist_model.keras")
9
+
10
+ def predict_digit(image):
11
+ if image is None:
12
+ return None
13
+
14
+ # 1. Convert to grayscale
15
+ image = image.convert('L')
16
+
17
+ # 2. Resize to 28x28 to match training data
18
+ image = image.resize((28, 28))
19
+
20
+ # 3. Invert colors (MNIST is white text on black background)
21
+ # Most user uploads are black text on white background (paper), so we usually need to invert
22
+ # We check mean pixel value; if > 127, it's likely a white background.
23
+ if np.mean(image) > 127:
24
+ image = ImageOps.invert(image)
25
+
26
+ # 4. Convert to numpy array and normalize
27
+ image_array = np.array(image) / 255.0
28
+
29
+ # 5. Flatten to shape (1, 784) as expected by the Dense input layer
30
+ image_array = image_array.reshape(1, 784)
31
+
32
+ # Predict
33
+ prediction = model.predict(image_array)
34
+
35
+ # Return dictionary for Gradio Label output
36
+ return {str(i): float(prediction[0][i]) for i in range(10)}
37
+
38
+ iface = gr.Interface(
39
+ fn=predict_digit,
40
+ inputs=gr.Image(type="pil", label="Upload an Image"),
41
+ outputs=gr.Label(num_top_classes=3, label="Predictions"),
42
+ title="MNIST Digit Classifier",
43
+ description="Upload an image of a handwritten digit (0-9) to see the prediction. Works best with a single digit centered in the image."
44
+ )
45
+
46
+ if __name__ == "__main__":
47
+ iface.launch()
mnist_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:24be0fec8219f8ad6247a3738889add9e4d6ac8d61b03d9a0aef362c6f4e9ea5
3
+ size 974585
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ numpy
4
+ pillow