CoolFace
Apppublic

mebeingme31/DIC-PHASE3

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py226 linesDownload Raw Back to root
1import gradio as gr2 3import pandas as pd4import pickle5import matplotlib.pyplot as plt6import pathlib7import matplotlib.lines as mlines8 9global_pr_qm=[]10global_da_ph=[]11answer=''12white_circle = mlines.Line2D([], [], color='white', markeredgecolor='black', marker='o', markersize=10, label='New Point', linestyle='None')13red_circle = mlines.Line2D([], [], color='red', marker='o', markersize=10, label='Legitimate', linestyle='None')14green_circle = mlines.Line2D([], [], color='green', marker='o', markersize=10, label='Phishing', linestyle='None')15 16def visualisations():17    global global_pr_qm18    with open('dataframe.pkl', 'rb') as file:19        df = pickle.load(file)20        plt.figure(figsize=(8, 5))21 22        red_points = df[df['is_legitimate'] == 0]23        blue_points = df[df['is_legitimate'] == 1]24 25        plt.scatter(red_points['nb_qm'], red_points['page_rank'], color='red' )26        plt.scatter(blue_points['nb_qm'], blue_points['page_rank'], color='green')27        if len(global_pr_qm)!=0:28            # if answer.split(',')[0]=='Legitimate':29            #     ccccc='green'30            # else:31            #     ccccc='red'32 33            ccccc=[]34            for i in answer[:-1].split(','):35                if i.startswith('Le'):36                    ccccc.append('green')37                else:38                    ccccc.append('red')39 40            x_coords, y_coords = zip(*global_pr_qm)41            plt.scatter(x_coords, y_coords,color=ccccc , s=100, edgecolor='black', zorder=5)42 43            # plt.scatter(global_pr_qm[0][0],global_pr_qm[0][1],color=ccccc , s=100, edgecolor='black', label='New Point', zorder=5)44        plt.title('scatter plot')45        plt.xlabel('Number of Question marks')46        plt.ylabel('page_rank')47        # plt.legend(title="Status")48        plt.legend(title="Status",handles=[white_circle, red_circle, green_circle])49        50        return plt.gcf() 51    52def visualisations2():53    global global_da_ph54    with open('dataframe.pkl', 'rb') as file:55        df = pickle.load(file)56        plt.figure(figsize=(8, 5))57 58        red_points = df[df['is_legitimate'] == 0]59        blue_points = df[df['is_legitimate'] == 1]60 61        plt.scatter(red_points['domain_age'], red_points['phish_hints'], color='red')62        plt.scatter(blue_points['domain_age'], blue_points['phish_hints'], color='green')63 64        if len(global_da_ph)!=0:65            66            ccccc=[]67            for i in answer[:-1].split(','):68                if i.startswith('Le'):69                    ccccc.append('green')70                else:71                    ccccc.append('red')72 73            # if answer.split(',')[0]=='Legitimate':74            #     ccccc='green'75            # else:76            #     ccccc='red'77            x_coords, y_coords = zip(*global_da_ph)78            plt.scatter(x_coords, y_coords,color=ccccc , s=100, edgecolor='black', zorder=5)79 80        plt.title('scatter plot')81        plt.xlabel('domain_age')82        plt.ylabel('phish_hints')83        # plt.legend(title="Status")84        plt.legend(title="Status",handles=[white_circle, red_circle, green_circle])85        return plt.gcf() 86 87 88def greet(aa):89    df_for_test=pd.DataFrame(columns=['length_url', 'length_hostname', 'ip', 'nb_dots', 'nb_hyphens', 'nb_at',90       'nb_qm', 'nb_and', 'nb_eq', 'nb_slash', 'nb_colon', 'nb_semicolumn',91       'nb_www', 'nb_com', 'nb_dslash', 'http_in_path', 'https_token',92       'ratio_digits_url', 'ratio_digits_host', 'tld_in_path',93       'tld_in_subdomain', 'abnormal_subdomain', 'nb_subdomains',94       'prefix_suffix', 'shortening_service', 'nb_external_redirection',95       'length_words_raw', 'shortest_word_host', 'shortest_word_path',96       'longest_words_raw', 'longest_word_host', 'longest_word_path',97       'avg_words_raw', 'avg_word_host', 'avg_word_path', 'phish_hints',98       'domain_in_brand', 'brand_in_subdomain', 'brand_in_path',99       'suspecious_tld', 'statistical_report', 'nb_hyperlinks',100       'ratio_inthyperlinks', 'ratio_exthyperlinks', 'nb_extcss',101       'ratio_extredirection', 'external_favicon', 'links_in_tags',102       'ratio_intmedia', 'ratio_extmedia', 'popup_window', 'safe_anchor',103       'empty_title', 'domain_in_title', 'domain_with_copyright',104       'whois_registered_domain', 'domain_registration_length', 'domain_age',105       'web_traffic', 'dns_record', 'google_index', 'page_rank'])106    df_for_test.loc[0]=eval(aa)107 108    with open('minMaxScalerForTestingData.pkl','rb') as f:109        scaler_objects=pickle.load(f)110 111    for i in list(scaler_objects.keys()):112        if i[0] in df_for_test.columns:113            df_for_test[i[0]]=scaler_objects[i].transform(df_for_test[i[0]].values.reshape(-1,1))114 115    with open('all_models.pkl', 'rb') as file:116        all_models = pickle.load(file)117 118    def predict_all_models(data):119        if all_models['SVM'].predict([data])[0] ==1:120            return 'Legitimate'121        return 'Phishing'    122    return predict_all_models(df_for_test.iloc[0].tolist()) 123 124def combined_interface(url=None):125    if url is None:126        greeting = "Please enter a feature vector to analyze."  127    else:128        greeting = greet(url)  129    return visualisations(), greeting130 131def load_on_start():132    global global_pr_qm,global_da_ph,answer133    answer=''134    global_pr_qm=[]135    global_da_ph=[]136    return visualisations2(),visualisations(), "Please enter a feature vector to analyze."137 138def upload_file(files):139    global global_da_ph,global_pr_qm140    global answer141    try:142        a=pd.read_csv(files)143        # print(a.columns)144    145        for i in range(len(a)):146            global_da_ph.append((a.loc[i,'domain_age'],a.loc[i,'phish_hints']))147            global_pr_qm.append((a.loc[i,'nb_qm'],a.loc[i,'page_rank']))148            149            answer=answer+greet(str(a.loc[i].values.tolist()))+','150    except Exception as e:151        print('Error:', e)152        return "Failed to process the file."153        154 155    print(files)156    # visualisations2()157    return answer[:-1],visualisations2(),visualisations()158    159 160with gr.Blocks() as demo:161    # plot_output2 = None162    with gr.Row():163        with gr.Column():164            # file_output = gr.File()165            text_output2 = gr.Textbox(label='Status of Website')166            upload_button=gr.UploadButton(label='upload csv',file_count="single")167            # plot_output2= gr.Plot()168            169            gr.DownloadButton("Download Input Template", value=pathlib.Path('FeatureVector.csv'))170            # url_input = gr.Textbox(label="Feature Vector")171            # submit_btn = gr.Button("Submit")172 173    with gr.Row():174        text_output = gr.Textbox(label='Status of Website',visible=False)175 176    gr.Markdown('# Visualisations')177    with gr.Row():178        with gr.Column():179            plot_output = gr.Plot()180        with gr.Column():181            gr.Markdown("## We can clearly understand that as the number of question marks are increasing (indicating more queries in URL), there are a lot of phishing sites indicating that phishing websites generally have more number of query parameters in a URL when compared to a legitimate one.") 182    with gr.Row():183        with gr.Column():184            plot_output2= gr.Plot()185            upload_button.upload(upload_file, upload_button,[text_output2,plot_output2,plot_output])186        with gr.Column():187            gr.Markdown("## We can see that as the age of the domain is increasing there are relatively less phishing site which indicates that Older domains might have fewer phishing hints due to established legitimacy over time. We can also interpret this as websites which are just created have more phish_hints potentially being a phishing website.") 188    # submit_btn.click(189    #     fn=lambda url: (visualisations2(),visualisations(), greet(url)),190    #     inputs=url_input,191    #     outputs=[plot_output, plot_output2,text_output]192    # )193 194 195    demo.load(load_on_start, inputs=None, outputs=[plot_output2,plot_output, text_output])196 197 198demo.launch()199 200# import pandas as pd201 202# df_for_test=pd.DataFrame(columns=['length_url', 'length_hostname', 'ip', 'nb_dots', 'nb_hyphens', 'nb_at',203#        'nb_qm', 'nb_and', 'nb_eq', 'nb_slash', 'nb_colon', 'nb_semicolumn',204#        'nb_www', 'nb_com', 'nb_dslash', 'http_in_path', 'https_token',205#        'ratio_digits_url', 'ratio_digits_host', 'tld_in_path',206#        'tld_in_subdomain', 'abnormal_subdomain', 'nb_subdomains',207#        'prefix_suffix', 'shortening_service', 'nb_external_redirection',208#        'length_words_raw', 'shortest_word_host', 'shortest_word_path',209#        'longest_words_raw', 'longest_word_host', 'longest_word_path',210#        'avg_words_raw', 'avg_word_host', 'avg_word_path', 'phish_hints',211#        'domain_in_brand', 'brand_in_subdomain', 'brand_in_path',212#        'suspecious_tld', 'statistical_report', 'nb_hyperlinks',213#        'ratio_inthyperlinks', 'ratio_exthyperlinks', 'nb_extcss',214#        'ratio_extredirection', 'external_favicon', 'links_in_tags',215#        'ratio_intmedia', 'ratio_extmedia', 'popup_window', 'safe_anchor',216#        'empty_title', 'domain_in_title', 'domain_with_copyright',217#        'whois_registered_domain', 'domain_registration_length', 'domain_age',218#        'web_traffic', 'dns_record', 'google_index', 'page_rank'])219 220# df_for_test.to_csv('./FeatureVector.csv')221 222# df=pd.read_csv('FeatureVector.csv')223# df.loc[0]=[68.0, 40.0, 0.0, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 1.0, 0.0, 0.0,2.0, 0.0, 0.0, 1.0, 0.029411765, 0.05, 0.0, 0.0, 0.0, 3.0, 0.0,0.0, 0.0, 7.0, 5.0, 3.0, 17.0, 17.0, 6.0, 6.857142857, 10.33333333,4.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 1.0, 0.0, 0.0, 0.0, 0.0,100.0, 0.0, 0.0, 0.0, 100.0, 0.0, 1.0, 0.0, 0.0, 2381.0, 0, 0.0,0.0, 1.0, 2.0]224# df.loc[1]=[55.0, 15.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 5.0, 1.0, 0.0, 1.0,0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0,6.0, 3.0, 4.0, 11.0, 7.0, 11.0, 6.333333333, 5.0, 7.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 102.0, 0.470588235, 0.529411765, 0.0,0.537037037, 0.0, 76.47058824, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0, 224.0, 8175.0, 8725.0, 0.0, 0.0, 6.0,]225# df.to_csv('test.csv',index=False)226