Hemavathineelirothu
commited on
Commit
•
c846681
1
Parent(s):
5f1e95f
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,51 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from tensorflow import keras
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
|
6 |
-
# Load the pre-trained Keras model
|
7 |
-
model = keras.models.load_model('
|
8 |
-
|
9 |
-
# Define class names for your model predictions
|
10 |
-
class_names = ['Healthy', 'Mild DR', 'Moderate DR', 'Proliferative DR', 'Severe DR']
|
11 |
-
|
12 |
-
# Function to provide additional care information based on the predicted condition
|
13 |
-
def eye_care_recommendations(predicted_class):
|
14 |
-
recommendations = {
|
15 |
-
'Healthy': 'Your eyes seem healthy. Continue with regular eye check-ups and maintain a balanced diet.',
|
16 |
-
'Mild DR': 'Mild signs of diabetic retinopathy. Ensure strict blood sugar control and regular eye exams.',
|
17 |
-
'Moderate DR': 'Moderate diabetic retinopathy detected. Consult with an ophthalmologist for treatment options.',
|
18 |
-
'Proliferative DR': 'Advanced stage detected. Immediate medical attention is required to prevent further vision loss.',
|
19 |
-
'Severe DR': 'Severe diabetic retinopathy detected. Medical intervention is necessary. Please visit a doctor immediately.'
|
20 |
-
}
|
21 |
-
return recommendations.get(predicted_class, "No recommendation available.")
|
22 |
-
|
23 |
-
# Prediction function that processes the image and returns the result and care advice
|
24 |
-
def predict(image):
|
25 |
-
# Resize image to the expected size for the model
|
26 |
-
image = image.resize((128, 128)) # Adjust this size based on your model's input size
|
27 |
-
image = np.expand_dims(np.array(image), axis=0) # Add batch dimension
|
28 |
-
|
29 |
-
# Make a prediction
|
30 |
-
predictions = model.predict(image)
|
31 |
-
predicted_class_index = np.argmax(predictions, axis=1)[0]
|
32 |
-
predicted_class = class_names[predicted_class_index]
|
33 |
-
|
34 |
-
# Get eye care recommendations based on the prediction
|
35 |
-
care_info = eye_care_recommendations(predicted_class)
|
36 |
-
|
37 |
-
# Return the prediction and care information
|
38 |
-
return f"Predicted Condition: {predicted_class}", care_info
|
39 |
-
|
40 |
-
# Gradio interface
|
41 |
-
interface = gr.Interface(
|
42 |
-
fn=predict,
|
43 |
-
inputs=gr.Image(type="pil"), # Accepts an image as input
|
44 |
-
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Eye Care Recommendations")], # Output text fields
|
45 |
-
title="Diabetic Retinopathy Prediction",
|
46 |
-
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."
|
47 |
-
)
|
48 |
-
|
49 |
-
# Launch the interface
|
50 |
-
if __name__ == "__main__":
|
51 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow import keras
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load the pre-trained Keras model
|
7 |
+
model = keras.models.load_model('retino_model.keras')
|
8 |
+
|
9 |
+
# Define class names for your model predictions
|
10 |
+
class_names = ['Healthy', 'Mild DR', 'Moderate DR', 'Proliferative DR', 'Severe DR']
|
11 |
+
|
12 |
+
# Function to provide additional care information based on the predicted condition
|
13 |
+
def eye_care_recommendations(predicted_class):
|
14 |
+
recommendations = {
|
15 |
+
'Healthy': 'Your eyes seem healthy. Continue with regular eye check-ups and maintain a balanced diet.',
|
16 |
+
'Mild DR': 'Mild signs of diabetic retinopathy. Ensure strict blood sugar control and regular eye exams.',
|
17 |
+
'Moderate DR': 'Moderate diabetic retinopathy detected. Consult with an ophthalmologist for treatment options.',
|
18 |
+
'Proliferative DR': 'Advanced stage detected. Immediate medical attention is required to prevent further vision loss.',
|
19 |
+
'Severe DR': 'Severe diabetic retinopathy detected. Medical intervention is necessary. Please visit a doctor immediately.'
|
20 |
+
}
|
21 |
+
return recommendations.get(predicted_class, "No recommendation available.")
|
22 |
+
|
23 |
+
# Prediction function that processes the image and returns the result and care advice
|
24 |
+
def predict(image):
|
25 |
+
# Resize image to the expected size for the model
|
26 |
+
image = image.resize((128, 128)) # Adjust this size based on your model's input size
|
27 |
+
image = np.expand_dims(np.array(image), axis=0) # Add batch dimension
|
28 |
+
|
29 |
+
# Make a prediction
|
30 |
+
predictions = model.predict(image)
|
31 |
+
predicted_class_index = np.argmax(predictions, axis=1)[0]
|
32 |
+
predicted_class = class_names[predicted_class_index]
|
33 |
+
|
34 |
+
# Get eye care recommendations based on the prediction
|
35 |
+
care_info = eye_care_recommendations(predicted_class)
|
36 |
+
|
37 |
+
# Return the prediction and care information
|
38 |
+
return f"Predicted Condition: {predicted_class}", care_info
|
39 |
+
|
40 |
+
# Gradio interface
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=predict,
|
43 |
+
inputs=gr.Image(type="pil"), # Accepts an image as input
|
44 |
+
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Eye Care Recommendations")], # Output text fields
|
45 |
+
title="Diabetic Retinopathy Prediction",
|
46 |
+
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."
|
47 |
+
)
|
48 |
+
|
49 |
+
# Launch the interface
|
50 |
+
if __name__ == "__main__":
|
51 |
+
interface.launch()
|