Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ================================
|
| 2 |
-
# 0. PATCH pour huggingface_hub
|
| 3 |
# ================================
|
| 4 |
import huggingface_hub
|
| 5 |
if not hasattr(huggingface_hub, 'HfFolder'):
|
|
@@ -14,22 +14,18 @@ if not hasattr(huggingface_hub, 'HfFolder'):
|
|
| 14 |
huggingface_hub.HfFolder = HfFolder
|
| 15 |
|
| 16 |
# ================================
|
| 17 |
-
# 1. PATCH pour
|
| 18 |
-
# (TypeError: argument of type 'bool' is not iterable)
|
| 19 |
# ================================
|
| 20 |
import gradio_client.utils
|
| 21 |
-
|
| 22 |
original_get_type = gradio_client.utils.get_type
|
| 23 |
-
|
| 24 |
def patched_get_type(schema):
|
| 25 |
if isinstance(schema, bool):
|
| 26 |
return "boolean"
|
| 27 |
return original_get_type(schema)
|
| 28 |
-
|
| 29 |
gradio_client.utils.get_type = patched_get_type
|
| 30 |
|
| 31 |
# ================================
|
| 32 |
-
# 2. IMPORTS
|
| 33 |
# ================================
|
| 34 |
import gradio as gr
|
| 35 |
import tensorflow as tf
|
|
@@ -37,19 +33,20 @@ import numpy as np
|
|
| 37 |
from PIL import Image
|
| 38 |
|
| 39 |
# ================================
|
| 40 |
-
# 3. CHARGEMENT DU MODÈLE
|
| 41 |
# ================================
|
| 42 |
MODEL_PATH = "lemon_model.h5"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ================================
|
| 49 |
-
# 4. NOMS DES CLASSES (
|
| 50 |
# ================================
|
| 51 |
-
# Liste obtenue via full_dataset.class_names dans le code d'entraînement.
|
| 52 |
-
# Remplacez par les vôtres si nécessaire.
|
| 53 |
class_names = [
|
| 54 |
"Bacterial Blight",
|
| 55 |
"Citrus Canker",
|
|
@@ -63,30 +60,27 @@ class_names = [
|
|
| 63 |
]
|
| 64 |
|
| 65 |
# ================================
|
| 66 |
-
# 5.
|
| 67 |
# ================================
|
| 68 |
def preprocess_image(img):
|
| 69 |
-
"""Redimensionne et normalise l'image (comme dans l'entraînement)."""
|
| 70 |
img = img.resize((224, 224))
|
| 71 |
img_array = np.array(img, dtype=np.float32) / 255.0
|
| 72 |
img_array = np.expand_dims(img_array, axis=0)
|
| 73 |
return img_array
|
| 74 |
|
| 75 |
def predict(img):
|
| 76 |
-
"""Retourne un dictionnaire {classe: probabilité}."""
|
| 77 |
processed = preprocess_image(img)
|
| 78 |
preds = model.predict(processed, verbose=0)[0]
|
| 79 |
-
|
| 80 |
-
return results
|
| 81 |
|
| 82 |
# ================================
|
| 83 |
-
# 6. INTERFACE
|
| 84 |
# ================================
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=predict,
|
| 87 |
inputs=gr.Image(type="pil", label="Chargez une image de feuille de citron"),
|
| 88 |
outputs=gr.Label(num_top_classes=3, label="Maladie prédite (top 3)"),
|
| 89 |
-
title="Classification des maladies des feuilles de citron",
|
| 90 |
description="Modèle MobileNet entraîné sur 9 classes (Bacterial Blight, Citrus Canker, Fungal Disease, Healthy Leaf, Lemon Scab, Mite, Spider Mites, Leaf Miner, Leaf Spot)."
|
| 91 |
)
|
| 92 |
|
|
|
|
| 1 |
# ================================
|
| 2 |
+
# 0. PATCH pour huggingface_hub
|
| 3 |
# ================================
|
| 4 |
import huggingface_hub
|
| 5 |
if not hasattr(huggingface_hub, 'HfFolder'):
|
|
|
|
| 14 |
huggingface_hub.HfFolder = HfFolder
|
| 15 |
|
| 16 |
# ================================
|
| 17 |
+
# 1. PATCH pour Gradio 4.44.0
|
|
|
|
| 18 |
# ================================
|
| 19 |
import gradio_client.utils
|
|
|
|
| 20 |
original_get_type = gradio_client.utils.get_type
|
|
|
|
| 21 |
def patched_get_type(schema):
|
| 22 |
if isinstance(schema, bool):
|
| 23 |
return "boolean"
|
| 24 |
return original_get_type(schema)
|
|
|
|
| 25 |
gradio_client.utils.get_type = patched_get_type
|
| 26 |
|
| 27 |
# ================================
|
| 28 |
+
# 2. IMPORTS
|
| 29 |
# ================================
|
| 30 |
import gradio as gr
|
| 31 |
import tensorflow as tf
|
|
|
|
| 33 |
from PIL import Image
|
| 34 |
|
| 35 |
# ================================
|
| 36 |
+
# 3. CHARGEMENT DU MODÈLE
|
| 37 |
# ================================
|
| 38 |
MODEL_PATH = "lemon_model.h5"
|
| 39 |
+
try:
|
| 40 |
+
model = tf.keras.models.load_model(MODEL_PATH, compile=False)
|
| 41 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 42 |
+
print("✅ Modèle chargé avec succès")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"❌ Erreur lors du chargement du modèle : {e}")
|
| 45 |
+
raise
|
| 46 |
|
| 47 |
# ================================
|
| 48 |
+
# 4. NOMS DES CLASSES (issus de l'entraînement)
|
| 49 |
# ================================
|
|
|
|
|
|
|
| 50 |
class_names = [
|
| 51 |
"Bacterial Blight",
|
| 52 |
"Citrus Canker",
|
|
|
|
| 60 |
]
|
| 61 |
|
| 62 |
# ================================
|
| 63 |
+
# 5. PRÉDICTION
|
| 64 |
# ================================
|
| 65 |
def preprocess_image(img):
|
|
|
|
| 66 |
img = img.resize((224, 224))
|
| 67 |
img_array = np.array(img, dtype=np.float32) / 255.0
|
| 68 |
img_array = np.expand_dims(img_array, axis=0)
|
| 69 |
return img_array
|
| 70 |
|
| 71 |
def predict(img):
|
|
|
|
| 72 |
processed = preprocess_image(img)
|
| 73 |
preds = model.predict(processed, verbose=0)[0]
|
| 74 |
+
return {class_names[i]: float(preds[i]) for i in range(len(class_names))}
|
|
|
|
| 75 |
|
| 76 |
# ================================
|
| 77 |
+
# 6. INTERFACE
|
| 78 |
# ================================
|
| 79 |
iface = gr.Interface(
|
| 80 |
fn=predict,
|
| 81 |
inputs=gr.Image(type="pil", label="Chargez une image de feuille de citron"),
|
| 82 |
outputs=gr.Label(num_top_classes=3, label="Maladie prédite (top 3)"),
|
| 83 |
+
title="🍋 Classification des maladies des feuilles de citron",
|
| 84 |
description="Modèle MobileNet entraîné sur 9 classes (Bacterial Blight, Citrus Canker, Fungal Disease, Healthy Leaf, Lemon Scab, Mite, Spider Mites, Leaf Miner, Leaf Spot)."
|
| 85 |
)
|
| 86 |
|