-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
133 lines (108 loc) Β· 5.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
from llama_cpp import Llama
import json
# Initialize the Llama model
llm = Llama(
model_path="../llama.cpp/models/mixtral-8x7B/ggml-model-Q4_K_M.gguf",
n_ctx=4096,
n_gpu_layers=-1,
chat_format="chatml"
)
st.set_page_config(page_title="π¬ Security ChatBot")
st.title('Security ChatBot - Ask me something')
if 'conversation_history' not in st.session_state:
st.session_state['conversation_history'] = []
if 'debug_info' not in st.session_state:
st.session_state['debug_info'] = {}
user_query = st.text_area("Enter your query related to cybersecurity research", '')
prompt_template = """
Q: {user_query}
Context: The user is seeking in-depth analysis and actionable insights on cybersecurity vulnerabilities.
Please provide detailed information, potential mitigation strategies, and reference relevant tools or resources.
A: """
def display_conversation():
"""
Display the conversation history with the most recent messages first.
Format messages appropriately, including code blocks.
"""
for exchange in st.session_state['conversation_history']:
# Check for the type of exchange to handle string-only history entries gracefully
if isinstance(exchange, dict):
message, sender = exchange['message'], exchange['sender']
else: # Fallback for string entries, assuming all older entries are from the user
message, sender = exchange, 'User'
# Format message as Markdown to properly display code blocks and maintain line breaks
if sender == 'Agent' and '```' in message:
# Assuming code blocks are enclosed in triple backticks
st.markdown(f"**{sender}:**\n```{message.split('```')[1]}```", unsafe_allow_html=True)
else:
st.markdown(f"**{sender}:**\n{message}", unsafe_allow_html=True) # clearly not safe
def display_debug_info():
if st.session_state['debug_info']:
st.json(st.session_state['debug_info'])
def export_conversation_history():
with open('conversation_history.txt', 'w') as file:
for exchange in reversed(st.session_state['conversation_history']):
if isinstance(exchange, dict):
line = f"{exchange['sender']}: {exchange['message']}\n"
else:
line = f"User: {exchange}\n"
file.write(line)
st.success('Conversation exported successfully!')
if st.button('Submit'):
if user_query:
st.session_state['conversation_history'].append({"sender": "Researcher", "message": user_query})
with st.spinner('Analyzing your query...'):
prompt = prompt_template.format(user_query=user_query)
output = llm(prompt, max_tokens=2048, stop=["Q:"], echo=True)
if 'choices' in output and len(output['choices']) > 0:
raw_response = output['choices'][0]['text']
user_friendly_response = raw_response.split('A: ')[-1].strip()
st.session_state['conversation_history'].append({"sender": "Agent", "message": user_friendly_response})
st.session_state['debug_info'] = output
else:
st.error("The model did not return a valid response. Please try again.")
else:
st.warning("Please enter a query.")
display_conversation()
# Sidebar for additional controls
with st.sidebar:
st.image('https://files.oaiusercontent.com/file-TAKOjgzaq5efA7OIusxh2Vkw?se=2024-03-20T06%3A55%3A23Z&sp=r&sv=2021-08-06&sr=b&rscc=max-age%3D31536000%2C%20immutable&rscd=attachment%3B%20filename%3Dbf88bd85-a472-465b-8707-3d315307bc9b.webp&sig=CuqICRvq4pHQ45NGIxADC5AcIjrzdutbcrrYLKA73AY%3D', width=100) # Consider adding a logo or related visual
st.markdown('## π Controls & Tools')
st.markdown("""---""")
# Reset Conversation Button
col1, col2 = st.columns([1, 4])
with col1:
if st.button('π', help='Reset Conversation'):
st.session_state.conversation_history = []
st.session_state.debug_info = {}
with col2:
st.markdown("**Reset Conversation**")
# Export Conversation History Button
col1, col2 = st.columns([1, 4])
with col1:
if st.button('πΎ', help='Export Conversation History'):
export_conversation_history()
with col2:
st.markdown("**Export Conversation History**")
# Show Debug Information Button
col1, col2 = st.columns([1, 4])
with col1:
if st.button('π', help='Show Debug Information'):
display_debug_info()
with col2:
st.markdown("**Show Debug Information**")
st.markdown("---") # Horizontal line for visual separation
st.markdown("## π Integration Controls")
# Slack Integration Button
if st.button("Slack"):
# stub
st.markdown("""---""") # Horizontal line for visual separation
# Link to Blog Post
st.markdown('π [Blog post on setup and how the app was built](https://blog.stellersjay.pub)')
st.markdown("---") # Horizontal line for visual separation
st.markdown("## π¬ Contact & Source Code")
# GitHub Repo Link
st.markdown("Check out the [GitHub repository](https://github.com/jwalker/your-repo-name) for this project.")
# Email Contact
st.markdown("Feel free to reach out via email: [jwalker](mailto:[email protected])")