Mister56 commited on
Commit
de07cc0
·
verified ·
1 Parent(s): 8b4737b

Update app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py → app.py +49 -55
gradio_app.py → app.py RENAMED
@@ -1,56 +1,50 @@
1
- import gradio as gr
2
- from tensorflow.keras.models import load_model # type: ignore
3
- from PIL import Image, ImageOps # Install pillow instead of PIL
4
- import numpy as np
5
- from tensorflow.keras.layers import DepthwiseConv2D # type: ignore
6
-
7
- # Définir la couche sans le paramètre 'groups'
8
- def custom_depthwise_conv2d(*args, **kwargs):
9
- if 'groups' in kwargs:
10
- del kwargs['groups'] # Retirer 'groups'
11
- return DepthwiseConv2D(*args, **kwargs)
12
-
13
- # Charger le modèle
14
- model = load_model("models/keras_model.h5", custom_objects={'DepthwiseConv2D': custom_depthwise_conv2d}, compile=False)
15
-
16
- # Charger les étiquettes
17
- with open("labels.txt", "r") as file:
18
- class_names = file.readlines()
19
-
20
- # Fonction pour prédire la classe d'une image
21
- def predict(image):
22
- # Redimensionner l'image à 224x224
23
- image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
24
-
25
- # Convertir l'image en tableau numpy
26
- image_array = np.asarray(image)
27
-
28
- # Normaliser l'image
29
- normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
30
-
31
- # Créer le tableau de données pour le modèle
32
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
33
- data[0] = normalized_image_array
34
-
35
- # Prédire avec le modèle
36
- prediction = model.predict(data)
37
- index = np.argmax(prediction)
38
- class_name = class_names[index].strip() # Supprimer les espaces supplémentaires
39
- confidence_score = prediction[0][index]
40
-
41
- return class_name, confidence_score
42
-
43
- # Créer l'interface Gradio
44
- iface = gr.Interface(
45
- fn=predict,
46
- inputs=gr.Image(type="pil", label="Téléchargez une image"),
47
- outputs=[
48
- gr.Label(label="Classe Prédite"),
49
- gr.Number(label="Score de Confiance")
50
- ],
51
- title="Medical Assistant",
52
- description="Téléchargez une image dentaire et notre application prédira sa classe."
53
- )
54
-
55
- # Lancer l'interface
56
  iface.launch(api_show=False)
 
1
+ import gradio as gr
2
+ from tensorflow.keras.models import load_model
3
+ from PIL import Image, ImageOps
4
+ import numpy as np
5
+ from tensorflow.keras.layers import DepthwiseConv2D
6
+
7
+ def custom_depthwise_conv2d(*args, **kwargs):
8
+ if 'groups' in kwargs:
9
+ del kwargs['groups'] # Retirer 'groups'
10
+ return DepthwiseConv2D(*args, **kwargs)
11
+
12
+ # Load model
13
+ model = load_model("keras_model.h5", custom_objects={'DepthwiseConv2D': custom_depthwise_conv2d}, compile=False)
14
+
15
+ # Load labels
16
+ with open("labels.txt", "r") as file:
17
+ class_names = file.readlines()
18
+
19
+ # Create predict fonction
20
+ def predict(image):
21
+ image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
22
+
23
+ image_array = np.asarray(image)
24
+
25
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
26
+
27
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
28
+ data[0] = normalized_image_array
29
+
30
+ # Make prediction
31
+ prediction = model.predict(data)
32
+ index = np.argmax(prediction)
33
+ class_name = class_names[index].strip()
34
+ confidence_score = prediction[0][index]
35
+
36
+ return class_name, confidence_score
37
+
38
+ # Créer l'interface Gradio
39
+ iface = gr.Interface(
40
+ fn=predict,
41
+ inputs=gr.Image(type="pil", label="Download image"),
42
+ outputs=[
43
+ gr.Label(label="Class predict"),
44
+ gr.Number(label="Confidence Score")
45
+ ],
46
+ title="Medical Assistant",
47
+ description="Upload a dental image and our app will predict its class."
48
+ )
49
+
 
 
 
 
 
 
50
  iface.launch(api_show=False)