import gradio as gr import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras import backend as K import numpy as np from PIL import Image import time print("[INFO] app.py script started at {}.".format(time.ctime())) # --- Configuration --- MODEL_PATH = "Model/ProposedNetwork_StratifiedKFold_Final_fold_5.hdf5" IMAGE_SIZE = 224 # --- Load the Model --- print("[INFO] Attempting to load the trained model from: {}".format(MODEL_PATH)) try: # We must tell load_model what 'K' means in our custom layers. model = load_model(MODEL_PATH, custom_objects={"K": K}) 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 e def getLabel(class_id): """Returns the string label for a given class ID.""" return 'NoFire' if class_id == 0 else 'Fire' # --- The Prediction Function --- def predict_fire(input_image): if input_image is None: return {"Error": 1.0} print("[INFO] Received image for prediction.") # Preprocess the image exactly as the model expects image = input_image.convert("RGB").resize((IMAGE_SIZE, IMAGE_SIZE)) image_array = np.asarray(image) image_array = image_array / 255.0 image_array = np.expand_dims(image_array, axis=0) print("[INFO] Making prediction...") prediction_probs = model.predict(image_array)[0] confidences = {getLabel(i): float(prob) for i, prob in enumerate(prediction_probs)} return confidences # ============================================================================== # --- CREATE THE FINAL GRADIO INTERFACE USING gr.Blocks FOR CUSTOM STYLING --- # ============================================================================== print("[INFO] Creating Gradio Blocks interface...") # Define the HTML for our custom-styled description for centering and font size description_html = """