Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,24 +33,23 @@ model = CNN()
|
|
| 33 |
model.load_state_dict(torch.load("model.pth",map_location="cpu"))
|
| 34 |
model.eval()
|
| 35 |
|
| 36 |
-
def predict(
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
|
| 44 |
-
output = model(
|
| 45 |
pred = torch.argmax(output,1).item()
|
| 46 |
|
| 47 |
-
return pred
|
| 48 |
|
| 49 |
interface = gr.Interface(
|
| 50 |
fn=predict,
|
| 51 |
-
inputs=gr.Image(
|
| 52 |
outputs="label",
|
| 53 |
title="Handwritten Digit Classifier"
|
| 54 |
)
|
| 55 |
-
|
| 56 |
interface.launch()
|
|
|
|
| 33 |
model.load_state_dict(torch.load("model.pth",map_location="cpu"))
|
| 34 |
model.eval()
|
| 35 |
|
| 36 |
+
def predict(img):
|
| 37 |
|
| 38 |
+
img = img / 255.0
|
| 39 |
+
img = np.resize(img, (28,28))
|
| 40 |
+
img = img.reshape(1,1,28,28)
|
| 41 |
|
| 42 |
+
img = torch.tensor(img, dtype=torch.float32)
|
| 43 |
|
| 44 |
+
output = model(img)
|
| 45 |
pred = torch.argmax(output,1).item()
|
| 46 |
|
| 47 |
+
return str(pred)
|
| 48 |
|
| 49 |
interface = gr.Interface(
|
| 50 |
fn=predict,
|
| 51 |
+
inputs=gr.Image(type="numpy", image_mode="L"),
|
| 52 |
outputs="label",
|
| 53 |
title="Handwritten Digit Classifier"
|
| 54 |
)
|
|
|
|
| 55 |
interface.launch()
|