Spaces:
Sleeping
Sleeping
Update import paths in test_inference_pipeline.py for phishing detection model
Browse files
app.py
CHANGED
|
@@ -54,6 +54,44 @@ def initialize_app():
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def gradio_interface(url: str) -> Tuple[str, str, str]:
|
| 58 |
"""
|
| 59 |
Gradio interface function.
|
|
@@ -151,42 +189,80 @@ def create_gradio_app():
|
|
| 151 |
"""
|
| 152 |
)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
-
with gr.Column(scale=1):
|
| 162 |
-
submit_btn = gr.Button("Check URL", variant="primary", size="lg")
|
| 163 |
-
|
| 164 |
-
with gr.Row():
|
| 165 |
-
result_output = gr.HTML(label="Prediction")
|
| 166 |
-
|
| 167 |
-
with gr.Row():
|
| 168 |
-
confidence_output = gr.HTML(label="Confidence")
|
| 169 |
-
|
| 170 |
-
with gr.Row():
|
| 171 |
-
details_output = gr.HTML(label="Details")
|
| 172 |
-
|
| 173 |
-
# Example URLs
|
| 174 |
-
gr.Markdown("### Example URLs to Try:")
|
| 175 |
-
gr.Examples(
|
| 176 |
-
examples=[
|
| 177 |
-
["https://google.com"],
|
| 178 |
-
["https://github.com"],
|
| 179 |
-
["https://facebook.com"],
|
| 180 |
-
],
|
| 181 |
-
inputs=url_input,
|
| 182 |
-
)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
gr.Markdown(
|
| 192 |
"""
|
|
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
|
| 57 |
+
def get_model_info() -> str:
|
| 58 |
+
"""Get model information as HTML."""
|
| 59 |
+
if not pipeline:
|
| 60 |
+
return "<p>Model information not available</p>"
|
| 61 |
+
|
| 62 |
+
# TODO: Check if pipeline has model_metrics attribute
|
| 63 |
+
metrics = getattr(pipeline, 'model_metrics', None)
|
| 64 |
+
if not metrics:
|
| 65 |
+
return "<p>Model metrics not available</p>"
|
| 66 |
+
|
| 67 |
+
version = getattr(pipeline, 'model_version', 'latest')
|
| 68 |
+
|
| 69 |
+
html = f"""
|
| 70 |
+
<div style="padding: 20px; background-color: #f5f5f5; border-radius: 10px; margin: 10px;">
|
| 71 |
+
<h3>Model Information</h3>
|
| 72 |
+
<p><strong>Model Name:</strong> {pipeline.model_name}</p>
|
| 73 |
+
<p><strong>Model Version:</strong> {version}</p>
|
| 74 |
+
<hr>
|
| 75 |
+
<h4>Performance Metrics</h4>
|
| 76 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
|
| 77 |
+
<div><strong>Test Accuracy:</strong> {metrics.get('test_accuracy', 'N/A'):.4f}</div>
|
| 78 |
+
<div><strong>Test Precision:</strong> {metrics.get('test_precision', 'N/A'):.4f}</div>
|
| 79 |
+
<div><strong>Test Recall:</strong> {metrics.get('test_recall', 'N/A'):.4f}</div>
|
| 80 |
+
<div><strong>Test F1 Score:</strong> {metrics.get('test_f1_score', 'N/A'):.4f}</div>
|
| 81 |
+
<div><strong>Test ROC-AUC:</strong> {metrics.get('test_roc_auc', 'N/A'):.4f}</div>
|
| 82 |
+
<div><strong>Validation F1 Score:</strong> {metrics.get('val_f1_score', 'N/A'):.4f}</div>
|
| 83 |
+
</div>
|
| 84 |
+
<hr>
|
| 85 |
+
<h4>Training Information</h4>
|
| 86 |
+
<p><strong>Number of Features:</strong> {metrics.get('n_features', 'N/A')}</p>
|
| 87 |
+
<p><strong>Training Samples:</strong> {metrics.get('n_train_samples', 'N/A')}</p>
|
| 88 |
+
<p><strong>Test Samples:</strong> {metrics.get('n_test_samples', 'N/A')}</p>
|
| 89 |
+
<p><strong>Best CV Score:</strong> {metrics.get('best_cv_score', 'N/A'):.4f}</p>
|
| 90 |
+
</div>
|
| 91 |
+
"""
|
| 92 |
+
return html
|
| 93 |
+
|
| 94 |
+
|
| 95 |
def gradio_interface(url: str) -> Tuple[str, str, str]:
|
| 96 |
"""
|
| 97 |
Gradio interface function.
|
|
|
|
| 189 |
"""
|
| 190 |
)
|
| 191 |
|
| 192 |
+
# Create tabs for URL Checker and Model Info
|
| 193 |
+
with gr.Tabs():
|
| 194 |
+
# Tab 1: URL Checker
|
| 195 |
+
with gr.Tab("🔍 URL Checker"):
|
| 196 |
+
with gr.Row():
|
| 197 |
+
with gr.Column(scale=3):
|
| 198 |
+
url_input = gr.Textbox(
|
| 199 |
+
label="URL to Check",
|
| 200 |
+
placeholder="Enter URL (e.g., example.com or https://example.com)",
|
| 201 |
+
lines=1
|
| 202 |
+
)
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
submit_btn = gr.Button("Check URL", variant="primary", size="lg")
|
| 205 |
+
|
| 206 |
+
with gr.Row():
|
| 207 |
+
result_output = gr.HTML(label="Prediction")
|
| 208 |
+
|
| 209 |
+
with gr.Row():
|
| 210 |
+
confidence_output = gr.HTML(label="Confidence")
|
| 211 |
+
|
| 212 |
+
with gr.Row():
|
| 213 |
+
details_output = gr.HTML(label="Details")
|
| 214 |
+
|
| 215 |
+
# Example URLs
|
| 216 |
+
gr.Markdown("### Example URLs to Try:")
|
| 217 |
+
gr.Examples(
|
| 218 |
+
examples=[
|
| 219 |
+
["https://google.com"],
|
| 220 |
+
["https://github.com"],
|
| 221 |
+
["https://facebook.com"],
|
| 222 |
+
],
|
| 223 |
+
inputs=url_input,
|
| 224 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
+
# Connect button to function
|
| 227 |
+
submit_btn.click(
|
| 228 |
+
fn=gradio_interface,
|
| 229 |
+
inputs=url_input,
|
| 230 |
+
outputs=[result_output, confidence_output, details_output]
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# Tab 2: Model Information
|
| 234 |
+
with gr.Tab("📊 Model Info"):
|
| 235 |
+
gr.HTML(value=get_model_info(), label="Model Statistics")
|
| 236 |
+
|
| 237 |
+
gr.Markdown("### Model Evaluation Visualizations")
|
| 238 |
+
|
| 239 |
+
# TODO: Check if pipeline has model_dir attribute to access images
|
| 240 |
+
model_dir = getattr(pipeline, 'model_dir', None) if pipeline else None
|
| 241 |
+
|
| 242 |
+
with gr.Row():
|
| 243 |
+
with gr.Column():
|
| 244 |
+
gr.Markdown("#### Confusion Matrix")
|
| 245 |
+
# TODO: Verify the image filename in Hopsworks model artifacts
|
| 246 |
+
if model_dir:
|
| 247 |
+
confusion_path = os.path.join(model_dir, "confusion_matrices.png")
|
| 248 |
+
if os.path.exists(confusion_path):
|
| 249 |
+
gr.Image(value=confusion_path, label="Confusion Matrix")
|
| 250 |
+
else:
|
| 251 |
+
gr.Markdown("*Confusion matrix image not found*")
|
| 252 |
+
else:
|
| 253 |
+
gr.Markdown("*Model directory not available*")
|
| 254 |
+
|
| 255 |
+
with gr.Column():
|
| 256 |
+
gr.Markdown("#### Feature Importance")
|
| 257 |
+
# TODO: Verify the image filename in Hopsworks model artifacts
|
| 258 |
+
if model_dir:
|
| 259 |
+
importance_path = os.path.join(model_dir, "feature_importance.png")
|
| 260 |
+
if os.path.exists(importance_path):
|
| 261 |
+
gr.Image(value=importance_path, label="Feature Importance")
|
| 262 |
+
else:
|
| 263 |
+
gr.Markdown("*Feature importance image not found*")
|
| 264 |
+
else:
|
| 265 |
+
gr.Markdown("*Model directory not available*")
|
| 266 |
|
| 267 |
gr.Markdown(
|
| 268 |
"""
|