Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,40 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def predict(image):
|
| 11 |
img = image.convert("RGB").resize((224, 224))
|
| 12 |
arr = np.expand_dims(np.array(img) / 255.0, axis=0)
|
| 13 |
preds = model.predict(arr)[0]
|
| 14 |
class_idx = np.argmax(preds)
|
| 15 |
confidence = float(preds[class_idx])
|
| 16 |
-
|
| 17 |
return {f"Predicted class: {class_idx}": confidence}
|
| 18 |
|
| 19 |
-
# β
|
| 20 |
demo = gr.Interface(
|
| 21 |
fn=predict,
|
| 22 |
inputs=gr.Image(label="Upload a leaf photo"),
|
| 23 |
outputs=gr.Label(num_top_classes=3),
|
| 24 |
title="πΏ Plant Health Checker AI",
|
| 25 |
-
description="Upload a photo of a leaf and detect potential plant diseases using a deep learning model."
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# β
4. Avvio Space
|
| 29 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
MODEL_URL = "https://huggingface.co/liriope/PlantDiseaseDetection/resolve/main/model.h5"
|
| 9 |
+
MODEL_PATH = "model.h5"
|
| 10 |
|
| 11 |
+
# β
1. Scarica il modello se non esiste ancora localmente
|
| 12 |
+
if not os.path.exists(MODEL_PATH):
|
| 13 |
+
print("π₯ Downloading model from Hugging Face...")
|
| 14 |
+
response = requests.get(MODEL_URL)
|
| 15 |
+
with open(MODEL_PATH, "wb") as f:
|
| 16 |
+
f.write(response.content)
|
| 17 |
+
print("β
Model downloaded and saved locally!")
|
| 18 |
+
|
| 19 |
+
# β
2. Carica il modello
|
| 20 |
+
model = load_model(MODEL_PATH)
|
| 21 |
+
print("β
Model loaded successfully!")
|
| 22 |
+
|
| 23 |
+
# β
3. Funzione di predizione
|
| 24 |
def predict(image):
|
| 25 |
img = image.convert("RGB").resize((224, 224))
|
| 26 |
arr = np.expand_dims(np.array(img) / 255.0, axis=0)
|
| 27 |
preds = model.predict(arr)[0]
|
| 28 |
class_idx = np.argmax(preds)
|
| 29 |
confidence = float(preds[class_idx])
|
|
|
|
| 30 |
return {f"Predicted class: {class_idx}": confidence}
|
| 31 |
|
| 32 |
+
# β
4. Interfaccia Gradio
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=predict,
|
| 35 |
inputs=gr.Image(label="Upload a leaf photo"),
|
| 36 |
outputs=gr.Label(num_top_classes=3),
|
| 37 |
title="πΏ Plant Health Checker AI",
|
| 38 |
+
description="Upload a photo of a leaf and detect potential plant diseases using a deep learning model.",
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
demo.launch()
|