Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ print("Checking for existing model...")
|
|
| 17 |
if os.path.exists(MODEL_PATH):
|
| 18 |
print("β
Loading pre-trained model...")
|
| 19 |
model = tf.keras.models.load_model(MODEL_PATH)
|
|
|
|
| 20 |
else:
|
| 21 |
print("β Model not found. Training new model...")
|
| 22 |
# Load and preprocess data
|
|
@@ -43,18 +44,20 @@ else:
|
|
| 43 |
model.save(MODEL_PATH)
|
| 44 |
print(f"πΎ Model saved to {MODEL_PATH}")
|
| 45 |
|
| 46 |
-
#
|
| 47 |
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
|
| 48 |
print(f"π Training complete! Test Accuracy: {test_accuracy:.4f}")
|
| 49 |
|
| 50 |
def preprocess_image(image):
|
| 51 |
if image is None:
|
|
|
|
| 52 |
return None
|
| 53 |
|
| 54 |
# Handle Gradio ImageEditor/UploadButton output
|
| 55 |
if isinstance(image, dict):
|
| 56 |
image = image.get('image', None)
|
| 57 |
if image is None:
|
|
|
|
| 58 |
return None
|
| 59 |
|
| 60 |
# Convert PIL image to NumPy array if needed
|
|
@@ -73,12 +76,16 @@ def preprocess_image(image):
|
|
| 73 |
# Resize to 28x28
|
| 74 |
resized = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA)
|
| 75 |
|
| 76 |
-
# Invert if background is
|
| 77 |
-
if resized.mean()
|
| 78 |
resized = 255 - resized
|
| 79 |
|
| 80 |
# Normalize
|
| 81 |
normalized = resized.astype('float32') / 255.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return normalized
|
| 83 |
|
| 84 |
def predict_digit(image):
|
|
@@ -101,7 +108,7 @@ def predict_digit(image):
|
|
| 101 |
|
| 102 |
# Gradio UI
|
| 103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
-
gr.Markdown("#Handwritten Digit Recognition")
|
| 105 |
gr.Markdown("Draw or upload a digit (0β9). Our AI will classify it!")
|
| 106 |
|
| 107 |
with gr.Row():
|
|
|
|
| 17 |
if os.path.exists(MODEL_PATH):
|
| 18 |
print("β
Loading pre-trained model...")
|
| 19 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 20 |
+
model.summary() # Print model summary for debugging
|
| 21 |
else:
|
| 22 |
print("β Model not found. Training new model...")
|
| 23 |
# Load and preprocess data
|
|
|
|
| 44 |
model.save(MODEL_PATH)
|
| 45 |
print(f"πΎ Model saved to {MODEL_PATH}")
|
| 46 |
|
| 47 |
+
# Evaluate
|
| 48 |
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
|
| 49 |
print(f"π Training complete! Test Accuracy: {test_accuracy:.4f}")
|
| 50 |
|
| 51 |
def preprocess_image(image):
|
| 52 |
if image is None:
|
| 53 |
+
print("Warning: Received None as image input.")
|
| 54 |
return None
|
| 55 |
|
| 56 |
# Handle Gradio ImageEditor/UploadButton output
|
| 57 |
if isinstance(image, dict):
|
| 58 |
image = image.get('image', None)
|
| 59 |
if image is None:
|
| 60 |
+
print("Warning: No image found in dictionary.")
|
| 61 |
return None
|
| 62 |
|
| 63 |
# Convert PIL image to NumPy array if needed
|
|
|
|
| 76 |
# Resize to 28x28
|
| 77 |
resized = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA)
|
| 78 |
|
| 79 |
+
# Invert if background is white (MNIST digits are white on black)
|
| 80 |
+
if resized.mean() > 128:
|
| 81 |
resized = 255 - resized
|
| 82 |
|
| 83 |
# Normalize
|
| 84 |
normalized = resized.astype('float32') / 255.0
|
| 85 |
+
|
| 86 |
+
print("Preprocessed image shape:", normalized.shape)
|
| 87 |
+
print("Preprocessed image mean:", normalized.mean())
|
| 88 |
+
|
| 89 |
return normalized
|
| 90 |
|
| 91 |
def predict_digit(image):
|
|
|
|
| 108 |
|
| 109 |
# Gradio UI
|
| 110 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 111 |
+
gr.Markdown("# π’ Kilele AI Tutor: Handwritten Digit Recognition")
|
| 112 |
gr.Markdown("Draw or upload a digit (0β9). Our AI will classify it!")
|
| 113 |
|
| 114 |
with gr.Row():
|