import gradio as gr import joblib import os import pandas as pd # Path to the model scores file model_scores_file = "report/model_summary_report_6_smote.csv" # Load model performance metrics from the provided CSV file if os.path.exists(model_scores_file): model_scores_df = pd.read_csv(model_scores_file) required_columns = {'Model Name', 'Model Sensitivity', 'Model Specificity'} if required_columns.issubset(model_scores_df.columns): model_performance = model_scores_df.set_index('Model Name')[['Model Sensitivity', 'Model Specificity']].T.to_dict() else: raise ValueError(f"The file '{model_scores_file}' must contain the columns: {required_columns}") else: raise FileNotFoundError(f"The model scores file '{model_scores_file}' was not found. Please ensure it exists in the 'report/' directory.") # Dictionary containing the model names and corresponding pickle file names model_paths = { 'AdaBoost': 'pjas-thyroid-AdaBoost.pkl', 'Decision Tree': 'pjas-thyroid-Decision Tree.pkl', 'Gaussian Naive Bayes': 'pjas-thyroid-Gaussian Naive Bayes.pkl', 'Gradient Boosting': 'pjas-thyroid-Gradient Boosting.pkl', 'K-Nearest Neighbors': 'pjas-thyroid-K-Nearest Neighbors.pkl', 'Logistic Regression': 'pjas-thyroid-Logistic Regression.pkl', 'Random Forest': 'pjas-thyroid-Random Forest.pkl', 'Support Vector Machine': 'pjas-thyroid-Support Vector Machine.pkl', 'XGBoost': 'pjas-thyroid-XGBoost.pkl' } # Preload all models at startup loaded_models = {} for model_name, pickle_file in model_paths.items(): model_file_path = os.path.join("model", pickle_file) if os.path.exists(model_file_path): try: loaded_models[model_name] = joblib.load(model_file_path) except Exception as e: print(f"Error loading {model_name}: {e}") else: print(f"Model file for {model_name} not found.") def predict_cancer(age, gender, T, N, Focality, Response): # Validate age if age is None or not (1 <= age <= 100): return "🔴 **Error:** Age must be a number between 1 and 100." # Validate gender if gender not in ["Female", "Male"]: return "🔴 **Error:** Please select a valid gender." # Validate T (Tumor Size) if T is None: return "🔴 **Error:** Please select a valid T (Tumor Size) option." # Validate N (Lymph Node Spread) if N is None: return "🔴 **Error:** Please select a valid N (Lymph Node Spread) option." # Validate Focality if Focality is None: return "🔴 **Error:** Please select a valid Focality option." # Validate Response if Response is None: return "🔴 **Error:** Please select a valid Response option." # Process gender and other fields gender_val = 0 if gender == "Female" else 1 response_val = int(Response) T_val = int(T) N_val = int(N) Focality_val = int(Focality) # Prepare features features = pd.DataFrame({ 'Age': [age], 'Gender': [gender_val], 'T': [T_val], 'N': [N_val], 'Focality': [Focality_val], 'Response': [response_val] }) # Validate scaler file scaler_file = "model/pjas-thyroid-Scaler.pkl" if not os.path.exists(scaler_file): return "🔴 **Error:** Scaler file not found. Please contact the administrator." scaler = joblib.load(scaler_file) features[['Age']] = scaler.transform(features[['Age']]) # Sort models based on sensitivity sorted_model_names = sorted( model_performance.keys(), key=lambda m: model_performance[m]['Model Sensitivity'], reverse=True ) # Generate HTML table table_header = """
Model | Recurrence Accuracy (%) | Non-Recurrence Accuracy (%) | Prediction |
---|---|---|---|
{model_name} | N/A | N/A | Error: Model not loaded |
{model_name} | {sensitivity:.2f}% | {specificity:.2f}% | {pred_text} |
{model_name} | N/A | N/A | Error: {str(e)} |