Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,37 +2,55 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import matplotlib
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
import asyncio
|
| 7 |
-
|
| 8 |
matplotlib.use('Agg')
|
|
|
|
| 9 |
|
| 10 |
-
# Load the phishing detection model
|
| 11 |
model = pipeline('text-classification', model="Ajay1311/phish")
|
| 12 |
|
| 13 |
-
# Function to create a
|
| 14 |
-
def
|
|
|
|
| 15 |
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw={'projection': 'polar'})
|
| 16 |
|
|
|
|
| 17 |
gauge_min, gauge_max = 0, 100
|
|
|
|
|
|
|
|
|
|
| 18 |
confidence_pct = confidence * 100
|
| 19 |
-
color = 'red' if is_phishing else 'green'
|
| 20 |
-
label = f"PHISHING: {confidence_pct:.1f}%" if is_phishing else f"BENIGN: {confidence_pct:.1f}%"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
ax.set_rticks([]) # No radial ticks
|
| 26 |
ax.set_xticks([]) # No angular ticks
|
| 27 |
-
ax.spines['polar'].set_visible(False)
|
| 28 |
|
|
|
|
| 29 |
ax.text(0, 0, label, ha='center', va='center', fontsize=12, fontweight='bold')
|
|
|
|
|
|
|
| 30 |
ax.set_ylim(0, 1.5)
|
| 31 |
|
| 32 |
return fig
|
| 33 |
|
| 34 |
-
# Function to analyze
|
| 35 |
-
|
| 36 |
if not text.strip():
|
| 37 |
return {
|
| 38 |
"result": "Please enter a URL or email text to analyze",
|
|
@@ -40,23 +58,25 @@ async def analyze_phishing(text):
|
|
| 40 |
"analysis": "No input provided"
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# Get model prediction
|
| 44 |
-
result =
|
| 45 |
|
|
|
|
| 46 |
label = result[0]['label']
|
| 47 |
score = result[0]['score']
|
| 48 |
|
|
|
|
| 49 |
is_phishing = label.lower() == 'phishing'
|
| 50 |
|
| 51 |
-
# Create
|
| 52 |
-
chart =
|
| 53 |
|
| 54 |
# Format the raw JSON for display
|
| 55 |
raw_json = f"""```json
|
| 56 |
{str(result)}
|
| 57 |
```"""
|
| 58 |
|
| 59 |
-
# Generate analysis
|
| 60 |
if is_phishing:
|
| 61 |
analysis = f"""
|
| 62 |
⚠️ **POTENTIAL PHISHING DETECTED**
|
|
@@ -65,6 +85,14 @@ async def analyze_phishing(text):
|
|
| 65 |
|
| 66 |
**Raw Model Output:**
|
| 67 |
{raw_json}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
"""
|
| 69 |
else:
|
| 70 |
analysis = f"""
|
|
@@ -74,6 +102,13 @@ async def analyze_phishing(text):
|
|
| 74 |
|
| 75 |
**Raw Model Output:**
|
| 76 |
{raw_json}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"""
|
| 78 |
|
| 79 |
return {
|
|
@@ -82,6 +117,15 @@ async def analyze_phishing(text):
|
|
| 82 |
"analysis": analysis
|
| 83 |
}
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# Create Gradio interface with custom theme
|
| 86 |
theme = gr.themes.Soft(
|
| 87 |
primary_hue="blue",
|
|
@@ -94,7 +138,6 @@ theme = gr.themes.Soft(
|
|
| 94 |
block_title_text_color="*primary_500",
|
| 95 |
)
|
| 96 |
|
| 97 |
-
# Gradio interface
|
| 98 |
with gr.Blocks(theme=theme, css="""
|
| 99 |
.footer {text-align: center; margin-top: 20px; color: #666;}
|
| 100 |
.container {max-width: 800px; margin: 0 auto;}
|
|
@@ -106,19 +149,29 @@ with gr.Blocks(theme=theme, css="""
|
|
| 106 |
.safe {color: #2ca02c; background: rgba(44, 160, 44, 0.1); padding: 10px; border-radius: 5px;}
|
| 107 |
""") as demo:
|
| 108 |
with gr.Column(elem_classes="container"):
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
# Input
|
| 112 |
with gr.Group():
|
| 113 |
gr.Markdown("### Enter URL or Email Text to Analyze")
|
| 114 |
-
input_text = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
#
|
| 117 |
with gr.Row():
|
| 118 |
analyze_btn = gr.Button("🔍 Analyze", variant="primary")
|
| 119 |
clear_btn = gr.Button("🗑️ Clear")
|
| 120 |
|
| 121 |
-
# Output
|
| 122 |
with gr.Group():
|
| 123 |
gr.Markdown("### Analysis Results")
|
| 124 |
with gr.Row():
|
|
@@ -129,13 +182,8 @@ with gr.Blocks(theme=theme, css="""
|
|
| 129 |
|
| 130 |
analysis_md = gr.Markdown(label="Detailed Analysis")
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
["Please verify your account by clicking on the link: http://amaz0n-security-alert.com/verify"],
|
| 135 |
-
["Your Netflix subscription is about to expire. Update payment at netfl1x-accounts.com/renew"],
|
| 136 |
-
["Meeting scheduled for tomorrow at 2 PM in the conference room. Agenda attached."]
|
| 137 |
-
]
|
| 138 |
-
|
| 139 |
gr.Examples(
|
| 140 |
examples=examples,
|
| 141 |
inputs=input_text,
|
|
@@ -145,7 +193,11 @@ with gr.Blocks(theme=theme, css="""
|
|
| 145 |
)
|
| 146 |
|
| 147 |
# Footer
|
| 148 |
-
gr.HTML("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Set up event handlers
|
| 151 |
analyze_btn.click(
|
|
@@ -159,6 +211,21 @@ with gr.Blocks(theme=theme, css="""
|
|
| 159 |
inputs=None,
|
| 160 |
outputs=[input_text, result_plot, analysis_md]
|
| 161 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import matplotlib
|
|
|
|
|
|
|
|
|
|
| 5 |
matplotlib.use('Agg')
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Load the phishing detection model
|
| 9 |
model = pipeline('text-classification', model="Ajay1311/phish")
|
| 10 |
|
| 11 |
+
# Function to create a gauge chart for confidence visualization
|
| 12 |
+
def create_gauge_chart(confidence, is_phishing):
|
| 13 |
+
# Create figure and axis
|
| 14 |
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw={'projection': 'polar'})
|
| 15 |
|
| 16 |
+
# Set gauge properties
|
| 17 |
gauge_min, gauge_max = 0, 100
|
| 18 |
+
gauge_range = gauge_max - gauge_min
|
| 19 |
+
|
| 20 |
+
# Convert confidence to percentage
|
| 21 |
confidence_pct = confidence * 100
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Set colors based on phishing status
|
| 24 |
+
if is_phishing:
|
| 25 |
+
color = 'red'
|
| 26 |
+
label = f"PHISHING DETECTED: {confidence_pct:.1f}% Confidence"
|
| 27 |
+
else:
|
| 28 |
+
color = 'green'
|
| 29 |
+
label = f"BENIGN: {confidence_pct:.1f}% Confidence"
|
| 30 |
+
|
| 31 |
+
# Plot gauge
|
| 32 |
+
theta = np.linspace(np.pi/2, -np.pi/2, 100)
|
| 33 |
+
ax.plot(theta, [1]*100, color='lightgray', linewidth=10, alpha=0.5)
|
| 34 |
|
| 35 |
+
# Plot filled portion
|
| 36 |
+
filled_theta = np.linspace(np.pi/2, np.pi/2 - (confidence_pct/100) * np.pi, 100)
|
| 37 |
+
ax.plot(filled_theta, [1]*len(filled_theta), color=color, linewidth=10)
|
| 38 |
+
|
| 39 |
+
# Customize gauge appearance
|
| 40 |
ax.set_rticks([]) # No radial ticks
|
| 41 |
ax.set_xticks([]) # No angular ticks
|
| 42 |
+
ax.spines['polar'].set_visible(False) # Hide the circular spine
|
| 43 |
|
| 44 |
+
# Add text
|
| 45 |
ax.text(0, 0, label, ha='center', va='center', fontsize=12, fontweight='bold')
|
| 46 |
+
|
| 47 |
+
# Set limits
|
| 48 |
ax.set_ylim(0, 1.5)
|
| 49 |
|
| 50 |
return fig
|
| 51 |
|
| 52 |
+
# Function to analyze and classify text
|
| 53 |
+
def analyze_phishing(text):
|
| 54 |
if not text.strip():
|
| 55 |
return {
|
| 56 |
"result": "Please enter a URL or email text to analyze",
|
|
|
|
| 58 |
"analysis": "No input provided"
|
| 59 |
}
|
| 60 |
|
| 61 |
+
# Get model prediction - this returns the format you showed
|
| 62 |
+
result = model(text)
|
| 63 |
|
| 64 |
+
# Extract label and score from your specific output format
|
| 65 |
label = result[0]['label']
|
| 66 |
score = result[0]['score']
|
| 67 |
|
| 68 |
+
# Determine if phishing
|
| 69 |
is_phishing = label.lower() == 'phishing'
|
| 70 |
|
| 71 |
+
# Create visualization
|
| 72 |
+
chart = create_gauge_chart(score, is_phishing)
|
| 73 |
|
| 74 |
# Format the raw JSON for display
|
| 75 |
raw_json = f"""```json
|
| 76 |
{str(result)}
|
| 77 |
```"""
|
| 78 |
|
| 79 |
+
# Generate detailed analysis
|
| 80 |
if is_phishing:
|
| 81 |
analysis = f"""
|
| 82 |
⚠️ **POTENTIAL PHISHING DETECTED**
|
|
|
|
| 85 |
|
| 86 |
**Raw Model Output:**
|
| 87 |
{raw_json}
|
| 88 |
+
|
| 89 |
+
**Common phishing indicators:**
|
| 90 |
+
- Suspicious URLs or email domains
|
| 91 |
+
- Urgent requests for personal information
|
| 92 |
+
- Grammatical errors or unusual formatting
|
| 93 |
+
- Requests to click on suspicious links
|
| 94 |
+
|
| 95 |
+
**Recommendation:** Exercise caution and verify the source before proceeding.
|
| 96 |
"""
|
| 97 |
else:
|
| 98 |
analysis = f"""
|
|
|
|
| 102 |
|
| 103 |
**Raw Model Output:**
|
| 104 |
{raw_json}
|
| 105 |
+
|
| 106 |
+
**Always exercise caution when:**
|
| 107 |
+
- Sharing personal information online
|
| 108 |
+
- Clicking on links from unknown sources
|
| 109 |
+
- Responding to unexpected requests
|
| 110 |
+
|
| 111 |
+
**Recommendation:** Continue with normal caution.
|
| 112 |
"""
|
| 113 |
|
| 114 |
return {
|
|
|
|
| 117 |
"analysis": analysis
|
| 118 |
}
|
| 119 |
|
| 120 |
+
# Example inputs for demonstration
|
| 121 |
+
examples = [
|
| 122 |
+
["Please verify your account by clicking on the link: http://amaz0n-security-alert.com/verify"],
|
| 123 |
+
["Your Netflix subscription is about to expire. Update payment at netfl1x-accounts.com/renew"],
|
| 124 |
+
["Meeting scheduled for tomorrow at 2 PM in the conference room. Agenda attached."],
|
| 125 |
+
["Your Amazon order #12345 has been shipped and will arrive on Friday."],
|
| 126 |
+
["URGENT: Your account has been compromised! Click here to reset: bit.ly/2xCvZ9"]
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
# Create Gradio interface with custom theme
|
| 130 |
theme = gr.themes.Soft(
|
| 131 |
primary_hue="blue",
|
|
|
|
| 138 |
block_title_text_color="*primary_500",
|
| 139 |
)
|
| 140 |
|
|
|
|
| 141 |
with gr.Blocks(theme=theme, css="""
|
| 142 |
.footer {text-align: center; margin-top: 20px; color: #666;}
|
| 143 |
.container {max-width: 800px; margin: 0 auto;}
|
|
|
|
| 149 |
.safe {color: #2ca02c; background: rgba(44, 160, 44, 0.1); padding: 10px; border-radius: 5px;}
|
| 150 |
""") as demo:
|
| 151 |
with gr.Column(elem_classes="container"):
|
| 152 |
+
# Header
|
| 153 |
+
gr.HTML("""
|
| 154 |
+
<div class="header">
|
| 155 |
+
<h1>🛡️ PhishGuard AI</h1>
|
| 156 |
+
<p>Advanced phishing detection powered by machine learning</p>
|
| 157 |
+
</div>
|
| 158 |
+
""")
|
| 159 |
|
| 160 |
+
# Input section
|
| 161 |
with gr.Group():
|
| 162 |
gr.Markdown("### Enter URL or Email Text to Analyze")
|
| 163 |
+
input_text = gr.Textbox(
|
| 164 |
+
placeholder="Paste a suspicious URL, email, or message here...",
|
| 165 |
+
lines=5,
|
| 166 |
+
label="Input Text"
|
| 167 |
+
)
|
| 168 |
|
| 169 |
+
# Action buttons
|
| 170 |
with gr.Row():
|
| 171 |
analyze_btn = gr.Button("🔍 Analyze", variant="primary")
|
| 172 |
clear_btn = gr.Button("🗑️ Clear")
|
| 173 |
|
| 174 |
+
# Output section
|
| 175 |
with gr.Group():
|
| 176 |
gr.Markdown("### Analysis Results")
|
| 177 |
with gr.Row():
|
|
|
|
| 182 |
|
| 183 |
analysis_md = gr.Markdown(label="Detailed Analysis")
|
| 184 |
|
| 185 |
+
# Examples section
|
| 186 |
+
gr.Markdown("### Example Inputs")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
gr.Examples(
|
| 188 |
examples=examples,
|
| 189 |
inputs=input_text,
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
# Footer
|
| 196 |
+
gr.HTML("""
|
| 197 |
+
<div class="footer">
|
| 198 |
+
<p>Created for Cybersecurity Hackathon 2025 | PhishGuard AI Team</p>
|
| 199 |
+
</div>
|
| 200 |
+
""")
|
| 201 |
|
| 202 |
# Set up event handlers
|
| 203 |
analyze_btn.click(
|
|
|
|
| 211 |
inputs=None,
|
| 212 |
outputs=[input_text, result_plot, analysis_md]
|
| 213 |
)
|
| 214 |
+
|
| 215 |
+
# Update CSS class based on result
|
| 216 |
+
def update_result_class(result):
|
| 217 |
+
if "PHISHING DETECTED" in result:
|
| 218 |
+
return "phishing" # Set class to "phishing" for phishing results
|
| 219 |
+
elif "BENIGN" in result:
|
| 220 |
+
return "safe" # Set class to "safe" for benign results
|
| 221 |
+
return "" # Default to no class if no match
|
| 222 |
+
|
| 223 |
+
result_text.change(
|
| 224 |
+
update_result_class,
|
| 225 |
+
inputs=result_text,
|
| 226 |
+
outputs=result_text # Use the component itself as output
|
| 227 |
+
)
|
| 228 |
|
| 229 |
+
# Launch the app
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
demo.launch()
|