asmaa1's picture
Update app.py
81a96fd verified
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import gradio as gr
from gradio.themes.base import Base
import cv2
from tensorflow.keras.models import load_model
model_path = "xray_model.h5"
model = load_model(model_path)
def load_and_prepare_image(image_path):
# Load the image
img = cv2.imread(image_path)
# Resize the image to the input shape of the model
img = cv2.resize(img, (256, 256))
# Normalize the image
img = img / 255.0
# Expand dimensions to match the input shape of the model
img = np.expand_dims(img, axis=0)
return img
def predict(image_path):
if image_path is None:
return "Please upload an image.", "How-AI-is-Used-in-Healthcare.png"
# Prepare the image
img = load_and_prepare_image(image_path)
# Make the prediction
prediction = model.predict(img)
# Convert prediction to descriptive sentence
label = (
'The X-ray indicates that the patient likely has pneumonia.'
if prediction[0][0] > 0.9
else 'The X-ray indicates that the patient\'s lungs are normal.'
)
return label, "How-AI-is-Used-in-Healthcare.png"
# Fixed image URL
fixed_image_url = "How-AI-is-Used-in-Healthcare.png"
# Example images and their descriptions
examples = [
["normal.jpeg", "Normal X-ray image."],
["pne.jpeg", "X-ray image indicating pneumonia."]
]
# Create the Gradio interface
iface = gr.Interface(
theme=gr.themes.Soft(),
fn=predict,
inputs=gr.Image(type="filepath", label="Upload Image"),
outputs=[
gr.Textbox(label="Prediction"),
gr.Image(value=fixed_image_url, label="Always ready to assist!") # Display the fixed image
],
title="Pneumonia Classification",
description="Please upload a chest X-ray image.",
examples=examples,
)
# Launch the interface
iface.launch(share=True)