dgith commited on
Commit
cc8a53d
Β·
verified Β·
1 Parent(s): 220ee7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -42,8 +42,7 @@ else:
42
  print("Starting training...")
43
  model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test), verbose=1)
44
 
45
- # Save model
46
- # Note: Consider using the native Keras format as suggested: model.save(MODEL_PATH.replace(".h5", ".keras"))
47
  model.save(MODEL_PATH)
48
  print(f"πŸ’Ύ Model saved to {MODEL_PATH}")
49
 
@@ -51,6 +50,7 @@ else:
51
  test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
52
  print(f"πŸŽ‰ Training complete! Test Accuracy: {test_accuracy:.4f}")
53
 
 
54
  def preprocess_image(image):
55
  if image is None:
56
  return None
@@ -92,14 +92,21 @@ def predict_digit(image):
92
  processed = preprocess_image(image)
93
  if processed is None:
94
  # Return empty probabilities if preprocessing failed
95
- return {str(i): 0.0 for i in range(10)}
96
 
97
  # Reshape for model input: (1, 28, 28)
98
  input_tensor = np.expand_dims(processed, axis=0)
99
  preds = model.predict(input_tensor, verbose=0)[0] # Get first prediction
100
 
101
  # Format output for Gradio Label component
102
- return {str(i): float(preds[i]) for i in range(10)}
 
 
 
 
 
 
 
103
 
104
 
105
  # Gradio UI
@@ -115,11 +122,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
115
  canvas_size=(200, 200),
116
  brush=gr.Brush(default_size=20),
117
  # Removed type="numpy" - handle conversion in the function
118
- # Removed invalid source "canvas". Only use valid ones if specifying.
119
- # sources=["upload"] # Example: only allow uploads
120
- # sources=["upload", "clipboard"] # Example: allow uploads and clipboard
121
- # If you want drawing AND uploads, just remove the 'sources' line entirely.
122
- # The canvas is the default and always available unless explicitly disabled.
123
  )
124
  # A separate upload button can still be used if preferred
125
  upload_btn = gr.UploadButton("πŸ“ Upload Image", file_types=["image"])
 
42
  print("Starting training...")
43
  model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test), verbose=1)
44
 
45
+ # Save model (consider using .keras format instead)
 
46
  model.save(MODEL_PATH)
47
  print(f"πŸ’Ύ Model saved to {MODEL_PATH}")
48
 
 
50
  test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
51
  print(f"πŸŽ‰ Training complete! Test Accuracy: {test_accuracy:.4f}")
52
 
53
+
54
  def preprocess_image(image):
55
  if image is None:
56
  return None
 
92
  processed = preprocess_image(image)
93
  if processed is None:
94
  # Return empty probabilities if preprocessing failed
95
+ return {str(i): 0.0 for i in range(10)}, [] # Return dict AND empty list for barplot
96
 
97
  # Reshape for model input: (1, 28, 28)
98
  input_tensor = np.expand_dims(processed, axis=0)
99
  preds = model.predict(input_tensor, verbose=0)[0] # Get first prediction
100
 
101
  # Format output for Gradio Label component
102
+ predictions_dict = {str(i): float(preds[i]) for i in range(10)}
103
+
104
+ # Format output for Gradio BarPlot component
105
+ # BarPlot expects a list of lists: [[x_value, y_value], ...]
106
+ # We want digits 0-9 on X-axis and their probabilities on Y-axis
107
+ barplot_data = [[str(i), float(preds[i])] for i in range(10)]
108
+
109
+ return predictions_dict, barplot_data
110
 
111
 
112
  # Gradio UI
 
122
  canvas_size=(200, 200),
123
  brush=gr.Brush(default_size=20),
124
  # Removed type="numpy" - handle conversion in the function
125
+ sources=["upload"] # Only allow uploads, not drawing
 
 
 
 
126
  )
127
  # A separate upload button can still be used if preferred
128
  upload_btn = gr.UploadButton("πŸ“ Upload Image", file_types=["image"])