|
| 1 | +import os |
| 2 | + |
| 3 | +import streamlit as st |
| 4 | +from guard.clients import GroqClient |
| 5 | +from streamlit_ace import st_ace |
| 6 | +from streamlit_theme import st_theme |
| 7 | + |
| 8 | +st.set_page_config( |
| 9 | + layout="wide", |
| 10 | + page_icon="🔍", |
| 11 | + page_title="Vulnerability Explorer", |
| 12 | + menu_items={ |
| 13 | + "Get Help": "https://github.com/codeguardai/explorer/issues", |
| 14 | + "Report a bug": "https://github.com/codeguardai/explorer/issues", |
| 15 | + }, |
| 16 | +) |
| 17 | +st.title("Vulnerability Explorer") |
| 18 | + |
| 19 | +# Ensure the Groq API key is set |
| 20 | +if not os.environ.get("GROQ_API_KEY"): |
| 21 | + st.error( |
| 22 | + "Groq API key not found. Please set the 'GROQ_API_KEY' environment variable." |
| 23 | + ) |
| 24 | + st.stop() |
| 25 | + |
| 26 | +# Initialize the AI client using GroqClient from guard |
| 27 | +ai_client = GroqClient("llama3-8b-8192") |
| 28 | + |
| 29 | +LANGUAGE_DISPLAY_MAP = { |
| 30 | + "C": "c_cpp", |
| 31 | + "C++": "c_cpp", |
| 32 | + "C#": "csharp", |
| 33 | + "Go": "golang", |
| 34 | + "Python": "python", |
| 35 | + "JavaScript": "javascript", |
| 36 | + "Java": "java", |
| 37 | + "Ruby": "ruby", |
| 38 | + "PHP": "php", |
| 39 | + "Swift": "swift", |
| 40 | + "Kotlin": "kotlin", |
| 41 | + "HTML": "html", |
| 42 | + "CSS": "css", |
| 43 | + "TypeScript": "typescript", |
| 44 | + "JSON": "json", |
| 45 | + "Markdown": "markdown", |
| 46 | + "Elixir": "elixir", |
| 47 | + "Rust": "rust", |
| 48 | + "Erlang": "erlang", |
| 49 | +} |
| 50 | +available_languages = sorted(LANGUAGE_DISPLAY_MAP.keys()) |
| 51 | + |
| 52 | +# Set the Ace editor theme based on the base theme |
| 53 | +theme = st_theme() |
| 54 | +base_theme = ( |
| 55 | + theme.get("base") |
| 56 | + if theme and isinstance(theme, dict) and "base" in theme |
| 57 | + else "dark" |
| 58 | +) |
| 59 | +ace_theme = "twilight" if base_theme == "dark" else "chrome" |
| 60 | + |
| 61 | +# Initialize session state for the result if not already initialized |
| 62 | +if "result" not in st.session_state: |
| 63 | + st.session_state["result"] = "Results will appear here." |
| 64 | + |
| 65 | +# Default Python code snippet |
| 66 | +default_python_code = """import os |
| 67 | +
|
| 68 | +def read_file(filepath): |
| 69 | + return open(filepath).read() |
| 70 | +
|
| 71 | +def login(user, pwd): |
| 72 | + if user == "admin" and pwd == "password123": |
| 73 | + print("Welcome!") |
| 74 | + else: |
| 75 | + print("Access denied!") |
| 76 | +
|
| 77 | +filepath = input("File path: ") |
| 78 | +print(read_file(filepath)) |
| 79 | +
|
| 80 | +user = input("Username: ") |
| 81 | +pwd = input("Password: ") |
| 82 | +login(user, pwd) |
| 83 | +""" |
| 84 | + |
| 85 | +# Define layout columns for input and results |
| 86 | +input_column, result_column = st.columns(2) |
| 87 | + |
| 88 | +with input_column: |
| 89 | + st.header("📝 Input Code") |
| 90 | + |
| 91 | + display_language = st.selectbox( |
| 92 | + "Choose Language:", |
| 93 | + available_languages, |
| 94 | + index=available_languages.index("Python"), |
| 95 | + ) |
| 96 | + ace_language = LANGUAGE_DISPLAY_MAP[display_language] |
| 97 | + |
| 98 | + # Code editor with syntax highlighting using st_ace |
| 99 | + code_input = st_ace( |
| 100 | + value=default_python_code if display_language == "Python" else "", |
| 101 | + placeholder="Enter your code here...", |
| 102 | + language=ace_language, |
| 103 | + theme=ace_theme, |
| 104 | + keybinding="vscode", |
| 105 | + font_size=14, |
| 106 | + tab_size=4, |
| 107 | + show_gutter=True, |
| 108 | + wrap=False, |
| 109 | + auto_update=True, |
| 110 | + min_lines=20, |
| 111 | + key="ace_editor", |
| 112 | + ) |
| 113 | + |
| 114 | + evaluate = st.button("Evaluate") |
| 115 | + |
| 116 | +st.markdown( |
| 117 | + """ |
| 118 | + <style> |
| 119 | + .scrollable-container { |
| 120 | + max-height: 100vh; |
| 121 | + overflow-y: auto; |
| 122 | + padding: 1em; |
| 123 | + } |
| 124 | + </style> |
| 125 | + """, |
| 126 | + unsafe_allow_html=True, |
| 127 | +) |
| 128 | + |
| 129 | +with result_column: |
| 130 | + st.header("🔍 Analysis") |
| 131 | + |
| 132 | + if evaluate: |
| 133 | + if code_input.strip() == "": |
| 134 | + st.warning("Please enter some code before evaluating.") |
| 135 | + else: |
| 136 | + with st.spinner("Analyzing code..."): |
| 137 | + try: |
| 138 | + code_with_language = f"Language: {display_language}\n{code_input}" |
| 139 | + result = ai_client.scan_code(code_with_language) |
| 140 | + st.session_state["result"] = result |
| 141 | + except Exception as e: |
| 142 | + st.session_state["result"] = f"An error occurred: {e}" |
| 143 | + |
| 144 | + st.markdown( |
| 145 | + f'<div class="scrollable-container">{st.session_state["result"]}</div>', |
| 146 | + unsafe_allow_html=True, |
| 147 | + ) |
0 commit comments