Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
from PIL import Image, ImageOps
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
# Charger le modèle
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
size = (224, 224)
|
16 |
-
image = ImageOps.fit(img, size, Image.Resampling.LANCZOS)
|
17 |
-
image_array = np.asarray(image)
|
18 |
-
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
index = np.argmax(prediction)
|
27 |
-
class_name = class_names[index]
|
28 |
-
confidence_score = float(prediction[0][index])
|
29 |
|
30 |
-
return
|
31 |
|
32 |
# Interface Gradio
|
33 |
-
|
34 |
-
fn=
|
35 |
-
inputs=gr.Image(type="pil"),
|
36 |
-
outputs=gr.Label(),
|
37 |
-
title="
|
38 |
-
description="
|
|
|
39 |
)
|
40 |
|
41 |
# Lancer l'application
|
42 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
|
|
3 |
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
6 |
|
7 |
+
# Charger le modèle (avec correction)
|
8 |
+
custom_objects = {"DepthwiseConv2D": tf.keras.layers.DepthwiseConv2D}
|
9 |
|
10 |
+
try:
|
11 |
+
model = load_model("keras_model.h5", custom_objects=custom_objects, compile=False)
|
12 |
+
print("✅ Modèle chargé avec succès !")
|
13 |
+
except Exception as e:
|
14 |
+
print(f"⚠️ Erreur lors du chargement du modèle : {e}")
|
15 |
|
16 |
+
# Définir les classes du modèle (à adapter selon ton dataset)
|
17 |
+
class_labels = ["Classe 1", "Classe 2", "Classe 3"] # Remplace par tes propres classes
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Fonction de prédiction
|
20 |
+
def predict(image):
|
21 |
+
img = image.resize((224, 224)) # Adapter à la taille d'entrée du modèle
|
22 |
+
img_array = img_to_array(img) / 255.0 # Normalisation
|
23 |
+
img_array = np.expand_dims(img_array, axis=0) # Ajouter une dimension batch
|
24 |
|
25 |
+
predictions = model.predict(img_array)[0] # Obtenir les prédictions
|
26 |
+
confidences = {class_labels[i]: float(predictions[i]) for i in range(len(class_labels))}
|
|
|
|
|
|
|
27 |
|
28 |
+
return confidences
|
29 |
|
30 |
# Interface Gradio
|
31 |
+
interface = gr.Interface(
|
32 |
+
fn=predict,
|
33 |
+
inputs=gr.Image(type="pil"), # Permet d'uploader une image
|
34 |
+
outputs=gr.Label(num_top_classes=3), # Affiche les probabilités des classes
|
35 |
+
title="🌟 Modèle Keras avec Gradio",
|
36 |
+
description="Téléverse une image et laisse le modèle faire sa prédiction.",
|
37 |
+
theme="compact"
|
38 |
)
|
39 |
|
40 |
# Lancer l'application
|
41 |
+
if __name__ == "__main__":
|
42 |
+
interface.launch(share=True) # `share=True` génère un lien public
|