Hemavathineelirothu's picture
Upload app.py
fa7d04e verified
raw
history blame
2.56 kB
import gradio as gr
from tensorflow import keras
import numpy as np
from PIL import Image
# Load the pre-trained Keras model
model = keras.models.load_model('c:/Users/neeli/diabet_intern/retino_model.keras') # Replace with your actual .keras model file path
# Define class names for your model predictions
class_names = ['Healthy', 'Mild DR', 'Moderate DR', 'Proliferative DR', 'Severe DR']
# Function to provide additional care information based on the predicted condition
def eye_care_recommendations(predicted_class):
recommendations = {
'Healthy': 'Your eyes seem healthy. Continue with regular eye check-ups and maintain a balanced diet.',
'Mild DR': 'Mild signs of diabetic retinopathy. Ensure strict blood sugar control and regular eye exams.',
'Moderate DR': 'Moderate diabetic retinopathy detected. Consult with an ophthalmologist for treatment options.',
'Proliferative DR': 'Advanced stage detected. Immediate medical attention is required to prevent further vision loss.',
'Severe DR': 'Severe diabetic retinopathy detected. Medical intervention is necessary. Please visit a doctor immediately.'
}
return recommendations.get(predicted_class, "No recommendation available.")
# Prediction function that processes the image and returns the result and care advice
def predict(image):
# Resize image to the expected size for the model
image = image.resize((128, 128)) # Adjust this size based on your model's input size
image = np.expand_dims(np.array(image), axis=0) # Add batch dimension
# Make a prediction
predictions = model.predict(image)
predicted_class_index = np.argmax(predictions, axis=1)[0]
predicted_class = class_names[predicted_class_index]
# Get eye care recommendations based on the prediction
care_info = eye_care_recommendations(predicted_class)
# Return the prediction and care information
return f"Predicted Condition: {predicted_class}", care_info
# Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"), # Accepts an image as input
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Eye Care Recommendations")], # Output text fields
title="Diabetic Retinopathy Prediction",
description="Upload a retinal image, and the model will predict the stage of Diabetic Retinopathy. Eye care recommendations will be provided based on the prediction."
)
# Launch the interface
if __name__ == "__main__":
interface.launch()