walidchaib commited on
Commit
971c16e
·
verified ·
1 Parent(s): 2eb7b59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ================================
2
+ # 0. PATCH pour huggingface_hub (contourne l'absence de HfFolder)
3
+ # ================================
4
+ import huggingface_hub
5
+ if not hasattr(huggingface_hub, 'HfFolder'):
6
+ class HfFolder:
7
+ _token = None
8
+ @staticmethod
9
+ def get_token():
10
+ return HfFolder._token
11
+ @staticmethod
12
+ def save_token(token):
13
+ HfFolder._token = token
14
+ huggingface_hub.HfFolder = HfFolder
15
+
16
+ # ================================
17
+ # 1. PATCH pour contourner le bug de Gradio 4.44.0
18
+ # ================================
19
+ import gradio_client.utils
20
+
21
+ original_get_type = gradio_client.utils.get_type
22
+
23
+ def patched_get_type(schema):
24
+ if isinstance(schema, bool):
25
+ return "boolean"
26
+ return original_get_type(schema)
27
+
28
+ gradio_client.utils.get_type = patched_get_type
29
+
30
+ # ================================
31
+ # 2. IMPORTS STANDARDS
32
+ # ================================
33
+ import gradio as gr
34
+ import tensorflow as tf
35
+ import numpy as np
36
+ from PIL import Image
37
+
38
+ # ================================
39
+ # 3. CHARGEMENT DU MODÈLE (9 classes)
40
+ # ================================
41
+ MODEL_PATH = "final_model.keras"
42
+ model = tf.keras.models.load_model(MODEL_PATH, compile=False)
43
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
44
+
45
+ # ================================
46
+ # 4. NOMS DES CLASSES (issus de l'entraînement)
47
+ # ================================
48
+ class_names = [
49
+ "Chinee apple",
50
+ "Lantana",
51
+ "Negative",
52
+ "Parkinsonia",
53
+ "Parthenium",
54
+ "Prickly acacia",
55
+ "Rubber vine",
56
+ "Siam weed",
57
+ "Snake weed"
58
+ ]
59
+
60
+ # ================================
61
+ # 5. PARAMÈTRES
62
+ # ================================
63
+ IMG_SIZE = 380 # taille utilisée lors de l'entraînement
64
+
65
+ # ================================
66
+ # 6. PRÉTRAITEMENT (identique à l'entraînement)
67
+ # ================================
68
+ def preprocess_image(img):
69
+ img = img.resize((IMG_SIZE, IMG_SIZE))
70
+ img_array = np.array(img)
71
+ # Appliquer le même preprocess_input que EfficientNetB4
72
+ img_array = tf.keras.applications.efficientnet.preprocess_input(img_array)
73
+ img_array = np.expand_dims(img_array, axis=0)
74
+ return img_array
75
+
76
+ def predict(img):
77
+ processed = preprocess_image(img)
78
+ preds = model.predict(processed, verbose=0)[0]
79
+ results = {class_names[i]: float(preds[i]) for i in range(len(class_names))}
80
+ return results
81
+
82
+ # ================================
83
+ # 7. INTERFACE GRADIO
84
+ # ================================
85
+ iface = gr.Interface(
86
+ fn=predict,
87
+ inputs=gr.Image(type="pil", label="Chargez une image de mauvaise herbe"),
88
+ outputs=gr.Label(num_top_classes=3, label="Espèce prédite (top 3)"),
89
+ title="🌿 Classification des mauvaises herbes",
90
+ description="Modèle EfficientNetB4 entraîné sur 9 espèces de mauvaises herbes (Chinee apple, Lantana, Negative, Parkinsonia, Parthenium, Prickly acacia, Rubber vine, Siam weed, Snake weed)."
91
+ )
92
+
93
+ if __name__ == "__main__":
94
+ iface.launch(share=True)