Elena
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,72 @@
|
|
1 |
import gradio as gr
|
2 |
from tensorflow.keras.models import load_model
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
|
|
5 |
|
|
|
6 |
model = load_model('xray_image_classifier_model.keras')
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
img =
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from tensorflow.keras.models import load_model
|
|
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load the trained model
|
7 |
model = load_model('xray_image_classifier_model.keras')
|
8 |
|
9 |
+
def predict(image):
|
10 |
+
# Preprocess the input image
|
11 |
+
img = image.resize((150, 150))
|
12 |
+
img_array = np.array(img) / 255.0
|
13 |
+
img_array = np.expand_dims(img_array, axis=0)
|
14 |
+
|
15 |
+
# Make a prediction
|
16 |
+
prediction = model.predict(img_array)
|
17 |
+
predicted_class = 'Pneumonia' if prediction > 0.5 else 'Normal'
|
18 |
+
|
19 |
+
return predicted_class
|
20 |
+
|
21 |
+
# Custom CSS for the interface
|
22 |
+
css = """
|
23 |
+
.gradio-container {
|
24 |
+
background-color: #f5f5f5;
|
25 |
+
font-family: Arial, sans-serif;
|
26 |
+
}
|
27 |
+
.gr-button {
|
28 |
+
background-color: #007bff;
|
29 |
+
color: white;
|
30 |
+
border-radius: 5px;
|
31 |
+
font-size: 16px;
|
32 |
+
}
|
33 |
+
.gr-button:hover {
|
34 |
+
background-color: #0056b3;
|
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;
|
56 |
+
text-align: center;
|
57 |
+
}
|
58 |
+
"""
|
59 |
+
|
60 |
+
# Gradio interface
|
61 |
+
with gr.Blocks(css=css) as interface:
|
62 |
+
gr.Markdown("<h1>Chest X-ray Pneumonia Classifier</h1>")
|
63 |
+
gr.Markdown("<p>Upload an X-ray image to classify it as 'Pneumonia' or 'Normal'.</p>")
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"])
|
67 |
+
output = gr.Textbox(label="Prediction", elem_classes=["gr-textbox", "gr-box-text"])
|
68 |
+
|
69 |
+
submit_btn = gr.Button("Classify X-ray", elem_classes=["gr-button"])
|
70 |
+
submit_btn.click(fn=predict, inputs=image_input, outputs=output)
|
71 |
+
|
72 |
+
interface.launch()
|