File size: 11,121 Bytes
331705d
 
b7a9d6e
50d8f85
331705d
 
b7a9d6e
331705d
 
 
be248ec
331705d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a9d6e
331705d
 
50d8f85
331705d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50d8f85
be248ec
331705d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a9d6e
 
331705d
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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)
# ---------------------------------------------------------------------------
@spaces.GPU(duration=120)
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()