Update app.py
Browse files- 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
|
3 |
-
from PIL import Image, ImageOps
|
4 |
-
import numpy as np
|
5 |
-
from tensorflow.keras.layers import DepthwiseConv2D
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
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)
|