Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- .gitattributes +1 -0
- Digit.keras +3 -0
- app.py +59 -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 |
+
Digit.keras filter=lfs diff=lfs merge=lfs -text
|
Digit.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0deb8876ae99141d3dd2e22aa9f1a18f8036695a8e90367a20ca415859839300
|
| 3 |
+
size 2739953
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
|
| 6 |
+
model = keras.models.load_model("Digit.keras")
|
| 7 |
+
|
| 8 |
+
def preprocess_image(img):
|
| 9 |
+
if img is None:
|
| 10 |
+
raise ValueError("No image provided.")
|
| 11 |
+
|
| 12 |
+
# If Sketchpad returns a dict, try common keys
|
| 13 |
+
if isinstance(img, dict):
|
| 14 |
+
if "image" in img and img["image"] is not None:
|
| 15 |
+
img = img["image"]
|
| 16 |
+
elif "composite" in img and img["composite"] is not None:
|
| 17 |
+
img = img["composite"]
|
| 18 |
+
else:
|
| 19 |
+
raise ValueError("No image data found in dictionary.")
|
| 20 |
+
|
| 21 |
+
# If still a NumPy array, convert to PIL
|
| 22 |
+
if isinstance(img, np.ndarray):
|
| 23 |
+
img = Image.fromarray(img.astype("uint8"))
|
| 24 |
+
|
| 25 |
+
# --- MNIST preprocessing ---
|
| 26 |
+
img = img.convert("L").resize((28, 28))
|
| 27 |
+
arr = np.array(img).astype("float32")
|
| 28 |
+
if arr.mean() > 127: # invert if white background
|
| 29 |
+
arr = 255 - arr
|
| 30 |
+
arr = arr / 255.0
|
| 31 |
+
return arr[np.newaxis, ..., np.newaxis] # (1,28,28,1)
|
| 32 |
+
|
| 33 |
+
def predict(img):
|
| 34 |
+
try:
|
| 35 |
+
x = preprocess_image(img)
|
| 36 |
+
probs = model.predict(x, verbose=0)[0]
|
| 37 |
+
pred = int(np.argmax(probs))
|
| 38 |
+
return str(pred), {str(i): float(probs[i]) for i in range(10)}
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error: {e}", {}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(title="MNIST Digit Classifier") as demo:
|
| 44 |
+
gr.Markdown("## ✍️ Draw a digit (0–9)")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
canvas = gr.Sketchpad(
|
| 48 |
+
canvas_size=(280, 280),
|
| 49 |
+
type="pil", # <-- forces PIL, avoids dicts
|
| 50 |
+
label="Draw a digit here"
|
| 51 |
+
)
|
| 52 |
+
btn = gr.Button("Predict")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
pred_txt = gr.Textbox(label="Predicted Digit", interactive=False)
|
| 55 |
+
probs = gr.Label(label="Class Probabilities", num_top_classes=10)
|
| 56 |
+
|
| 57 |
+
btn.click(predict, inputs=canvas, outputs=[pred_txt, probs])
|
| 58 |
+
|
| 59 |
+
demo.launch()
|