Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 4,636 Bytes
95a8a23 12032b1 95a8a23 aade157 95a8a23 63b204d 95a8a23 12032b1 95a8a23 199daad 12032b1 d4523b3 13bbb15 95a8a23 12032b1 95a8a23 aade157 95a8a23 | 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 | #!/usr/bin/env bash
set -euo pipefail
# Fail fast on broken Space metadata, duplicate templates, or stale cache artifacts.
python3 /home/user/app/scripts/preflight.py
# ============================================================
# Required non-model settings
# ============================================================
: "${MARINE_API_URL:?Missing MARINE_API_URL variable}"
# ============================================================
# Model key
#
# We do NOT require or use OPENCODE_API_KEY.
# Prefer the dedicated OpenCode Go secret.
# Fall back to OPENAI_API_KEY only if you chose to store the same
# OpenCode Go key under that name in Hugging Face.
# ============================================================
MODEL_API_KEY="${OPENCODE_GO_API_KEY:-${OPENAI_API_KEY:-}}"
if [ -z "${MODEL_API_KEY}" ]; then
echo "[startup] ERROR: no OpenCode key was injected."
echo "[startup] Expected: OPENCODE_GO_API_KEY"
echo "[startup] Available environment variable names containing KEY/API:"
env | cut -d= -f1 | grep -E 'KEY|API' | sort || true
exit 1
fi
# ============================================================
# Official OpenAI Codex CLI runtime
# ============================================================
export OPENAI_API_KEY="${MODEL_API_KEY}"
# OpenCode is an OpenAI-compatible provider for Codex CLI
export OPENAI_BASE_URL="https://opencode.ai/zen/go/v1"
# OpenCode Go's low-cost Responses model. Override only with another model
# listed by https://opencode.ai/zen/go/v1/models.
export OPENCODE_GO_MODEL="${OPENCODE_GO_MODEL:-gpt-5.6-luna}"
export CODEX_MODEL="${CODEX_MODEL:-${OPENCODE_GO_MODEL}}"
# ============================================================
# Web / Runtime
# ============================================================
export PORT="${PORT:-7860}"
export FISHERIES_EXPORT_ROOT="${FISHERIES_EXPORT_ROOT:-/tmp/squid_fisheries_exports}"
export HF_FISHERIES_CACHE_ROOT="${HF_FISHERIES_CACHE_ROOT:-/tmp/squid_hf_fisheries_cache}"
if [ -z "${PUBLIC_BASE_URL:-}" ] && [ -n "${SPACE_HOST:-}" ]; then
export PUBLIC_BASE_URL="https://${SPACE_HOST}"
fi
# ============================================================
# Build private Marine MCP runtime configuration
# ============================================================
RUNTIME_MCP_CONFIG="/tmp/codex-marine-mcp.json"
export RUNTIME_MCP_CONFIG
python3 - <<'PY_MCP_CONFIG'
import json
import os
from pathlib import Path
env = {
"MARINE_API_URL": os.environ["MARINE_API_URL"],
"HF_SQUID_DATASET_REPO": (
os.environ.get("HF_SQUID_DATASET_REPO")
or os.environ.get("HF_DATASET_REPO")
or "globalsquiddatabase/squid_dataset"
),
"HF_TUNA_DATASET_REPO": (
os.environ.get("HF_TUNA_DATASET_REPO")
or "globalsquiddatabase/Tuna-Fisheries-Dataset"
),
"FISHERIES_EXPORT_ROOT": os.environ.get(
"FISHERIES_EXPORT_ROOT", "/tmp/squid_fisheries_exports"
),
"HF_FISHERIES_CACHE_ROOT": os.environ.get(
"HF_FISHERIES_CACHE_ROOT", "/tmp/squid_hf_fisheries_cache"
),
"HF_SQUID_DATASET_REVISION": (
os.environ.get("HF_SQUID_DATASET_REVISION")
or os.environ.get("HF_DATASET_REVISION", "")
),
"HF_TUNA_DATASET_REVISION": os.environ.get("HF_TUNA_DATASET_REVISION", ""),
"PUBLIC_BASE_URL": os.environ.get("PUBLIC_BASE_URL", ""),
}
hf_token = os.environ.get("HF_TOKEN", "").strip()
if hf_token:
env["HF_TOKEN"] = hf_token
cfg = {
"timeouts": {
"connect_timeout": 15,
"execute_timeout": 900,
"read_timeout": 900,
},
"servers": {
"marine": {
"command": "python3",
"args": ["/home/user/app/marine_mcp.py"],
"disabled": False,
"required": False,
"env": env,
}
},
}
p = Path(os.environ["RUNTIME_MCP_CONFIG"])
p.write_text(
json.dumps(cfg, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
p.chmod(0o600)
print("[startup] private Marine MCP runtime config ready")
PY_MCP_CONFIG
# ============================================================
# Marine API health check
# ============================================================
if ! curl -fsS --max-time 12 "${MARINE_API_URL%/}/health"; then
echo "[startup] WARNING: Marine API health check failed. Chat remains available; Ocean tools will be unavailable until MARINE_API_URL is corrected."
fi
echo
# ============================================================
# Start Squid Agent Web UI
# ============================================================
exec uvicorn ui_server:app \
--host 0.0.0.0 \
--port "${PORT}" \
--proxy-headers
|