File size: 1,905 Bytes
a7ffb8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import joblib
import pandas as pd
import re
import os

# Download model from GitHub or use local
# For Hugging Face, you'll upload the model file

def extract_features(url):
    features = {
        'url_length': len(url),
        'num_dots': url.count('.'),
        'num_hyphens': url.count('-'),
        'num_slash': url.count('/'),
        'num_underscore': url.count('_'),
        'num_digits': sum(c.isdigit() for c in url),
        'has_https': 1 if url.startswith('https') else 0,
        'has_ip': 1 if re.search(r'\d+\.\d+\.\d+\.\d+', url) else 0,
        'num_suspicious': sum(1 for word in ['login','verify','secure','account','signin','auth'] 
                              if word in url.lower()),
        'num_at': url.count('@'),
        'num_question': url.count('?'),
        'num_equal': url.count('=')
    }
    return pd.DataFrame([features])

def predict_url(url):
    if not url:
        return "⚠️ Enter a URL", ""
    
    # Load model (will be in same folder on Hugging Face)
    model = joblib.load('malicious_url_detector.pkl')
    features = extract_features(url)
    pred = model.predict(features)[0]
    prob = model.predict_proba(features)[0]
    
    if pred == 1:
        return f"🚨 MALICIOUS\nConfidence: {prob[1]:.1%}", ""
    return f"✅ SAFE\nConfidence: {prob[0]:.1%}", ""

interface = gr.Interface(
    fn=predict_url,
    inputs=gr.Textbox(label="Enter URL", placeholder="https://...", lines=2),
    outputs=gr.Textbox(label="Result", lines=3),
    title="🛡️ AI Malicious URL Detector",
    description="This AI model detects phishing and malicious URLs in real-time.",
    examples=[
        ["https://google.com"],
        ["https://paypal.com.login.verify.secure.com"],
        ["https://github.com"],
        ["http://192.168.1.1/banking/login"]
    ]
)

interface.launch()