-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
147 lines (126 loc) · 3.74 KB
/
app.py
File metadata and controls
147 lines (126 loc) · 3.74 KB
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
142
143
144
145
146
147
import os
import streamlit as st
from guard.clients import GroqClient
from streamlit_ace import st_ace
from streamlit_theme import st_theme
st.set_page_config(
layout="wide",
page_icon="🔍",
page_title="Vulnerability Explorer",
menu_items={
"Get Help": "https://github.com/codeguardai/explorer/issues",
"Report a bug": "https://github.com/codeguardai/explorer/issues",
},
)
st.title("Vulnerability Explorer")
# Ensure the Groq API key is set
if not os.environ.get("GROQ_API_KEY"):
st.error(
"Groq API key not found. Please set the 'GROQ_API_KEY' environment variable."
)
st.stop()
# Initialize the AI client using GroqClient from guard
ai_client = GroqClient("llama3-8b-8192")
LANGUAGE_DISPLAY_MAP = {
"C": "c_cpp",
"C++": "c_cpp",
"C#": "csharp",
"Go": "golang",
"Python": "python",
"JavaScript": "javascript",
"Java": "java",
"Ruby": "ruby",
"PHP": "php",
"Swift": "swift",
"Kotlin": "kotlin",
"HTML": "html",
"CSS": "css",
"TypeScript": "typescript",
"JSON": "json",
"Markdown": "markdown",
"Elixir": "elixir",
"Rust": "rust",
"Erlang": "erlang",
}
available_languages = sorted(LANGUAGE_DISPLAY_MAP.keys())
# Set the Ace editor theme based on the base theme
theme = st_theme()
base_theme = (
theme.get("base")
if theme and isinstance(theme, dict) and "base" in theme
else "dark"
)
ace_theme = "twilight" if base_theme == "dark" else "chrome"
# Initialize session state for the result if not already initialized
if "result" not in st.session_state:
st.session_state["result"] = "Results will appear here."
# Default Python code snippet
default_python_code = """import os
def read_file(filepath):
return open(filepath).read()
def login(user, pwd):
if user == "admin" and pwd == "password123":
print("Welcome!")
else:
print("Access denied!")
filepath = input("File path: ")
print(read_file(filepath))
user = input("Username: ")
pwd = input("Password: ")
login(user, pwd)
"""
# Define layout columns for input and results
input_column, result_column = st.columns(2)
with input_column:
st.header("📝 Input Code")
display_language = st.selectbox(
"Choose Language:",
available_languages,
index=available_languages.index("Python"),
)
ace_language = LANGUAGE_DISPLAY_MAP[display_language]
# Code editor with syntax highlighting using st_ace
code_input = st_ace(
value=default_python_code if display_language == "Python" else "",
placeholder="Enter your code here...",
language=ace_language,
theme=ace_theme,
keybinding="vscode",
font_size=14,
tab_size=4,
show_gutter=True,
wrap=False,
auto_update=True,
min_lines=20,
key="ace_editor",
)
evaluate = st.button("Evaluate")
st.markdown(
"""
<style>
.scrollable-container {
max-height: 100vh;
overflow-y: auto;
padding: 1em;
}
</style>
""",
unsafe_allow_html=True,
)
with result_column:
st.header("🔍 Analysis")
if evaluate:
if code_input.strip() == "":
st.warning("Please enter some code before evaluating.")
else:
with st.spinner("Analyzing code..."):
try:
code_with_language = f"Language: {display_language}\n{code_input}"
result = ai_client.scan_code(code_with_language)
st.session_state["result"] = result
except Exception as e:
st.session_state["result"] = f"An error occurred: {e}"
st.markdown(
f'<div class="scrollable-container">{st.session_state["result"]}</div>',
unsafe_allow_html=True,
)