Stormx101's picture
Upload 3 files
a7ffb8f verified
Raw
History Blame Contribute Delete
1.91 kB
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()