Spaces:
Build error
Build error
Download app.py from duckeydevv/Koda-IDS-Frontier: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/spaces/duckeydevv/Koda-IDS-Frontier/resolve/main/app.py
- Command line
-
hf download hf://spaces/duckeydevv/Koda-IDS-Frontier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/duckeydevv/Koda-IDS-Frontier/resolve/main/app.py
11.1 kB
| import os | |
| import spaces | |
| import gradio as gr | |
| from llama_cpp import Llama | |
| from huggingface_hub import hf_hub_download | |
| from typing import List, Dict, Any | |
| # --------------------------------------------------------------------------- | |
| # Model configuration | |
| # --------------------------------------------------------------------------- | |
| MODEL_REPO = "netgoat-ai/koda-ids-frontier" | |
| MODEL_FILE = "koda-ids-Q4_K_M.gguf" # ~1.7 GB – practical for ZeroGPU | |
| CONTEXT_SIZE = 8192 | |
| MAX_NEW_TOKENS = 1024 | |
| # Recommended generation defaults from the model card | |
| DEFAULT_TEMPERATURE = 0.1 | |
| DEFAULT_TOP_K = 50 | |
| DEFAULT_TOP_P = 0.7 | |
| DEFAULT_REPEAT_PENALTY = 1.1 | |
| # System prompt tailored to Koda IDS Frontier philosophy | |
| SYSTEM_PROMPT = """You are Koda IDS Frontier, a lightweight open cybersecurity reasoning model specialized in defensive security analysis. | |
| Your principles: | |
| - Evidence first: only claim what is supported by the provided evidence. | |
| - Explicit uncertainty: when evidence is insufficient, clearly state what is unknown. | |
| - Multiple hypotheses: never treat every anomaly as malicious; consider legitimate causes (deployments, backups, monitoring, config changes, etc.). | |
| - Tool honesty: never claim to have executed commands, queried systems, or obtained results unless they were explicitly provided. | |
| - Proportionate response: recommend defensive actions that match the available evidence and severity. | |
| - Distinguishing observed / inferred / hypothesized / unknown is mandatory. | |
| Structure your analysis when appropriate: | |
| 1. What is directly observed? | |
| 2. What explanations are plausible? | |
| 3. What evidence is missing? | |
| 4. What should be investigated next? | |
| 5. Severity and confidence justified by the evidence. | |
| You are for defensive use only: incident triage, telemetry interpretation, investigation support, and security research.""" | |
| # --------------------------------------------------------------------------- | |
| # Model loading (lazy, once) | |
| # --------------------------------------------------------------------------- | |
| llm: Llama | None = None | |
| def load_model() -> Llama: | |
| global llm | |
| if llm is not None: | |
| return llm | |
| print(f"Downloading {MODEL_FILE} from {MODEL_REPO} …") | |
| model_path = hf_hub_download( | |
| repo_id=MODEL_REPO, | |
| filename=MODEL_FILE, | |
| resume_download=True, | |
| ) | |
| print(f"Loading model from {model_path} …") | |
| # ZeroGPU provides CUDA; offload as many layers as possible. | |
| # Falls back gracefully if CUDA is unavailable. | |
| llm = Llama( | |
| model_path=model_path, | |
| n_ctx=CONTEXT_SIZE, | |
| n_gpu_layers=-1, # full offload when GPU is present | |
| n_threads=os.cpu_count() or 4, | |
| verbose=False, | |
| chat_format="chatml", # LFM2 / ChatML-like template | |
| ) | |
| print("Model ready.") | |
| return llm | |
| # --------------------------------------------------------------------------- | |
| # Chat generation (GPU-decorated for ZeroGPU) | |
| # --------------------------------------------------------------------------- | |
| def generate( | |
| message: str, | |
| history: List[Dict[str, str]], | |
| system_prompt: str, | |
| temperature: float, | |
| top_p: float, | |
| top_k: int, | |
| max_tokens: int, | |
| repeat_penalty: float, | |
| ) -> str: | |
| model = load_model() | |
| messages = [{"role": "system", "content": system_prompt.strip() or SYSTEM_PROMPT}] | |
| for turn in history: | |
| if turn.get("role") in ("user", "assistant") and turn.get("content"): | |
| messages.append({"role": turn["role"], "content": turn["content"]}) | |
| messages.append({"role": "user", "content": message}) | |
| response = model.create_chat_completion( | |
| messages=messages, | |
| temperature=temperature, | |
| top_p=top_p, | |
| top_k=top_k, | |
| max_tokens=max_tokens, | |
| repeat_penalty=repeat_penalty, | |
| stream=False, | |
| ) | |
| return response["choices"][0]["message"]["content"].strip() | |
| def chat_fn( | |
| message: str, | |
| history: List[Dict[str, str]], | |
| system_prompt: str, | |
| temperature: float, | |
| top_p: float, | |
| top_k: int, | |
| max_tokens: int, | |
| repeat_penalty: float, | |
| ): | |
| if not message.strip(): | |
| yield history | |
| return | |
| history = history + [{"role": "user", "content": message}] | |
| yield history # show user message immediately | |
| try: | |
| reply = generate( | |
| message, | |
| history[:-1], # history without the just-added user turn | |
| system_prompt, | |
| temperature, | |
| top_p, | |
| top_k, | |
| max_tokens, | |
| repeat_penalty, | |
| ) | |
| except Exception as e: | |
| reply = f"⚠️ Generation error: {type(e).__name__}: {e}" | |
| history = history + [{"role": "assistant", "content": reply}] | |
| yield history | |
| # --------------------------------------------------------------------------- | |
| # Gradio UI – clean cybersecurity theme | |
| # --------------------------------------------------------------------------- | |
| CUSTOM_CSS = """ | |
| .gradio-container { | |
| font-family: 'Inter', 'Segoe UI', system-ui, sans-serif !important; | |
| } | |
| .main-header { | |
| text-align: center; | |
| padding: 1.5rem 1rem; | |
| background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%); | |
| border-radius: 16px; | |
| margin-bottom: 1.25rem; | |
| border: 1px solid #334155; | |
| } | |
| .main-header h1 { | |
| color: #38bdf8; | |
| margin: 0 0 0.35rem 0; | |
| font-size: 1.85rem; | |
| font-weight: 700; | |
| letter-spacing: -0.02em; | |
| } | |
| .main-header p { | |
| color: #94a3b8; | |
| margin: 0; | |
| font-size: 0.95rem; | |
| } | |
| .chatbot { | |
| border-radius: 12px !important; | |
| border: 1px solid #334155 !important; | |
| } | |
| footer { display: none !important; } | |
| """ | |
| EXAMPLE_PROMPTS = [ | |
| [ | |
| "Triage this alert:\n\n" | |
| "Tenant: api.example.com (SaaS)\n" | |
| "Baseline: 180 req/s\n" | |
| "Current: 2 140 req/s (11.9×), 94 unique source IPs, 78 % 4xx errors\n" | |
| "Vector claim: NTP amplification reflected DDoS\n" | |
| "Signal: high volume UDP/123 traffic to our DNS resolvers\n\n" | |
| "What is observed, what is hypothesized, and what should we investigate next?" | |
| ], | |
| [ | |
| "Analyze the following authentication anomaly:\n\n" | |
| "User: admin@corp.internal\n" | |
| "Event: 47 failed logins in 6 minutes from 12 distinct ASNs, followed by 1 successful login from a residential ISP in a country the user has never visited.\n" | |
| "MFA: not enrolled for this account\n" | |
| "Context: the account was recently provisioned for a contractor.\n\n" | |
| "Give severity, confidence, plausible explanations, and recommended next steps." | |
| ], | |
| [ | |
| "DNS spike detected:\n\n" | |
| "- 9.2× increase in outbound DNS queries over 12 minutes\n" | |
| "- Top queried domains: internal package mirror + known CDN hostnames\n" | |
| "- Timing coincides with a scheduled Kubernetes rolling update\n" | |
| "- No corresponding increase in HTTP 5xx or unusual process activity on nodes\n\n" | |
| "Is this malicious? What evidence is still missing?" | |
| ], | |
| [ | |
| "Process telemetry on a Windows endpoint:\n\n" | |
| "Parent: explorer.exe\n" | |
| "Child: powershell.exe -enc <long base64>\n" | |
| "Network: outbound connection to a newly registered domain on port 443\n" | |
| "User: interactive domain user, normal working hours\n" | |
| "EDR: no known-bad hash match yet\n\n" | |
| "Walk through the investigation priorities." | |
| ], | |
| ] | |
| with gr.Blocks( | |
| title="Koda IDS Frontier – Cybersecurity Reasoning", | |
| theme=gr.themes.Soft( | |
| primary_hue="sky", | |
| secondary_hue="slate", | |
| neutral_hue="slate", | |
| radius_size="lg", | |
| font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"], | |
| ), | |
| css=CUSTOM_CSS, | |
| ) as demo: | |
| gr.HTML( | |
| """ | |
| <div class="main-header"> | |
| <h1>🛡️ Koda IDS Frontier</h1> | |
| <p>Lightweight defensive cybersecurity reasoning · based on Liquid AI LFM2.5-2.6B · NetGoat OSS</p> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| chatbot = gr.Chatbot( | |
| type="messages", | |
| height=560, | |
| show_copy_button=True, | |
| avatar_images=(None, "🛡️"), | |
| bubble_full_width=False, | |
| render_markdown=True, | |
| label="Conversation", | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| placeholder="Describe a security event, alert, or telemetry… (Enter to send)", | |
| scale=9, | |
| lines=2, | |
| max_lines=8, | |
| show_label=False, | |
| container=False, | |
| ) | |
| submit_btn = gr.Button("Analyze", variant="primary", scale=1) | |
| with gr.Accordion("⚙️ Generation settings", open=False): | |
| system_prompt = gr.Textbox( | |
| label="System prompt", | |
| value=SYSTEM_PROMPT, | |
| lines=8, | |
| max_lines=16, | |
| ) | |
| with gr.Row(): | |
| temperature = gr.Slider(0.0, 1.5, value=DEFAULT_TEMPERATURE, step=0.05, label="Temperature") | |
| top_p = gr.Slider(0.1, 1.0, value=DEFAULT_TOP_P, step=0.05, label="Top-p") | |
| with gr.Row(): | |
| top_k = gr.Slider(1, 100, value=DEFAULT_TOP_K, step=1, label="Top-k") | |
| max_tokens = gr.Slider(128, 2048, value=MAX_NEW_TOKENS, step=64, label="Max new tokens") | |
| repeat_penalty = gr.Slider(1.0, 1.5, value=DEFAULT_REPEAT_PENALTY, step=0.05, label="Repetition penalty") | |
| clear_btn = gr.Button("🗑️ Clear conversation", variant="secondary") | |
| with gr.Column(scale=1, min_width=260): | |
| gr.Markdown("### Example prompts") | |
| gr.Examples( | |
| examples=EXAMPLE_PROMPTS, | |
| inputs=msg, | |
| label=None, | |
| ) | |
| gr.Markdown( | |
| """ | |
| ### Model card | |
| | | | | |
| |---|---| | |
| | **Model** | netgoat-ai/koda-ids-frontier | | |
| | **Base** | Liquid AI LFM2.5-2.6B | | |
| | **Params** | ~2.6 B | | |
| | **Quant** | Q4_K_M GGUF | | |
| | **License** | Apache-2.0 | | |
| | **Focus** | Defensive cyber reasoning | | |
| > Outputs are research / assistive only. | |
| > Always validate against real telemetry and tooling. | |
| """ | |
| ) | |
| # Event wiring | |
| inputs = [msg, chatbot, system_prompt, temperature, top_p, top_k, max_tokens, repeat_penalty] | |
| submit_btn.click( | |
| fn=chat_fn, | |
| inputs=inputs, | |
| outputs=chatbot, | |
| queue=True, | |
| ).then(lambda: "", None, msg) | |
| msg.submit( | |
| fn=chat_fn, | |
| inputs=inputs, | |
| outputs=chatbot, | |
| queue=True, | |
| ).then(lambda: "", None, msg) | |
| clear_btn.click(fn=lambda: [], outputs=chatbot) | |
| # Enable queue so ZeroGPU can schedule GPU work reliably | |
| demo.queue(max_size=16) | |
| if __name__ == "__main__": | |
| demo.launch() | |