Update app.py
Browse files
app.py
CHANGED
@@ -4,217 +4,123 @@ from pathlib import Path
|
|
4 |
from processor import DataProcessor
|
5 |
from llm_handler import LLMHandler
|
6 |
|
7 |
-
class
|
8 |
def __init__(self):
|
9 |
-
#
|
10 |
st.set_page_config(
|
11 |
-
page_title="DDoS
|
12 |
page_icon=":shield:",
|
13 |
layout="wide",
|
14 |
initial_sidebar_state="expanded"
|
15 |
)
|
16 |
|
17 |
# Initialize session state
|
18 |
-
self.
|
19 |
|
20 |
# Initialize processor and LLM handler
|
21 |
self.processor = DataProcessor()
|
22 |
self.llm_handler = LLMHandler()
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
'Light': {
|
27 |
-
'base': 'light',
|
28 |
-
'primaryColor': '#3B82F6',
|
29 |
-
'backgroundColor': '#FFFFFF',
|
30 |
-
'secondaryBackgroundColor': '#F3F4F6',
|
31 |
-
'textColor': '#000000'
|
32 |
-
},
|
33 |
-
'Dark': {
|
34 |
-
'base': 'dark',
|
35 |
-
'primaryColor': '#10B981',
|
36 |
-
'backgroundColor': '#1F2937',
|
37 |
-
'secondaryBackgroundColor': '#374151',
|
38 |
-
'textColor': '#FFFFFF'
|
39 |
-
},
|
40 |
-
'Neon': {
|
41 |
-
'base': 'dark',
|
42 |
-
'primaryColor': '#22D3EE',
|
43 |
-
'backgroundColor': '#000000',
|
44 |
-
'secondaryBackgroundColor': '#1F2937',
|
45 |
-
'textColor': '#22D3EE'
|
46 |
-
}
|
47 |
-
}
|
48 |
-
|
49 |
-
def setup_session_state(self):
|
50 |
-
"""Initialize Streamlit session state variables."""
|
51 |
-
st.write("Setting up session state.")
|
52 |
session_keys = [
|
53 |
-
'chat_history'
|
54 |
-
'current_file',
|
55 |
-
'analysis_complete',
|
56 |
-
'selected_theme',
|
57 |
-
'preview_data'
|
58 |
]
|
59 |
-
|
60 |
for key in session_keys:
|
61 |
if key not in st.session_state:
|
62 |
st.session_state[key] = None if key != 'chat_history' else []
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
"""Apply selected theme to Streamlit app."""
|
68 |
-
theme_name = st.session_state.get('selected_theme', 'Light')
|
69 |
-
theme = self.themes[theme_name]
|
70 |
-
|
71 |
-
st.markdown(f"""
|
72 |
-
<style>
|
73 |
-
.stApp {{
|
74 |
-
background-color: {theme['backgroundColor']};
|
75 |
-
color: {theme['textColor']};
|
76 |
-
}}
|
77 |
-
.stTabs [data-baseweb="tab-list"] {{
|
78 |
-
background-color: {theme['secondaryBackgroundColor']};
|
79 |
-
}}
|
80 |
-
.stButton>button {{
|
81 |
-
background-color: {theme['primaryColor']};
|
82 |
-
color: white;
|
83 |
-
}}
|
84 |
-
</style>
|
85 |
-
""", unsafe_allow_html=True)
|
86 |
-
|
87 |
-
def render_theme_icons(self):
|
88 |
-
"""Render theme selection icons on the right sidebar."""
|
89 |
-
st.sidebar.header("🌈 Theme Selector")
|
90 |
-
|
91 |
-
# Create buttons for theme selection with icons
|
92 |
-
light_icon = "☀️"
|
93 |
-
dark_icon = "🌙"
|
94 |
-
neon_icon = "💡"
|
95 |
-
|
96 |
-
col1, col2, col3 = st.sidebar.columns(3)
|
97 |
|
98 |
with col1:
|
99 |
-
|
100 |
-
st.session_state.selected_theme = 'Light'
|
101 |
-
self.apply_theme()
|
102 |
|
103 |
with col2:
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
if st.button(neon_icon):
|
110 |
-
st.session_state.selected_theme = 'Neon'
|
111 |
-
self.apply_theme()
|
112 |
|
113 |
def render_file_upload(self):
|
114 |
-
"""Render file upload
|
115 |
-
st.
|
116 |
-
|
117 |
-
uploaded_file = st.sidebar.file_uploader(
|
118 |
-
"Choose a CSV file", type=['csv'],
|
119 |
-
help="Upload your network traffic data for DDoS analysis"
|
120 |
-
)
|
121 |
-
|
122 |
if uploaded_file:
|
123 |
try:
|
124 |
-
# Attempt to read the file
|
125 |
df = pd.read_csv(uploaded_file)
|
126 |
-
st.session_state.current_file =
|
127 |
-
st.
|
128 |
-
|
129 |
-
if st.sidebar.button("🚀 Process Data", use_container_width=True):
|
130 |
-
self.process_uploaded_file(uploaded_file)
|
131 |
-
|
132 |
except Exception as e:
|
133 |
-
st.error(f"Error
|
134 |
-
|
135 |
-
def process_uploaded_file(self, file):
|
136 |
-
"""Process the uploaded file and perform analysis."""
|
137 |
-
try:
|
138 |
-
st.write("Processing uploaded file.")
|
139 |
-
with st.spinner("Processing data..."):
|
140 |
-
# Preprocess data
|
141 |
-
df = self.processor.preprocess_data(file)
|
142 |
-
|
143 |
-
# Calculate statistics
|
144 |
-
stats = self.processor.calculate_statistics(df)
|
145 |
-
|
146 |
-
# Trigger LLM analysis
|
147 |
-
results = self.llm_handler.analyze_data(df)
|
148 |
-
|
149 |
-
# Update session state
|
150 |
-
st.session_state.analysis_complete = True
|
151 |
-
st.success("Analysis completed successfully!")
|
152 |
-
|
153 |
-
except Exception as e:
|
154 |
-
st.error(f"Error processing file: {e}")
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
if not st.session_state.current_file:
|
161 |
-
st.info("Please upload a CSV file to begin analysis")
|
162 |
return
|
163 |
|
164 |
-
#
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
def render_analysis_tab(self):
|
174 |
-
"""Render analysis results tab."""
|
175 |
-
if st.session_state.preview_data is not None:
|
176 |
-
st.subheader("📋 Data Preview")
|
177 |
-
st.dataframe(st.session_state.preview_data)
|
178 |
-
|
179 |
-
if st.session_state.analysis_complete:
|
180 |
-
st.subheader("🔍 Analysis Results")
|
181 |
-
# Placeholder for more detailed analysis visualization
|
182 |
-
st.info("Full analysis results would be displayed here.")
|
183 |
|
184 |
-
|
185 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
# Display chat history
|
187 |
for message in st.session_state.chat_history:
|
188 |
with st.chat_message(message['role']):
|
189 |
st.write(message['content'])
|
190 |
|
191 |
# Chat input
|
192 |
-
if prompt := st.
|
193 |
# Add user message to chat history
|
194 |
st.session_state.chat_history.append({
|
195 |
-
'role': 'user',
|
196 |
'content': prompt
|
197 |
})
|
198 |
|
199 |
-
# Get
|
200 |
response = self.llm_handler.get_chat_response(prompt)
|
201 |
|
202 |
-
# Add
|
203 |
st.session_state.chat_history.append({
|
204 |
-
'role': 'assistant',
|
205 |
'content': response
|
206 |
})
|
207 |
|
208 |
-
def
|
209 |
-
"""
|
210 |
-
self.
|
211 |
-
|
212 |
-
# Main content header styling for better UI experience
|
213 |
-
st.title("🛡️ DDoS Traffic Analyzer")
|
214 |
self.render_file_upload()
|
215 |
-
self.
|
|
|
216 |
|
217 |
-
# Run the application
|
218 |
if __name__ == "__main__":
|
219 |
-
app =
|
220 |
-
app.
|
|
|
4 |
from processor import DataProcessor
|
5 |
from llm_handler import LLMHandler
|
6 |
|
7 |
+
class DDoSResistanceHelper:
|
8 |
def __init__(self):
|
9 |
+
# Configure Streamlit app
|
10 |
st.set_page_config(
|
11 |
+
page_title="DDoS Resistance Helper URF LLM Network Analyzer",
|
12 |
page_icon=":shield:",
|
13 |
layout="wide",
|
14 |
initial_sidebar_state="expanded"
|
15 |
)
|
16 |
|
17 |
# Initialize session state
|
18 |
+
self.initialize_session_state()
|
19 |
|
20 |
# Initialize processor and LLM handler
|
21 |
self.processor = DataProcessor()
|
22 |
self.llm_handler = LLMHandler()
|
23 |
|
24 |
+
def initialize_session_state(self):
|
25 |
+
"""Set up Streamlit session state variables."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
session_keys = [
|
27 |
+
'current_file', 'preprocessed_data', 'analysis_results', 'chat_history'
|
|
|
|
|
|
|
|
|
28 |
]
|
|
|
29 |
for key in session_keys:
|
30 |
if key not in st.session_state:
|
31 |
st.session_state[key] = None if key != 'chat_history' else []
|
32 |
|
33 |
+
def render_top_bar(self):
|
34 |
+
"""Render the top bar with theme and upload options."""
|
35 |
+
col1, col2 = st.columns([8, 2])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
with col1:
|
38 |
+
st.title("🛡️ DDoS Resistance Helper URF LLM Network Analyzer")
|
|
|
|
|
39 |
|
40 |
with col2:
|
41 |
+
st.markdown("### Theme Selector")
|
42 |
+
if st.button("Light"):
|
43 |
+
st.markdown("<style>.stApp { background-color: #ffffff; }</style>", unsafe_allow_html=True)
|
44 |
+
elif st.button("Dark"):
|
45 |
+
st.markdown("<style>.stApp { background-color: #1f1f1f; color: white; }</style>", unsafe_allow_html=True)
|
|
|
|
|
|
|
46 |
|
47 |
def render_file_upload(self):
|
48 |
+
"""Render the file upload component."""
|
49 |
+
uploaded_file = st.file_uploader("Upload Network Traffic Data (CSV)", type=["csv"],
|
50 |
+
label_visibility="collapsed")
|
|
|
|
|
|
|
|
|
|
|
51 |
if uploaded_file:
|
52 |
try:
|
|
|
53 |
df = pd.read_csv(uploaded_file)
|
54 |
+
st.session_state.current_file = df
|
55 |
+
st.success("File uploaded successfully!")
|
|
|
|
|
|
|
|
|
56 |
except Exception as e:
|
57 |
+
st.error(f"Error reading file: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
def render_analysis(self):
|
60 |
+
"""Render the analysis results."""
|
61 |
+
if st.session_state.current_file is None:
|
62 |
+
st.info("Please upload a CSV file to start analysis.")
|
|
|
|
|
63 |
return
|
64 |
|
65 |
+
# Preprocess the data
|
66 |
+
st.subheader("Preprocessing Data")
|
67 |
+
with st.spinner("Preprocessing data..."):
|
68 |
+
try:
|
69 |
+
preprocessed_data = self.processor.preprocess_data(st.session_state.current_file)
|
70 |
+
st.session_state.preprocessed_data = preprocessed_data
|
71 |
+
st.success("Data preprocessed successfully!")
|
72 |
+
except Exception as e:
|
73 |
+
st.error(f"Error during preprocessing: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Perform LLM analysis
|
76 |
+
st.subheader("Performing LLM Analysis")
|
77 |
+
with st.spinner("Analyzing data with LLM..."):
|
78 |
+
try:
|
79 |
+
results = self.llm_handler.analyze_data(st.session_state.preprocessed_data)
|
80 |
+
st.session_state.analysis_results = results
|
81 |
+
st.success("Analysis completed successfully!")
|
82 |
+
except Exception as e:
|
83 |
+
st.error(f"Error during LLM analysis: {e}")
|
84 |
+
|
85 |
+
# Show results
|
86 |
+
if st.session_state.analysis_results is not None:
|
87 |
+
st.subheader("Analysis Results")
|
88 |
+
st.dataframe(st.session_state.analysis_results)
|
89 |
+
csv_path = Path("~/.dataset/PROBABILITY_OF_EACH_ROW_DDOS_AND_BENGNIN.csv").expanduser()
|
90 |
+
st.download_button("Download Results as CSV", csv_path.read_bytes(), "analysis_results.csv")
|
91 |
+
|
92 |
+
def render_chat_interface(self):
|
93 |
+
"""Render a chat interface for interacting with the LLM."""
|
94 |
+
st.sidebar.header("💬 Chat Interface")
|
95 |
# Display chat history
|
96 |
for message in st.session_state.chat_history:
|
97 |
with st.chat_message(message['role']):
|
98 |
st.write(message['content'])
|
99 |
|
100 |
# Chat input
|
101 |
+
if prompt := st.sidebar.text_input("Ask about the analysis or mitigation steps..."):
|
102 |
# Add user message to chat history
|
103 |
st.session_state.chat_history.append({
|
104 |
+
'role': 'user',
|
105 |
'content': prompt
|
106 |
})
|
107 |
|
108 |
+
# Get LLM response
|
109 |
response = self.llm_handler.get_chat_response(prompt)
|
110 |
|
111 |
+
# Add LLM response to chat history
|
112 |
st.session_state.chat_history.append({
|
113 |
+
'role': 'assistant',
|
114 |
'content': response
|
115 |
})
|
116 |
|
117 |
+
def run(self):
|
118 |
+
"""Run the Streamlit app."""
|
119 |
+
self.render_top_bar()
|
|
|
|
|
|
|
120 |
self.render_file_upload()
|
121 |
+
self.render_analysis()
|
122 |
+
self.render_chat_interface()
|
123 |
|
|
|
124 |
if __name__ == "__main__":
|
125 |
+
app = DDoSResistanceHelper()
|
126 |
+
app.run()
|