Spaces:
Runtime error
Runtime error
| 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() | |