Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import subprocess
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
import streamlit.components.v1 as components
|
7 |
+
|
8 |
+
# ---------------------------- Helper Function for NER Data ----------------------------
|
9 |
+
|
10 |
+
def generate_ner_data():
|
11 |
+
# Sample NER data for different entities
|
12 |
+
data_person = [{"text": f"Person example {i}", "entities": [{"entity": "Person", "value": f"Person {i}"}]} for i in range(1, 21)]
|
13 |
+
data_organization = [{"text": f"Organization example {i}", "entities": [{"entity": "Organization", "value": f"Organization {i}"}]} for i in range(1, 21)]
|
14 |
+
data_location = [{"text": f"Location example {i}", "entities": [{"entity": "Location", "value": f"Location {i}"}]} for i in range(1, 21)]
|
15 |
+
data_date = [{"text": f"Date example {i}", "entities": [{"entity": "Date", "value": f"Date {i}"}]} for i in range(1, 21)]
|
16 |
+
data_product = [{"text": f"Product example {i}", "entities": [{"entity": "Product", "value": f"Product {i}"}]} for i in range(1, 21)]
|
17 |
+
|
18 |
+
# Create a dictionary of all NER examples
|
19 |
+
ner_data = {
|
20 |
+
"Person": data_person,
|
21 |
+
"Organization": data_organization,
|
22 |
+
"Location": data_location,
|
23 |
+
"Date": data_date,
|
24 |
+
"Product": data_product
|
25 |
+
}
|
26 |
+
|
27 |
+
return ner_data
|
28 |
+
|
29 |
+
# ---------------------------- Fun NER Data Function ----------------------------
|
30 |
+
|
31 |
+
def ner_demo():
|
32 |
+
st.header("π€ LLM NER Model Demo π΅οΈββοΈ")
|
33 |
+
|
34 |
+
# Generate NER data
|
35 |
+
ner_data = generate_ner_data()
|
36 |
+
|
37 |
+
# Pick a random entity type to display
|
38 |
+
entity_type = random.choice(list(ner_data.keys()))
|
39 |
+
st.subheader(f"Here comes the {entity_type} entity recognition, ready to show its magic! π©β¨")
|
40 |
+
|
41 |
+
# Select a random record to display
|
42 |
+
example = random.choice(ner_data[entity_type])
|
43 |
+
st.write(f"Analyzing: *{example['text']}*")
|
44 |
+
|
45 |
+
# Display recognized entity
|
46 |
+
for entity in example["entities"]:
|
47 |
+
st.success(f"π Found a {entity['entity']}: **{entity['value']}**")
|
48 |
+
|
49 |
+
# A bit of rhyme to lighten up the task
|
50 |
+
st.write("There once was an AI so bright, π")
|
51 |
+
st.write("It could spot any name in sight, ποΈ")
|
52 |
+
st.write("With a click or a tap, it put on its cap, π©")
|
53 |
+
st.write("And found entities day or night! π")
|
54 |
+
|
55 |
+
# ---------------------------- Header and Introduction ----------------------------
|
56 |
+
|
57 |
+
st.set_page_config(page_title="LLMs for Cyber Security", page_icon="π", layout="wide", initial_sidebar_state="expanded")
|
58 |
+
st.title("ππ LLMs for Cyber Security: State-of-the-Art Surveysππ")
|
59 |
+
st.markdown("This app is based on the paper: [Large Language Models for Cyber Security](https://arxiv.org/pdf/2405.04760v3). It showcases LLMs in the cybersecurity landscape, summarizing key surveys and insights.")
|
60 |
+
st.markdown('ππ https://arxiv.org/abs/2405.04760v3')
|
61 |
+
st.markdown("---")
|
62 |
+
|
63 |
+
# ---------------------------- Call NER Demo ----------------------------
|
64 |
+
|
65 |
+
if st.button('π§ͺ Run NER Model Demo'):
|
66 |
+
ner_demo()
|
67 |
+
else:
|
68 |
+
st.write("Click the button above to start the AI NER magic! π©β¨")
|
69 |
+
|
70 |
+
# ---------------------------- Data Preparation ----------------------------
|
71 |
+
|
72 |
+
data = {
|
73 |
+
"Reference": ["Motlagh et al.", "Divakaran et al.", "Yao et al.", "Yigit et al.", "Coelho et al.", "Novelli et al.", "LLM4Security"],
|
74 |
+
"Year": [2024, 2024, 2023, 2024, 2024, 2024, 2024],
|
75 |
+
"Scope": ["Security application", "Security application", "Security application, Security of LLM", "Security application, Security of LLM", "Security application", "Security application", "Security application"],
|
76 |
+
"Dimensions": ["Task", "Task", "Model, Task", "Task", "Task, Domain specific technique", "Task, Model, Domain specific technique", "Model, Task, Domain specific technique, Data"],
|
77 |
+
"Time frame": ["2022-2023", "2020-2024", "2019-2024", "2020-2024", "2021-2023", "2020-2024", "2020-2024"],
|
78 |
+
"Papers": ["Not specified", "Not specified", 281, "Not specified", 19, "Not specified", 127]
|
79 |
+
}
|
80 |
+
df = pd.DataFrame(data)
|
81 |
+
|
82 |
+
# ---------------------------- Display Data Table ----------------------------
|
83 |
+
|
84 |
+
st.subheader("π Survey Overview Table")
|
85 |
+
st.dataframe(df, height=300)
|
86 |
+
st.markdown("---")
|
87 |
+
|
88 |
+
# ---------------------------- Mermaid Diagram Visualization ----------------------------
|
89 |
+
|
90 |
+
st.subheader("π‘οΈ Security Model Visualization with Mermaid")
|
91 |
+
|
92 |
+
mermaid_code = '''
|
93 |
+
graph TD;
|
94 |
+
A[LLMs in Security] --> B[Security Application]
|
95 |
+
B --> C[Task]
|
96 |
+
B --> D[Model]
|
97 |
+
D --> E[Domain-Specific Techniques]
|
98 |
+
E --> F[Data]
|
99 |
+
'''
|
100 |
+
|
101 |
+
# HTML component for Mermaid diagram
|
102 |
+
mermaid_html = f"""
|
103 |
+
<html>
|
104 |
+
<body>
|
105 |
+
<pre class="mermaid">
|
106 |
+
{mermaid_code}
|
107 |
+
</pre>
|
108 |
+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
109 |
+
<script>
|
110 |
+
mermaid.initialize({{ startOnLoad: true }});
|
111 |
+
</script>
|
112 |
+
</body>
|
113 |
+
</html>
|
114 |
+
"""
|
115 |
+
|
116 |
+
components.html(mermaid_html, height=300)
|
117 |
+
|
118 |
+
st.markdown("""
|
119 |
+
Figure: The diagram illustrates how Large Language Models (LLMs) are applied in security, highlighting the flow from general applications to specific tasks, models, domain-specific techniques, and data considerations.
|
120 |
+
""")
|
121 |
+
st.markdown("---")
|
122 |
+
|
123 |
+
# ---------------------------- Interactive Chart Example ----------------------------
|
124 |
+
|
125 |
+
st.subheader("π Interactive Chart Example")
|
126 |
+
|
127 |
+
# Sample data for the chart
|
128 |
+
chart_data = [
|
129 |
+
{"year": 2020, "papers": 50},
|
130 |
+
{"year": 2021, "papers": 80},
|
131 |
+
{"year": 2022, "papers": 120},
|
132 |
+
{"year": 2023, "papers": 200},
|
133 |
+
{"year": 2024, "papers": 250},
|
134 |
+
]
|
135 |
+
|
136 |
+
# HTML component for Chart.js
|
137 |
+
chart_html = f"""
|
138 |
+
<html>
|
139 |
+
<head>
|
140 |
+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
141 |
+
</head>
|
142 |
+
<body>
|
143 |
+
<canvas id="myChart" width="400" height="200"></canvas>
|
144 |
+
<script>
|
145 |
+
var ctx = document.getElementById('myChart').getContext('2d');
|
146 |
+
var myChart = new Chart(ctx, {{
|
147 |
+
type: 'line',
|
148 |
+
data: {{
|
149 |
+
labels: {[d['year'] for d in chart_data]},
|
150 |
+
datasets: [{{
|
151 |
+
label: 'Number of Papers',
|
152 |
+
data: {[d['papers'] for d in chart_data]},
|
153 |
+
borderColor: 'rgb(75, 192, 192)',
|
154 |
+
tension: 0.1
|
155 |
+
}}]
|
156 |
+
}},
|
157 |
+
options: {{
|
158 |
+
responsive: true,
|
159 |
+
scales: {{
|
160 |
+
y: {{
|
161 |
+
beginAtZero: true
|
162 |
+
}}
|
163 |
+
}}
|
164 |
+
}}
|
165 |
+
}});
|
166 |
+
</script>
|
167 |
+
</body>
|
168 |
+
</html>
|
169 |
+
"""
|
170 |
+
|
171 |
+
components.html(chart_html, height=300)
|
172 |
+
st.markdown("This interactive chart shows the growth in the number of papers on LLMs in cybersecurity over the years.")
|
173 |
+
st.markdown("---")
|
174 |
+
|
175 |
+
# ---------------------------- Footer and Additional Resources ----------------------------
|
176 |
+
|
177 |
+
st.subheader("π Additional Resources")
|
178 |
+
st.markdown("""
|
179 |
+
- [Official Streamlit Documentation](https://docs.streamlit.io/)
|
180 |
+
- [pip-audit GitHub Repository](https://github.com/pypa/pip-audit)
|
181 |
+
- [Mermaid Live Editor](https://mermaid.live/) - Design and preview Mermaid diagrams.
|
182 |
+
- [Azure Container Apps Documentation](https://docs.microsoft.com/en-us/azure/container-apps/)
|
183 |
+
- [Cybersecurity Best Practices by CISA](https://www.cisa.gov/cybersecurity-best-practices)
|
184 |
+
""")
|
185 |
+
st.markdown("---")
|
186 |
+
|
187 |
+
# ---------------------------- Sidebar Content ----------------------------
|
188 |
+
|
189 |
+
st.sidebar.title("Navigation")
|
190 |
+
st.sidebar.markdown("""
|
191 |
+
- [Introduction](#llms-for-cyber-security-state-of-the-art-surveys)
|
192 |
+
- [Survey Overview Table](#survey-overview-table)
|
193 |
+
- [Security Model Visualization](#security-model-visualization-with-mermaid)
|
194 |
+
- [Interactive Chart](#interactive-chart-example)
|
195 |
+
- [Additional Resources](#additional-resources)
|
196 |
+
""", unsafe_allow_html=True)
|
197 |
+
|
198 |
+
st.sidebar.title("About")
|
199 |
+
st.sidebar.info("""
|
200 |
+
This Streamlit app was developed to demonstrate the intersection of Large Language Models and Cybersecurity, highlighting recent surveys and providing tools and recommendations for secure coding practices.
|
201 |
+
""")
|
202 |
+
|
203 |
+
# ---------------------------- End of App ----------------------------
|
204 |
+
|
205 |
+
# ---------------------------- Self-Assessment ----------------------------
|
206 |
+
|
207 |
+
# Score: 9/10
|
208 |
+
# Rationale: The app integrates humor, creativity, and interactivity well with solid features. It creates an engaging experience for the user by adding playful commentary and jokes.
|
209 |
+
# Points for improvement: More advanced
|