duclo90 commited on
Commit
7700e60
Β·
verified Β·
1 Parent(s): 7eb26e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -6
app.py CHANGED
@@ -9,17 +9,221 @@ model.eval()
9
  label_map = {0: "Safe", 1: "Phishing"}
10
 
11
  def classify(text):
 
 
 
12
  inputs = tokenizer(text, return_tensors="pt")
13
  with torch.no_grad():
14
  logits = model(**inputs).logits
 
15
  pred = torch.argmax(logits, dim=-1).item()
16
- return label_map[pred]
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- demo = gr.Interface(
19
- fn=classify,
20
- inputs="text",
21
- outputs="text"
22
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  demo.launch()
25
 
 
9
  label_map = {0: "Safe", 1: "Phishing"}
10
 
11
  def classify(text):
12
+ if not text.strip():
13
+ return "⚠️ Please enter some text to analyze", None
14
+
15
  inputs = tokenizer(text, return_tensors="pt")
16
  with torch.no_grad():
17
  logits = model(**inputs).logits
18
+ probs = torch.nn.functional.softmax(logits, dim=-1)
19
  pred = torch.argmax(logits, dim=-1).item()
20
+ confidence = probs[0][pred].item() * 100
21
+
22
+ result = label_map[pred]
23
+
24
+ if result == "Safe":
25
+ status = f"βœ… **SAFE** - This content appears legitimate"
26
+ color_indicator = "🟒"
27
+ else:
28
+ status = f"🚨 **PHISHING DETECTED** - This content may be malicious"
29
+ color_indicator = "πŸ”΄"
30
+
31
+ detailed_result = f"""
32
+ {color_indicator} **Result:** {result}
33
+ πŸ“Š **Confidence:** {confidence:.2f}%
34
 
35
+ {status}
36
+ """
37
+
38
+ return detailed_result.strip(), confidence
39
+
40
+ # Custom CSS for a modern cybersecurity aesthetic
41
+ custom_css = """
42
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
43
+
44
+ * {
45
+ font-family: 'Inter', sans-serif !important;
46
+ }
47
+
48
+ .gradio-container {
49
+ background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 50%, #16213e 100%) !important;
50
+ color: #e0e0e0 !important;
51
+ }
52
+
53
+ #component-0 {
54
+ max-width: 900px !important;
55
+ margin: 0 auto !important;
56
+ padding: 2rem !important;
57
+ }
58
+
59
+ .contain {
60
+ background: rgba(255, 255, 255, 0.03) !important;
61
+ backdrop-filter: blur(10px) !important;
62
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
63
+ border-radius: 16px !important;
64
+ padding: 2rem !important;
65
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3) !important;
66
+ }
67
+
68
+ .input-text textarea {
69
+ background: rgba(255, 255, 255, 0.05) !important;
70
+ border: 2px solid rgba(100, 200, 255, 0.3) !important;
71
+ border-radius: 12px !important;
72
+ color: #e0e0e0 !important;
73
+ font-size: 16px !important;
74
+ padding: 1rem !important;
75
+ transition: all 0.3s ease !important;
76
+ }
77
+
78
+ .input-text textarea:focus {
79
+ border-color: rgba(100, 200, 255, 0.6) !important;
80
+ box-shadow: 0 0 20px rgba(100, 200, 255, 0.2) !important;
81
+ outline: none !important;
82
+ }
83
+
84
+ button.primary {
85
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
86
+ border: none !important;
87
+ border-radius: 12px !important;
88
+ color: white !important;
89
+ font-weight: 600 !important;
90
+ padding: 0.75rem 2rem !important;
91
+ font-size: 16px !important;
92
+ transition: all 0.3s ease !important;
93
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
94
+ }
95
+
96
+ button.primary:hover {
97
+ transform: translateY(-2px) !important;
98
+ box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
99
+ }
100
+
101
+ .output-text {
102
+ background: rgba(255, 255, 255, 0.05) !important;
103
+ border: 2px solid rgba(100, 200, 255, 0.2) !important;
104
+ border-radius: 12px !important;
105
+ padding: 1.5rem !important;
106
+ color: #e0e0e0 !important;
107
+ font-size: 16px !important;
108
+ line-height: 1.8 !important;
109
+ }
110
+
111
+ h1 {
112
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
113
+ -webkit-background-clip: text;
114
+ -webkit-text-fill-color: transparent;
115
+ background-clip: text;
116
+ font-weight: 700 !important;
117
+ font-size: 2.5rem !important;
118
+ margin-bottom: 0.5rem !important;
119
+ text-align: center !important;
120
+ }
121
+
122
+ .description {
123
+ color: #b0b0b0 !important;
124
+ text-align: center !important;
125
+ font-size: 1.1rem !important;
126
+ margin-bottom: 2rem !important;
127
+ }
128
+
129
+ .footer {
130
+ text-align: center !important;
131
+ margin-top: 2rem !important;
132
+ padding-top: 1.5rem !important;
133
+ border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
134
+ color: #808080 !important;
135
+ font-size: 0.9rem !important;
136
+ }
137
+
138
+ .progress {
139
+ background: rgba(100, 200, 255, 0.2) !important;
140
+ border-radius: 8px !important;
141
+ }
142
+
143
+ .progress-bar {
144
+ background: linear-gradient(90deg, #667eea 0%, #764ba2 100%) !important;
145
+ }
146
+ """
147
+
148
+ # Create the interface with enhanced design
149
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
150
+ gr.Markdown(
151
+ """
152
+ # πŸ›‘οΈ Phishing Content Detector
153
+ ### AI-Powered Security Analysis
154
+ """
155
+ )
156
+
157
+ gr.Markdown(
158
+ """
159
+ <p class="description">
160
+ Protect yourself from phishing attacks. Paste suspicious emails, messages, or text below for instant AI analysis.
161
+ </p>
162
+ """,
163
+ elem_classes="description"
164
+ )
165
+
166
+ with gr.Row():
167
+ with gr.Column(scale=1):
168
+ input_text = gr.Textbox(
169
+ label="πŸ“ Content to Analyze",
170
+ placeholder="Paste suspicious email, message, or text here...\n\nExample: 'Your account has been locked. Click here immediately to verify your identity and avoid suspension.'",
171
+ lines=8,
172
+ elem_classes="input-text"
173
+ )
174
+
175
+ analyze_btn = gr.Button("πŸ” Analyze Content", variant="primary", size="lg")
176
+
177
+ gr.Markdown(
178
+ """
179
+ <div class="footer">
180
+ <strong>πŸ’‘ Tips:</strong> Look for urgent language, suspicious links, requests for personal information, or grammar errors.
181
+ <br>
182
+ <em>Powered by AI β€’ Model: duclo90/PhishingClassifier</em>
183
+ </div>
184
+ """
185
+ )
186
+
187
+ with gr.Row():
188
+ with gr.Column(scale=1):
189
+ output_text = gr.Textbox(
190
+ label="🎯 Analysis Result",
191
+ lines=6,
192
+ elem_classes="output-text"
193
+ )
194
+
195
+ confidence_slider = gr.Slider(
196
+ label="Confidence Level",
197
+ minimum=0,
198
+ maximum=100,
199
+ value=0,
200
+ interactive=False,
201
+ elem_classes="progress"
202
+ )
203
+
204
+ # Examples section
205
+ gr.Examples(
206
+ examples=[
207
+ ["Congratulations! You've won $1,000,000! Click here now to claim your prize before it expires!"],
208
+ ["Hi team, the quarterly meeting is scheduled for next Tuesday at 2 PM in Conference Room B."],
209
+ ["URGENT: Your account will be suspended. Verify your identity immediately by clicking this link."],
210
+ ["Your package delivery failed. Update your address at: legitimate-shipping-company.com"],
211
+ ],
212
+ inputs=input_text,
213
+ label="πŸ“‹ Try These Examples"
214
+ )
215
+
216
+ analyze_btn.click(
217
+ fn=classify,
218
+ inputs=input_text,
219
+ outputs=[output_text, confidence_slider]
220
+ )
221
+
222
+ input_text.submit(
223
+ fn=classify,
224
+ inputs=input_text,
225
+ outputs=[output_text, confidence_slider]
226
+ )
227
 
228
  demo.launch()
229