Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
from tensorflow import keras
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
MODEL_PATH = "cats-vs-dogs-finetuned.keras"
|
| 9 |
-
IMAGE_SIZE = (180, 180)
|
| 10 |
-
CLASS_LABELS = ['Cat', 'Dog']
|
| 11 |
-
|
| 12 |
-
# --- Load the Model ---
|
| 13 |
-
# We load the Keras model. Hugging Face Spaces will automatically find this file
|
| 14 |
-
# if you upload it to your repository.
|
| 15 |
-
try:
|
| 16 |
-
model = keras.models.load_model(MODEL_PATH)
|
| 17 |
-
print(f"Model loaded successfully from {MODEL_PATH}")
|
| 18 |
-
except Exception as e:
|
| 19 |
-
# If the model fails to load (e.g., during initial setup before it's uploaded),
|
| 20 |
-
# we use a placeholder function. This helps the app start.
|
| 21 |
-
print(f"Error loading model: {e}. Using a placeholder function.")
|
| 22 |
-
model = None
|
| 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) # 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 |
-
pdog=float(predictions[0][0])
|
| 56 |
-
return {"dog":pdog,"cat":1-pdog}
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
# Catch any error, log it, and return it to the user in a visible format
|
| 60 |
-
error_message = f"CRITICAL RUNTIME ERROR: {str(e)}"
|
| 61 |
-
detailed_trace = traceback.format_exc()
|
| 62 |
-
|
| 63 |
-
print("\n--- DETAILED RUNTIME ERROR LOG ---")
|
| 64 |
-
print(error_message)
|
| 65 |
-
print(detailed_trace)
|
| 66 |
-
print("------------------------------------\n")
|
| 67 |
-
|
| 68 |
-
# This format should force Gradio to display the specific error message
|
| 69 |
-
return {f"💥 {error_message}": 1.0}
|
| 70 |
-
|
| 71 |
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# 'example_cat.jpg',
|
| 83 |
-
# 'example_dog.jpg'
|
| 84 |
-
]
|
| 85 |
|
| 86 |
-
# Create the Gradio interface
|
| 87 |
demo = gr.Interface(
|
| 88 |
fn=predict_image,
|
| 89 |
-
inputs=
|
| 90 |
-
outputs=
|
| 91 |
-
title="
|
| 92 |
-
description="
|
| 93 |
-
theme=gr.themes.Soft(),
|
| 94 |
-
# Optional: Add examples if you upload them
|
| 95 |
-
# examples=examples
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# Launch the app
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# --- Config ---
|
| 7 |
MODEL_PATH = "cats-vs-dogs-finetuned.keras"
|
| 8 |
+
IMAGE_SIZE = (180, 180) # change if your model expects a different size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load model once
|
| 11 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 12 |
|
| 13 |
+
def predict_image(img: Image.Image):
|
| 14 |
+
if img is None:
|
| 15 |
+
return {"Cat": 0.5, "Dog": 0.5}
|
| 16 |
|
| 17 |
+
# Preprocess
|
| 18 |
+
x = img.convert("RGB").resize(IMAGE_SIZE)
|
| 19 |
+
x = np.asarray(x, dtype=np.float32) / 255.0
|
| 20 |
+
x = np.expand_dims(x, 0) # (1, H, W, 3)
|
| 21 |
|
| 22 |
+
# Predict: model outputs shape (1,1) with sigmoid for "Dog" probability
|
| 23 |
+
p_dog = float(model.predict(x, verbose=0)[0, 0]) # cast to Python float
|
| 24 |
+
return {"Cat": 1.0 - p_dog, "Dog": p_dog}
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn=predict_image,
|
| 28 |
+
inputs=gr.Image(type="pil", label="Upload a Cat or Dog"),
|
| 29 |
+
outputs=gr.Label(num_top_classes=2, label="Prediction"),
|
| 30 |
+
title="Cats vs Dogs (Keras, single-logit)",
|
| 31 |
+
description="keras image classification model for cat-vs-dog images"
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
if __name__ == "__main__":
|
| 35 |
demo.launch()
|