|
import gradio as gr |
|
from tensorflow.keras.models import load_model |
|
import numpy as np |
|
from PIL import Image |
|
|
|
model = load_model('xray_image_classifier_model.keras') |
|
|
|
def predict(image): |
|
img = image.resize((150, 150)) |
|
img_array = np.array(img) / 255.0 |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
prediction = model.predict(img_array) |
|
predicted_class = 'Pneumonia' if prediction > 0.5 else 'Normal' |
|
|
|
return predicted_class |
|
|
|
|
|
css = """ |
|
.gradio-container { |
|
background-color: #f5f5f5; |
|
font-family: Arial, sans-serif; |
|
} |
|
.gr-button { |
|
background-color: #007bff; |
|
color: white; |
|
border-radius: 5px; |
|
font-size: 16px; |
|
} |
|
.gr-button:hover { |
|
background-color: #0056b3; |
|
} |
|
.gr-textbox, .gr-image { |
|
border: 2px dashed #007bff; |
|
padding: 20px; |
|
border-radius: 10px; |
|
background-color: #ffffff; |
|
} |
|
.gr-box-text { |
|
color: #007bff; |
|
font-size: 22px; |
|
font-weight: bold; |
|
text-align: center; |
|
} |
|
h1 { |
|
font-size: 36px; |
|
color: #007bff; |
|
text-align: center; |
|
} |
|
p { |
|
font-size: 20px; |
|
color: #333; |
|
text-align: center; |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(css=css) as interface: |
|
gr.Markdown("<h1>Chest X-ray Pneumonia Classifier</h1>") |
|
gr.Markdown("<p>Upload an X-ray image to classify it as 'Pneumonia' or 'Normal'.</p>") |
|
|
|
with gr.Row(): |
|
image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"]) |
|
output = gr.Textbox(label="Prediction", elem_classes=["gr-textbox", "gr-box-text"]) |
|
|
|
submit_btn = gr.Button("Classify X-ray", elem_classes=["gr-button"]) |
|
submit_btn.click(fn=predict, inputs=image_input, outputs=output) |
|
|
|
interface.launch() |
|
|