abdull4h commited on
Commit
d15086a
Β·
verified Β·
1 Parent(s): a9c27e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -25
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
 
 
2
  from transformers import pipeline
3
  import numpy as np
4
 
5
  # Initialize pipelines
6
- ocr = pipeline("image-to-text", model="microsoft/trocr-base-printed")
7
- classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
8
 
9
  # Common phishing indicators
10
  SUSPICIOUS_PHRASES = [
@@ -14,6 +16,9 @@ SUSPICIOUS_PHRASES = [
14
  "security alert", "confirm your identity", "gift card", "lottery winner"
15
  ]
16
 
 
 
 
17
  def extract_text_from_image(image):
18
  if image is None:
19
  return ""
@@ -67,13 +72,15 @@ def analyze_text(text):
67
  </div>
68
  """
69
 
70
- # Find suspicious phrases
71
  found_phrases = []
72
  text_lower = text.lower()
73
  for phrase in SUSPICIOUS_PHRASES:
74
  if phrase in text_lower:
75
  found_phrases.append(phrase)
76
 
 
 
77
  # Generate detailed analysis report with modern styling
78
  report = [
79
  "<div style='background: white; padding: 24px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05);'>",
@@ -88,17 +95,28 @@ def analyze_text(text):
88
  "</div>"
89
  ]
90
 
91
- if found_phrases:
92
- report.extend([
93
- "<div style='margin-top: 20px;'>",
94
- "<h4 style='color: #d32f2f;'>🚩 Suspicious Elements Detected:</h4>",
95
- "<ul style='list-style-type: none; padding-left: 0;'>"
96
- ])
97
- for phrase in found_phrases:
98
- report.append(f"<li style='margin-bottom: 8px; padding: 8px 12px; background: #ffebee; border-radius: 6px;'>Found: '{phrase}'</li>")
99
- report.append("</ul></div>")
 
 
 
 
 
 
 
 
 
 
 
100
  else:
101
- report.append("<p style='color: #2e7d32;'>βœ… No common suspicious phrases detected.</p>")
102
 
103
  report.append("<div style='margin-top: 20px; padding: 16px; background: #e3f2fd; border-radius: 8px;'>")
104
  if confidence > 0.9:
@@ -137,16 +155,26 @@ custom_theme = gr.themes.Soft().set(
137
  input_background_fill="white",
138
  button_primary_background_fill="#1a237e",
139
  button_primary_text_color="white",
 
 
140
  )
141
 
142
- # Create Gradio interface with modern design
143
- with gr.Blocks(theme=custom_theme, css="""
144
  .container { max-width: 1000px; margin: auto; }
145
  .header { text-align: center; margin-bottom: 2rem; }
146
  .tool-description { max-width: 800px; margin: 0 auto 2rem auto; }
147
  .input-section { margin-bottom: 2rem; }
148
  .analysis-section { margin-top: 2rem; }
149
- """) as demo:
 
 
 
 
 
 
 
 
150
  gr.HTML("""
151
  <div class="header">
152
  <h1 style="color: #1a237e; font-size: 2.5rem; margin-bottom: 1rem;">πŸ›‘οΈ AI Phishing Guard</h1>
@@ -160,17 +188,17 @@ with gr.Blocks(theme=custom_theme, css="""
160
  <div class="tool-description">
161
  <h3 style="color: #1a237e;">How to Use</h3>
162
  <ol style="color: #555; line-height: 1.6;">
163
- <li>Either paste message text or upload a screenshot</li>
164
- <li>Click 'Analyze' to check for phishing indicators</li>
165
- <li>Review the detailed analysis results</li>
166
  </ol>
167
  <div style="background: #e3f2fd; padding: 16px; border-radius: 8px; margin-top: 1rem;">
168
  <h4 style="color: #1a237e; margin-top: 0;">This tool detects:</h4>
169
  <ul style="color: #555; margin-bottom: 0;">
170
  <li>Suspicious language patterns</li>
171
  <li>Common phishing phrases</li>
 
172
  <li>Urgency indicators</li>
173
- <li>Security threat language</li>
174
  </ul>
175
  </div>
176
  </div>
@@ -192,31 +220,49 @@ with gr.Blocks(theme=custom_theme, css="""
192
  elem_id="image_input"
193
  )
194
 
195
- analyze_button = gr.Button("πŸ” Analyze", variant="primary", size="lg")
 
 
 
 
196
 
197
- with gr.Column(visible=True) as output_col:
198
  alert_html = gr.HTML()
199
  analysis = gr.HTML()
200
 
201
  # Examples
202
  examples = [
203
- ["Subject: URGENT - Account Security Alert\n\nDear User,\n\nWe detected unusual activity in your account. Click here immediately to verify your identity and reset your password. If you don't respond within 24 hours, your account will be suspended.\n\nBank Security Team", None],
204
  ["Subject: Team Meeting Tomorrow\n\nHi everyone,\n\nJust a reminder that we have our weekly team meeting tomorrow at 10 AM in the main conference room. Please bring your project updates.\n\nBest regards,\nSarah", None],
205
  ]
206
 
207
  gr.Examples(
208
  examples=examples,
209
  inputs=[text_input, image_input],
210
- outputs=[alert_html, analysis],
211
  fn=process_input,
212
  cache_examples=True
213
  )
214
 
215
- analyze_button.click(
 
 
 
 
216
  fn=process_input,
217
  inputs=[text_input, image_input],
218
  outputs=[alert_html, analysis, output_col]
219
  )
 
 
 
 
 
 
 
 
 
 
220
 
221
  # Launch the app
222
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import re
3
+ import html
4
  from transformers import pipeline
5
  import numpy as np
6
 
7
  # Initialize pipelines
8
+ ocr = pipeline("image-to-text", model="microsoft/trocr-base-printed") # Consider upgrading to trocr-large-printed
9
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") # Consider fine-tuning for phishing
10
 
11
  # Common phishing indicators
12
  SUSPICIOUS_PHRASES = [
 
16
  "security alert", "confirm your identity", "gift card", "lottery winner"
17
  ]
18
 
19
+ # URL detection regex
20
+ URL_PATTERN = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
21
+
22
  def extract_text_from_image(image):
23
  if image is None:
24
  return ""
 
72
  </div>
73
  """
74
 
75
+ # Find suspicious phrases and URLs
76
  found_phrases = []
77
  text_lower = text.lower()
78
  for phrase in SUSPICIOUS_PHRASES:
79
  if phrase in text_lower:
80
  found_phrases.append(phrase)
81
 
82
+ urls = re.findall(URL_PATTERN, text)
83
+
84
  # Generate detailed analysis report with modern styling
85
  report = [
86
  "<div style='background: white; padding: 24px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05);'>",
 
95
  "</div>"
96
  ]
97
 
98
+ if found_phrases or urls:
99
+ report.append("<div style='margin-top: 20px;'>")
100
+ if found_phrases:
101
+ report.extend([
102
+ "<h4 style='color: #d32f2f;'>🚩 Suspicious Elements Detected:</h4>",
103
+ "<ul style='list-style-type: none; padding-left: 0;'>"
104
+ ])
105
+ for phrase in found_phrases:
106
+ report.append(f"<li style='margin-bottom: 8px; padding: 8px 12px; background: #ffebee; border-radius: 6px;'>Found: '{html.escape(phrase)}'</li>")
107
+ report.append("</ul>")
108
+
109
+ if urls:
110
+ report.extend([
111
+ "<h4 style='color: #d32f2f;'>πŸ”— Detected URLs:</h4>",
112
+ "<ul style='list-style-type: none; padding-left: 0;'>"
113
+ ])
114
+ for url in urls:
115
+ report.append(f"<li style='margin-bottom: 8px; padding: 8px 12px; background: #e3f2fd; border-radius: 6px; word-break: break-all;'>{html.escape(url)}</li>")
116
+ report.append("</ul>")
117
+ report.append("</div>")
118
  else:
119
+ report.append("<p style='color: #2e7d32;'>βœ… No common suspicious phrases or URLs detected.</p>")
120
 
121
  report.append("<div style='margin-top: 20px; padding: 16px; background: #e3f2fd; border-radius: 8px;'>")
122
  if confidence > 0.9:
 
155
  input_background_fill="white",
156
  button_primary_background_fill="#1a237e",
157
  button_primary_text_color="white",
158
+ button_secondary_background_fill="#e0e0e0",
159
+ button_secondary_text_color="#333",
160
  )
161
 
162
+ # CSS for responsiveness and polish
163
+ custom_css = """
164
  .container { max-width: 1000px; margin: auto; }
165
  .header { text-align: center; margin-bottom: 2rem; }
166
  .tool-description { max-width: 800px; margin: 0 auto 2rem auto; }
167
  .input-section { margin-bottom: 2rem; }
168
  .analysis-section { margin-top: 2rem; }
169
+ @media (max-width: 600px) {
170
+ .header h1 { font-size: 1.8rem; }
171
+ .input-section { padding: 0 10px; }
172
+ .gr-button { width: 100%; margin-bottom: 10px; }
173
+ }
174
+ """
175
+
176
+ # Create Gradio interface
177
+ with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
178
  gr.HTML("""
179
  <div class="header">
180
  <h1 style="color: #1a237e; font-size: 2.5rem; margin-bottom: 1rem;">πŸ›‘οΈ AI Phishing Guard</h1>
 
188
  <div class="tool-description">
189
  <h3 style="color: #1a237e;">How to Use</h3>
190
  <ol style="color: #555; line-height: 1.6;">
191
+ <li>Paste message text or upload a screenshot</li>
192
+ <li>Analyze instantly as you type or click 'Analyze' for images</li>
193
+ <li>Review the detailed results and stay cautious</li>
194
  </ol>
195
  <div style="background: #e3f2fd; padding: 16px; border-radius: 8px; margin-top: 1rem;">
196
  <h4 style="color: #1a237e; margin-top: 0;">This tool detects:</h4>
197
  <ul style="color: #555; margin-bottom: 0;">
198
  <li>Suspicious language patterns</li>
199
  <li>Common phishing phrases</li>
200
+ <li>URLs and potential threats</li>
201
  <li>Urgency indicators</li>
 
202
  </ul>
203
  </div>
204
  </div>
 
220
  elem_id="image_input"
221
  )
222
 
223
+ with gr.Row():
224
+ analyze_button = gr.Button("πŸ” Analyze", variant="primary", size="lg")
225
+ clear_button = gr.Button("πŸ—‘οΈ Clear", variant="secondary", size="lg")
226
+
227
+ loading = gr.HTML("<div style='text-align: center; padding: 10px;'>Analyzing... <span>⏳</span></div>", visible=False)
228
 
229
+ with gr.Column(visible=False) as output_col:
230
  alert_html = gr.HTML()
231
  analysis = gr.HTML()
232
 
233
  # Examples
234
  examples = [
235
+ ["Subject: URGENT - Account Security Alert\n\nDear User,\n\nWe detected unusual activity in your account. Click here immediately to verify your identity and reset your password at https://fakebank.com/reset. If you don't respond within 24 hours, your account will be suspended.\n\nBank Security Team", None],
236
  ["Subject: Team Meeting Tomorrow\n\nHi everyone,\n\nJust a reminder that we have our weekly team meeting tomorrow at 10 AM in the main conference room. Please bring your project updates.\n\nBest regards,\nSarah", None],
237
  ]
238
 
239
  gr.Examples(
240
  examples=examples,
241
  inputs=[text_input, image_input],
242
+ outputs=[alert_html, analysis, output_col],
243
  fn=process_input,
244
  cache_examples=True
245
  )
246
 
247
+ # Event handlers
248
+ def with_loading(*args):
249
+ return (gr.update(visible=True), *process_input(*args), gr.update(visible=False))
250
+
251
+ text_input.change(
252
  fn=process_input,
253
  inputs=[text_input, image_input],
254
  outputs=[alert_html, analysis, output_col]
255
  )
256
+ analyze_button.click(
257
+ fn=with_loading,
258
+ inputs=[text_input, image_input],
259
+ outputs=[loading, alert_html, analysis, output_col]
260
+ )
261
+ clear_button.click(
262
+ fn=lambda: ("", None, "", "", gr.update(visible=False)),
263
+ inputs=[],
264
+ outputs=[text_input, image_input, alert_html, analysis, output_col]
265
+ )
266
 
267
  # Launch the app
268
  if __name__ == "__main__":