Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +28 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model = tf.keras.models.load_model("my_mnist_model.keras")
|
| 7 |
+
|
| 8 |
+
def predict_digit(image):
|
| 9 |
+
# Convert to grayscale if needed
|
| 10 |
+
if image.ndim == 3:
|
| 11 |
+
image = image[..., 0]
|
| 12 |
+
# Resize and preprocess
|
| 13 |
+
image = np.array(image).astype("float32")
|
| 14 |
+
image = image.reshape(1, 28, 28)
|
| 15 |
+
image = image / 255.0
|
| 16 |
+
prediction = model.predict(image)
|
| 17 |
+
return str(np.argmax(prediction))
|
| 18 |
+
|
| 19 |
+
iface = gr.Interface(
|
| 20 |
+
fn=predict_digit,
|
| 21 |
+
inputs=gr.Image(shape=(28, 28), image_mode='L', source="canvas", tool="editor"),
|
| 22 |
+
outputs=gr.Label(num_top_classes=1),
|
| 23 |
+
title="MNIST Digit Classifier",
|
| 24 |
+
description="Draw a digit (0-9) and the model will predict it."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
gradio
|
| 3 |
+
numpy
|