Spaces:
Sleeping
Sleeping
Download app.py from bchoudha/C7HackathonGroup4: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/spaces/bchoudha/C7HackathonGroup4/resolve/main/app.py
- Command line
-
hf download hf://spaces/bchoudha/C7HackathonGroup4/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bchoudha/C7HackathonGroup4/resolve/main/app.py
12.2 kB
| import gradio as gr | |
| from cybersecurityAgents import ( | |
| run_analysis, | |
| EXAMPLE_INPUTS, | |
| build_llm, | |
| build_embeddings, | |
| build_cybersec_graph, | |
| VectorKnowledgeBase, | |
| CyberAgentState, | |
| Config, | |
| check_vector_store_exists, | |
| ) | |
| from datetime import datetime | |
| import json | |
| import time | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv("cs.env") | |
| # ββ Security Configuration βββββββββββββββββββββββββββββββββββββββββββββ | |
| GRADIO_USER = os.getenv("GRADIO_USER", "admin") | |
| GRADIO_PASSWORD = os.getenv("GRADIO_PASSWORD", "CyberSec@2026!") | |
| MAX_INPUT_LENGTH = 10000 | |
| REQUEST_COOLDOWN_SECONDS = 30 | |
| MAX_CONCURRENT_THREADS = 2 | |
| _last_request_time: float = 0.0 | |
| # ββ Singleton pipeline βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| _graph = None | |
| _kb = None | |
| _init_status = {} | |
| def get_pipeline(): | |
| global _graph, _kb, _init_status | |
| if _graph is None: | |
| store_exists = check_vector_store_exists() | |
| _init_status["vector_store_preexisted"] = store_exists | |
| llm = build_llm() | |
| _init_status["llm"] = True | |
| embeddings = build_embeddings() | |
| _init_status["embeddings"] = True | |
| _kb = VectorKnowledgeBase(embeddings) | |
| _init_status["vector_store"] = True | |
| _init_status["vector_store_loaded_existing"] = _kb.loaded_existing | |
| _graph = build_cybersec_graph(llm, _kb) | |
| _init_status["graph"] = True | |
| return _graph | |
| def get_status_markdown(): | |
| if not _init_status: | |
| return ( | |
| "### β³ Not Initialized\n\n" | |
| "Click **Run Analysis** to initialize.\n\n" | |
| "---\n" | |
| "#### Components\n" | |
| "- β¬ OpenRouter LLM\n" | |
| "- β¬ HuggingFace Embeddings\n" | |
| "- β¬ ChromaDB Vector Store\n" | |
| "- β¬ LangGraph Pipeline" | |
| ) | |
| lines = ["### π’ Initialized\n"] | |
| checks = [ | |
| ("llm", "OpenRouter LLM"), | |
| ("embeddings", "HuggingFace Embeddings"), | |
| ("vector_store", "ChromaDB Vector Store"), | |
| ("graph", "LangGraph Pipeline"), | |
| ] | |
| for key, label in checks: | |
| if _init_status.get(key): | |
| lines.append(f"- β {label}") | |
| else: | |
| lines.append(f"- β¬ {label}") | |
| lines.append("\n---") | |
| if _init_status.get("vector_store_loaded_existing"): | |
| lines.append("π **Loaded from disk**\n(no re-indexing)") | |
| elif _init_status.get("vector_store"): | |
| lines.append("π **Freshly indexed**\n(knowledge base built)") | |
| lines.append(f"\n---\n**Model:** `{Config.LLM_MODEL}`") | |
| lines.append(f"\n**Embeddings:** `{Config.EMBEDDING_MODEL.split('/')[-1]}`") | |
| lines.append(f"\n**Vector DB:** `{Config.CHROMA_PERSIST_DIR}`") | |
| return "\n".join(lines) | |
| LANGGRAPH_PIPELINE_MD = """### π LangGraph Pipeline | |
| ``` | |
| START | |
| β | |
| βΌ | |
| π·οΈ classify_input | |
| β | |
| βΌ | |
| π rag_retrieve | |
| β | |
| βΌ | |
| π‘ log_monitor | |
| β | |
| βΌ | |
| π΅οΈ threat_intel | |
| β | |
| βΌ | |
| π vuln_scanner | |
| β | |
| βΌ | |
| π§ code_fixer ββββ | |
| β (loop max 3x) | |
| β fixed? β | |
| βββ No βββββββββββ | |
| βΌ Yes | |
| π¨ incident_response | |
| β | |
| βΌ | |
| π policy_checker | |
| β | |
| βΌ | |
| π synthesize_report | |
| β | |
| βΌ | |
| END | |
| ``` | |
| --- | |
| ### π€ Agents | |
| | # | Agent | Role | | |
| |---|-------|------| | |
| | 1 | Log Monitor | Detect attacks in logs | | |
| | 2 | Threat Intel | CVE & MITRE mapping | | |
| | 3 | Vuln Scanner | OWASP/CWE scan | | |
| | 4 | Code Fixer | Iterative bug fix | | |
| | 5 | Incident Response | NIST 800-61 plan | | |
| | 6 | Policy Checker | ISO/NIST/SOC2 gaps | | |
| """ | |
| SCENARIO_MAP = { | |
| "Log Monitor Agent": "ssh_brute_force", | |
| "Threat Intelligence Agent": "ssh_brute_force", | |
| "Vulnerability Scanner Agent": "vulnerable_code", | |
| "Code Fixer Agent": "vulnerable_code", | |
| "Incident Response Agent": "docker_misconfig", | |
| "Policy Checker Agent": "policy_audit", | |
| } | |
| SCENARIO_DESCRIPTIONS = { | |
| "custom": "> **Paste your own logs, code, config, or policy text below.**", | |
| "Log Monitor Agent": "> **Reads system and network logs to detect unusual activity or attacks.**", | |
| "Threat Intelligence Agent": "> **Looks up known security threats (CVE data, reports) and checks if your system is affected.**", | |
| "Vulnerability Scanner Agent": "> **Scans code, APIs, or Docker images to find security weaknesses.**", | |
| "Code Fixer Agent": "> **Iteratively fixes security vulnerabilities and bugs in your code (up to 3 passes).**", | |
| "Incident Response Agent": "> **Creates step-by-step action plans when an issue is found.**", | |
| "Policy Checker Agent": "> **Checks your setup against standards like ISO, NIST, or SOC2 and shows where you need fixes.**", | |
| } | |
| def authenticate(username, password): | |
| """Validate login credentials against cs.env values.""" | |
| return username == GRADIO_USER and password == GRADIO_PASSWORD | |
| def analyze(text, scenario): | |
| global _last_request_time | |
| # Rate limiting: enforce cooldown between requests | |
| now = time.time() | |
| elapsed_since_last = now - _last_request_time | |
| if elapsed_since_last < REQUEST_COOLDOWN_SECONDS and _last_request_time > 0: | |
| wait = int(REQUEST_COOLDOWN_SECONDS - elapsed_since_last) | |
| return f"β³ Rate limited. Please wait {wait}s before next request.", {}, {}, {}, {}, {}, "", get_status_markdown(), "" | |
| _last_request_time = now | |
| # Input validation: enforce max length | |
| input_text = text | |
| if not input_text or not input_text.strip(): | |
| return "β οΈ Please provide input or select a scenario.", {}, {}, {}, {}, {}, "", get_status_markdown(), "" | |
| if len(input_text) > MAX_INPUT_LENGTH: | |
| return f"β Input too long ({len(input_text)} chars). Maximum is {MAX_INPUT_LENGTH} characters.", {}, {}, {}, {}, {}, "", get_status_markdown(), "" | |
| try: | |
| app = get_pipeline() | |
| except Exception as e: | |
| return f"β Pipeline initialization failed: {e}", {}, {}, {}, {}, {}, "", get_status_markdown(), "" | |
| initial_state: CyberAgentState = { | |
| "raw_input": input_text, | |
| "input_type": "general", | |
| "log_findings": None, | |
| "threat_intel": None, | |
| "vuln_scan": None, | |
| "code_fix": None, | |
| "incident_plan": None, | |
| "policy_gaps": None, | |
| "agent_trace": [], | |
| "errors": [], | |
| "rag_context": None, | |
| "final_report": None, | |
| "started_at": datetime.now().isoformat(), | |
| } | |
| t0 = time.time() | |
| state = app.invoke(initial_state) | |
| elapsed = time.time() - t0 | |
| report = state.get("final_report", "No report generated.") | |
| report += f"\n\nβ± Analysis completed in {elapsed:.1f}s" | |
| # Extract fixed code for display | |
| code_fix = state.get("code_fix") or {} | |
| fixed_code = code_fix.get("fixed_code", "") | |
| if not fixed_code or code_fix.get("skipped"): | |
| fixed_code = "# No code fix generated (input was not code or code fixer was skipped)" | |
| return ( | |
| report, | |
| state.get("log_findings") or {}, | |
| state.get("threat_intel") or {}, | |
| state.get("vuln_scan") or {}, | |
| code_fix, | |
| state.get("policy_gaps") or {}, | |
| "\n".join(state.get("agent_trace", [])), | |
| get_status_markdown(), | |
| fixed_code, | |
| ) | |
| def load_scenario(scenario): | |
| if scenario == "custom": | |
| return "", SCENARIO_DESCRIPTIONS["custom"] | |
| key = SCENARIO_MAP.get(scenario, "ssh_brute_force") | |
| return EXAMPLE_INPUTS.get(key, ""), SCENARIO_DESCRIPTIONS.get(scenario, "") | |
| def check_init_status(): | |
| store_exists = check_vector_store_exists() | |
| if store_exists: | |
| return get_status_markdown() if _init_status else ( | |
| "### π’ Ready\n\n" | |
| "- β ChromaDB vector store found on disk\n\n" | |
| "---\n" | |
| "Pipeline will load existing data on first run.\n\n" | |
| f"**Path:** `{Config.CHROMA_PERSIST_DIR}`" | |
| ) | |
| else: | |
| return ( | |
| "### π‘ First Run\n\n" | |
| "- β¬ No vector store found\n\n" | |
| "---\n" | |
| "Knowledge base will be indexed on first analysis run." | |
| ) | |
| # ββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| with gr.Blocks(title="CyberSec Multi-Agent Analyzer", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π‘οΈ CyberSecurity Multi-Agent Analyzer") | |
| with gr.Row(): | |
| # ββ LEFT: LangGraph Pipeline βββββββββββββββββββββββββββββββββββ | |
| with gr.Column(scale=1, min_width=250): | |
| gr.Markdown(LANGGRAPH_PIPELINE_MD) | |
| # ββ CENTER: Main Playground ββββββββββββββββββββββββββββββββββββ | |
| with gr.Column(scale=3): | |
| scenario = gr.Dropdown( | |
| choices=[ | |
| "custom", | |
| "Log Monitor Agent", | |
| "Threat Intelligence Agent", | |
| "Vulnerability Scanner Agent", | |
| "Code Fixer Agent", | |
| "Incident Response Agent", | |
| "Policy Checker Agent", | |
| ], | |
| value="Log Monitor Agent", | |
| label="Select Agent Scenario", | |
| ) | |
| scenario_desc = gr.Markdown(value=SCENARIO_DESCRIPTIONS["Log Monitor Agent"]) | |
| input_box = gr.Textbox( | |
| lines=8, | |
| label="Input (logs, code, config, or policy)", | |
| placeholder="Paste your input here or select a scenario above...", | |
| value=EXAMPLE_INPUTS["ssh_brute_force"], | |
| ) | |
| with gr.Row(): | |
| run_btn = gr.Button("π Run Analysis", variant="primary", scale=2) | |
| status_btn = gr.Button("π Check Status", scale=1) | |
| with gr.Tabs(): | |
| with gr.Tab("π Report"): | |
| report_out = gr.Markdown() | |
| with gr.Tab("π Log Findings"): | |
| log_out = gr.JSON() | |
| with gr.Tab("π΅οΈ Threat Intel"): | |
| threat_out = gr.JSON() | |
| with gr.Tab("β οΈ Vulnerabilities"): | |
| vuln_out = gr.JSON() | |
| with gr.Tab("π§ Code Fix"): | |
| codefix_out = gr.JSON() | |
| with gr.Tab("π Compliance"): | |
| policy_out = gr.JSON() | |
| with gr.Tab("π Trace"): | |
| trace_out = gr.Textbox(lines=12, label="Agent Execution Trace") | |
| # ββ RIGHT: Storage & Initialization ββββββββββββββββββββββββββββ | |
| with gr.Column(scale=1, min_width=250): | |
| gr.Markdown("### π¦ Storage & Init") | |
| status_out = gr.Markdown(value=check_init_status()) | |
| gr.Markdown("---") | |
| gr.Markdown("### π§ Fixed Code Output") | |
| fixed_code_out = gr.Code( | |
| label="Fixed Code (read-only)", | |
| language="python", | |
| interactive=False, | |
| lines=15, | |
| ) | |
| # ββ Event Handlers βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| scenario.change(fn=load_scenario, inputs=[scenario], outputs=[input_box, scenario_desc]) | |
| run_btn.click( | |
| fn=analyze, | |
| inputs=[input_box, scenario], | |
| outputs=[report_out, log_out, threat_out, vuln_out, codefix_out, policy_out, trace_out, status_out, fixed_code_out], | |
| ) | |
| status_btn.click(fn=check_init_status, inputs=[], outputs=[status_out]) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| auth=authenticate, | |
| auth_message="π CyberSecurity Multi-Agent System β Login Required", | |
| server_name="0.0.0.0", | |
| max_threads=MAX_CONCURRENT_THREADS, | |
| share=False, | |
| ssr_mode=False, # Disabling SSR is recommended for multi-agent streaming UIs on HF | |
| ) | |