import streamlit as st import asyncio import re import os from llama_cpp import Llama import requests from bs4 import BeautifulSoup # Set page configuration st.set_page_config(page_title="Security Assistant", page_icon="π", layout="wide") # Custom CSS for styling st.markdown( """ """, unsafe_allow_html=True ) # Cache the model loading @st.cache_resource def load_model(): # Model path consistent across environments model_path = os.path.join("models", "pentest_ai.Q4_0.gguf") if not os.path.exists(model_path): st.error(f"Model file not found at {model_path}. Please ensure itβs placed correctly.") return None try: model = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False) return model except Exception as e: st.error(f"Failed to load model: {e}") return None # Execute tools asynchronously async def run_tool(command: str) -> str: try: process = await asyncio.create_subprocess_shell( command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) stdout, stderr = await process.communicate() return stdout.decode() if stdout else stderr.decode() except Exception as e: return f"Error executing tool: {str(e)}" # Fetch vulnerability info via web scraping (no API keys) def get_vulnerability_info(query: str) -> str: try: url = f"https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={query}" response = requests.get(url, timeout=10) soup = BeautifulSoup(response.text, "html.parser") results = soup.find_all("tr")[1:6] # Top 5 results vulns = [f"{row.find_all('td')[0].text}: {row.find_all('td')[1].text}" for row in results] return "\n".join(vulns) if vulns else "No vulnerabilities found." except Exception as e: return f"Error fetching vulnerability data: {str(e)}" # Session state management if "messages" not in st.session_state: st.session_state.messages = [] # Add message to chat history def add_message(content: str, is_user: bool): st.session_state.messages.append({"content": content, "is_user": is_user}) # Render chat history def render_chat(): for msg in st.session_state.messages: bubble_class = "user-message" if msg["is_user"] else "assistant-message" st.markdown(f'