File size: 2,900 Bytes
bd4e1e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
import gradio as gr

IS_HF_SPACE = bool(os.getenv("SPACE_ID"))
LOCAL_WORKER_URL = os.getenv("LOCAL_WORKER_URL", "http://127.0.0.1:8085")
API_TOKEN = os.getenv("WORKER_TOKEN", "changeme")


def check_worker_health() -> str:
    try:
        res = requests.get(f"{LOCAL_WORKER_URL}/health", timeout=2)
        if res.status_code == 200:
            return "🟢 Local Worker Online (Ollama + CLI Ready)"
    except Exception:
        pass
    return "🟡 Local Worker Offline (Cloud Fallback Mode)"


def run_cloud_fallback(prompt: str, task_type: str) -> str:
    return f"[Cloud Gateway Fallback] Queued or triaged '{task_type}': {prompt}"


def dispatch_task(prompt: str, task_type: str, target: str):
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    payload = {"prompt": prompt, "task_type": task_type, "target": target}

    try:
        res = requests.post(
            f"{LOCAL_WORKER_URL}/execute", json=payload, headers=headers, timeout=60
        )
        res.raise_for_status()
        data = res.json()
        return data.get("output", "No output returned."), data.get(
            "engine", "Local Worker"
        )
    except Exception as e:
        fallback_msg = run_cloud_fallback(prompt, task_type)
        return f"{fallback_msg}\n\n(Worker unreachable: {e})", "Cloud Gateway"


with gr.Blocks(title="NexusInstruments Hybrid Orchestrator") as demo:
    env_label = "Hugging Face Cloud Gateway" if IS_HF_SPACE else "Local Kali Host"
    gr.Markdown(f"## 🛡️ NexusInstruments Hybrid Orchestrator (`{env_label}`)")

    status_box = gr.Textbox(
        label="Bridge Status", value=check_worker_health, interactive=False
    )
    refresh_btn = gr.Button("Refresh Worker Status", size="sm")
    refresh_btn.click(fn=check_worker_health, outputs=status_box)

    with gr.Row():
        task_type = gr.Dropdown(
            choices=[
                "OSINT Domain Recon",
                "Log Anomaly Analysis (Ollama)",
                "Public Record Lookup",
                "CVE / Threat Intel Triage",
            ],
            value="OSINT Domain Recon",
            label="Pipeline Workflow",
        )
        target_input = gr.Textbox(
            label="Target / Scope (Domain, IP, Log Path)",
            placeholder="example.com",
        )

    prompt_input = gr.Textbox(
        lines=3,
        label="Instructions / Analyst Query",
        placeholder="Run passive recon and summarize attack surface...",
    )
    run_btn = gr.Button("Execute Pipeline", variant="primary")

    engine_used = gr.Textbox(label="Execution Node", interactive=False)
    output_box = gr.Markdown(label="Results")

    run_btn.click(
        fn=dispatch_task,
        inputs=[prompt_input, task_type, target_input],
        outputs=[output_box, engine_used],
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)