Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567893 unmodified lines131415161718192021222324252612 unmodified lines39404142434445import gradio as grimport tensorflow as tffrom tensorflow.keras.models import load_modelimport numpy as npfrom PIL import Imageimport time # Import the time library for diagnosticsprint("[INFO] app.py script started at {}.".format(time.ctime()))3 unmodified lines# --- Load the Model (this will happen once when the app starts) ---print("[INFO] Attempting to load the trained model from: {}".format(MODEL_PATH))# This is the most likely point of failure.try:model = load_model(MODEL_PATH)print("[SUCCESS] Model loaded successfully into memory.")except Exception as e:print("[FATAL ERROR] Failed to load the model.")print("Error details: {}".format(e))# If the model fails to load, raise the exception to stop the appraise edef getLabel(class_id):12 unmodified lines# Preprocess the imageimage = input_image.convert("RGB").resize((IMAGE_SIZE, IMAGE_SIZE))image_array = np.asarray(image)image_array = image_array / 255.0 # Use 255.0 for normalizationimage_array = np.expand_dims(image_array, axis=0)# --- Make Prediction ---123456789103 unmodified lines1415161718192021222324252627282912 unmodified lines42434445464748import gradio as grimport tensorflow as tffrom tensorflow.keras.models import load_modelfrom tensorflow.keras import backend as K # <--- 1. IMPORT THE KERAS BACKENDimport numpy as npfrom PIL import Imageimport timeprint("[INFO] app.py script started at {}.".format(time.ctime()))3 unmodified lines# --- Load the Model (this will happen once when the app starts) ---print("[INFO] Attempting to load the trained model from: {}".format(MODEL_PATH))try:# --- 2. THE CRITICAL FIX IS HERE ---# We must tell load_model what 'K' means in our custom layers.model = load_model(MODEL_PATH, custom_objects={"K": K})# --- END OF FIX ---print("[SUCCESS] Model loaded successfully into memory.")except Exception as e:print("[FATAL ERROR] Failed to load the model.")print("Error details: {}".format(e))raise edef getLabel(class_id):12 unmodified lines# Preprocess the imageimage = input_image.convert("RGB").resize((IMAGE_SIZE, IMAGE_SIZE))image_array = np.asarray(image)image_array = image_array / 255.0image_array = np.expand_dims(image_array, axis=0)# --- Make Prediction ---
app.py+8-5