|
|
|
|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import cv2 |
|
|
from tensorflow.keras.models import model_from_json |
|
|
import os |
|
|
|
|
|
print("β
Gradio app: Starting up...") |
|
|
|
|
|
|
|
|
|
|
|
model_architecture_path = './model_architecture.json' |
|
|
model_weights_path = './model_weights.weights.h5' |
|
|
|
|
|
model = None |
|
|
|
|
|
try: |
|
|
print("β
Gradio app: Attempting to load model from JSON and H5 weights...") |
|
|
if os.path.exists(model_architecture_path) and os.path.exists(model_weights_path): |
|
|
with open(model_architecture_path, 'r') as json_file: |
|
|
loaded_model_json = json_file.read() |
|
|
|
|
|
|
|
|
|
|
|
model = model_from_json(loaded_model_json) |
|
|
model.load_weights(model_weights_path) |
|
|
print("β
Gradio app: Model loaded successfully from JSON and H5 weights") |
|
|
else: |
|
|
print("β Gradio app: Model architecture or H5 weights file not found.") |
|
|
|
|
|
|
|
|
except Exception as load_e: |
|
|
print(f"β Gradio app: Error loading model: {load_e}") |
|
|
model = None |
|
|
|
|
|
|
|
|
if model is None: |
|
|
print("π Gradio app: Model could not be loaded. Prediction function will not work.") |
|
|
|
|
|
|
|
|
def predict_cataract(image): |
|
|
"""Predict cataract with the loaded model""" |
|
|
if model is None: |
|
|
return "Error: Model could not be loaded. Cannot make prediction." |
|
|
|
|
|
try: |
|
|
|
|
|
img_array = np.array(image) |
|
|
|
|
|
if img_array.shape[-1] == 4: |
|
|
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGBA2RGB) |
|
|
elif len(img_array.shape) == 2: |
|
|
img_array = cv2.cvtColor(img_array, cv2.COLOR_GRAY2RGB) |
|
|
|
|
|
img_array = cv2.resize(img_array, (224, 224)) |
|
|
img_array = img_array.astype('float32') / 255.0 |
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
|
|
|
prediction = model.predict(img_array)[0][0] |
|
|
|
|
|
|
|
|
probability = float(prediction) |
|
|
class_name = "Normal" if probability < 0.5 else "Cataract" |
|
|
|
|
|
confidence = probability if class_name == "Cataract" else (1 - probability) |
|
|
confidence_percent = confidence * 100 |
|
|
|
|
|
|
|
|
result = f""" |
|
|
π¨ **OVERFITTED MODEL WARNING** π¨ |
|
|
This model has data leakage - results are unreliable! |
|
|
|
|
|
π **Prediction**: {class_name} |
|
|
π **Confidence**: {confidence_percent:.1f}% |
|
|
π **Raw Score**: {probability:.4f} |
|
|
|
|
|
β οΈ **Do NOT trust these results for medical decisions!** |
|
|
This is for educational demonstration only. |
|
|
""" |
|
|
|
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error during prediction: {str(e)}" |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=predict_cataract, |
|
|
inputs=gr.Image(type="pil", label="Upload Eye Image"), |
|
|
outputs=gr.Textbox(label="Overfitted Prediction (Unreliable!)"), |
|
|
title="ποΈ Cataract Detection - OVERFITTED BEAST π¨", |
|
|
description=""" |
|
|
**β οΈ WARNING: This model has intentional data leakage!** |
|
|
|
|
|
This demonstrates what happens when ML models "cheat" by seeing the same data during training and validation. |
|
|
The high accuracy (96.7%) is FAKE and doesn't represent real medical AI capability. |
|
|
|
|
|
π― **Educational Purpose**: Show the importance of proper data splitting in medical AI. |
|
|
π₯ **Real Medical AI**: Typically achieves 80-90% accuracy with proper validation. |
|
|
""", |
|
|
|
|
|
|
|
|
examples=[], |
|
|
theme=gr.themes.Soft() |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print(" |
|
|
π Gradio app: Launching interface...") |
|
|
demo.launch() |
|
|
|