import gradio as gr import torch from unsloth import FastLanguageModel from transformers import AutoTokenizer import json import time from datetime import datetime import os class PhishingDetector: def __init__(self, model_path="shukdevdatta123/DeepSeek-R1-Phishing-Detector-Improved"): """ Initialize the phishing detection model for Hugging Face Spaces Args: model_path (str): Hugging Face model repository path """ self.model_path = model_path self.model = None self.tokenizer = None self.device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {self.device}") self.load_model() def load_model(self): """Load the trained phishing detection model from Hugging Face""" try: print(f"Loading model from {self.model_path}...") # Load the model and tokenizer from Hugging Face self.model, self.tokenizer = FastLanguageModel.from_pretrained( model_name=self.model_path, max_seq_length=2048, dtype=None, load_in_4bit=True, ) # Set model to inference mode FastLanguageModel.for_inference(self.model) print("â Model loaded successfully!") except Exception as e: print(f"â Error loading model: {str(e)}") raise def analyze_content(self, content): """ Analyze content for phishing detection Args: content (str): Content to analyze (URL, email, SMS, etc.) Returns: tuple: (classification, confidence, full_analysis, inference_time) """ if not content or not content.strip(): return "â Error", "N/A", "Please enter some content to analyze.", "0.00" prompt = f"""You are a cybersecurity expert specializing in phishing detection. Analyze the given content and determine if it's phishing or benign. Content to analyze: {content} Think step by step and provide your analysis:""" try: # Tokenize input inputs = self.tokenizer([prompt], return_tensors="pt").to(self.device) # Generate response start_time = time.time() with torch.no_grad(): outputs = self.model.generate( input_ids=inputs.input_ids, attention_mask=inputs.attention_mask, max_new_tokens=500, use_cache=True, temperature=0.3, do_sample=True, pad_token_id=self.tokenizer.eos_token_id, repetition_penalty=1.1, ) inference_time = time.time() - start_time # Decode response response = self.tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] # Extract the analysis part if "Think step by step and provide your analysis:" in response: analysis = response.split("Think step by step and provide your analysis:")[1].strip() else: analysis = response # Parse the results classification = "đ UNKNOWN" confidence = "UNKNOWN" if "PHISHING" in analysis.upper(): classification = "đ¨ PHISHING" elif "BENIGN" in analysis.upper(): classification = "â BENIGN" if "High" in analysis: confidence = "High" elif "Medium" in analysis: confidence = "Medium" elif "Low" in analysis: confidence = "Low" return classification, confidence, analysis, f"{inference_time:.2f}s" except Exception as e: error_msg = f"Error during analysis: {str(e)}" return "â Error", "N/A", error_msg, "0.00" # Initialize the detector print("đ Initializing Phishing Detection Model...") detector = PhishingDetector() def analyze_phishing(content): """ Gradio interface function for phishing analysis Args: content (str): Content to analyze Returns: tuple: Results for Gradio interface """ classification, confidence, analysis, inference_time = detector.analyze_content(content) # Format the output for better display result_color = "red" if "PHISHING" in classification else "green" if "BENIGN" in classification else "orange" return classification, confidence, analysis, inference_time def batch_analyze(file_path): """ Batch analysis function for file upload Args: file_path (str): Path to uploaded file Returns: str: Formatted results """ if not file_path: return "Please upload a file with content to analyze (one item per line)" try: # Read the file content with open(file_path, 'r', encoding='utf-8') as f: file_content = f.read() except Exception as e: return f"Error reading file: {str(e)}" lines = [line.strip() for line in file_content.split('\n') if line.strip()] if not lines: return "No valid content found in the file" results = [] phishing_count = 0 benign_count = 0 for i, content in enumerate(lines, 1): classification, confidence, analysis, inference_time = detector.analyze_content(content) if "PHISHING" in classification: phishing_count += 1 elif "BENIGN" in classification: benign_count += 1 results.append(f"**Item {i}:** {content[:50]}{'...' if len(content) > 50 else ''}") results.append(f"**Result:** {classification} (Confidence: {confidence})") results.append(f"**Time:** {inference_time}") results.append("---") summary = f""" ## Batch Analysis Summary - **Total Items:** {len(lines)} - **Phishing Detected:** {phishing_count} - **Benign Content:** {benign_count} - **Unknown/Errors:** {len(lines) - phishing_count - benign_count} ## Detailed Results """ return summary + "\n".join(results) # Comprehensive examples organized by category examples = [ # Suspicious URLs - Banking/Finance ["https://secure-paypal-verification.malicious-site.com/verify-account-now"], ["https://banking-security-update.fake-bank.org/login-verification"], ["https://chase-account-suspended.suspicious-domain.net/reactivate"], ["http://wellsfargo-security-alert.phishing.site/confirm-identity"], ["https://creditcard-fraud-alert.fake-visa.com/verify-transaction"], # Legitimate URLs - Banking/Finance ["https://www.paypal.com/signin"], ["https://www.chase.com/personal/online-banking"], ["https://www.wellsfargo.com/"], ["https://www.bankofamerica.com/online-banking/"], ["https://www.citi.com/credit-cards"], # Suspicious URLs - E-commerce ["https://amazon-security-alert.fake-domain.com/login-required"], ["https://ebay-account-limitation.suspicious.org/resolve-issue"], ["https://apple-id-locked.phishing-site.net/unlock-account"], ["https://microsoft-security-warning.malicious.com/verify-now"], ["https://netflix-billing-problem.fake-streaming.org/update-payment"], # Legitimate URLs - E-commerce ["https://www.amazon.com/your-account"], ["https://www.ebay.com/signin"], ["https://appleid.apple.com/"], ["https://account.microsoft.com/"], ["https://www.netflix.com/youraccount"], # Phishing Emails - Financial Scams ["URGENT: Your PayPal account has been limited due to suspicious activity. Click here to restore access immediately: http://paypal-restore.malicious.com"], ["Your bank account will be closed in 24 hours unless you verify your information. Click here: http://bank-verification.fake.org"], ["Congratulations! You've been selected for a $5000 grant. No repayment required! Claim now: http://free-money-grant.scam.net"], ["FINAL NOTICE: Your credit score needs immediate attention. Fix it now for free: http://credit-repair-scam.fake.com"], ["You've won the lottery! Claim your $50,000 prize immediately: http://lottery-winner.phishing.org"], # Legitimate Emails - Financial ["Your monthly bank statement is now available for download on our secure portal. Please log in to view your transactions."], ["Thank you for your recent purchase. Your receipt and tracking information are attached to this email."], ["Your automatic payment has been processed successfully. Your account balance is updated."], ["Reminder: Your credit card payment is due in 3 days. You can pay online or set up automatic payments."], ["Welcome to our mobile banking app! Here's how to get started with your new digital banking experience."], # Phishing SMS Messages ["ALERT: Suspicious activity on your account. Verify immediately or account will be suspended: bit.ly/verify-account-123"], ["You've won a FREE iPhone 15! Claim now before it expires: txt.me/free-iphone-winner"], ["Your package delivery failed. Reschedule now: fedex-redelivery.suspicious.com/reschedule"], ["COVID-19 relief funds available. Claim $2000 now: covid-relief.fake-gov.org/apply"], ["Your Netflix subscription expires today! Renew now to avoid interruption: netflix-renewal.sketchy.com"], # Legitimate SMS Messages ["Your verification code is 123456. Do not share this code with anyone."], ["Your order #12345 has shipped and will arrive on Friday. Track: ups.com/tracking"], ["Appointment reminder: You have a doctor's appointment tomorrow at 2 PM."], ["Your flight AB123 is delayed by 30 minutes. New departure time: 3:30 PM."], ["Thank you for your purchase at Store Name. Receipt: $25.99 for item XYZ."], # Social Engineering - Tech Support Scams ["Microsoft Windows Alert: Your computer is infected with 5 viruses. Call 1-800-FAKE-TECH immediately for free removal."], ["Apple Security Warning: Your iPhone has been hacked. Download our security app now: fake-apple-security.com"], ["Google Chrome Critical Update Required: Your browser is outdated and vulnerable. Update now: chrome-update.malicious.org"], ["Your antivirus subscription has expired. Renew now to protect your computer: antivirus-renewal.scam.net"], ["PC Performance Alert: Your computer is running slow. Download our optimizer: pc-speedup.fake-software.com"], # Legitimate Tech Communications ["Your software update is ready to install. This update includes security improvements and bug fixes."], ["Welcome to our technical support. We'll help you resolve your issue step by step."], ["Your device backup was completed successfully. All your files are safely stored."], ["Security tip: Enable two-factor authentication to better protect your account."], ["Your subscription to our service will renew automatically on the billing date shown in your account."], # Romance/Dating Scams ["Hi beautiful, I'm a soldier deployed overseas and need help with finances. Can you help me? Contact: lonely-soldier.romance-scam.org"], ["I'm a widower with a large inheritance. I'd like to share it with someone special. Email me: rich-widower@fake-romance.com"], ["You seem special. I'm traveling and my wallet was stolen. Can you send money? I'll pay you back: travel-emergency.dating-scam.net"], # Cryptocurrency/Investment Scams ["Make $10,000 per day with Bitcoin! Limited time offer - invest now: bitcoin-millionaire.crypto-scam.org"], ["Elon Musk is giving750 giving away FREE cryptocurrency! Claim yours now: musk-crypto-giveaway.fake-tesla.com"], ["Join our exclusive trading group. 1000% returns guaranteed: forex-millionaire.trading-scam.net"], # Fake Government/Authority Messages ["IRS Notice: You owe back taxes. Pay immediately to avoid arrest: irs-tax-notice.fake-gov.org"], ["Police Warning: There's a warrant for your arrest. Resolve now: police-warrant.fake-authority.com"], ["Social Security Administration: Your benefits will be suspended. Verify now: ssa-benefits.fake-gov.net"], # Legitimate Government Style ["Official notice: Your tax return has been processed and your refund will be direct deposited within 7-10 business days."], ["Voter registration reminder: The deadline to register for the upcoming election is next month."], ["Census notification: Please complete the official census form that was mailed to your address."], # Job/Employment Scams ["Work from home opportunity! Make $500/day stuffing envelopes. No experience needed: work-from-home.job-scam.org"], ["You've been selected for a high-paying remote position. Send $200 for training materials: fake-job-offer.scam.com"], ["Mystery shopper needed! Get paid to shop. Send personal info to start: mystery-shopping.employment-scam.net"], # Legitimate Job Communications ["Thank you for applying to our company. We'll review your application and contact you within two weeks."], ["Interview scheduled: Please confirm your availability for next Tuesday at 2 PM for our video interview."], ["Welcome to the team! Your first day is Monday. Here's what to expect and what to bring."], # Fake Charity/Donation Scams ["Help disaster victims now! 100% of donations go directly to families in need: fake-disaster-relief.charity-scam.org"], ["Sick children need your help! Donate now to save lives: children-charity.donation-scam.com"], ["Veterans need your support. Donate to help homeless veterans: fake-veterans.charity-scam.net"], # Legitimate Charity Style ["Thank you for your interest in volunteering. Here's information about upcoming community service opportunities."], ["Annual report: See how your donations helped our community this year. View our financial transparency report."], ["Upcoming fundraising event: Join us for our annual charity walk to support local families in need."], # Fake Subscription/Service Notifications ["Your Amazon Prime membership expires today! Renew now: amazon-prime-renewal.fake-shopping.com"], ["Disney+ account suspended due to payment failure. Update billing: disney-billing.streaming-scam.org"], ["Spotify Premium cancelled. Reactivate now to keep your playlists: spotify-reactivate.music-scam.net"], # Travel/Vacation Scams ["Congratulations! You've won a free vacation to Hawaii! Claim now: free-vacation-winner.travel-scam.com"], ["Last minute cruise deal! 7 days Caribbean for $99. Book now: cruise-deal.vacation-scam.org"], ["Exclusive resort offer: 5-star hotel for $50/night. Limited time: luxury-resort.travel-fraud.net"], # Health/Medical Scams ["New miracle weight loss pill! Lose 50 pounds in 30 days guaranteed: miracle-diet.health-scam.com"], ["COVID-19 cure discovered! Order now before government bans it: covid-cure.medical-fraud.org"], ["Free health insurance quotes! Save thousands on premiums: health-insurance.medical-scam.net"], # Legitimate Health Communications ["Appointment reminder: Your annual checkup is scheduled for next week. Please arrive 15 minutes early."], ["Lab results are ready. Please call our office to schedule a follow-up appointment to discuss results."], ["Prescription refill reminder: Your medication is ready for pickup at the pharmacy."], # Educational/Scholarship Scams ["You qualify for a $10,000 education grant! No repayment required. Apply now: education-grant.scholarship-scam.org"], ["Congratulations! You've been selected for a full scholarship. Send $500 processing fee: fake-scholarship.edu-scam.com"], ["Student loan forgiveness available! Eliminate your debt now: loan-forgiveness.student-scam.net"], # General Legitimate Communications ["Your order confirmation: Thank you for your purchase. Your item will ship within 2-3 business days."], ["Weather alert: Severe thunderstorm warning in your area. Take necessary precautions and stay indoors."], ["Library notice: The book you reserved is now available for pickup. Hold expires in 7 days."], ["School district notice: Parent-teacher conferences are scheduled for next week. Sign up online."], ["Utility company: Scheduled maintenance in your area may cause brief service interruption on Tuesday."], # Social Media Scams ["Facebook security alert: Someone tried to access your account from Russia. Verify now: facebook-security.social-scam.com"], ["Instagram: Your account will be deleted unless you verify. Click here: instagram-verify.social-fraud.org"], ["LinkedIn: You have 99+ new connection requests! View them now: linkedin-connections.career-scam.net"], # Fake Product Reviews/Testimonials ["I made $50,000 last month with this simple system! You can too: money-making-system.get-rich-scam.com"], ["This skincare product made me look 20 years younger in just 7 days! Order now: miracle-skincare.beauty-scam.org"], ["I lost 100 pounds without diet or exercise! Here's my secret: weight-loss-secret.fitness-fraud.net"] ] # Quick test button functions def set_suspicious_1(): return "Urgent: Your account will be suspended in 24 hours! Verify now: secure-verification.fake-bank.com" def set_suspicious_2(): return "Congratulations! You've won $10,000! Claim immediately: lottery-winner.scam-site.org" def set_suspicious_3(): return "Apple ID locked due to suspicious activity. Unlock now: apple-security.phishing-domain.net" def set_legitimate_1(): return "Your monthly statement is ready for download on our secure banking portal." def set_legitimate_2(): return "Thank you for your purchase. Your order will ship within 2-3 business days." def set_legitimate_3(): return "Appointment reminder: Your doctor's appointment is scheduled for tomorrow at 2 PM." # Create Gradio interface with center alignment with gr.Blocks( title="đ PhishGuard AI - Advanced Phishing Detection", theme=gr.themes.Ocean(), css=""" .gradio-container { max-width: 1400px !important; margin: 0 auto !important; } .title { text-align: center; font-size: 2.8em; font-weight: bold; margin-bottom: 0.5em; background: linear-gradient(45deg, #FF6B6B, #4ECDC4); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; } .subtitle { text-align: center; font-size: 1.3em; color: #666; margin-bottom: 2em; } .feature-box { border: 2px solid #e1e5e9; border-radius: 10px; padding: 1em; margin: 0.5em auto; background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); text-align: center; } .container { text-align: center; } .main-content { margin: 0 auto; padding: 20px; } .tab-nav { justify-content: center; } .gradio-row { justify-content: center; } .gradio-column { display: flex; flex-direction: column; align-items: center; } """ ) as app: gr.HTML("""