walidchaib commited on
Commit
0dc7e53
·
verified ·
1 Parent(s): 00eab23

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # (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 STANDARDS
33
+ # ================================
34
+ import gradio as gr
35
+ import numpy as np
36
+ from PIL import Image
37
+ from ultralytics import YOLO
38
+
39
+ # ================================
40
+ # 3. CHARGEMENT DU MODÈLE (YOLOv8 classification)
41
+ # ================================
42
+ MODEL_PATH = "best.pt" # le modèle doit être présent à la racine
43
+ model = YOLO(MODEL_PATH)
44
+
45
+ # ================================
46
+ # 4. FONCTION DE PRÉDICTION
47
+ # ================================
48
+ def predict(image):
49
+ """
50
+ Prend une image PIL, retourne un dictionnaire {classe: probabilité}
51
+ """
52
+ # Redimensionner à 224x224 (taille utilisée lors de l'entraînement)
53
+ image = image.resize((224, 224))
54
+ # Convertir en numpy array
55
+ img_np = np.array(image)
56
+ # Faire la prédiction avec YOLO
57
+ results = model.predict(img_np, verbose=False)
58
+ probs = results[0].probs # objet Probs
59
+ # Les probabilités sont dans probs.data (tensor)
60
+ prob_weed = float(probs.data[1].cpu().numpy()) # classe 1 = weed
61
+ prob_crop = float(probs.data[0].cpu().numpy()) # classe 0 = crop
62
+ return {"crop": prob_crop, "weed": prob_weed}
63
+
64
+ # ================================
65
+ # 5. INTERFACE GRADIO
66
+ # ================================
67
+ iface = gr.Interface(
68
+ fn=predict,
69
+ inputs=gr.Image(type="pil", label="Chargez une image de champ"),
70
+ outputs=gr.Label(num_top_classes=2, label="Prédiction"),
71
+ title="🌾 Classification Culture / Mauvaise Herbe (YOLOv8)",
72
+ description="Modèle YOLOv8-cls entraîné sur le dataset DeepWeeds pour distinguer les cultures des mauvaises herbes."
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ iface.launch(share=True)