File size: 4,719 Bytes
0f6a0eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
EDEN OS — Entry Point
Boots the full operating system and serves:
- EDEN Studio frontend at /
- EDEN OS API at /api/v1/
- WebSocket stream at /api/v1/sessions/{id}/stream

Deploy to HuggingFace Spaces as Docker SDK.
Space ID: AIBRUH/eden-os
"""

import os
import sys
import time
from pathlib import Path

import yaml
import uvicorn
from loguru import logger

# Configure loguru
logger.remove()
logger.add(
    sys.stderr,
    format="<green>{time:HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan> — <level>{message}</level>",
    level=os.getenv("EDEN_LOG_LEVEL", "INFO"),
)


def detect_hardware() -> dict:
    """Auto-detect GPU hardware and select profile."""
    try:
        import torch
        if torch.cuda.is_available():
            gpu_name = torch.cuda.get_device_name(0)
            vram_bytes = torch.cuda.get_device_properties(0).total_mem
            vram_gb = vram_bytes / (1024 ** 3)

            if "H100" in gpu_name or "A100" in gpu_name:
                profile = "h100_cinematic"
            elif "4090" in gpu_name:
                profile = "rtx4090_production"
            elif "3090" in gpu_name or "3080" in gpu_name:
                profile = "rtx3090_standard"
            elif "L4" in gpu_name or "T4" in gpu_name:
                profile = "l4_cloud"
            else:
                profile = "l4_cloud"  # default GPU profile

            return {
                "gpu": gpu_name,
                "vram_gb": round(vram_gb, 1),
                "gpu_available": True,
                "profile": profile,
            }
    except Exception:
        pass

    return {
        "gpu": "none",
        "vram_gb": 0,
        "gpu_available": False,
        "profile": "cpu_edge",
    }


def load_config(hardware_profile: str = "auto", models_cache: str = "models_cache") -> dict:
    """Load and merge configuration."""
    config_dir = Path(__file__).parent / "config"

    # Load default config
    default_path = config_dir / "default.yaml"
    if default_path.exists():
        with open(default_path) as f:
            config = yaml.safe_load(f)
    else:
        config = {}

    # Load hardware profile
    if hardware_profile != "auto":
        profile_path = config_dir / "hardware_profiles" / f"{hardware_profile}.yaml"
        if profile_path.exists():
            with open(profile_path) as f:
                profile = yaml.safe_load(f)
            # Merge profile into config (profile overrides defaults)
            _deep_merge(config, profile)

    config["models_cache"] = models_cache
    config["hardware_profile"] = hardware_profile
    return config


def _deep_merge(base: dict, override: dict) -> dict:
    """Deep merge override into base."""
    for key, value in override.items():
        if key in base and isinstance(base[key], dict) and isinstance(value, dict):
            _deep_merge(base[key], value)
        else:
            base[key] = value
    return base


def boot():
    """Boot EDEN OS."""
    start_time = time.monotonic()

    logger.info("=" * 55)
    logger.info(" EDEN OS v1.0 — BOOTING")
    logger.info("=" * 55)

    # Step 1: Detect hardware
    hw = detect_hardware()
    hardware_profile = os.getenv("EDEN_HARDWARE_PROFILE", "auto")
    if hardware_profile == "auto":
        hardware_profile = hw["profile"]
    logger.info(f"Hardware: {hw['gpu']} — Profile: {hardware_profile}")

    # Step 2: Load configuration
    models_cache = os.getenv("EDEN_MODELS_CACHE", "models_cache")
    config = load_config(hardware_profile, models_cache)

    # Step 3: Create FastAPI app
    from eden_os.gateway import create_app
    host = os.getenv("EDEN_HOST", "0.0.0.0")
    port = int(os.getenv("EDEN_PORT", "7860"))

    app = create_app(
        host=host,
        port=port,
        hardware_profile=hardware_profile,
        models_cache=models_cache,
    )

    # Store config and hardware info on app state
    app.state.config = config
    app.state.hardware = hw

    boot_time = time.monotonic() - start_time

    logger.info("=" * 55)
    logger.info(f" EDEN OS v1.0 — LIVE")
    logger.info(f" URL: http://{host}:{port}")
    logger.info(f" API: http://{host}:{port}/api/v1/docs")
    logger.info(f" Hardware: {hw['gpu']} — Profile: {hardware_profile}")
    logger.info(f" Eden Protocol: ACTIVE — Threshold: 0.3")
    logger.info(f" Boot time: {boot_time:.1f}s")
    logger.info(f" OWN THE SCIENCE.")
    logger.info("=" * 55)

    return app, host, port


# Create app at module level for uvicorn import
app, _host, _port = boot()

if __name__ == "__main__":
    uvicorn.run(
        app,
        host=_host,
        port=_port,
        ws_max_size=16 * 1024 * 1024,
        log_level="info",
    )