Update app.py
Browse files
app.py
CHANGED
|
@@ -3,88 +3,32 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load the model
|
| 7 |
-
model = tf.keras.models.load_model(
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
CLASS_NAMES = ['Gingivitis', 'Caries']
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Resize image
|
| 24 |
-
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 25 |
-
|
| 26 |
-
# Convert to array
|
| 27 |
-
img_array = np.array(image)
|
| 28 |
-
|
| 29 |
-
# Add batch dimension and normalize
|
| 30 |
-
img_array = np.expand_dims(img_array, 0)
|
| 31 |
-
img_array = img_array.astype('float32') / 255.0
|
| 32 |
-
|
| 33 |
-
return img_array
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return {
|
| 43 |
-
CLASS_NAMES[0]: 0.0,
|
| 44 |
-
CLASS_NAMES[1]: 0.0
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
# Make prediction
|
| 48 |
-
prediction = model.predict(processed_image, verbose=0)
|
| 49 |
-
probability = prediction[0][0]
|
| 50 |
-
|
| 51 |
-
# Create response dictionary
|
| 52 |
-
results = {
|
| 53 |
-
CLASS_NAMES[0]: float(1 - probability),
|
| 54 |
-
CLASS_NAMES[1]: float(probability)
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
return results
|
| 58 |
-
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"Error in prediction: {str(e)}")
|
| 61 |
-
return {
|
| 62 |
-
CLASS_NAMES[0]: 0.0,
|
| 63 |
-
CLASS_NAMES[1]: 0.0
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
# Create the Gradio interface
|
| 67 |
-
iface = gr.Interface(
|
| 68 |
-
fn=predict_dental_condition,
|
| 69 |
-
inputs=gr.Image(type="pil"),
|
| 70 |
-
outputs=gr.Label(num_top_classes=2),
|
| 71 |
-
title="Dental Condition Classifier",
|
| 72 |
-
description="""Upload an image of a dental condition to classify between Gingivitis and Caries.
|
| 73 |
-
This model helps identify dental conditions based on visual examination.""",
|
| 74 |
-
article="""
|
| 75 |
-
<div style="text-align: center;">
|
| 76 |
-
<p><b>How to use:</b></p>
|
| 77 |
-
<ol style="text-align: left;">
|
| 78 |
-
<li>Upload a clear image of the dental condition</li>
|
| 79 |
-
<li>Wait for the model to process the image</li>
|
| 80 |
-
<li>View the prediction results and confidence scores</li>
|
| 81 |
-
</ol>
|
| 82 |
-
<p><b>Note:</b> This is an AI assistance tool and should not replace professional medical advice.</p>
|
| 83 |
-
</div>
|
| 84 |
-
""",
|
| 85 |
-
theme=gr.themes.Soft()
|
| 86 |
)
|
| 87 |
|
| 88 |
# Launch the app
|
| 89 |
if __name__ == "__main__":
|
| 90 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load the saved model
|
| 7 |
+
model = tf.keras.models.load_model("saved_model_cnn.keras")
|
| 8 |
|
| 9 |
+
# Define the classes
|
| 10 |
+
classes = ['Gingivitis', 'Caries']
|
|
|
|
| 11 |
|
| 12 |
+
# Prediction function
|
| 13 |
+
def predict(image):
|
| 14 |
+
image = Image.fromarray(np.uint8(image)).convert("RGB")
|
| 15 |
+
image = image.resize((224, 224)) # Assuming the model expects 224x224 images
|
| 16 |
+
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
|
| 17 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 18 |
+
prediction = model.predict(image_array)
|
| 19 |
+
predicted_class = classes[np.argmax(prediction)]
|
| 20 |
+
confidence = np.max(prediction)
|
| 21 |
+
return f"Class: {predicted_class}, Confidence: {confidence:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Gradio interface
|
| 24 |
+
interface = gr.Interface(
|
| 25 |
+
fn=predict,
|
| 26 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 27 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 28 |
+
title="Dental Health Predictor",
|
| 29 |
+
description="Upload an image to predict between Gingivitis and Caries."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
# Launch the app
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
interface.launch()
|