Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| from keras.models import load_model | |
| from PIL import Image | |
| # Load the trained model | |
| model = load_model("mnist_model.h5") | |
| # Prediction function | |
| def predict_digit(image): | |
| image = image.convert('L').resize((28, 28)) | |
| img_array = np.array(image).astype("float32") / 255.0 | |
| img_array = img_array.reshape(1, 28, 28) | |
| prediction = model.predict(img_array) | |
| predicted_class = np.argmax(prediction) | |
| confidence = float(np.max(prediction)) | |
| return f"Prediction: {predicted_class} (Confidence: {confidence:.2f})" | |
| # Gradio Interface (no shape argument) | |
| interface = gr.Interface( | |
| fn=predict_digit, | |
| inputs=gr.Image(type="pil", label="Upload a Digit Image"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="Handwritten Digit Recognition", | |
| description="Upload a handwritten digit image (0–9) to classify it using a model trained on the MNIST dataset." | |
| ) | |
| interface.launch() | |