|
|
import os |
|
|
import sys |
|
|
import threading |
|
|
import time |
|
|
import uvicorn |
|
|
from fastapi import FastAPI |
|
|
import os |
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
import huggingface_hub |
|
|
if not hasattr(huggingface_hub, "HfFolder"): |
|
|
print("[NACC] Patching huggingface_hub.HfFolder for Gradio compatibility...") |
|
|
class HfFolder: |
|
|
@classmethod |
|
|
def save_token(cls, token): |
|
|
pass |
|
|
@classmethod |
|
|
def get_token(cls): |
|
|
return os.getenv("HF_TOKEN") |
|
|
huggingface_hub.HfFolder = HfFolder |
|
|
except ImportError: |
|
|
pass |
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "src")) |
|
|
|
|
|
from nacc_orchestrator.server import build_service, create_app |
|
|
from nacc_orchestrator.config import NodeDefinition |
|
|
from nacc_ui.professional_ui_v2 import create_professional_ui_v2 as create_ui |
|
|
from nacc_node.tools import NodeServer |
|
|
from nacc_node.config import NodeConfig |
|
|
|
|
|
|
|
|
service = build_service() |
|
|
|
|
|
|
|
|
local_node_def = NodeDefinition( |
|
|
node_id="hf-space-local", |
|
|
transport="http", |
|
|
base_url="http://127.0.0.1:8001", |
|
|
display_name="Local Node (Demo)", |
|
|
tags=["local", "demo", "linux"], |
|
|
priority=10 |
|
|
) |
|
|
service.registry.add_node(local_node_def) |
|
|
print(f"β
Auto-registered local node: {local_node_def.node_id}") |
|
|
|
|
|
|
|
|
orchestrator_app = create_app(service) |
|
|
|
|
|
|
|
|
|
|
|
api_app = FastAPI() |
|
|
api_app.mount("/api", orchestrator_app) |
|
|
|
|
|
def start_local_node(): |
|
|
"""Start a local node in the background for the HF Space demo.""" |
|
|
print("π Starting local NACC Node for HF Space...") |
|
|
try: |
|
|
|
|
|
config = NodeConfig( |
|
|
node_id="hf-space-local", |
|
|
root_dir=Path.cwd(), |
|
|
display_name="Local Node (Demo)", |
|
|
tags=["local", "demo"], |
|
|
allowed_commands=[ |
|
|
"/bin/sh", "/bin/bash", |
|
|
"ls", "cat", "echo", "grep", "head", "tail", "pwd", "whoami", |
|
|
"mkdir", "touch", "rm", "cp", "mv", |
|
|
"python", "python3", "pip", "pip3", |
|
|
"curl", "wget", "git", |
|
|
"df", "du", "free", "top", "ps" |
|
|
], |
|
|
sync_targets={} |
|
|
) |
|
|
|
|
|
|
|
|
server = NodeServer(config, host="127.0.0.1", port=8001) |
|
|
server.serve_forever() |
|
|
except Exception as e: |
|
|
print(f"β Failed to start local node: {e}") |
|
|
|
|
|
def start_orchestrator_api(): |
|
|
"""Start the orchestrator API in the background.""" |
|
|
print("π§ Starting NACC Orchestrator API...") |
|
|
uvicorn.run(orchestrator_app, host="127.0.0.1", port=8888) |
|
|
|
|
|
|
|
|
|
|
|
if not os.environ.get("NACC_SERVICES_STARTED"): |
|
|
os.environ["NACC_SERVICES_STARTED"] = "1" |
|
|
|
|
|
|
|
|
api_thread = threading.Thread(target=start_orchestrator_api, daemon=True) |
|
|
api_thread.start() |
|
|
|
|
|
|
|
|
node_thread = threading.Thread(target=start_local_node, daemon=True) |
|
|
node_thread.start() |
|
|
|
|
|
|
|
|
time.sleep(3) |
|
|
|
|
|
|
|
|
print("π» Launching NACC UI...") |
|
|
demo = create_ui() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|