File size: 3,130 Bytes
8015fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
from pathlib import Path

import gradio as gr
import torch
from model import KernelMindPolicy
from runtime import ModelMachine
from safetensors.torch import load_file
from schema import ACTIONS, INTENTS, TARGETS, encode_scenario

ARTIFACT_DIR = Path(__file__).resolve().parent / "artifacts" / "kernelmind-ai-os"
MODEL = KernelMindPolicy()
MODEL.load_state_dict(load_file(ARTIFACT_DIR / "policy.safetensors"))
MODEL.eval()


@torch.inference_mode()
def run_model_machine(
    intent: str,
    target: str,
    administrator: bool,
    network: bool,
    confirmed: bool,
    exists: bool,
    service_running: bool,
) -> tuple[dict, dict]:
    scenario = {
        "intent": intent,
        "target": target,
        "administrator": administrator,
        "network": network,
        "confirmed": confirmed,
        "exists": exists,
        "service_running": service_running,
    }
    tokens = torch.tensor([encode_scenario(scenario)])
    logits = MODEL(tokens)[0]
    indexes = logits.argmax(1).tolist()
    plan = [ACTIONS[index] for index in indexes if ACTIONS[index] != "PAD"]
    confidence = [
        float(torch.softmax(slot, dim=0).max())
        for slot, index in zip(logits, indexes, strict=True)
        if ACTIONS[index] != "PAD"
    ]
    machine = ModelMachine(scenario)
    before = json.loads(json.dumps(machine.state))
    after = machine.execute(plan)
    decision = {
        "learned_plan": plan,
        "action_confidence": confidence,
        "capability_gate": "independent deterministic runtime enforcement",
    }
    execution = {"before": before, "after": after}
    return decision, execution


with gr.Blocks(title="KernelMind AI OS") as demo:
    gr.Markdown(
        "# KernelMind AI OS + Model Machine\n"
        "A trained policy kernel converts intent and machine state into a structured "
        "plan. A separate capability gate executes it only inside a virtual computer."
    )
    with gr.Row():
        intent = gr.Dropdown(INTENTS, value="backup", label="Intent")
        target = gr.Dropdown(TARGETS, value="notes", label="Target")
    with gr.Row():
        administrator = gr.Checkbox(False, label="Administrator capability")
        network = gr.Checkbox(True, label="Network available")
        confirmed = gr.Checkbox(False, label="Destructive action confirmed")
        exists = gr.Checkbox(True, label="Target exists")
        service = gr.Checkbox(True, label="Service running")
    initial = run_model_machine("backup", "notes", False, True, False, True, True)
    with gr.Row():
        decision = gr.JSON(value=initial[0], label="AI OS policy decision")
        execution = gr.JSON(value=initial[1], label="Model Machine transition")
    button = gr.Button("Plan and execute safely", variant="primary")
    button.click(
        run_model_machine,
        inputs=[
            intent,
            target,
            administrator,
            network,
            confirmed,
            exists,
            service,
        ],
        outputs=[decision, execution],
    )


if __name__ == "__main__":
    demo.launch()