| import gradio as gr |
| import pandas as pd |
| import numpy as np |
| import pickle |
|
|
| print("\n--- Creating Gradio Interface ---") |
|
|
| file = 'final_model.pkl' |
| final_model = pickle.load(file) |
|
|
| file = 'scaler.pkl' |
| scaler = pickle.load(file) |
|
|
| file = 'label_encoder.pkl' |
| label_encoder = pickle.load(file) |
|
|
| print("✓ Models, Scaler, and Label Encoder loaded successfully.") |
|
|
| |
| |
| |
| |
| original_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( *args ): |
| """ |
| Prediction function for Gradio interface. |
| Takes 30 numerical inputs, preprocesses them, and returns diagnosis and confidence. |
| """ |
| if len(args) != len(original_feature_columns): |
| raise ValueError(f"Expected {len(original_feature_columns)} inputs, but got {len(args)}") |
|
|
| |
| input_data = pd.DataFrame([args], columns=original_feature_columns) |
|
|
| |
| input_scaled = scaler.transform(input_data) |
| input_scaled_df = pd.DataFrame(input_scaled, columns=original_feature_columns) |
|
|
| |
| if 'radius_mean' in input_scaled_df.columns and 'area_mean' in input_scaled_df.columns: |
| input_scaled_df['radius_area_ratio'] = input_scaled_df['radius_mean'] / (input_scaled_df['area_mean'] + 1e-6) |
| if 'perimeter_mean' in input_scaled_df.columns and 'area_mean' in input_scaled_df.columns: |
| input_scaled_df['perimeter_area_ratio'] = input_scaled_df['perimeter_mean'] / (input_scaled_df['area_mean'] + 1e-6) |
| if 'concavity_mean' in input_scaled_df.columns and 'concave points_mean' in input_scaled_df.columns: |
| input_scaled_df['concavity_points_product'] = input_scaled_df['concavity_mean'] * input_scaled_df['concave points_mean'] |
|
|
| |
| prediction_proba = final_model.predict_proba(input_scaled_df)[0] |
| prediction_class_idx = np.argmax(prediction_proba) |
| prediction_class = label_encoder.inverse_transform([prediction_class_idx])[0] |
|
|
| confidence = prediction_proba[prediction_class_idx] |
|
|
| |
| diagnosis_map = {'M': 'Malignant (Cancer)', 'B': 'Benign (Non-cancerous)'} |
| predicted_diagnosis = diagnosis_map.get(prediction_class, prediction_class) |
|
|
| return predicted_diagnosis, f"{confidence*100:.2f}%" |
|
|
| |
| inputs = [] |
| for col in original_feature_columns: |
| |
| inputs.append(gr.Number(label=col, value=0.0)) |
|
|
| |
| |
| example_inputs = [ |
| 12.45, 15.7 , 82.57, 477.1, 0.1045, 0.08947, 0.04991, 0.02111, 0.1716, 0.06337, |
| 0.3344, 1.157 , 2.508 , 32.43, 0.007624, 0.01802, 0.01993, 0.008453, 0.01538, 0.003463, |
| 13.78, 20.8 , 91.18, 592.7, 0.146 , 0.2158 , 0.1672 , 0.07899, 0.2823, 0.07526 |
| ] |
|
|
| |
| interface = gr.Interface( |
| fn=predict_cancer, |
| inputs=inputs, |
| outputs=[gr.Textbox(label="Predicted Diagnosis"), gr.Textbox(label="Confidence")], |
| title="Breast Cancer Prediction", |
| description="Enter patient's cell nuclei measurements to predict breast cancer diagnosis.", |
| examples=[example_inputs] |
| ) |
|
|
| |
| interface.launch(debug=True) |
| print("\n--- Gradio interface launched ---") |