Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
def send_email_via_gmail(subject, body):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 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()
|