Delete app.py
Browse files
app.py
DELETED
|
@@ -1,170 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
-
from joblib import load
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
# --- Configuration & Model Loading ---
|
| 8 |
-
# Model path is confirmed to be in the root directory.
|
| 9 |
-
MODEL_PATH = "orb_bow_svm.joblib"
|
| 10 |
-
|
| 11 |
-
# --- Model Constants (should match your training environment) ---
|
| 12 |
-
IMG_SIZE = (200, 200)
|
| 13 |
-
VOCAB_SIZE = 300
|
| 14 |
-
orb = cv2.ORB_create(nfeatures=500)
|
| 15 |
-
|
| 16 |
-
# Default classes
|
| 17 |
-
DEFAULT_CLASSES = [
|
| 18 |
-
"Non Demented",
|
| 19 |
-
"Very mild Dementia",
|
| 20 |
-
"Mild Dementia",
|
| 21 |
-
"Moderate Dementia"
|
| 22 |
-
]
|
| 23 |
-
|
| 24 |
-
# --- CORRECTED & EXPANDED EXAMPLE FILES (MUST MATCH UPLOADED FILES IN ROOT) ---
|
| 25 |
-
# Includes all 7 image files found in your root directory
|
| 26 |
-
EXAMPLE_IMAGES = [
|
| 27 |
-
"mild_9.jpg",
|
| 28 |
-
"moderate_7.jpg",
|
| 29 |
-
"non_93.jpg",
|
| 30 |
-
"verymild_986.jpg",
|
| 31 |
-
"moderate_36.jpg", # Newly added file
|
| 32 |
-
"verymild_795.jpg", # Newly added file
|
| 33 |
-
"verymild_8.jpg" # Newly added file
|
| 34 |
-
]
|
| 35 |
-
# -----------------------------------------------------------------------------
|
| 36 |
-
|
| 37 |
-
# Attempt to load the model components
|
| 38 |
-
kmeans, scaler, svm = None, None, None
|
| 39 |
-
classes = DEFAULT_CLASSES
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
print(f"Attempting to load model from: {MODEL_PATH}")
|
| 43 |
-
model_data = load(MODEL_PATH)
|
| 44 |
-
kmeans = model_data["kmeans"]
|
| 45 |
-
scaler = model_data["scaler"]
|
| 46 |
-
svm = model_data["svm"]
|
| 47 |
-
classes = model_data["classes"]
|
| 48 |
-
print("Model loaded successfully!")
|
| 49 |
-
except FileNotFoundError:
|
| 50 |
-
print(f"ERROR: Model file '{MODEL_PATH}' not found.")
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"ERROR: An unexpected error occurred during model loading: {e}.")
|
| 53 |
-
|
| 54 |
-
# Define real/dummy functions based on successful load
|
| 55 |
-
if svm is None:
|
| 56 |
-
# Dummy functions for failure state
|
| 57 |
-
def encode(descriptors, kmeans_model): return np.zeros(VOCAB_SIZE)
|
| 58 |
-
def gradio_predict(input_img):
|
| 59 |
-
return "⚠️ Model not loaded. Cannot perform prediction.", {cls: 0.0 for cls in DEFAULT_CLASSES}
|
| 60 |
-
else:
|
| 61 |
-
# Real functions for success state
|
| 62 |
-
def encode(descriptors, kmeans_model):
|
| 63 |
-
if descriptors is None or len(descriptors) == 0:
|
| 64 |
-
return np.zeros(VOCAB_SIZE)
|
| 65 |
-
words = kmeans_model.predict(descriptors)
|
| 66 |
-
hist, _ = np.histogram(words, bins=np.arange(VOCAB_SIZE + 1))
|
| 67 |
-
return hist
|
| 68 |
-
|
| 69 |
-
def gradio_predict(input_img):
|
| 70 |
-
# Preprocessing
|
| 71 |
-
img = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY)
|
| 72 |
-
img = cv2.resize(img, IMG_SIZE)
|
| 73 |
-
|
| 74 |
-
# Feature Extraction
|
| 75 |
-
kps, des = orb.detectAndCompute(img, None)
|
| 76 |
-
feat = encode(des, kmeans).reshape(1, -1)
|
| 77 |
-
feat_scaled = scaler.transform(feat)
|
| 78 |
-
|
| 79 |
-
# Prediction
|
| 80 |
-
prediction_index = svm.predict(feat_scaled)[0]
|
| 81 |
-
probabilities = svm.predict_proba(feat_scaled)[0]
|
| 82 |
-
|
| 83 |
-
# Format output
|
| 84 |
-
predicted_class = classes[prediction_index]
|
| 85 |
-
confidence_score = probabilities[prediction_index] * 100
|
| 86 |
-
|
| 87 |
-
output_message = f"**Diagnosis: {predicted_class}**\n" \
|
| 88 |
-
f"Confidence: {confidence_score:.2f}%"
|
| 89 |
-
|
| 90 |
-
prob_dict = {cls: prob for cls, prob in zip(classes, probabilities)}
|
| 91 |
-
|
| 92 |
-
return output_message, prob_dict
|
| 93 |
-
|
| 94 |
-
# --- Gradio Interface Definition (Unchanged layout/design) ---
|
| 95 |
-
|
| 96 |
-
colorful_theme = gr.Theme.from_hub("gradio/seafoam")
|
| 97 |
-
|
| 98 |
-
with gr.Blocks(theme=colorful_theme, title="Dementia Stage Classifier 🧠") as demo:
|
| 99 |
-
|
| 100 |
-
gr.Markdown(
|
| 101 |
-
"""
|
| 102 |
-
# 🧠 Brain Scan Analyzer: Dementia Stage Classification 🌟
|
| 103 |
-
... (Your impressive introductory text) ...
|
| 104 |
-
"""
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
with gr.Row(variant="panel"):
|
| 108 |
-
# Input Column
|
| 109 |
-
with gr.Column(scale=1):
|
| 110 |
-
gr.Markdown("## 📤 Input Image")
|
| 111 |
-
image_input = gr.Image(
|
| 112 |
-
type="numpy",
|
| 113 |
-
label="Upload MRI Brain Scan Image",
|
| 114 |
-
height=350,
|
| 115 |
-
width=350,
|
| 116 |
-
interactive=True
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
# Action Button
|
| 120 |
-
submit_btn = gr.Button("✨ Classify Scan ✨", variant="primary", size="lg")
|
| 121 |
-
gr.Markdown("---")
|
| 122 |
-
gr.Markdown(
|
| 123 |
-
"""
|
| 124 |
-
### 💡 Quick Test Examples:
|
| 125 |
-
Click on any image below to load and classify it instantly!
|
| 126 |
-
"""
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
# This section now always works because the file paths are confirmed.
|
| 130 |
-
gr.Examples(
|
| 131 |
-
examples=EXAMPLE_IMAGES,
|
| 132 |
-
inputs=image_input,
|
| 133 |
-
outputs=[gr.Textbox(), gr.Label()],
|
| 134 |
-
fn=gradio_predict,
|
| 135 |
-
cache_examples=True,
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
# Output Column
|
| 140 |
-
with gr.Column(scale=2):
|
| 141 |
-
gr.Markdown("## ✅ Prediction Results")
|
| 142 |
-
output_text = gr.Textbox(
|
| 143 |
-
label="Predicted Dementia Stage & Confidence",
|
| 144 |
-
value="Upload an image and click 'Classify Scan' to see the results.",
|
| 145 |
-
lines=3,
|
| 146 |
-
show_copy_button=True,
|
| 147 |
-
elem_id="prediction_output_box"
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
output_label = gr.Label(
|
| 151 |
-
label="Detailed Probability Distribution",
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
gr.Markdown(
|
| 155 |
-
"""
|
| 156 |
-
---
|
| 157 |
-
### 📚 Our Methodology:
|
| 158 |
-
... (Your methodology text) ...
|
| 159 |
-
"""
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
# Connect the button to the prediction function
|
| 163 |
-
submit_btn.click(
|
| 164 |
-
fn=gradio_predict,
|
| 165 |
-
inputs=[image_input],
|
| 166 |
-
outputs=[output_text, output_label]
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
# Launch the Gradio app
|
| 170 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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