Spaces:
Sleeping
Sleeping
nathan ayers
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# 1) Load your pretrained model
|
7 |
+
model = pickle.load(open("mnist_model.pkl", "rb"))
|
8 |
+
|
9 |
+
# 2) Define a prediction function
|
10 |
+
def classify_digit(img):
|
11 |
+
# convert to grayscale 28×28
|
12 |
+
img = img.convert("L").resize((28, 28))
|
13 |
+
arr = np.array(img).reshape(1, -1)
|
14 |
+
pred = model.predict(arr)[0]
|
15 |
+
return f"Predicted digit: {pred}"
|
16 |
+
|
17 |
+
# 3) Wire up Gradio
|
18 |
+
iface = gr.Interface(
|
19 |
+
fn=classify_digit,
|
20 |
+
inputs=gr.Image(type="pil", label="Upload a 28×28 digit"),
|
21 |
+
outputs=gr.Textbox(label="Prediction"),
|
22 |
+
title="MNIST Digit Classifier",
|
23 |
+
description="Upload a handwritten digit and get a prediction!"
|
24 |
+
)
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
iface.launch()
|