File size: 5,729 Bytes
7509dd0
 
 
8d8777e
 
 
 
 
 
aa49469
7509dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60c0b01
 
7509dd0
 
 
 
 
 
 
 
 
60c0b01
7509dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa49469
 
 
4d9a3de
8d8777e
aa49469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9a3de
aa49469
 
4d9a3de
 
8d8777e
aa49469
 
 
8d8777e
7509dd0
8d8777e
7509dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9a3de
7509dd0
 
 
 
 
 
60c0b01
 
7509dd0
 
 
 
60c0b01
 
 
 
7509dd0
60c0b01
 
b5338ac
7509dd0
 
 
 
60c0b01
7509dd0
 
 
8d8777e
aa49469
8d8777e
 
 
7509dd0
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
set -e

# Kill any leftover health daemon from the base image at runtime
pkill -f "health" 2>/dev/null || true
pkill -f "hc-" 2>/dev/null || true
crontab -r 2>/dev/null || true
echo "[boot] Health daemon silenced"

echo "[boot] Pulling OpenClaw state from Dataset..."
python3 -c "
import sys, os; sys.path.insert(0, '/app')
HF_TOKEN = os.environ.get('HF_TOKEN', '')
DATASET_REPO = os.environ.get('OPENCLAW_DATASET_REPO', '')
if HF_TOKEN and DATASET_REPO:
    from huggingface_hub import hf_hub_download
    from huggingface_hub.utils import EntryNotFoundError
    for fname in ['openclaw.json', 'SOUL.md']:
        try:
            hf_hub_download(repo_id=DATASET_REPO, filename=f'openclaw/{fname}',
                          repo_type='dataset', token=HF_TOKEN,
                          local_dir='/root', local_dir_use_symlinks=False)
        except EntryNotFoundError: pass
        except Exception as e: print(f'  {e}')
"

echo "[boot] Configuring OpenClaw..."
mkdir -p ~/.openclaw
cat > ~/.openclaw/openclaw.json << JSONEOF
{
  "gateway": {"port": 8080, "bind": "0.0.0.0"},
  "identity": {"name": "YT Pipeline Assistant"},
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "${TELEGRAM_BOT_TOKEN}",
      "dmPolicy": "pairing"
    }
  },
  "models": {
    "providers": {
      "ollama": {
        "baseUrl": "http://127.0.0.1:11434/v1",
        "apiKey": "ollama-local",
        "api": "openai-chat",
        "models": [
          {
            "id": "deepseek-qwen-14b",
            "name": "DeepSeek R1 Distill Qwen 14B IQ2_XS (128K)",
            "contextWindow": 131072,
            "maxTokens": 8192
          }
        ]
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {"primary": "ollama/deepseek-qwen-14b"},
      "workspace": "/app/yt-flow"
    }
  }
}
JSONEOF

[ -f /app/SOUL.md ] && cp /app/SOUL.md ~/.openclaw/SOUL.md

git config --global user.name "YT Pipeline Bot"
git config --global user.email "yt-pipe-bot@hf.space"
git clone https://arshitmalik:${HF_TOKEN}@huggingface.co/spaces/arshitmalik/yt-flow /app/yt-flow 2>/dev/null || true

cat > /tmp/backup.sh << 'CRONEOF'
#!/bin/bash
python3 -c "
import os, sys; sys.path.insert(0, '/app')
HF_TOKEN = os.environ.get('HF_TOKEN', '')
DATASET_REPO = os.environ.get('OPENCLAW_DATASET_REPO', '')
if not HF_TOKEN or not DATASET_REPO: sys.exit(0)
from huggingface_hub import HfApi
api = HfApi(token=HF_TOKEN)
from pathlib import Path
for f in Path('/root/.openclaw').glob('*'):
    if f.is_file():
        try:
            api.upload_file(
                path_or_fileobj=str(f),
                path_in_repo=f'openclaw/{f.name}',
                repo_id=DATASET_REPO,
                repo_type='dataset'
            )
        except Exception as e: pass
" 2>/dev/null
CRONEOF
chmod +x /tmp/backup.sh
echo "*/15 * * * * /tmp/backup.sh" | crontab -

python3 -c "
import http.server, urllib.request
class Proxy(http.server.BaseHTTPRequestHandler):
    def _forward(self, method):
        try:
            url = f'http://127.0.0.1:8081{self.path}'
            data = None
            if method == 'POST':
                cl = int(self.headers.get('Content-Length', 0))
                data = self.rfile.read(cl)
            req = urllib.request.Request(url, data=data, method=method)
            for k, v in self.headers.items():
                if k.lower() not in ('host', 'content-length'):
                    req.add_header(k, v)
            resp = urllib.request.urlopen(req, timeout=30)
            body = resp.read()
            self.send_response(resp.status)
            for k, v in resp.getheaders():
                if k.lower() != 'transfer-encoding':
                    self.send_header(k, v)
            self.send_header('Content-Length', len(body))
            self.end_headers()
            self.wfile.write(body)
        except Exception:
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'YT Claw booting...')
    def do_GET(self): self._forward('GET')
    def do_POST(self): self._forward('POST')
    def log_message(self, *a): pass
http.server.HTTPServer(('0.0.0.0', 8080), Proxy).serve_forever()
" &
echo "[boot] Smart proxy on :8080 → :8081"

echo "[boot] Starting nginx..."
nginx

echo "[boot] Starting Ollama..."
export OLLAMA_FLASH_ATTENTION=1
export OLLAMA_KV_CACHE_TYPE=q4_0
OLLAMA_HOST=127.0.0.1 OLLAMA_NUM_PARALLEL=1 OLLAMA_MAX_LOADED_MODELS=1 ollama serve &

for i in $(seq 1 30); do
    if curl -sf http://127.0.0.1:11434/api/tags > /dev/null 2>&1; then
        echo "[boot] Ollama ready after ${i}s"; break
    fi
    sleep 2
done

echo "[boot] Building DeepSeek R1 Distill Qwen 14B IQ2_XS..."
python3 << PYEOF
import subprocess, sys, os
from pathlib import Path
from huggingface_hub import hf_hub_download

gguf_path = hf_hub_download(
    repo_id="bartowski/DeepSeek-R1-Distill-Qwen-14B-GGUF",
    filename="DeepSeek-R1-Distill-Qwen-14B-IQ2_XS.gguf",
    local_dir="/tmp/gguf",
    token=os.environ.get("HF_TOKEN", None)
)
modelfile = f"""FROM {gguf_path}
TEMPLATE \"\"\"<|begin▁of▁sentence|>{{ .System }}

{{ .Prompt }}<|end▁of▁sentence|>
<|begin▁of▁sentence|>assistant
\"\"\"
PARAMETER stop "<|begin▁of▁sentence|>"
PARAMETER stop "<|end▁of▁sentence|>"
PARAMETER temperature 0.6
PARAMETER num_ctx 131072
PARAMETER num_predict 8192
"""
(Path("/tmp") / "Modelfile").write_text(modelfile)
subprocess.run(["ollama", "create", "deepseek-qwen-14b", "-f", "/tmp/Modelfile"], check=True)
Path(gguf_path).unlink()
PYEOF

echo "[boot] Starting OpenClaw gateway on :8081..."
openclaw gateway 2>&1 &
echo "[boot] OpenClaw PID: $!"
echo "[boot] All services running."
wait $! 2>/dev/null
sleep infinity