# OpenClaw on Hugging Face Spaces - 修复版 # 复制此内容到 HF Space 的 Dockerfile 中 FROM node:22-slim # 1. 基础依赖 RUN apt-get update && apt-get install -y --no-install-recommends \ git openssh-client build-essential python3 python3-pip \ g++ make ca-certificates curl \ && rm -rf /var/lib/apt/lists/* # 2. 安装 Hugging Face Hub RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages # 3. Git 配置 RUN update-ca-certificates && \ git config --global http.sslVerify false && \ git config --global url."https://github.com/".insteadOf ssh://git@github.com/ # 4. 安装 OpenClaw RUN npm install -g openclaw@latest --unsafe-perm # 5. 环境变量预设 ENV PORT=7860 \ OPENCLAW_GATEWAY_MODE=local \ HOME=/root \ NODE_ENV=production # 6. 同步脚本 - 修复多行格式 RUN cat > /usr/local/bin/sync.py << 'SYNC_EOF' import os, sys, tarfile from huggingface_hub import HfApi, hf_hub_download from datetime import datetime, timedelta api = HfApi() repo_id = os.getenv("HF_DATASET", "") token = os.getenv("HF_TOKEN", "") def restore(): if not repo_id or not token: print("No HF_DATASET or HF_TOKEN, skipping restore") return False try: files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token) now = datetime.now() for i in range(5): day = (now - timedelta(days=i)).strftime("%Y-%m-%d") name = f"backup_{day}.tar.gz" if name in files: path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token) with tarfile.open(path, "r:gz") as tar: tar.extractall(path="/root/.openclaw/") print(f"Restored from {name}") return True print("No backup found") except Exception as e: print(f"Restore warning: {e}") return False def backup(): if not repo_id or not token: return try: day = datetime.now().strftime("%Y-%m-%d") name = f"backup_{day}.tar.gz" with tarfile.open(name, "w:gz") as tar: if os.path.exists("/root/.openclaw/sessions"): tar.add("/root/.openclaw/sessions", arcname="sessions") if os.path.exists("/root/.openclaw/openclaw.json"): tar.add("/root/.openclaw/openclaw.json", arcname="openclaw.json") api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token) print(f"Backup {name} done") os.remove(name) except Exception as e: print(f"Backup warning: {e}") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "backup": backup() else: restore() SYNC_EOF RUN chmod +x /usr/local/bin/sync.py # 7. 启动脚本 - 修复多行格式 RUN cat > /usr/local/bin/start-openclaw << 'START_EOF' #!/bin/bash set -e mkdir -p /root/.openclaw/sessions # 尝试恢复数据(可选) python3 /usr/local/bin/sync.py restore || true # 准备 API 配置 CLEAN_BASE="${OPENAI_API_BASE:-https://integrate.api.nvidia.com/v1}" CLEAN_BASE=$(echo "$CLEAN_BASE" | sed 's|/chat/completions||g' | sed 's|/v1/|/v1|g' | sed 's|/v1$|/v1|') # 生成令牌(如果没设置) GATEWAY_TOKEN="${OPENCLAW_GATEWAY_TOKEN:-$(openssl rand -hex 16)}" MODEL_NAME="${MODEL:-moonshotai/kimi-k2.5}" # 创建 OpenClaw 配置 cat > /root/.openclaw/openclaw.json <