File size: 2,874 Bytes
3fa6b6d
 
 
 
 
0fa5114
 
 
3fa6b6d
0fa5114
 
0ef6a73
0fa5114
 
0ef6a73
0fa5114
 
0ef6a73
3fa6b6d
0fa5114
 
 
 
 
3fa6b6d
 
 
 
 
 
 
 
 
 
 
 
0fa5114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fa6b6d
 
 
0fa5114
 
 
 
 
 
3fa6b6d
 
0fa5114
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
import pandas as pd
import numpy as np
import pickle

# ==============================
# Load Saved Model Files
# ==============================

with open("final_model.pkl", "rb") as f:
    final_model = pickle.load(f)

with open("scaler.pkl", "rb") as f:
    scaler = pickle.load(f)

with open("label_encoder.pkl", "rb") as f:
    label_encoder = pickle.load(f)


# ==============================
# Feature Columns (Same as Training)
# ==============================

feature_columns = [
    'radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean',
    'smoothness_mean', 'compactness_mean', 'concavity_mean',
    'concave points_mean', 'symmetry_mean', 'fractal_dimension_mean',
    'radius_se', 'texture_se', 'perimeter_se', 'area_se',
    'smoothness_se', 'compactness_se', 'concavity_se',
    'concave points_se', 'symmetry_se', 'fractal_dimension_se',
    'radius_worst', 'texture_worst', 'perimeter_worst', 'area_worst',
    'smoothness_worst', 'compactness_worst', 'concavity_worst',
    'concave points_worst', 'symmetry_worst', 'fractal_dimension_worst'
]


# ==============================
# Prediction Function
# ==============================

def predict_cancer(*inputs):

    # Convert input into dataframe
    input_df = pd.DataFrame([inputs], columns=feature_columns)

    # Scale data
    scaled_data = scaler.transform(input_df)
    scaled_df = pd.DataFrame(scaled_data, columns=feature_columns)

    # Feature Engineering (must match training)
    scaled_df['radius_area_ratio'] = scaled_df['radius_mean'] / (scaled_df['area_mean'] + 1e-6)
    scaled_df['perimeter_area_ratio'] = scaled_df['perimeter_mean'] / (scaled_df['area_mean'] + 1e-6)
    scaled_df['concavity_points_product'] = (
        scaled_df['concavity_mean'] * scaled_df['concave points_mean']
    )

    # Prediction
    probabilities = final_model.predict_proba(scaled_df)[0]
    class_index = np.argmax(probabilities)
    predicted_label = label_encoder.inverse_transform([class_index])[0]
    confidence = probabilities[class_index] * 100

    diagnosis_map = {
        "M": "Malignant (Cancer)",
        "B": "Benign (Non-cancerous)"
    }

    result = diagnosis_map.get(predicted_label, predicted_label)

    return result, f"{confidence:.2f}%"


# ==============================
# Create Gradio UI
# ==============================

inputs = [gr.Number(label=col, value=0.0) for col in feature_columns]

interface = gr.Interface(
    fn=predict_cancer,
    inputs=inputs,
    outputs=[
        gr.Textbox(label="Predicted Diagnosis"),
        gr.Textbox(label="Confidence")
    ],
    title="Breast Cancer Prediction App",
    description="Enter the 30 medical features to predict whether the tumor is Benign or Malignant."
)

# ==============================
# Launch App
# ==============================

if __name__ == "__main__":
    interface.launch()