Mahit's picture
Update app.py
aa24e6b
import gradio as gr
import joblib
def make(Flow_Bytes_per_s,Fwd_Packets_Length_Total,Init_Fwd_Win_Bytes,Packet_Length_Variance,Packet_Length_Std,Down_Up_Ratio,Flow_IAT_Min,ACK_Flag_Count,Init_Bwd_Win_Bytes,Fwd_Seg_Size_Min,Bwd_Packet_Length_Min,Fwd_Packet_Length_Std,Fwd_IAT_Min,Fwd_Header_Length,URG_Flag_Count):
#def make(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o):
#l=[Flow Bytes/s,Fwd Packets Length Total,Init Fwd Win Bytes,Packet Length Variance,Packet Length Std,Down/Up Ratio,Flow IAT Min,ACK Flag Count,Init Bwd Win Bytes,Fwd Seg Size Min,Bwd Packet Length Min,Fwd Packet Length Std,Fwd IAT Min,Fwd Header Length,URG Flag Count]
# Input =Input.strip("'") # Remove leading and trailing double quotes
#l=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o]
l=[Flow_Bytes_per_s,Fwd_Packets_Length_Total,Init_Fwd_Win_Bytes,Packet_Length_Variance,Packet_Length_Std,Down_Up_Ratio,Flow_IAT_Min,ACK_Flag_Count,Init_Bwd_Win_Bytes,Fwd_Seg_Size_Min,Bwd_Packet_Length_Min,Fwd_Packet_Length_Std,Fwd_IAT_Min,Fwd_Header_Length,URG_Flag_Count]
reverse_encoding={
0:"Benign",
1:"DNS",
2:"LDAP",
3:'MSSQL',
4:"NTP",
5:"NetBIOS",
6:"Portmap",
7:"SNMP",
8:"Syn",
9:"TFTP",
10:"UDP",
11:"UDPLag",
12:"WebDDoS"}
if l==[0]*15: return "Invalid Input"
else:
s=[]
s.append(l)
loaded_model = joblib.load('your_model.pkl')
predictions=loaded_model.predict(s)
# for i in range(len(predictions)):
# print("Prediction",i+1,':',reverse_encoding[predictions[i]])
import csv
input_file = "predictions.csv"
new_row = l
new_row.append(reverse_encoding[predictions[0]])
with open(input_file, 'r', newline='') as infile:
reader = csv.reader(infile)
data = list(reader)
with open(input_file, 'a', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerow(new_row)
return reverse_encoding[predictions[0]]
headline ="""
Department of Information Technology \n
National Institute of Technology Karnataka \n
DDoS Attack Classification Tool Using Random Forest Classifier \n
Contributors: Mahit Nandan (211AI001), Ishan Godbole (211AI020) \n
Under the guidance of Dr. Jaidhar C.D. \n
"""
iface=gr.Interface(fn=make,
inputs=[gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float),gr.Number(value=float)],
#outputs="text",
outputs=[gr.Textbox(lines=1,label="Type of DDoS Attack")],
title=headline,
examples=[[9638.51,2088.0,-1,1130.1428,33.617596,0.0,0.0,0,-1,14,0.0,35.08846,0.0,96,0],
[21989.434,3552.0,257,442608.88,665.2886,1.0,1.0,0,386,20,6.0,877.0259,2.0,120,1],
[46730160.0,2944.0,-1,0.0,0.0,0.0,63.0,0,-1,-1062718972,0.0,0.0,63.0,-2125437944,0]
])
# iface.launch(share=True)
iface.launch(share=True,debug=False,favicon_path="logo.png")