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)