DataWizard9742's picture
Update app.py
0ef6a73 verified
raw
history blame
4.29 kB
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.")
# Get original feature names from X (from previous execution, assuming it's available or re-derivable)
# If X is not in kernel state, we'd need to load the original dataset and derive column names
# For this example, assuming X.columns can be reconstructed or is available.
# Let's manually list the original 30 feature columns based on previous EDA/preprocessing steps
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)}")
# Create a DataFrame from the inputs
input_data = pd.DataFrame([args], columns=original_feature_columns)
# Apply scaling
input_scaled = scaler.transform(input_data)
input_scaled_df = pd.DataFrame(input_scaled, columns=original_feature_columns)
# Apply feature engineering (same as done during training)
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']
# Make prediction
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]
# Map output to more readable format
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}%"
# Create Gradio input components
inputs = []
for col in original_feature_columns:
# Using gr.Number for all numerical features
inputs.append(gr.Number(label=col, value=0.0)) # Default value can be adjusted
# Example values from a benign case (e.g., from df.head() with diagnosis B)
# Using averages for a generic starting point, adjust as needed
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
]
# Create Gradio interface
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]
)
# Launch the interface
interface.launch(debug=True)
print("\n--- Gradio interface launched ---")