Plant_checker / app.py
GiuseppeMT's picture
Update app.py
02bcd61 verified
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
import requests
import os
MODEL_URL = "https://huggingface.co/liriope/PlantDiseaseDetection/resolve/main/model.h5"
MODEL_PATH = "model.h5"
# βœ… 1. Scarica il modello se non esiste ancora localmente
if not os.path.exists(MODEL_PATH):
print("πŸ“₯ Downloading model from Hugging Face...")
response = requests.get(MODEL_URL)
with open(MODEL_PATH, "wb") as f:
f.write(response.content)
print("βœ… Model downloaded and saved locally!")
# βœ… 2. Carica il modello
model = load_model(MODEL_PATH)
print("βœ… Model loaded successfully!")
# βœ… 3. Funzione di predizione
def predict(image):
img = image.convert("RGB").resize((224, 224))
arr = np.expand_dims(np.array(img) / 255.0, axis=0)
preds = model.predict(arr)[0]
class_idx = np.argmax(preds)
confidence = float(preds[class_idx])
return {f"Predicted class: {class_idx}": confidence}
# βœ… 4. Interfaccia Gradio
demo = gr.Interface(
fn=predict,
inputs=gr.Image(label="Upload a leaf photo"),
outputs=gr.Label(num_top_classes=3),
title="🌿 Plant Health Checker AI",
description="Upload a photo of a leaf and detect potential plant diseases using a deep learning model.",
)
demo.launch()