GiuseppeMT commited on
Commit
02bcd61
Β·
verified Β·
1 Parent(s): 1f28394

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
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
- # βœ… 1. Carica il modello (dal repo Hugging Face di liriope)
7
- model = load_model("https://huggingface.co/liriope/PlantDiseaseDetection/resolve/main/model.h5")
8
 
9
- # βœ… 2. Preprocessing + predizione
 
 
 
 
 
 
 
 
 
 
 
 
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
- # βœ… 3. Interfaccia Gradio
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()