import gradio as gr import joblib import requests import os from sklearn.ensemble import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier from sklearn.tree import DecisionTreeClassifier # Load the saved models rf_model = joblib.load('rf_model.pkl') dt_model = joblib.load('decision_tree_model.pkl') bagging_model = joblib.load('model_bagging.pkl') ada_model = joblib.load('model_adaboost.pkl') # Define the feature names feature_names = [ "src_ip", "src_port", "dst_ip", "dst_port", "proto", "service", "duration", "src_bytes", "dst_bytes", "conn_state", "missed_bytes", "src_pkts", "src_ip_bytes", "dst_pkts", "dst_ip_bytes", "dns_query", "dns_qclass", "dns_qtype", "dns_rcode", "dns_AA", "dns_RD", "dns_RA", "dns_rejected", "ssl_version", "ssl_cipher", "ssl_resumed", "ssl_established", "ssl_subject", "ssl_issuer", "http_trans_depth", "http_method", "http_uri", "http_version", "http_request_body_len", "http_response_body_len", "http_status_code", "http_user_agent", "http_orig_mime_types", "http_resp_mime_types", "weird_name", "weird_addl", "weird_notice", "label" ] class_labels = { 0: "normal", 1: "backdoor", 2: "ddos", 3: "dos", 4: "injection", 5: "password", 6: "ransomware", 7: "scanning", 8: "xss", 9: "mitm" } def detect_intrusion(feature_values, model_choice="Random Forest"): # Ensure the length of feature_values matches feature_names if len(feature_values) != len(feature_names): return "Please fill in all the required feature values." # Convert the input values to floats and match them with feature names try: feature_values = [float(value) for value in feature_values] except ValueError: return "Please enter valid numerical values for all fields." # Choose the model based on user selection if model_choice == "Random Forest": model = rf_model elif model_choice == "Decision Tree": model = dt_model elif model_choice == "Bagging Classifier": model = bagging_model elif model_choice == "AdaBoost Classifier": model = ada_model else: return "Invalid model choice!" # Predict the class (multi-class classification) prediction = model.predict([feature_values]) predicted_class = prediction[0] # Get the predicted class (an integer between 0-9) # Notify the user of the detected attack or normal traffic if predicted_class == 0: return "No Intrusion Detected" else: return f"Intrusion Detected: {class_labels.get(predicted_class, 'Unknown Attack')}" # Create Gradio input fields for each feature inputs = [gr.Textbox(label=feature_name) for feature_name in feature_names[:-1]] # Exclude "label" field from inputs # Add model choice dropdown inputs.append(gr.Dropdown(choices=["Random Forest", "Decision Tree", "Bagging Classifier", "AdaBoost Classifier"], label="Select Model")) # Create the Gradio interface iface = gr.Interface( fn=detect_intrusion, inputs=inputs, outputs="text", title="Intrusion Detection System", description="Fill in the blank fields for the network traffic features, and choose the model to detect intrusions." ) # Launch the interface locally for testing iface.launch()