Spaces:
Runtime error
Runtime error
Load model metadata and images from Hopsworks, update model info display
Browse files11 unmodified lines1213141516179 unmodified lines27282930313215 unmodified lines4849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394141 unmodified lines23623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526611 unmodified linesimport osimport sysimport loggingimport gradio as grfrom typing import Tuple9 unmodified lines# Global inference pipelinepipeline = Nonedef initialize_app():15 unmodified linespipeline.load_model_from_hopsworks()logger.info("Inference pipeline initialized successfully!")return Trueexcept Exception as e:logger.error(f"Failed to initialize app: {e}")return Falsedef get_model_info() -> str:"""Get model information as HTML."""if not pipeline:return "<p>Model information not available</p>"# TODO: Check if pipeline has model_metrics attributemetrics = getattr(pipeline, 'model_metrics', None)if not metrics:return "<p>Model metrics not available</p>"version = getattr(pipeline, 'model_version', 'latest')html = f"""<div style="padding: 20px; background-color: #f5f5f5; border-radius: 10px; margin: 10px;"><h3>Model Information</h3><p><strong>Model Name:</strong> {pipeline.model_name}</p><p><strong>Model Version:</strong> {version}</p><hr><h4>Performance Metrics</h4><div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;"><div><strong>Test Accuracy:</strong> {metrics.get('test_accuracy', 'N/A'):.4f}</div><div><strong>Test Precision:</strong> {metrics.get('test_precision', 'N/A'):.4f}</div><div><strong>Test Recall:</strong> {metrics.get('test_recall', 'N/A'):.4f}</div><div><strong>Test F1 Score:</strong> {metrics.get('test_f1_score', 'N/A'):.4f}</div><div><strong>Test ROC-AUC:</strong> {metrics.get('test_roc_auc', 'N/A'):.4f}</div><div><strong>Validation F1 Score:</strong> {metrics.get('val_f1_score', 'N/A'):.4f}</div></div><hr><h4>Training Information</h4><p><strong>Number of Features:</strong> {metrics.get('n_features', 'N/A')}</p><p><strong>Training Samples:</strong> {metrics.get('n_train_samples', 'N/A')}</p><p><strong>Test Samples:</strong> {metrics.get('n_test_samples', 'N/A')}</p><p><strong>Best CV Score:</strong> {metrics.get('best_cv_score', 'N/A'):.4f}</p></div>"""return html141 unmodified linesgr.Markdown("### Model Evaluation Visualizations")# TODO: Check if pipeline has model_dir attribute to access imagesmodel_dir = getattr(pipeline, 'model_dir', None) if pipeline else Nonewith gr.Row():with gr.Column():gr.Markdown("#### Confusion Matrix")if model_dir:confusion_path = os.path.join(model_dir, "evaluation_image_1.png")if os.path.exists(confusion_path):gr.Image(value=confusion_path, label="Confusion Matrix")else:gr.Markdown("*Confusion matrix image not found*")else:gr.Markdown("*Model directory not available*")with gr.Column():gr.Markdown("#### Feature Importance")if model_dir:importance_path = os.path.join(model_dir, "evaluation_image_2.png")if os.path.exists(importance_path):gr.Image(value=importance_path, label="Feature Importance")else:gr.Markdown("*Feature importance image not found*")else:gr.Markdown("*Model directory not available*")gr.Markdown("""11 unmodified lines121314151617189 unmodified lines2829303132333415 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235141 unmodified lines37737837938038138238338438538638738838939039139239339439539611 unmodified linesimport osimport sysimport reimport loggingimport gradio as grfrom typing import Tuple9 unmodified lines# Global inference pipelinepipeline = Nonemodel_info_cache = Nonedef initialize_app():15 unmodified linespipeline.load_model_from_hopsworks()logger.info("Inference pipeline initialized successfully!")# Load model metadata (metrics and images)logger.info("Loading model metadata and images...")load_model_metadata()return Trueexcept Exception as e:logger.error(f"Failed to initialize app: {e}")return Falsedef load_model_metadata():"""Load model metadata and images from Hopsworks."""global model_info_cachetry:from phising_detection.utils.hopsworks_utils import connect_to_hopsworksproject = connect_to_hopsworks()mr = project.get_model_registry()# Get the same model that pipeline loadedmodel_version = getattr(pipeline, 'model_version', None) if pipeline else Nonemodel_name = pipeline.model_name if pipeline else "phishing_detector"if model_version:model_registry = mr.get_model(model_name, version=model_version)else:model_registry = mr.get_model(model_name)# Download model artifacts to get images and metricsmodel_dir = model_registry.download()# Parse hyperparameters.txt for metricsmetrics = {}hyperparams_path = os.path.join(model_dir, "hyperparameters.txt")if os.path.exists(hyperparams_path):with open(hyperparams_path, 'r') as f:content = f.read()# Extract model name (first line)lines = content.split('\n')if lines:first_line = lines[0].strip()if 'Phishing Detection Model' in first_line:model_type = first_line.split('-')[-1].strip() if '-' in first_line else 'Unknown'metrics['model_type'] = model_type# Extract training infoif 'CV Folds:' in content:cv_match = re.search(r'CV Folds:\s*(\d+)', content)if cv_match:metrics['cv_folds'] = int(cv_match.group(1))iter_match = re.search(r'RandomizedSearchCV iterations:\s*(\d+)', content)if iter_match:metrics['search_iterations'] = int(iter_match.group(1))cv_score_match = re.search(r'Best CV Score:\s*([\d.]+)', content)if cv_score_match:metrics['best_cv_score'] = float(cv_score_match.group(1))# Extract Test Performance metricsif 'Test Performance:' in content:test_section = content.split('Test Performance:')[1].split('Features:')[0]acc_match = re.search(r'Accuracy:\s*([\d.]+)', test_section)if acc_match:metrics['test_accuracy'] = float(acc_match.group(1))prec_match = re.search(r'Precision:\s*([\d.]+)', test_section)if prec_match:metrics['test_precision'] = float(prec_match.group(1))rec_match = re.search(r'Recall:\s*([\d.]+)', test_section)if rec_match:metrics['test_recall'] = float(rec_match.group(1))f1_match = re.search(r'F1 Score:\s*([\d.]+)', test_section)if f1_match:metrics['test_f1_score'] = float(f1_match.group(1))roc_match = re.search(r'ROC-AUC:\s*([\d.]+)', test_section)if roc_match:metrics['test_roc_auc'] = float(roc_match.group(1))# Extract number of featuresif 'Features:' in content:features_section = content.split('Features:')[1].strip()# Count features in the listfeature_list = re.findall(r"'([^']+)'", features_section)metrics['n_features'] = len(feature_list)metrics['feature_names'] = ', '.join(feature_list)# Get image pathsconfusion_path = os.path.join(model_dir, "evaluation_image_1.png")importance_path = os.path.join(model_dir, "evaluation_image_2.png")model_info_cache = {'version': model_registry.version,'metrics': metrics,'confusion_matrix_path': confusion_path if os.path.exists(confusion_path) else None,'feature_importance_path': importance_path if os.path.exists(importance_path) else None}logger.info(f"Loaded model metadata: version {model_registry.version}, {len(metrics)} metrics")except Exception as e:logger.error(f"Error loading model metadata: {e}")model_info_cache = Nonedef get_model_info() -> str:"""Get model information as HTML."""if not model_info_cache:return "<p>Model information not available. Try refreshing the page.</p>"metrics = model_info_cache.get('metrics', {})version = model_info_cache.get('version', 'unknown')model_name = pipeline.model_name if pipeline else "phishing_detector"# Check if we have any metricsif not metrics:return "<p>No metrics found in model artifacts.</p>"# Helper function to safely format metric valuesdef format_metric(key, default='N/A'):value = metrics.get(key, default)if value == default:return defaultif isinstance(value, float):return f"{value:.4f}"if isinstance(value, int):return str(value)return str(value)# Get model type from metrics or use defaultmodel_type = metrics.get('model_type', 'Unknown')html = f"""<div style="padding: 20px; background-color: #f5f5f5; border-radius: 10px; margin: 10px;"><h3>🤖 Model Information</h3><p><strong>Model Type:</strong> {model_type}</p><p><strong>Model Name:</strong> {model_name}</p><p><strong>Version:</strong> {version}</p><hr><h4>📊 Test Performance</h4><div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin: 15px 0;"><div style="padding: 10px; background-color: white; border-radius: 5px;"><strong>Accuracy:</strong> <span style="font-size: 1.2em; color: #2196F3;">{format_metric('test_accuracy')}</span></div><div style="padding: 10px; background-color: white; border-radius: 5px;"><strong>Precision:</strong> <span style="font-size: 1.2em; color: #4CAF50;">{format_metric('test_precision')}</span></div><div style="padding: 10px; background-color: white; border-radius: 5px;"><strong>Recall:</strong> <span style="font-size: 1.2em; color: #FF9800;">{format_metric('test_recall')}</span></div><div style="padding: 10px; background-color: white; border-radius: 5px;"><strong>F1 Score:</strong> <span style="font-size: 1.2em; color: #9C27B0;">{format_metric('test_f1_score')}</span></div><div style="padding: 10px; background-color: white; border-radius: 5px; grid-column: span 2;"><strong>ROC-AUC:</strong> <span style="font-size: 1.2em; color: #F44336;">{format_metric('test_roc_auc')}</span></div></div><hr><h4>🎯 Training Details</h4><p><strong>CV Folds:</strong> {format_metric('cv_folds')}</p><p><strong>Search Iterations:</strong> {format_metric('search_iterations')}</p><p><strong>Best CV Score:</strong> {format_metric('best_cv_score')}</p><p><strong>Number of Features:</strong> {format_metric('n_features')}</p>"""# Add feature names if availableif 'feature_names' in metrics:html += f"""<hr><h4>📝 Features Used</h4><p style="font-size: 0.9em; line-height: 1.6;">{metrics['feature_names']}</p>"""html += "</div>"return html141 unmodified linesgr.Markdown("### Model Evaluation Visualizations")with gr.Row():with gr.Column():gr.Markdown("#### Confusion Matrix")if model_info_cache and model_info_cache.get('confusion_matrix_path'):gr.Image(value=model_info_cache['confusion_matrix_path'], label="Confusion Matrix")else:gr.Markdown("*Confusion matrix image not available*")with gr.Column():gr.Markdown("#### Feature Importance")if model_info_cache and model_info_cache.get('feature_importance_path'):gr.Image(value=model_info_cache['feature_importance_path'], label="Feature Importance")else:gr.Markdown("*Feature importance image not available*")gr.Markdown("""
app.py+171-41