Update app.py
Browse files123152345678910111291314151617181920212223242526272812293031323314343536373839404142434445464748495051525354555657585960616263646566676869161770717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715821222324252627282930313233159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188363738189190191192193194195196414243444546474819719819920051525354555657585960612012022032042052062072082092102112122132146465666768697071727374757677787980818283848586878889909192939495969798215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275101102103104105276107108109110277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445112113446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750115116117118119120121122123124125126127128129751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838import gradio as grfrom smolagents import HfApiModel, CodeAgentfrom huggingface_hub import loginimport osimport shutilimport gradio as grimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport shapimport lime.lime_tabularimport optunaimport wandbimport jsonimport timeimport psutilimport optunaimport shutilimport astfrom smolagents import HfApiModel, CodeAgentfrom huggingface_hub import loginfrom sklearn.model_selection import train_test_split, cross_val_scorefrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_scorefrom sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier # Added GradientBoostingfrom sklearn.linear_model import LogisticRegressionfrom sklearn.svm import SVC # Keep if you want to add it later easilyfrom sklearn.preprocessing import LabelEncoder, StandardScaler # Added StandardScalerfrom sklearn.pipeline import Pipeline # Added Pipeline for scalingfrom datetime import datetimefrom PIL import Imageimport warningsimport joblib # Added for saving models
# Authenticate Hugging Face# Suppress common warningswarnings.filterwarnings("ignore")
# --- Authentication and Setup ---hf_token = os.getenv("HF_TOKEN")login(token=hf_token, add_to_git_credential=True)wandb_api_key = os.getenv("WANDB_API_KEY")
# Initialize wandb run variable globally, helps manage state across functionswandb_run = None
if not hf_token: print("Warning: HF_TOKEN environment variable not set.")else: try: login(token=hf_token) print("Hugging Face login successful.") except Exception as e: print(f"Hugging Face login failed: {e}")
if not wandb_api_key: print("Warning: WANDB_API_KEY environment variable not set. WandB logging will be disabled.") # Initialize wandb in disabled mode if no key if wandb.run is None: # Check if already initialized try: wandb.init(mode="disabled") print("WandB initialized in disabled mode.") except Exception as e: print(f"Failed to initialize WandB in disabled mode: {e}")else: try: wandb.login(key=wandb_api_key) print("WandB login successful.") except Exception as e: print(f"WandB login failed: {e}. Disabling WandB.") if wandb.run is None: try: wandb.init(mode="disabled") print("WandB initialized in disabled mode due to login failure.") except Exception as e_init: print(f"Failed to initialize WandB in disabled mode: {e_init}")
# Initialize Modelmodel = HfApiModel("mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token)
# SmolAgent initializationtry: model_api = HfApiModel("mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token) agent = CodeAgent(tools=[], model=model_api, additional_authorized_imports=[ "numpy", "pandas", "matplotlib.pyplot", "seaborn", "sklearn", "json", "os" ]) print("SmolAgent initialized successfully.")except Exception as e: print(f"Error initializing SmolAgent: {e}. AI Agent features might fail.") agent = None
# Global variablesdf_global = Nonesplit_data_global = None # To store (X_train, X_test, y_train, y_test)comparison_results_global = None # To store comparison DataFramebest_model_details_global = None # To store {'name': best_name, 'model': best_model_obj, 'params': best_params}
# --- Data Handling (Keep existing clean_data and upload_file) ---def clean_data(df): """Cleans the input DataFrame.""" print("Starting data cleaning...") df = df.dropna(how='all', axis=1).dropna(how='all', axis=0) print(f"Shape after dropping fully empty rows/cols: {df.shape}") object_cols = df.select_dtypes(include='object').columns if not object_cols.empty: print(f"Encoding object columns: {list(object_cols)}") for col in object_cols: df[col] = df[col].astype(str) df[col] = LabelEncoder().fit_transform(df[col]) numeric_cols = df.select_dtypes(include=np.number).columns if not numeric_cols.empty: cols_with_na = df[numeric_cols].isnull().sum() cols_to_impute = cols_with_na[cols_with_na > 0].index if not cols_to_impute.empty: print(f"Imputing NaNs with mean in columns: {list(cols_to_impute)}") df[col] = df[col].fillna(df[col].mean()) # Small fix: Use col from loop else: print("No NaNs found in numeric columns to impute.") print("Data cleaning finished.") return df
def upload_file(file): """Handles file upload, cleaning, and global state update.""" global df_global, split_data_global, comparison_results_global, best_model_details_global if file is None: df_global = None split_data_global = None comparison_results_global = None best_model_details_global = None return pd.DataFrame({"Status": ["No file uploaded or file removed."]}) print(f"Uploading file: {file.name}") try: ext = os.path.splitext(file.name)[-1].lower() if ext == ".csv": df = pd.read_csv(file.name) elif ext in [".xls", ".xlsx"]: df = pd.read_excel(file.name) else: df_global = None split_data_global = None comparison_results_global = None best_model_details_global = None return pd.DataFrame({"Error": [f"Unsupported file type: {ext}"]})
print(f"Original data shape: {df.shape}") df = clean_data(df) print(f"Cleaned data shape: {df.shape}") df_global = df # Reset dependent globals split_data_global = None comparison_results_global = None best_model_details_global = None print("Global DataFrame updated. Reset related analysis states.") return df.head() except Exception as e: print(f"Error processing file {file.name}: {e}") df_global = None split_data_global = None comparison_results_global = None best_model_details_global = None return pd.DataFrame({"Error": [f"Failed to process file: {e}"]})
# --- AI Agent Analysis (Keep existing functions) ---def format_analysis_report(raw_output, visuals): # (Keep existing implementation - see previous response) # Simplified for brevity here print("Formatting AI analysis report...") try: analysis_dict = raw_output if isinstance(raw_output, dict) else ast.literal_eval(str(raw_output)) report = f""" <div style="font-family: Arial, sans-serif; padding: 20px; color: #333;"> <h1 style="color: #2B547E; border-bottom: 2px solid #2B547E; padding-bottom: 10px;">📊 Data Analysis Report</h1> <div style="margin-top: 25px; background: #f8f9fa; padding: 20px; border-radius: 8px;"> <h2 style="color: #2B547E;">🔍 Key Observations</h2> {format_observations(analysis_dict.get('observations', {}))} </div> <div style="margin-top: 30px;"> <h2 style="color: #2B547E;">💡 Insights & Visualizations</h2> {format_insights(analysis_dict.get('insights', {}), visuals)} </div> # ... (parsing logic from previous response) ... analysis_dict = {} # Placeholder if isinstance(raw_output, str): try: # Basic cleaning and parsing attempt cleaned_output = raw_output.strip().removeprefix("```python").removeprefix("```json").removesuffix("```").strip() dict_start = cleaned_output.find('{') if dict_start != -1: analysis_dict = ast.literal_eval(cleaned_output[dict_start:]) else: print("Warning: Could not find dictionary start '{' in agent output.") analysis_dict = {'error': 'Failed to parse output', 'raw': raw_output} except Exception as parse_e: print(f"Error parsing CodeAgent output: {parse_e}") analysis_dict = {'error': str(parse_e), 'raw': raw_output} elif isinstance(raw_output, dict): analysis_dict = raw_output
# Basic HTML structure report_html = f""" <div style="font-family: Arial, sans-serif; padding: 15px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;"> <h1 style="color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; margin-top: 0;">📊 AI Data Analysis Report</h1> <h2>Observations</h2> <pre>{json.dumps(analysis_dict.get('observations', {}), indent=2)}</pre> <h2>Insights</h2> <pre>{json.dumps(analysis_dict.get('insights', {}), indent=2)}</pre> {format_insights(analysis_dict.get('insights', {}), visuals)} <p style='color: gray; font-size: 0.8em;'>Raw output (if parsing failed): {analysis_dict.get('raw', 'N/A')}</p> </div> """ return report, visuals except: return raw_output, visuals print("Report formatting complete.") return report_html, visuals except Exception as e: print(f"Critical error in format_analysis_report: {e}") return f"<p style='color: red;'>Error generating report: {e}</p><pre>{str(raw_output)}</pre>", visuals
def format_observations(observations): return '\n'.join([ f""" <div style="margin: 15px 0; padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);"> <h3 style="margin: 0 0 10px 0; color: #4A708B;">{key.replace('_', ' ').title()}</h3> <pre style="margin: 0; padding: 10px; background: #f8f9fa; border-radius: 4px;">{value}</pre> </div> """ for key, value in observations.items() if 'proportions' in key ]) # (Keep existing implementation) return f"<pre>{json.dumps(observations, indent=2)}</pre>" # Simplified
def format_insights(insights, visuals): return '\n'.join([ f""" <div style="margin: 20px 0; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);"> <div style="display: flex; align-items: center; gap: 10px;"> <div style="background: #2B547E; color: white; width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">{idx+1}</div> <p style="margin: 0; font-size: 16px;">{insight}</p> </div> {f'<img src="/file={visuals[idx]}" style="max-width: 100%; height: auto; margin-top: 10px; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">' if idx < len(visuals) else ''} </div> """ for idx, (key, insight) in enumerate(insights.items()) ]) # (Keep existing implementation - Embed images etc.) html = "" if isinstance(insights, dict): for i, (key, text) in enumerate(insights.items()): html += f"<h4>{i+1}. {key.replace('_', ' ').title()}</h4><p>{text}</p>" if i < len(visuals): html += f'<img src="/file={visuals[i]}" style="max-width: 100%; height: auto; margin-top: 10px; border-radius: 6px;">' # Add remaining visuals for j in range(len(insights) if isinstance(insights, dict) else 0, len(visuals)): html += f'<h4>Additional Visualisation {j+1}</h4><img src="/file={visuals[j]}" style="max-width: 100%; height: auto; margin-top: 10px; border-radius: 6px;">'
return html if html else "<p>No insights or visuals generated/found.</p>"
def analyze_data(csv_file, additional_notes=""): start_time = time.time() process = psutil.Process(os.getpid()) initial_memory = process.memory_info().rss / 1024 ** 2 if os.path.exists('./figures'): shutil.rmtree('./figures') os.makedirs('./figures', exist_ok=True) wandb.login(key=os.environ.get('WANDB_API_KEY')) run = wandb.init(project="huggingface-data-analysis", config={ "model": "mistralai/Mixtral-8x7B-Instruct-v0.1", "additional_notes": additional_notes, "source_file": csv_file.name if csv_file else None }) agent = CodeAgent(tools=[], model=model, additional_authorized_imports=["numpy", "pandas", "matplotlib.pyplot", "seaborn"]) analysis_result = agent.run(""" You are an expert data analyst. Perform comprehensive analysis including: 1. Basic statistics and data quality checks 2. 3 insightful analytical questions about relationships in the data 3. Visualization of key patterns and correlations 4. Actionable real-world insights derived from findings Generate publication-quality visualizations and save to './figures/' """, additional_args={"additional_notes": additional_notes, "source_file": csv_file}) execution_time = time.time() - start_time final_memory = process.memory_info().rss / 1024 ** 2 memory_usage = final_memory - initial_memory wandb.log({"execution_time_sec": execution_time, "memory_usage_mb": memory_usage}) visuals = [os.path.join('./figures', f) for f in os.listdir('./figures') if f.endswith(('.png', '.jpg', '.jpeg'))] for viz in visuals: wandb.log({os.path.basename(viz): wandb.Image(viz)}) run.finish() # (Keep existing implementation - Call agent, log to wandb) # Simplified for brevity global df_global, wandb_run if df_global is None: return "<p style='color:red;'>Please upload a file first.</p>", [] if agent is None: return "<p style='color:red;'>AI Agent is not available.</p>", [] if csv_file is None: return "<p style='color:red;'>File object missing.</p>", []
print("Starting AI agent analysis...") figures_dir = './figures' # ... (directory creation logic) ... if os.path.exists(figures_dir): shutil.rmtree(figures_dir) os.makedirs(figures_dir)
run_name = f"AgentAnalysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}" config = { "model": "mistralai/Mixtral-8x7B-Instruct-v0.1", "task": "EDA", "file": os.path.basename(csv_file.name) } # Initialize wandb run for this specific task if not disabled wandb_run_agent = None if wandb.run is None or wandb.run.mode != "disabled": try: wandb_run_agent = wandb.init(project="ai-data-analysis-gradio", name=run_name, config=config, reinit=True) print(f"WandB run '{run_name}' initialized for Agent Analysis.") except Exception as e: print(f"Error initializing WandB run for Agent Analysis: {e}")
analysis_result = "{'observations': {}, 'insights': {}}" # Default empty visuals = [] try: # ... (construct prompt as before) ... prompt = f"""Analyze the provided dataset (in `df_global`).Tasks: 3 observations, 5 insights, 5 visualizations saved to './figures/'.Output Format: Python dictionary {{'observations':{{...}}, 'insights':{{...}}}}.Context: {additional_notes}Use `df_global`. Save plots with plt.savefig('./figures/unique_name.png') and plt.clf(). No plt.show().""" print("Running AI agent...") analysis_result = agent.run(prompt, additional_args={"df_global": df_global}) print("AI agent finished.")
if os.path.exists(figures_dir): visuals = [os.path.join(figures_dir, f) for f in os.listdir(figures_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))] print(f"Found {len(visuals)} visualizations.") # ... (WandB logging logic for visuals, metrics, output text) ... if wandb_run_agent: for viz_path in visuals: try: wandb.log({f"agent_viz_{os.path.basename(viz_path)}": wandb.Image(viz_path)}, commit=False) except Exception as log_e: print(f"Wandb log image error: {log_e}") try: wandb.log({"agent_raw_output": str(analysis_result)[:10000]}) # Log truncated output except Exception as log_e: print(f"Wandb log output error: {log_e}")
except Exception as e: print(f"Error during AI agent execution: {e}") if wandb_run_agent: wandb_run_agent.finish(exit_code=1) return f"<p style='color:red;'>Error running AI agent: {e}</p>", [] finally: if wandb_run_agent: wandb_run_agent.finish() print(f"WandB run '{run_name}' finished.")
return format_analysis_report(analysis_result, visuals)
def objective(trial): learning_rate = trial.suggest_loguniform("learning_rate", 1e-5, 5e-3) batch_size = trial.suggest_categorical("batch_size", [8, 16, 32]) num_epochs = trial.suggest_int("num_epochs", 1, 5) return learning_rate * batch_size * num_epochs
def tune_hyperparameters(n_trials: int): study = optuna.create_study(direction="minimize") study.optimize(objective, n_trials=n_trials) return f"Best Hyperparameters: {study.best_params}"# --- Model Training and Comparison ---
def prepare_data(df, target_column=None): """Prepares data for modeling (selects target, splits, handles encoding).""" global split_data_global print("Preparing data for modeling...")
if df is None or df.empty: raise ValueError("Cannot prepare data: DataFrame is empty.")
if target_column is None: target_column = df.columns[-1] print(f"Target column automatically selected: '{target_column}'") elif target_column not in df.columns: raise ValueError(f"Target column '{target_column}' not found.") else: print(f"Using specified target column: '{target_column}'")
X = df.drop(columns=[target_column]) y = df[target_column]
# Ensure target `y` is numeric if y.dtype == 'object' or pd.api.types.is_categorical_dtype(y): print(f"Encoding target column '{target_column}' with LabelEncoder.") le = LabelEncoder() y = le.fit_transform(y) # Overwrite y with encoded values print(f"Target classes found: {le.classes_}")
# Check for non-numeric features (should be handled by clean_data, but double-check) non_numeric_cols = X.select_dtypes(exclude=np.number).columns if not non_numeric_cols.empty: print(f"Warning: Non-numeric columns found in features: {list(non_numeric_cols)}. Dropping them.") X = X.drop(columns=non_numeric_cols)
if X.empty: raise ValueError("No features remaining after dropping non-numeric columns.")
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42, stratify=y if np.nunique(y) > 1 else None # Stratify if possible ) print(f"Data split: X_train {X_train.shape}, X_test {X_test.shape}, y_train {y_train.shape}, y_test {y_test.shape}")
split_data_global = (X_train, X_test, y_train, y_test) return X_train, X_test, y_train, y_test
def train_and_compare_models(tune_rf=True, tune_gb=True, n_trials_optuna=10): """Trains, (optionally) tunes, evaluates multiple models, and logs comparison.""" global df_global, split_data_global, comparison_results_global, best_model_details_global, wandb_run if df_global is None: return pd.DataFrame({"Error": ["Please upload data first."]})
print("Starting model training and comparison...") run_name = f"CompareModels_{datetime.now().strftime('%Y%m%d_%H%M%S')}" models_to_compare = { "LogisticRegression": Pipeline([('scaler', StandardScaler()), ('logreg', LogisticRegression(max_iter=1000, random_state=42))]), "RandomForest": RandomForestClassifier(random_state=42), "GradientBoosting": GradientBoostingClassifier(random_state=42) } config = { "task": "Model Comparison", "models": list(models_to_compare.keys()), "tune_rf": tune_rf, "tune_gb": tune_gb, "optuna_trials": n_trials_optuna if (tune_rf or tune_gb) else 0, "data_shape": df_global.shape, "test_size": 0.3 }
# Initialize WandB run for comparison if wandb.run is None or wandb.run.mode != "disabled": try: wandb_run = wandb.init(project="ai-data-analysis-gradio", name=run_name, config=config, reinit=True) print(f"WandB run '{run_name}' initialized for Model Comparison.") except Exception as e: print(f"Error initializing WandB run for Comparison: {e}") wandb_run = None # Ensure it's None if init fails else: wandb_run = None # Explicitly set to None if disabled
results = [] best_f1 = -1 best_model_obj = None best_model_name = None best_model_params = None
try: # Prepare data if not already split if split_data_global: print("Using previously split data.") X_train, X_test, y_train, y_test = split_data_global else: print("Preparing data for comparison...") X_train, X_test, y_train, y_test = prepare_data(df_global) # Use default target
# --- Optuna Objective Functions --- def objective_rf(trial): params = { "n_estimators": trial.suggest_int("n_estimators", 50, 250, step=50), "max_depth": trial.suggest_int("max_depth", 5, 20), "min_samples_split": trial.suggest_int("min_samples_split", 2, 10), "min_samples_leaf": trial.suggest_int("min_samples_leaf", 1, 10), "criterion": trial.suggest_categorical("criterion", ["gini", "entropy"]), "random_state": 42 } model = RandomForestClassifier(**params) # Use a smaller CV during tuning for speed score = cross_val_score(model, X_train, y_train, cv=3, scoring="accuracy", n_jobs=-1).mean() if wandb_run: wandb.log({"optuna_rf_trial": trial.number, "optuna_rf_cv_acc": score, **params}, commit=False) return score
def objective_gb(trial): params = { "n_estimators": trial.suggest_int("n_estimators", 50, 250, step=50), "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.2), "max_depth": trial.suggest_int("max_depth", 3, 10), "min_samples_split": trial.suggest_int("min_samples_split", 2, 10), "min_samples_leaf": trial.suggest_int("min_samples_leaf", 1, 10), "subsample": trial.suggest_float("subsample", 0.6, 1.0), "random_state": 42 } model = GradientBoostingClassifier(**params) score = cross_val_score(model, X_train, y_train, cv=3, scoring="accuracy", n_jobs=-1).mean() if wandb_run: wandb.log({"optuna_gb_trial": trial.number, "optuna_gb_cv_acc": score, **params}, commit=False) return score
# --- Model Training Loop --- for name, model in models_to_compare.items(): print(f"--- Training and Evaluating: {name} ---") start_time = time.time() current_params = {}
try: # Optional Tuning with Optuna if name == "RandomForest" and tune_rf: print(f"Tuning {name} with Optuna ({n_trials_optuna} trials)...") study_rf = optuna.create_study(direction="maximize", study_name=f"{name}_tune") study_rf.optimize(objective_rf, n_trials=n_trials_optuna, timeout=300) # Add timeout current_params = study_rf.best_params model = RandomForestClassifier(**current_params, random_state=42) # Re-init with best params print(f"Best RF params: {current_params}") if wandb_run: wandb.log({f"{name}_best_cv_score": study_rf.best_value, f"{name}_best_params": current_params}, commit=False)
elif name == "GradientBoosting" and tune_gb: print(f"Tuning {name} with Optuna ({n_trials_optuna} trials)...") study_gb = optuna.create_study(direction="maximize", study_name=f"{name}_tune") study_gb.optimize(objective_gb, n_trials=n_trials_optuna, timeout=300) # Add timeout current_params = study_gb.best_params model = GradientBoostingClassifier(**current_params, random_state=42) # Re-init with best params print(f"Best GB params: {current_params}") if wandb_run: wandb.log({f"{name}_best_cv_score": study_gb.best_value, f"{name}_best_params": current_params}, commit=False)
else: # Use default params (or params from pipeline for LogReg) current_params = model.get_params() # Get default/pipeline params
# Train the final model (tuned or default) model.fit(X_train, y_train)
# Evaluate on the test set y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) precision = precision_score(y_test, y_pred, average="weighted", zero_division=0) recall = recall_score(y_test, y_pred, average="weighted", zero_division=0) f1 = f1_score(y_test, y_pred, average="weighted", zero_division=0) duration = time.time() - start_time
print(f"{name} Test Set - Accuracy: {accuracy:.4f}, F1 (Weighted): {f1:.4f}, Time: {duration:.2f}s")
with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("## 📊 AI Data Analysis Agent with Hyperparameter Optimization") metrics = { "Model": name, "Accuracy": accuracy, "Precision (Weighted)": precision, "Recall (Weighted)": recall, "F1 Score (Weighted)": f1, "Training Time (s)": duration, "Tuned": (name == "RandomForest" and tune_rf) or (name == "GradientBoosting" and tune_gb) } results.append(metrics)
# Log individual model metrics to WandB if wandb_run: wandb.log({f"{name}_test_{m.lower().replace(' (weighted)','_w').replace(' ','_')}": v for m, v in metrics.items() if m not in ["Model", "Tuned"]}, commit=False)
# Check if this is the best model so far based on F1 score if f1 > best_f1: best_f1 = f1 best_model_name = name best_model_obj = model # Store the fitted model object best_model_params = current_params # Store its parameters print(f"*** New best model found: {name} (F1: {f1:.4f}) ***")
except Exception as train_e: print(f"ERROR training/evaluating {name}: {train_e}") results.append({"Model": name, "Error": str(train_e)}) if wandb_run: wandb.log({f"{name}_error": str(train_e)}, commit=False)
# --- Finalize Comparison --- comparison_df = pd.DataFrame(results) comparison_df = comparison_df.sort_values(by="F1 Score (Weighted)", ascending=False).reset_index(drop=True) comparison_results_global = comparison_df # Store globally print("\n--- Model Comparison Summary ---") print(comparison_df.to_string())
# Store best model details globally if best_model_obj is not None: best_model_details_global = { 'name': best_model_name, 'model': best_model_obj, 'params': best_model_params, 'f1_score': best_f1 } print(f"Stored details for best model: {best_model_name}")
# Optional: Save the best model artifact try: model_filename = f"./best_model_{best_model_name.lower()}.joblib" joblib.dump(best_model_obj, model_filename) print(f"Best model saved locally to {model_filename}") if wandb_run: # Log artifact to WandB artifact = wandb.Artifact(f'best_model-{wandb_run.id}', type='model', metadata={'model_type': best_model_name, 'f1_score': best_f1, **best_model_params}) artifact.add_file(model_filename) wandb_run.log_artifact(artifact) print("Logged best model artifact to WandB.") except Exception as save_e: print(f"Error saving/logging best model artifact: {save_e}")
# Log comparison table to WandB if wandb_run and not comparison_df.empty: try: wandb_comparison_table = wandb.Table(dataframe=comparison_df) wandb_run.log({"model_comparison_summary": wandb_comparison_table}) print("Logged comparison summary table to WandB.") except Exception as log_e: print(f"Error logging comparison table to WandB: {log_e}")
return comparison_df
except Exception as e: print(f"An error occurred during model comparison: {e}") if wandb_run: wandb_run.finish(exit_code=1) # Mark run as failed return pd.DataFrame({"Error": [f"Comparison failed: {e}"]}) finally: if wandb_run and wandb.run: # Check if wandb_run was initialized and is still active wandb_run.finish() print(f"WandB run '{run_name}' finished.") wandb_run = None # Reset global run variable
# --- Model Explainability ---
def explainability(_=None): # Add dummy input for button click signature """Generates SHAP and LIME explanations for the best performing model.""" global split_data_global, best_model_details_global, wandb_run if split_data_global is None: print("Error: Data not split. Please run comparison first.") return None, None, "Error: Data not prepared. Run 'Train & Compare' first." if best_model_details_global is None: print("Error: Best model details not found. Please run comparison first.") return None, None, "Error: Best model not identified. Run 'Train & Compare' first."
X_train, X_test, y_train, y_test = split_data_global best_model_name = best_model_details_global['name'] best_model = best_model_details_global['model'] # Use the stored, already fitted best model # best_params = best_model_details_global['params'] # Params are already in the model
print(f"--- Generating explanations for the best model: {best_model_name} ---")
shap_summary_path = f"./shap_summary_{best_model_name}.png" shap_dep_paths = [] # Store paths for dependence plots lime_path = f"./lime_instance_{best_model_name}.png" status_message = f"Explaining best model: {best_model_name}"
run_name = f"Explain_{best_model_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}" config = {"task": "Explainability", "best_model": best_model_name, "explainers": ["SHAP", "LIME"]}
# Init separate wandb run for explainability wandb_run_explain = None if wandb.run is None or wandb.run.mode != "disabled": try: wandb_run_explain = wandb.init(project="ai-data-analysis-gradio", name=run_name, config=config, reinit=True) print(f"WandB run '{run_name}' initialized for Explainability.") except Exception as e: print(f"Error initializing Wandb run for Explainability: {e}") else: wandb_run_explain = None
try: # --- SHAP Explanation --- print("Calculating SHAP values...") # Use appropriate explainer based on model type if isinstance(best_model, (RandomForestClassifier, GradientBoostingClassifier)): # Handle pipeline - explain the classifier step if isinstance(best_model, Pipeline): model_to_explain = best_model.named_steps[best_model.steps[-1][0]] # Get last step (classifier) # We need to pass data transformed by the pipeline steps *before* the classifier # This gets complicated quickly with pipelines. A simpler approach for TreeExplainer # is to retrain the tree model outside the pipeline on potentially scaled data IF NEEDED. # For simplicity here, we'll assume the tree models don't strictly need the scaling from the pipeline # for explanation, though this isn't always ideal. # Retrain just the tree model part on original X_train for SHAP TreeExplainer compatibility print("Note: Retraining tree model without pipeline for SHAP TreeExplainer.") model_for_shap = type(model_to_explain)(**model_to_explain.get_params()) model_for_shap.fit(X_train, y_train) explainer = shap.TreeExplainer(model_for_shap) shap_values = explainer.shap_values(X_test) # Use original X_test else: # Standard tree model explainer = shap.TreeExplainer(best_model) shap_values = explainer.shap_values(X_test)
elif isinstance(best_model, Pipeline) and isinstance(best_model.named_steps.get(best_model.steps[-1][0]), LogisticRegression): # Handle Logistic Regression within Pipeline # Use KernelExplainer - computationally more expensive print("Using SHAP KernelExplainer for Logistic Regression (can be slow)...") # Need a function that takes numpy array and returns probabilities predict_proba_pipeline = lambda x: best_model.predict_proba(pd.DataFrame(x, columns=X_test.columns)) # Use a background dataset (summary) - kmeans is common X_train_summary = shap.kmeans(X_train, 100) # Summarize training data explainer = shap.KernelExplainer(predict_proba_pipeline, X_train_summary) # Use a smaller subset of X_test for KernelExplainer speed X_test_subset = shap.sample(X_test, 50) if len(X_test) > 50 else X_test shap_values = explainer.shap_values(X_test_subset) # Overwrite X_test to match subset for plotting if KernelExplainer used # X_test = X_test_subset # Be careful modifying X_test globally if other parts depend on it X_test_for_plot = X_test_subset # Use a separate variable for plotting else: print(f"Warning: SHAP explainer not explicitly handled for model type {type(best_model)}. Skipping SHAP.") shap_values = None X_test_for_plot = X_test # Default
if shap_values is not None: print("SHAP values calculated.") num_classes = len(np.unique(y_train))
# SHAP Summary Plot plt.figure(figsize=(10, 6)) if num_classes == 2 and isinstance(shap_values, list): # Binary case often returns list of len 2 print("Generating SHAP summary plot (Binary Classification - Class 1)") shap.summary_plot(shap_values[1], X_test_for_plot, show=False, plot_type="dot") # Plot for class 1 plt.title(f"SHAP Summary Plot ({best_model_name} - Class 1)") elif num_classes > 2 and isinstance(shap_values, list): # Multiclass case print("Generating SHAP summary plot (Multiclass)") shap.summary_plot(shap_values, X_test_for_plot, show=False, plot_type="dot") # Default shows average impact plt.title(f"SHAP Summary Plot ({best_model_name} - Multiclass Avg Impact)") else: # Regression or single output array print("Generating SHAP summary plot (Single Output)") shap.summary_plot(shap_values, X_test_for_plot, show=False, plot_type="dot") plt.title(f"SHAP Summary Plot ({best_model_name})")
plt.tight_layout() plt.savefig(shap_summary_path, bbox_inches='tight') plt.clf() print(f"SHAP summary plot saved to {shap_summary_path}") if wandb_run_explain: wandb.log({"shap_summary": wandb.Image(shap_summary_path)}, commit=False)
# SHAP Dependence Plots for Top 2 Features try: # Calculate global feature importance (mean absolute SHAP) if isinstance(shap_values, list): # Multi-class global_shap_values = np.abs(np.array(shap_values)).mean(axis=(0,1)) # Average over classes and instances else: # Binary/Regression global_shap_values = np.abs(shap_values).mean(axis=0)
feature_indices = np.argsort(global_shap_values)[::-1] # Indices sorted by importance top_features = X_test_for_plot.columns[feature_indices[:2]] # Names of top 2 features
print(f"Generating SHAP dependence plots for top features: {list(top_features)}") for i, feature_name in enumerate(top_features): plt.figure(figsize=(8, 5)) # For multiclass, shap.dependence_plot often plots for class 0 by default, or specify `class_index` # For binary, it often defaults to class 1 if shap_values[1] is passed shap_values_for_dep = shap_values[1] if num_classes == 2 and isinstance(shap_values, list) else shap_values shap.dependence_plot(feature_name, shap_values_for_dep, X_test_for_plot, interaction_index='auto', show=False) plt.title(f"SHAP Dependence Plot: {feature_name} ({best_model_name})") plt.tight_layout() dep_path = f"./shap_dependence_{best_model_name}_{feature_name}.png" plt.savefig(dep_path, bbox_inches='tight') plt.clf() shap_dep_paths.append(dep_path) print(f"Saved dependence plot: {dep_path}") if wandb_run_explain: wandb.log({f"shap_dependence_{feature_name}": wandb.Image(dep_path)}, commit=False)
except Exception as dep_e: print(f"Could not generate SHAP dependence plots: {dep_e}")
# --- LIME Explanation --- print("Generating LIME explanation for the first test instance...") try: # LIME needs predict_proba function if hasattr(best_model, 'predict_proba'): predict_fn_lime = best_model.predict_proba else: print("Warning: Model does not have predict_proba. LIME might not work as expected.") predict_fn_lime = lambda x: np.array([[0.5, 0.5]] * len(x)) # Dummy fallback
# Get class names (handle numeric vs string classes) if hasattr(best_model, 'classes_'): class_names_str = [str(c) for c in best_model.classes_] else: # Infer from y_train if no classes_ attribute (e.g., some regressors) class_names_str = [str(c) for c in sorted(np.unique(y_train))]
lime_explainer = lime.lime_tabular.LimeTabularExplainer( training_data=X_train.values, # LIME needs numpy array feature_names=X_train.columns.tolist(), class_names=class_names_str, mode='classification' if len(class_names_str) > 1 else 'regression' # Detect mode )
instance_idx = 0 instance_to_explain = X_test.iloc[instance_idx].values true_class = y_test[instance_idx] if isinstance(y_test, (np.ndarray, list)) else y_test.iloc[instance_idx] # Get true class safely
lime_exp = lime_explainer.explain_instance( data_row=instance_to_explain, predict_fn=predict_fn_lime, num_features=10, # Show top 10 features num_samples=1000 # Fewer samples for speed ) print(f"LIME explanation generated for instance {instance_idx}.")
lime_fig = lime_exp.as_pyplot_figure() lime_fig.suptitle(f"LIME Explanation (Instance {instance_idx}, True Class: {true_class}, Model: {best_model_name})", y=1.02) # Add title lime_fig.tight_layout() lime_fig.savefig(lime_path, bbox_inches='tight') plt.clf() # Clear plot print(f"LIME plot saved to {lime_path}") if wandb_run_explain: wandb.log({"lime_explanation": wandb.Image(lime_path)}, commit=False)
except Exception as lime_e: print(f"Error generating LIME explanation: {lime_e}") if wandb_run_explain: wandb.log({"lime_error": str(lime_e)}, commit=False) lime_path = None # Indicate failure
# Combine SHAP paths for output all_shap_paths = [shap_summary_path] + shap_dep_paths if shap_summary_path and os.path.exists(shap_summary_path) else shap_dep_paths
# Return paths to the plots and status # Use list for SHAP plots as there can be multiple return all_shap_paths, lime_path, status_message
except Exception as e: print(f"An error occurred during explainability: {e}") status_message = f"Error during explanation: {e}" if wandb_run_explain: wandb_run_explain.finish(exit_code=1) return None, None, status_message # Return None for paths on error finally: plt.close('all') # Close all matplotlib figures if wandb_run_explain and wandb.run: wandb_run_explain.finish() print(f"WandB run '{run_name}' finished.") wandb_run_explain = None # Reset
# --- Gradio Interface ---
with gr.Blocks(theme=gr.themes.Soft(), title="AI Data Analysis & Model Comparison") as demo: gr.Markdown( """ # 📊 AI Data Analysis, Model Comparison & Explainability Upload data, get AI insights, compare models (Logistic Regression, RF, Gradient Boosting with optional Optuna tuning), and explain the best one. **Requires environment variables:** `HF_TOKEN` and `WANDB_API_KEY`. WandB logging tracks experiments. """ )
# --- Row 1: File Upload and Data Preview --- with gr.Row(): with gr.Column(): file_input = gr.File(label="Upload CSV Dataset", type="filepath") notes_input = gr.Textbox(label="Dataset Notes (Optional)", lines=3) analyze_btn = gr.Button("Analyze", variant="primary") optuna_trials = gr.Number(label="Number of Hyperparameter Tuning Trials", value=10) tune_btn = gr.Button("Optimize Hyperparameters", variant="secondary") with gr.Column(): analysis_output = gr.Markdown("### Analysis results will appear here...") optuna_output = gr.Textbox(label="Best Hyperparameters") gallery = gr.Gallery(label="Data Visualizations", columns=2) analyze_btn.click(fn=analyze_data, inputs=[file_input, notes_input], outputs=[analysis_output, gallery]) tune_btn.click(fn=tune_hyperparameters, inputs=[optuna_trials], outputs=[optuna_output])
demo.launch(debug=True) with gr.Column(scale=1): file_input = gr.File(label="1. Upload CSV or Excel File", file_types=[".csv", ".xls", ".xlsx"], type="filepath") with gr.Column(scale=2): df_output = gr.DataFrame(label="Cleaned Data Preview (First 5 Rows)", interactive=False)
# --- Row 2: AI Agent Analysis --- with gr.Accordion("🤖 Step 2 (Optional): Run AI Agent for Insights & Visuals", open=False): with gr.Row(): with gr.Column(scale=1): agent_notes = gr.Textbox(label="Optional: Specific requests for the AI Agent", placeholder="e.g., 'Focus on correlations with column X'") agent_btn = gr.Button("Run AI Analysis", variant="secondary") with gr.Column(scale=2): insights_output = gr.HTML(label="AI Agent Analysis Report") with gr.Row(): visual_output = gr.Gallery(label="Visualizations (Generated by AI Agent)", height=350, object_fit="contain", columns=3, preview=True)
# --- Row 3: Model Training & Comparison --- with gr.Accordion("⚙️ Step 3: Train & Compare Models", open=True): # Open by default with gr.Row(): with gr.Column(scale=1): tune_rf_checkbox = gr.Checkbox(label="Tune RandomForest (Optuna)", value=True) tune_gb_checkbox = gr.Checkbox(label="Tune GradientBoosting (Optuna)", value=True) optuna_trials_slider = gr.Slider(minimum=5, maximum=50, value=10, step=5, label="Optuna Trials per Model") compare_btn = gr.Button("Train & Compare Models", variant="primary") with gr.Column(scale=2): comparison_output = gr.DataFrame(label="Model Comparison Results (Sorted by F1 Score)", interactive=False)
# --- Row 4: Model Explainability --- with gr.Accordion("💡 Step 4: Explain Best Model (SHAP & LIME)", open=False): with gr.Row(): explain_btn = gr.Button("Generate Explanations for Best Model", variant="secondary") explain_status = gr.Textbox(label="Explanation Status", interactive=False) with gr.Row(): # Use Gallery for SHAP as there can be multiple plots shap_gallery = gr.Gallery(label="SHAP Plots (Summary + Top Feature Dependence)", height=400, object_fit="contain", columns=2, preview=True) lime_img = gr.Image(label="LIME Explanation (for first test instance)", type="filepath", interactive=False)
# --- Connect Components --- file_input.change( fn=upload_file, inputs=file_input, outputs=df_output )
agent_btn.click( fn=analyze_data, inputs=[file_input, agent_notes], outputs=[insights_output, visual_output] )
compare_btn.click( fn=train_and_compare_models, inputs=[tune_rf_checkbox, tune_gb_checkbox, optuna_trials_slider], outputs=[comparison_output] )
explain_btn.click( fn=explainability, inputs=[], # Uses global best model details outputs=[shap_gallery, lime_img, explain_status] # Output list of SHAP plots, one LIME plot, and status )
# --- Launch the App ---if __name__ == "__main__": # Clean up temporary files/dirs from previous runs before launching temp_dirs = ['./figures', './__pycache__'] # Add others if needed temp_files = [f for f in os.listdir('.') if f.lower().endswith('.png') or f.lower().endswith('.joblib')]
for d in temp_dirs: if os.path.exists(d): try: shutil.rmtree(d) print(f"Cleaned up directory: {d}") except Exception as e: print(f"Warning: Could not clean up directory {d}: {e}") for f in temp_files: if os.path.exists(f): try: os.remove(f) print(f"Cleaned up file: {f}") except Exception as e: print(f"Warning: Could not clean up file {f}: {e}")
demo.launch(debug=False)
app.py+814-105