Download app.py from DataWizard9742/BreastCancerPrediction: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/spaces/DataWizard9742/BreastCancerPrediction/resolve/main/app.py
- Command line
-
hf download hf://spaces/DataWizard9742/BreastCancerPrediction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DataWizard9742/BreastCancerPrediction/resolve/main/app.py
2.87 kB
| 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() |