File size: 1,782 Bytes
0f0bf58
42855a6
 
 
 
0f0bf58
42855a6
 
0f0bf58
42855a6
 
 
 
 
 
 
 
 
 
 
ddaa8f4
 
42855a6
 
 
 
 
 
 
 
 
 
 
ddaa8f4
 
42855a6
 
0f0bf58
42855a6
ddaa8f4
42855a6
 
 
 
ddaa8f4
 
 
 
 
 
 
 
42855a6
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
import gradio as gr
import pickle
import pandas as pd
from urlextract import URLExtract
import URLFeatureExtraction

with open("models/spam-clf.pkl", "rb") as f:
    model = pickle.load(f)

with open("models/xgb.dat", "rb") as f:
    model2 = pickle.load(f)

df = pd.DataFrame(columns=['URL', 'Phishy?'])

def isPhishing(link):
    global df

    features = URLFeatureExtraction.featureExtraction(link)
    print(features)
    prediction = model2.predict([features])
    print(prediction[0])
    
    df = df._append({'URL': link, 'Phishy?': "UnSafe" if prediction[0] else "Safe"}, ignore_index=True)
    return prediction[0]

def isSpam(Email):
    out = model.predict([Email])
    return "Spam" if out[0] else "Not Spam"

def check_URL(Email):
    extractor = URLExtract()
    urls = extractor.find_urls(Email)
    n_urls = len(urls)

    if not urls: return -1
    if urls:
        bad_urls = sum([isPhishing(url) for url in urls])
    else:
        bad_urls = 0
    print("Out of {} urls {} are legitimate.".format(n_urls, n_urls - bad_urls))

    return bad_urls

def check_Mail(Email):
    bad_urls = check_URL(Email)
    if bad_urls==-1:
        return [isSpam(Email), 0, 'Safe', df]
    
    if URLFeatureExtraction.flag or bad_urls:
        return [isSpam(Email), bad_urls, 'Risky', df]
    
    return [isSpam(Email), bad_urls, 'Safe', df]

iface = gr.Interface(
    fn=check_Mail,
    inputs=gr.Textbox(lines=6, placeholder="Enter or paste email here", label="Email"),
    outputs=[
        gr.Textbox(label="Spam or Not"),
        gr.Textbox(label="Phishing Links Detected"),
        gr.Textbox(label="Consider this Mail as"),
        gr.Dataframe(label="Insights", interactive=False)
    ]
)
iface.launch()