| import gradio as gr |
| import pandas as pd |
| import numpy as np |
| import pickle |
|
|
| |
| |
| |
|
|
| 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 = [ |
| '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' |
| ] |
|
|
|
|
| |
| |
| |
|
|
| def predict_cancer(*inputs): |
|
|
| |
| input_df = pd.DataFrame([inputs], columns=feature_columns) |
|
|
| |
| scaled_data = scaler.transform(input_df) |
| scaled_df = pd.DataFrame(scaled_data, columns=feature_columns) |
|
|
| |
| 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'] |
| ) |
|
|
| |
| 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}%" |
|
|
|
|
| |
| |
| |
|
|
| 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." |
| ) |
|
|
| |
| |
| |
|
|
| if __name__ == "__main__": |
| interface.launch() |