Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,30 @@ from tensorflow.keras.models import load_model
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
|
|
|
| 6 |
model = load_model('xray_image_classifier_model.keras')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def predict(image):
|
| 9 |
img = image.resize((150, 150))
|
| 10 |
img_array = np.array(img) / 255.0
|
| 11 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
| 12 |
prediction = model.predict(img_array)
|
| 13 |
-
predicted_class =
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
css = """
|
| 17 |
.gradio-container {
|
| 18 |
background-color: #f5f5f5;
|
|
@@ -20,36 +34,40 @@ css = """
|
|
| 20 |
}
|
| 21 |
|
| 22 |
.gr-button {
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
}
|
| 32 |
|
| 33 |
.gr-button:hover {
|
| 34 |
-
|
| 35 |
}
|
|
|
|
| 36 |
.gr-textbox, .gr-image {
|
| 37 |
border: 2px dashed #007bff;
|
| 38 |
padding: 20px;
|
| 39 |
border-radius: 10px;
|
| 40 |
background-color: #ffffff;
|
| 41 |
}
|
|
|
|
| 42 |
.gr-box-text {
|
| 43 |
color: #007bff;
|
| 44 |
font-size: 22px;
|
| 45 |
font-weight: bold;
|
| 46 |
text-align: center;
|
| 47 |
}
|
|
|
|
| 48 |
h1 {
|
| 49 |
font-size: 36px;
|
| 50 |
-
color: #007bff;
|
| 51 |
text-align: center;
|
| 52 |
}
|
|
|
|
| 53 |
p {
|
| 54 |
font-size: 20px;
|
| 55 |
color: #333;
|
|
@@ -57,6 +75,7 @@ css = """
|
|
| 57 |
}
|
| 58 |
"""
|
| 59 |
|
|
|
|
| 60 |
description = """
|
| 61 |
**Automated Pneumonia Detection via Chest X-ray Classification**
|
| 62 |
|
|
@@ -67,34 +86,22 @@ This model leverages deep learning techniques to classify chest X-ray images as
|
|
| 67 |
- InceptionV3 for transfer learning
|
| 68 |
- Numpy, Pandas, and Matplotlib for data handling and visualization
|
| 69 |
- Flask and Gradio for deployment and user interaction
|
| 70 |
-
|
| 71 |
"""
|
| 72 |
|
| 73 |
-
|
| 74 |
-
["samples/normal_xray1.png"],
|
| 75 |
-
["samples/pneumonia_xray1.png"],
|
| 76 |
-
]
|
| 77 |
-
|
| 78 |
with gr.Blocks(css=css) as interface:
|
| 79 |
gr.Markdown("<h1>Automated Pneumonia Detection via Chest X-ray Classification</h1>")
|
| 80 |
gr.Markdown("<p>Submit a chest X-ray image below.</p>")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"])
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
submit_btn = gr.Button("Initiate Diagnostic Analysis", elem_classes=["gr-button"])
|
| 87 |
-
submit_btn.click(fn=predict, inputs=image_input, outputs=
|
| 88 |
-
|
| 89 |
-
gr.Markdown(
|
| 90 |
-
'<div style="background-color: yellow; padding: 10px; border-radius: 5px; font-weight: bold;">'
|
| 91 |
-
'Sample Images: To test the model, select one of the sample images provided below. '
|
| 92 |
-
'Click on an image and then press the "Initiate Diagnostic Analysis" button to receive the results.'
|
| 93 |
-
'</div>'
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
gr.Examples(examples=examples, inputs=image_input)
|
| 97 |
|
| 98 |
gr.Markdown(description)
|
| 99 |
|
|
|
|
| 100 |
interface.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
model = load_model('xray_image_classifier_model.keras')
|
| 8 |
|
| 9 |
+
# Define solutions
|
| 10 |
+
solutions = {
|
| 11 |
+
"Pneumonia": "Consult a doctor immediately. Follow prescribed antibiotics if given, rest well, and stay hydrated.",
|
| 12 |
+
"Normal": "Your X-ray appears normal. However, if you experience symptoms, consult a doctor for further evaluation."
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Prediction function
|
| 16 |
def predict(image):
|
| 17 |
img = image.resize((150, 150))
|
| 18 |
img_array = np.array(img) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
+
|
| 21 |
prediction = model.predict(img_array)
|
| 22 |
+
predicted_class = "Pneumonia" if prediction > 0.5 else "Normal"
|
| 23 |
+
|
| 24 |
+
# Get the corresponding solution
|
| 25 |
+
solution = solutions.get(predicted_class, "No specific advice available.")
|
| 26 |
+
|
| 27 |
+
return predicted_class, solution
|
| 28 |
|
| 29 |
+
# CSS Styling
|
| 30 |
css = """
|
| 31 |
.gradio-container {
|
| 32 |
background-color: #f5f5f5;
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
.gr-button {
|
| 37 |
+
background-color:#007bff;
|
| 38 |
+
color: white;
|
| 39 |
+
border: none;
|
| 40 |
+
border-radius: 5px;
|
| 41 |
+
font-size: 16px;
|
| 42 |
+
padding: 10px 20px;
|
| 43 |
+
cursor: pointer;
|
| 44 |
+
transition: background-color 0.3s ease;
|
| 45 |
}
|
| 46 |
|
| 47 |
.gr-button:hover {
|
| 48 |
+
background-color: #0056b3;
|
| 49 |
}
|
| 50 |
+
|
| 51 |
.gr-textbox, .gr-image {
|
| 52 |
border: 2px dashed #007bff;
|
| 53 |
padding: 20px;
|
| 54 |
border-radius: 10px;
|
| 55 |
background-color: #ffffff;
|
| 56 |
}
|
| 57 |
+
|
| 58 |
.gr-box-text {
|
| 59 |
color: #007bff;
|
| 60 |
font-size: 22px;
|
| 61 |
font-weight: bold;
|
| 62 |
text-align: center;
|
| 63 |
}
|
| 64 |
+
|
| 65 |
h1 {
|
| 66 |
font-size: 36px;
|
| 67 |
+
color: #007bff;
|
| 68 |
text-align: center;
|
| 69 |
}
|
| 70 |
+
|
| 71 |
p {
|
| 72 |
font-size: 20px;
|
| 73 |
color: #333;
|
|
|
|
| 75 |
}
|
| 76 |
"""
|
| 77 |
|
| 78 |
+
# Description
|
| 79 |
description = """
|
| 80 |
**Automated Pneumonia Detection via Chest X-ray Classification**
|
| 81 |
|
|
|
|
| 86 |
- InceptionV3 for transfer learning
|
| 87 |
- Numpy, Pandas, and Matplotlib for data handling and visualization
|
| 88 |
- Flask and Gradio for deployment and user interaction
|
|
|
|
| 89 |
"""
|
| 90 |
|
| 91 |
+
# Gradio UI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
with gr.Blocks(css=css) as interface:
|
| 93 |
gr.Markdown("<h1>Automated Pneumonia Detection via Chest X-ray Classification</h1>")
|
| 94 |
gr.Markdown("<p>Submit a chest X-ray image below.</p>")
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"])
|
| 98 |
+
output_prediction = gr.Textbox(label="Model Analysis Output", elem_classes=["gr-textbox", "gr-box-text"])
|
| 99 |
+
output_solution = gr.Textbox(label="Recommended Solution", elem_classes=["gr-textbox", "gr-box-text"])
|
| 100 |
|
| 101 |
submit_btn = gr.Button("Initiate Diagnostic Analysis", elem_classes=["gr-button"])
|
| 102 |
+
submit_btn.click(fn=predict, inputs=image_input, outputs=[output_prediction, output_solution])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
gr.Markdown(description)
|
| 105 |
|
| 106 |
+
# Launch the app
|
| 107 |
interface.launch()
|