Ajay1311 commited on
Commit
db47ffe
·
verified ·
1 Parent(s): dcd2726

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -29
app.py CHANGED
@@ -1,11 +1,17 @@
1
- import smtplib
2
- from email.mime.text import MIMEText
3
- from email.mime.multipart import MIMEMultipart
4
  import gradio as gr
5
  import numpy as np
6
  import matplotlib.pyplot as plt
7
  from transformers import pipeline
8
- import logging
 
 
 
 
 
 
 
9
 
10
  # Set up logging to log phishing URLs or content
11
  logging.basicConfig(filename='phishing_urls.log', level=logging.INFO, format='%(asctime)s - %(message)s')
@@ -14,31 +20,37 @@ logging.basicConfig(filename='phishing_urls.log', level=logging.INFO, format='%(
14
  model = pipeline('text-classification', model="Ajay1311/phish")
15
 
16
  # Gmail SMTP setup for sending emails
17
- smtp_server = "smtp.gmail.com"
18
- smtp_port = 587
19
- sender_email = "pukumar2003@gmail.com" # Replace with your Gmail address
20
- sender_password = "qwaw bfdb xdlt qyup" # Replace with your generated App Password
21
- recipient_email = "report@openphish.com" # Replace with recipient's email (can be your email)
 
22
 
23
  def send_email_via_gmail(subject, body):
24
- """Function to send email via Gmail SMTP server."""
25
- try:
26
- # Create the email content
27
- message = MIMEMultipart()
28
- message["From"] = sender_email
29
- message["To"] = recipient_email
30
- message["Subject"] = subject
31
- message.attach(MIMEText(body, "plain"))
32
-
33
- # Establish connection to Gmail's SMTP server and send the email
34
- with smtplib.SMTP(smtp_server, smtp_port) as server:
35
- server.starttls() # Secure connection
36
- server.login(sender_email, sender_password)
37
- server.sendmail(sender_email, recipient_email, message.as_string())
38
-
39
- print("Phishing report email sent successfully!")
40
- except Exception as e:
41
- print(f"Failed to send email: {e}")
 
 
 
 
 
42
 
43
  def log_phishing(url_or_text):
44
  """Log phishing URLs or content into a log file."""
@@ -95,8 +107,9 @@ def analyze_phishing(text):
95
  """
96
 
97
  # Send email and log the phishing URL/content
98
- send_email_via_gmail("Phishing Detected", f"Phishing detected with confidence {score*100:.1f}%: {text}")
99
  log_phishing(text)
 
100
 
101
  else:
102
  analysis = f"""
@@ -113,6 +126,15 @@ def analyze_phishing(text):
113
 
114
  return f"{'Phishing Detected' if is_phishing else 'Content Safe'} (Confidence: {score*100:.1f}%)", chart, analysis
115
 
 
 
 
 
 
 
 
 
 
116
  theme = gr.themes.Soft(primary_hue="blue", secondary_hue="gray").set(
117
  button_primary_background_fill="*primary_600",
118
  button_primary_text_color="white",
@@ -140,6 +162,7 @@ with gr.Blocks(theme=theme, css="""
140
 
141
  with gr.Row():
142
  analyze_btn = gr.Button("Analyze", variant="primary")
 
143
  clear_btn = gr.Button("Clear")
144
 
145
  with gr.Group():
@@ -153,6 +176,12 @@ with gr.Blocks(theme=theme, css="""
153
  outputs=[result_text, result_plot, analysis_md]
154
  )
155
 
 
 
 
 
 
 
156
  clear_btn.click(
157
  lambda: ("", None, ""),
158
  inputs=None,
@@ -162,4 +191,4 @@ with gr.Blocks(theme=theme, css="""
162
  gr.HTML("""<div class="footer">Cyber Swa Raksha</div>""")
163
 
164
  if __name__ == "__main__":
165
- demo.launch(share=True)
 
1
+ import os
2
+ import logging
 
3
  import gradio as gr
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
  from transformers import pipeline
7
+ import smtplib
8
+ from email.mime.text import MIMEText
9
+ from email.mime.multipart import MIMEMultipart
10
+ from dotenv import load_dotenv
11
+ import time
12
+
13
+ # Load environment variables
14
+ load_dotenv()
15
 
16
  # Set up logging to log phishing URLs or content
17
  logging.basicConfig(filename='phishing_urls.log', level=logging.INFO, format='%(asctime)s - %(message)s')
 
20
  model = pipeline('text-classification', model="Ajay1311/phish")
21
 
22
  # Gmail SMTP setup for sending emails
23
+ SMTP_SERVER = "smtp.gmail.com"
24
+ SMTP_PORT = 587
25
+ SENDER_EMAIL = "pukumar2003@gmail.com"
26
+ SENDER_PASSWORD = "qwaw bfdb xdlt qyup"
27
+
28
+ RECIPIENT_EMAIL = "pukumar2003@gmail.com"
29
 
30
  def send_email_via_gmail(subject, body):
31
+ message = MIMEMultipart()
32
+ message["From"] = SENDER_EMAIL
33
+ message["To"] = RECIPIENT_EMAIL
34
+ message["Subject"] = subject
35
+ message.attach(MIMEText(body, "plain"))
36
+
37
+ max_retries = 3
38
+ retry_delay = 5 # seconds
39
+ timeout = 10 # seconds
40
+ for attempt in range(max_retries):
41
+ try:
42
+ with smtplib.SMTP(SMTP_SERVER, SMTP_PORT, timeout=timeout) as server:
43
+ server.starttls() # Secure connection
44
+ server.login(SENDER_EMAIL, SENDER_PASSWORD)
45
+ server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, message.as_string())
46
+ logging.info("Email sent successfully")
47
+ return "Phishing report email sent successfully!"
48
+ except (smtplib.SMTPException, OSError) as e:
49
+ logging.error(f"Failed to send email (attempt {attempt + 1}/{max_retries}): {e}")
50
+ if attempt < max_retries - 1:
51
+ time.sleep(retry_delay)
52
+ continue
53
+ return f"Failed to send email: {e}"
54
 
55
  def log_phishing(url_or_text):
56
  """Log phishing URLs or content into a log file."""
 
107
  """
108
 
109
  # Send email and log the phishing URL/content
110
+ email_result = send_email_via_gmail("Phishing Detected", f"Phishing detected with confidence {score*100:.1f}%: {text}")
111
  log_phishing(text)
112
+ analysis += f"\n**Email Notification:** {email_result}"
113
 
114
  else:
115
  analysis = f"""
 
126
 
127
  return f"{'Phishing Detected' if is_phishing else 'Content Safe'} (Confidence: {score*100:.1f}%)", chart, analysis
128
 
129
+ def open_awareness_page():
130
+ """Return HTML to open the CyberSwaRaksha awareness page in a new tab."""
131
+ return """
132
+ <script>
133
+ window.open('https://pukumars2003.github.io/CyberSwaRaksha/', '_blank');
134
+ </script>
135
+ <p>Opening CyberSwaRaksha awareness page...</p>
136
+ """
137
+
138
  theme = gr.themes.Soft(primary_hue="blue", secondary_hue="gray").set(
139
  button_primary_background_fill="*primary_600",
140
  button_primary_text_color="white",
 
162
 
163
  with gr.Row():
164
  analyze_btn = gr.Button("Analyze", variant="primary")
165
+ awareness_btn = gr.Button("Cyber Safety Tips", variant="secondary")
166
  clear_btn = gr.Button("Clear")
167
 
168
  with gr.Group():
 
176
  outputs=[result_text, result_plot, analysis_md]
177
  )
178
 
179
+ awareness_btn.click(
180
+ open_awareness_page,
181
+ inputs=None,
182
+ outputs=gr.HTML(label="Awareness Page")
183
+ )
184
+
185
  clear_btn.click(
186
  lambda: ("", None, ""),
187
  inputs=None,
 
191
  gr.HTML("""<div class="footer">Cyber Swa Raksha</div>""")
192
 
193
  if __name__ == "__main__":
194
+ demo.launch()