Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,121 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
# 1. Load
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
# This downloads the model automatically on first run.
|
| 9 |
-
print("Loading model... this may take a moment.")
|
| 10 |
-
pipe = pipeline("text-classification", model="pelz-y3mi/phishing-detector")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
| 18 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if not url:
|
| 20 |
return None
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
# The pipeline usually returns a list of dicts: [{'label': 'LABEL_1', 'score': 0.99}]
|
| 25 |
-
top_result = results[0]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Usually: LABEL_1 = Phishing, LABEL_0 = Legitimate (Safe) for this specific model type
|
| 33 |
-
if label == "LABEL_1":
|
| 34 |
-
final_label = "🚨 Phishing"
|
| 35 |
-
color = "red"
|
| 36 |
else:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return {final_label: score}
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
# Using a specific theme for a professional look
|
| 46 |
theme = gr.themes.Soft(
|
| 47 |
primary_hue="blue",
|
| 48 |
secondary_hue="slate",
|
| 49 |
-
).set(
|
| 50 |
-
loader_color="#FF0000",
|
| 51 |
-
slider_selected_color="#FF0000",
|
| 52 |
)
|
| 53 |
|
| 54 |
-
with gr.Blocks(theme=theme, title="PhishGuard
|
| 55 |
|
| 56 |
-
# Header
|
| 57 |
with gr.Row():
|
| 58 |
gr.Markdown(
|
| 59 |
"""
|
| 60 |
-
#
|
| 61 |
-
###
|
| 62 |
-
|
| 63 |
"""
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# Main
|
| 67 |
with gr.Row():
|
| 68 |
-
# Left Column: Input
|
| 69 |
with gr.Column(scale=1):
|
| 70 |
url_input = gr.Textbox(
|
| 71 |
lines=3,
|
| 72 |
placeholder="https://example.com",
|
| 73 |
-
label="
|
| 74 |
-
info="
|
| 75 |
)
|
| 76 |
-
submit_btn = gr.Button("
|
| 77 |
|
| 78 |
-
# Examples
|
| 79 |
gr.Examples(
|
| 80 |
examples=[
|
| 81 |
-
["https://
|
| 82 |
-
["http://
|
| 83 |
-
["https://huggingface.co"]
|
| 84 |
],
|
| 85 |
inputs=url_input
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# Right Column: Output
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
-
output_label = gr.Label(label="
|
| 91 |
-
|
| 92 |
-
# Additional info box
|
| 93 |
-
gr.Markdown(
|
| 94 |
-
"""
|
| 95 |
-
> **Note:** This tool uses the `pelz-y3mi/phishing-detector` model.
|
| 96 |
-
> While highly accurate, always double-check URLs manually before entering sensitive info.
|
| 97 |
-
"""
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
submit_btn.click(
|
| 102 |
fn=predict_phishing,
|
| 103 |
inputs=url_input,
|
| 104 |
outputs=output_label
|
| 105 |
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# 1. Load Your Specific Model
|
| 7 |
+
# -----------------------------
|
| 8 |
+
MODEL_PATH = "models/phishing_rf_model.pt"
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
print(f"Loading model from {MODEL_PATH}...")
|
| 11 |
+
|
| 12 |
+
# We use torch.load because the file extension is .pt
|
| 13 |
+
# map_location='cpu' ensures it works on servers without massive GPUs
|
| 14 |
+
try:
|
| 15 |
+
model = torch.load(MODEL_PATH, map_location=torch.device('cpu'))
|
| 16 |
+
print("✅ Model loaded successfully!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"❌ Failed to load model: {e}")
|
| 19 |
+
raise e
|
| 20 |
+
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# 2. Feature Extraction (No Scaler)
|
| 23 |
+
# -----------------------------
|
| 24 |
+
def extract_features(url: str) -> np.ndarray:
|
| 25 |
"""
|
| 26 |
+
Extracts the features the model expects.
|
| 27 |
+
Since we are skipping the scaler, we feed these raw numbers directly.
|
| 28 |
"""
|
| 29 |
+
length = len(url)
|
| 30 |
+
dots = url.count('.')
|
| 31 |
+
hyphens = url.count('-')
|
| 32 |
+
digits = sum(c.isdigit() for c in url)
|
| 33 |
+
at_sign = url.count('@')
|
| 34 |
+
|
| 35 |
+
# Create the array shape [1, 5] (1 sample, 5 features)
|
| 36 |
+
return np.array([[length, dots, hyphens, digits, at_sign]], dtype=float)
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# 3. Prediction Logic
|
| 40 |
+
# -----------------------------
|
| 41 |
+
def predict_phishing(url: str):
|
| 42 |
if not url:
|
| 43 |
return None
|
| 44 |
+
|
| 45 |
+
# 1. Extract features
|
| 46 |
+
features = extract_features(url)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# 2. Predict
|
| 49 |
+
# We assume the model inside the .pt file is a standard sklearn model
|
| 50 |
+
# (RandomForest) that supports .predict_proba()
|
| 51 |
+
try:
|
| 52 |
+
pred_prob = model.predict_proba(features)[0]
|
| 53 |
+
except AttributeError:
|
| 54 |
+
# Fallback if the model doesn't support probabilities
|
| 55 |
+
pred = model.predict(features)[0]
|
| 56 |
+
# Mock probabilities if exact confidence isn't available
|
| 57 |
+
pred_prob = [1.0, 0.0] if pred == 0 else [0.0, 1.0]
|
| 58 |
+
|
| 59 |
+
# 3. Format Output
|
| 60 |
+
# Assuming Index 0 = Safe, Index 1 = Phishing
|
| 61 |
+
label_index = pred_prob.argmax()
|
| 62 |
+
confidence = float(pred_prob[label_index])
|
| 63 |
|
| 64 |
+
if label_index == 1:
|
| 65 |
+
label = "🚨 Phishing"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
+
label = "✅ Safe"
|
| 68 |
+
|
| 69 |
+
return {label: confidence}
|
|
|
|
| 70 |
|
| 71 |
+
# -----------------------------
|
| 72 |
+
# 4. Unique Professional UI
|
| 73 |
+
# -----------------------------
|
|
|
|
| 74 |
theme = gr.themes.Soft(
|
| 75 |
primary_hue="blue",
|
| 76 |
secondary_hue="slate",
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
+
with gr.Blocks(theme=theme, title="PhishGuard Local") as iface:
|
| 80 |
|
| 81 |
+
# Header
|
| 82 |
with gr.Row():
|
| 83 |
gr.Markdown(
|
| 84 |
"""
|
| 85 |
+
# 🛡️ PhishGuard (Local Model)
|
| 86 |
+
### Custom Random Forest Detector
|
| 87 |
+
Running locally using your `phishing_rf_model.pt` file.
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# Main Interface
|
| 92 |
with gr.Row():
|
|
|
|
| 93 |
with gr.Column(scale=1):
|
| 94 |
url_input = gr.Textbox(
|
| 95 |
lines=3,
|
| 96 |
placeholder="https://example.com",
|
| 97 |
+
label="Check URL",
|
| 98 |
+
info="Paste the link you want to test."
|
| 99 |
)
|
| 100 |
+
submit_btn = gr.Button("Scan URL 🔍", variant="primary")
|
| 101 |
|
|
|
|
| 102 |
gr.Examples(
|
| 103 |
examples=[
|
| 104 |
+
["https://google.com"],
|
| 105 |
+
["http://fake-login-secure.com/update"]
|
|
|
|
| 106 |
],
|
| 107 |
inputs=url_input
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
+
output_label = gr.Label(label="Result")
|
| 112 |
+
gr.Markdown("> **Note:** Running without feature scaler. Results depend on raw feature interpretation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# Actions
|
| 115 |
submit_btn.click(
|
| 116 |
fn=predict_phishing,
|
| 117 |
inputs=url_input,
|
| 118 |
outputs=output_label
|
| 119 |
)
|
| 120 |
|
| 121 |
+
iface.launch(share=True)
|
|
|