Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| from tensorflow.keras.models import load_model | |
| model = load_model("saved_models/model.h5") | |
| def predict_digit(image): | |
| img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
| img = cv2.resize(img, (28, 28)) | |
| img = cv2.bitwise_not(img) | |
| _, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY) | |
| img = img.astype("float32") / 255.0 | |
| img = np.expand_dims(img, axis=-1) | |
| img = np.expand_dims(img, axis=0) | |
| prediction = model.predict(img) | |
| digit = np.argmax(prediction) | |
| confidence = np.max(prediction) * 100 | |
| return f"π’ Predicted Digit: {digit}\nπ Confidence: {confidence:.2f}%" | |
| gr.Interface( | |
| fn=predict_digit, | |
| inputs=gr.Image(shape=(200, 200), tool="editor", label="Draw a digit"), | |
| outputs="text", | |
| title="π§ Handwritten Digit Recognizer", | |
| description="Draw a digit (0β9) and let the model predict it" | |
| ).launch() | |