Spaces:
Sleeping
Sleeping
Commit ·
7f83de3
1
Parent(s): f61af91
deploy mnist gradio model
Browse files- app.py +29 -3
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -1,7 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
|
| 6 |
+
# Load model (Space-friendly)
|
| 7 |
+
model = tf.keras.models.load_model(
|
| 8 |
+
"mobilenetv2_mnist.keras",
|
| 9 |
+
compile=False,
|
| 10 |
+
safe_mode=False
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def predict(image):
|
| 14 |
+
image = image.resize((32, 32))
|
| 15 |
+
image = image.convert("RGB")
|
| 16 |
+
|
| 17 |
+
img = np.array(image) / 255.0
|
| 18 |
+
img = np.expand_dims(img, axis=0)
|
| 19 |
+
|
| 20 |
+
preds = model.predict(img)
|
| 21 |
+
digit = int(np.argmax(preds))
|
| 22 |
+
|
| 23 |
+
return f"Predicted Digit: {digit}"
|
| 24 |
+
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=predict,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="MNIST MobileNetV2 Classifier",
|
| 30 |
+
description="Upload a digit image (0–9)"
|
| 31 |
+
)
|
| 32 |
|
|
|
|
| 33 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.15
|
| 2 |
+
keras==3.0.5
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|