elhamb commited on
Commit
48264d8
·
verified ·
1 Parent(s): 8bda483

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -23,45 +23,55 @@ except Exception as e:
23
 
24
  # --- Prediction Function ---
25
  def predict_image(input_img_pil):
26
- """
27
- Predicts the class (Cat or Dog) given a PIL Image object.
28
 
29
- Args:
30
- input_img_pil: A PIL Image object received from Gradio's Image input.
31
-
32
- Returns:
33
- A dictionary of class labels and their probabilities (for Gradio's Label output).
34
- """
35
- if model is None:
36
- # Placeholder behavior if model loading failed
37
- return {"Error ********": 1.0}
38
 
39
- # 1. Preprocessing: Resize and convert to NumPy array
40
- img_resized = input_img_pil.resize(IMAGE_SIZE)
41
- print("image resized")
42
- img_array = keras.preprocessing.image.img_to_array(img_resized)
43
- print(" image converted to array")
 
 
 
 
 
 
 
 
 
 
44
 
45
- # 2. Rescaling and Batch dimension:
46
- # Keras models usually expect input shapes like (Batch_Size, Height, Width, Channels)
47
- # and often expect pixel values to be normalized (e.g., 0-1 range).
48
- # Please adjust the normalization based on how your model was trained!
49
- img_array = img_array / 255.0 # Common normalization step
50
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
51
 
52
- # 3. Prediction
53
- predictions = model.predict(img_array)[0] # Get the single prediction result
 
 
54
 
55
- # 4. Format the output for Gradio's Label component
56
- # The output is expected to be a dictionary: {'label': probability, ...}
57
-
58
- # Assuming predictions is a 2-element array: [prob_cat, prob_dog]
59
- output_dict = {
60
- CLASS_LABELS[0]: float(predictions[0]),
61
- CLASS_LABELS[1]: float(predictions[1])
62
- }
63
 
64
- return output_dict
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
 
67
  # --- Gradio Interface Setup ---
 
23
 
24
  # --- Prediction Function ---
25
  def predict_image(input_img_pil):
 
 
26
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # WRAP ENTIRE LOGIC IN TRY/EXCEPT FOR MAXIMUM ERROR CAPTURE
29
+ try:
30
+ # 0. Crucial check: ensure an image was actually uploaded
31
+ if input_img_pil is None:
32
+ # Return a simple dictionary indicating missing input
33
+ return {"Please upload an image first.": 1.0}
34
+
35
+ if model is None:
36
+ # Model loading failed during initialization
37
+ return {"MODEL NOT FOUND": 1.0, "Please check if cat-vs-dog.keras exists.": 0.0}
38
+
39
+ # 1. Preprocessing: Resize and convert to NumPy array
40
+ print(f"Original image size: {input_img_pil.size}")
41
+ img_resized = input_img_pil.resize(IMAGE_SIZE)
42
+ img_array = keras.preprocessing.image.img_to_array(img_resized)
43
 
44
+ # 2. Rescaling and Batch dimension:
45
+ img_array = img_array / 255.0 # Common normalization step
46
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
 
 
 
47
 
48
+ # 3. Prediction
49
+ print(f"Array shape for model input: {img_array.shape}")
50
+ predictions = model.predict(img_array)[0] # Get the single prediction result
51
+ print(f"Raw model predictions: {predictions}")
52
 
53
+ # 4. Format the output for Gradio's Label component
54
+ # Assuming predictions is a 2-element array: [prob_cat, prob_dog]
55
+ output_dict = {
56
+ CLASS_LABELS[0]: float(predictions[0]),
57
+ CLASS_LABELS[1]: float(predictions[1])
58
+ }
59
+
60
+ return output_dict
61
 
62
+ except Exception as e:
63
+ # Catch any error, log it, and return it to the user in a visible format
64
+ error_message = f"CRITICAL RUNTIME ERROR: {str(e)}"
65
+ detailed_trace = traceback.format_exc()
66
+
67
+ print("\n--- DETAILED RUNTIME ERROR LOG ---")
68
+ print(error_message)
69
+ print(detailed_trace)
70
+ print("------------------------------------\n")
71
+
72
+ # This format should force Gradio to display the specific error message
73
+ return {f"💥 {error_message}": 1.0}
74
+
75
 
76
 
77
  # --- Gradio Interface Setup ---