import gradio as gr import keras from keras.models import load_model from tensorflow.keras.preprocessing import image import numpy as np from PIL import Image # Load the trained model (ensure this path is correct in your Space) model = load_model('human_identifier_model.h5') model.compile(optimizer=keras.optimizers.Adam(), # Choose the same optimizer used during training loss='binary_crossentropy', # Use the same loss function as during training metrics=['accuracy']) # Define the image size for input (based on your model's input size) IMG_SIZE = (299, 299) # Define the image preparation function def prepare_image(img): img = img.resize(IMG_SIZE) img_array = np.array(img) img_array = np.expand_dims(img_array, axis=0) # Add batch dimension img_array = img_array / 255.0 # Normalize if necessary return img_array # Define the prediction function def predict(image): # Prepare the image for prediction img_array = prepare_image(image) # Make a prediction prediction = model.predict(img_array) # Return the result result = 'Human' if prediction[0] < 0.5 else 'Non-Human' return result, prediction[0] # Create the Gradio interface iface = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), # Accepts image input outputs=[gr.Label(num_top_classes=1), gr.Textbox()], # Displays result and confidence live=True, # Optional: updates the result as soon as the user uploads an image title="Human vs Non-Human Classifier", description="Upload an image to classify whether it's a human or non-human." ) # Launch the Gradio interface iface.launch()