Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,41 +10,42 @@ from sklearn.metrics import accuracy_score
|
|
| 10 |
import joblib
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
# ---------------------------
|
| 14 |
-
# 1. Train or Load Model
|
| 15 |
-
# ---------------------------
|
| 16 |
MODEL_FILE = "brain_tumor_model.pkl"
|
| 17 |
|
| 18 |
if not os.path.exists(MODEL_FILE):
|
| 19 |
np.random.seed(42)
|
| 20 |
n_samples = 500
|
| 21 |
|
| 22 |
-
# Simulated dataset
|
| 23 |
X = pd.DataFrame({
|
| 24 |
"tumor_size": np.random.uniform(1, 10, n_samples), # cm
|
| 25 |
-
"texture_score": np.random.uniform(0, 1, n_samples), #
|
| 26 |
-
"age": np.random.randint(20, 80, n_samples), #
|
| 27 |
"contrast_intensity": np.random.uniform(50, 200, n_samples), # MRI contrast
|
| 28 |
})
|
| 29 |
|
| 30 |
-
#
|
| 31 |
y = (
|
| 32 |
(X["tumor_size"] > 5).astype(int) |
|
| 33 |
(X["contrast_intensity"] > 120).astype(int)
|
| 34 |
)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 38 |
-
X, y, test_size=0.2, random_state=42
|
| 39 |
)
|
| 40 |
|
| 41 |
-
pipeline = Pipeline(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
]
|
| 47 |
-
)
|
| 48 |
|
| 49 |
pipeline.fit(X_train, y_train)
|
| 50 |
|
|
@@ -53,20 +54,14 @@ if not os.path.exists(MODEL_FILE):
|
|
| 53 |
|
| 54 |
joblib.dump(pipeline, MODEL_FILE)
|
| 55 |
|
| 56 |
-
# Load
|
| 57 |
model = joblib.load(MODEL_FILE)
|
| 58 |
|
| 59 |
-
# ---------------------------
|
| 60 |
-
# 2. Prediction Function
|
| 61 |
-
# ---------------------------
|
| 62 |
def predict_brain_tumor(tumor_size, texture_score, age, contrast_intensity):
|
| 63 |
input_data = np.array([[tumor_size, texture_score, age, contrast_intensity]])
|
| 64 |
pred = model.predict(input_data)[0]
|
| 65 |
return "🧠 Tumor Detected" if pred == 1 else "✅ No Tumor Detected"
|
| 66 |
|
| 67 |
-
# ---------------------------
|
| 68 |
-
# 3. Gradio UI
|
| 69 |
-
# ---------------------------
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("# 🧠 Brain Tumor Detection")
|
| 72 |
gr.Markdown("Predict brain tumor presence using MRI-based features (simulated demo).")
|
|
|
|
| 10 |
import joblib
|
| 11 |
import os
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
MODEL_FILE = "brain_tumor_model.pkl"
|
| 14 |
|
| 15 |
if not os.path.exists(MODEL_FILE):
|
| 16 |
np.random.seed(42)
|
| 17 |
n_samples = 500
|
| 18 |
|
| 19 |
+
# Simulated dataset
|
| 20 |
X = pd.DataFrame({
|
| 21 |
"tumor_size": np.random.uniform(1, 10, n_samples), # cm
|
| 22 |
+
"texture_score": np.random.uniform(0, 1, n_samples), # texture feature
|
| 23 |
+
"age": np.random.randint(20, 80, n_samples), # age
|
| 24 |
"contrast_intensity": np.random.uniform(50, 200, n_samples), # MRI contrast
|
| 25 |
})
|
| 26 |
|
| 27 |
+
# Create balanced tumor labels
|
| 28 |
y = (
|
| 29 |
(X["tumor_size"] > 5).astype(int) |
|
| 30 |
(X["contrast_intensity"] > 120).astype(int)
|
| 31 |
)
|
| 32 |
+
|
| 33 |
+
# Force balance: make half tumor, half no tumor
|
| 34 |
+
half = n_samples // 2
|
| 35 |
+
y[:half] = 0
|
| 36 |
+
y[half:] = 1
|
| 37 |
+
|
| 38 |
+
print("Class distribution:", np.bincount(y))
|
| 39 |
|
| 40 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 41 |
+
X, y, test_size=0.2, random_state=42, stratify=y
|
| 42 |
)
|
| 43 |
|
| 44 |
+
pipeline = Pipeline([
|
| 45 |
+
("scaler", StandardScaler()),
|
| 46 |
+
("select", SelectKBest(score_func=f_classif, k=3)),
|
| 47 |
+
("classifier", RandomForestClassifier(n_estimators=200, random_state=42)),
|
| 48 |
+
])
|
|
|
|
|
|
|
| 49 |
|
| 50 |
pipeline.fit(X_train, y_train)
|
| 51 |
|
|
|
|
| 54 |
|
| 55 |
joblib.dump(pipeline, MODEL_FILE)
|
| 56 |
|
| 57 |
+
# Load model
|
| 58 |
model = joblib.load(MODEL_FILE)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
def predict_brain_tumor(tumor_size, texture_score, age, contrast_intensity):
|
| 61 |
input_data = np.array([[tumor_size, texture_score, age, contrast_intensity]])
|
| 62 |
pred = model.predict(input_data)[0]
|
| 63 |
return "🧠 Tumor Detected" if pred == 1 else "✅ No Tumor Detected"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("# 🧠 Brain Tumor Detection")
|
| 67 |
gr.Markdown("Predict brain tumor presence using MRI-based features (simulated demo).")
|