import gradio as gr import tensorflow as tf from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.preprocessing import image import numpy as np # Load model model = tf.keras.models.load_model("model.h5") # Class labels (Italian animal names) class_names = [ "cane", "cavallo", "elefante", "farfalla", "gallina", "gatto", "mucca", "pecora", "ragno", "scoiattolo" ] # Prediction function def predict(img): img = img.convert("RGB") img = img.resize((224, 224)) img_array = image.img_to_array(img) img_array = preprocess_input(img_array) img_array = np.expand_dims(img_array, axis=0) predictions = model.predict(img_array)[0] return {class_names[i]: float(predictions[i]) for i in range(len(class_names))} # Gradio Interface interface = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs=gr.Label(num_top_classes=3), title="Animal Classifier (10 Species)", description="Upload an image of an animal. The model will classify it as one of: cane, cavallo, elefante, farfalla, gallina, gatto, mucca, pecora, ragno, scoiattolo." ) if __name__ == "__main__": interface.launch()