Spaces:
Build error
Build error
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import pickle | |
| import matplotlib.pyplot as plt | |
| # Load model | |
| with open('random_forest_classifier (1).pkl', 'rb') as file: | |
| model = pickle.load(file) | |
| # Define the possible outcomes | |
| class_labels = ['phishing', 'benign', 'defacement', 'malware'] | |
| def main(): | |
| st.set_page_config(page_title="Malicious URL Detection", layout="wide") | |
| st.title("Malicious URL Detection") | |
| st.markdown(""" | |
| <style> | |
| .main {background-color: #f5f5f5;} | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown("### Enter the URL to predict its category") | |
| url_input = st.text_input("Enter URL") | |
| if st.button("Predict"): | |
| if url_input: | |
| # Perform prediction | |
| prediction, prediction_probabilities = predict_url(url_input) | |
| display_prediction(prediction, prediction_probabilities) | |
| else: | |
| st.warning("Please enter a URL for prediction.") | |
| def predict_url(url): | |
| # Logic to extract features from the URL and perform prediction | |
| # For demonstration purposes, let's just return a random prediction and random probabilities | |
| prediction = np.random.choice(class_labels) | |
| prediction_probabilities = np.random.rand(len(class_labels)) | |
| prediction_probabilities /= np.sum(prediction_probabilities) # Normalize to get probabilities | |
| return prediction, prediction_probabilities | |
| def display_prediction(prediction, prediction_probabilities): | |
| st.subheader(f"The URL is predicted to be: {prediction}") | |
| if prediction == 'phishing': | |
| st.error("The URL is predicted to be: phishing") | |
| elif prediction == 'malware': | |
| st.warning("The URL is predicted to be: malware") | |
| elif prediction == 'defacement': | |
| st.warning("The URL is predicted to be: defacement") | |
| else: | |
| st.success("The URL is predicted to be: benign") | |
| # Display prediction probabilities | |
| st.subheader("Prediction Probabilities:") | |
| prob_df = pd.DataFrame(prediction_probabilities, index=class_labels, columns=["Probability"]) | |
| st.bar_chart(prob_df) | |
| # Another example chart: Prediction distribution (simulated) | |
| st.subheader("Prediction Distribution (Simulated):") | |
| prediction_counts = np.random.randint(1, 100, size=len(class_labels)) | |
| prediction_counts[class_labels.index(prediction)] += 100 # Simulate the current prediction | |
| fig, ax = plt.subplots() | |
| ax.bar(class_labels, prediction_counts, color=['red', 'green', 'blue', 'orange']) | |
| ax.set_xlabel('Class') | |
| ax.set_ylabel('Count') | |
| ax.set_title('Prediction Distribution') | |
| st.pyplot(fig) | |
| if __name__ == '__main__': | |
| main() | |