-
Notifications
You must be signed in to change notification settings - Fork 36
/
streamlit_app.py
141 lines (114 loc) · 4.28 KB
/
streamlit_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
133
134
135
136
137
138
139
140
141
# github.com/deadbits/vigil-llm
import os
import json
# import yara
import requests
import streamlit as st
from streamlit_extras.badges import badge
from streamlit_extras.stateful_button import button
st.header('Vigil - LLM security scanner')
st.subheader('Web Playground', divider='rainbow')
# Initialize session state for storing history
if 'history' not in st.session_state:
st.session_state['history'] = []
with st.sidebar:
st.header('Vigil - LLM security scanner', divider='rainbow')
st.write('[documentation](https://vigil.deadbits.ai) | [github](https://github.com/deadbits/vigil-llm)')
badge(type="github", name="deadbits/vigil-llm")
st.divider()
page = st.sidebar.radio(
"Select a page:",
[
"Prompt Analysis",
"Upload YARA Rule",
"History",
"Settings"
]
)
if page == "Prompt Analysis":
# Text input for the user to enter the prompt
prompt = st.text_area("Enter prompt:")
if button("Submit", key="button1"):
if prompt:
response = requests.post(
"http://localhost:5000/analyze/prompt",
headers={"Content-Type": "application/json"},
data=json.dumps({"prompt": prompt})
)
# Check if the response was successful
if response.status_code == 200:
data = response.json()
# Add to history
st.session_state['history'].append({
"timestamp": data["timestamp"],
"prompt": prompt,
"response": data
})
# Display the input prompt
st.write("**Prompt:** ", data["prompt"])
st.write("**Entropy:** ", data["prompt_entropy"])
st.write("**UUID:** ", data["uuid"])
# Display messages
if data["messages"]:
for message in data["messages"]:
# the messages field holds scanners matches so raise them
# as a "warning" on the UI
st.warning(message, icon="⚠️")
# Display errors
if data["errors"]:
st.write("Errors:")
for error in data["errors"]:
st.error(error)
# Display results
for scanner, details in data["results"].items():
st.write(f"**{scanner}**")
for match in details["matches"]:
st.write(match)
else:
st.error("Failed to get a response from the server.")
elif page == "History":
st.title("History")
# Sort history by timestamp (newest first)
sorted_history = sorted(
st.session_state['history'], key=lambda x: x['timestamp'], reverse=True
)
for item in sorted_history:
st.write("Timestamp:", item["timestamp"])
st.write("Prompt:", item["prompt"])
st.write("Response:", item["response"])
st.write("-" * 50)
elif page == "Settings":
st.title("Settings")
response = requests.get("http://localhost:5000/settings")
if response.status_code == 200:
settings_data = response.json()
st.json(settings_data)
else:
st.error("Failed to retrieve settings from the server.")
elif page == "Upload YARA Rule":
st.title("Upload Custom YARA Rule")
# YARA rule template
template = """
rule MyRule_custom: customTag
{
meta:
category = "Prompt Injection"
description = "Detects prompts that contain some custom strings or regex"
strings:
...
condition:
...
}
"""
rule_name = st.text_input("Rule Name", "MyRule_custom")
rule_content = st.text_area("YARA Rule", value=template, height=300)
if st.button("Save"):
rule_filename = f"{rule_name}.yar"
path = os.path.join("data", "yara", rule_filename)
# Check if the directory exists, if not, create it
if not os.path.exists(os.path.join("data", "yara")):
os.makedirs(os.path.join("data", "yara"))
# Save the YARA rule to the specified file
with open(path, "w") as file:
file.write(rule_content)
st.success(f"Saved YARA rule to {path}")