CJHauser commited on
Commit
fc0a7bb
·
verified ·
1 Parent(s): 7f0aa20

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +139 -197
Dockerfile CHANGED
@@ -1,197 +1,139 @@
1
- FROM oven/bun:1-slim
2
-
3
- ARG DEBIAN_FRONTEND=noninteractive
4
- RUN apt-get update && apt-get install -y --no-install-recommends \
5
- git ca-certificates curl sqlite3 rsync python3 \
6
- && rm -rf /var/lib/apt/lists/*
7
-
8
- WORKDIR /app
9
-
10
- ARG LUMIVERSE_REPO=https://github.com/CloudCompile/Lumiverse.git
11
- ARG LUMIVERSE_REF=Leaderboard
12
- RUN git clone --depth 1 --branch "${LUMIVERSE_REF}" "${LUMIVERSE_REPO}" .
13
-
14
- # This patch only applies to forks/branches that still have the old
15
- # unpatched host-only rewrite block. If the fork has already diverged
16
- # (different code, or already fixed upstream), the patch is skipped
17
- # instead of failing the whole build.
18
- RUN cat > /tmp/patch-auth-rewrite.mjs <<'PATCH'
19
- import { readFileSync, writeFileSync } from "fs";
20
-
21
- const path = "src/app.ts";
22
- const src = readFileSync(path, "utf8");
23
-
24
- const before = `app.on(["POST", "GET"], "/api/auth/*", (c) => {
25
- const host = c.req.header("host");
26
- if (host) {
27
- const url = new URL(c.req.url);
28
- const rewritten = new URL(url.pathname + url.search, \`http://\${host}\`);
29
- return auth.handler(new Request(rewritten.toString(), c.req.raw));
30
- }
31
- return auth.handler(c.req.raw);
32
- });`;
33
-
34
- const after = `app.on(["POST", "GET"], "/api/auth/*", (c) => {
35
- const host = c.req.header("x-forwarded-host") || c.req.header("host");
36
- const proto = c.req.header("x-forwarded-proto") || "http";
37
-
38
- if (host) {
39
- const url = new URL(c.req.url);
40
- const rewritten = new URL(url.pathname + url.search, \`\${proto}://\${host}\`);
41
- return auth.handler(new Request(rewritten.toString(), c.req.raw));
42
- }
43
- return auth.handler(c.req.raw);
44
- });`;
45
-
46
- if (src.includes("x-forwarded-host") || src.includes("x-forwarded-proto")) {
47
- console.log("[patch] src/app.ts already forwards x-forwarded-host/proto — skipping patch");
48
- process.exit(0);
49
- }
50
-
51
- if (!src.includes(before)) {
52
- console.warn("[patch] Expected auth rewrite block not found in src/app.ts — skipping patch");
53
- console.warn("[patch] This fork/branch may have a different auth setup. If HTTPS auth redirects");
54
- console.warn("[patch] fail at runtime, this file will need a manual patch.");
55
- process.exit(0);
56
- }
57
-
58
- writeFileSync(path, src.replace(before, after), "utf8");
59
- console.log("[patch] Patched src/app.ts for x-forwarded-proto/host");
60
- PATCH
61
-
62
- RUN bun /tmp/patch-auth-rewrite.mjs
63
-
64
- RUN rm -f package-lock.json && bun install --production
65
-
66
- WORKDIR /app/frontend
67
- RUN rm -f package-lock.json && bun install && bun run build
68
- RUN printf "self.addEventListener('install',e=>self.skipWaiting());self.addEventListener('activate',e=>e.waitUntil(self.clients.claim()));\n" > /app/frontend/dist/sw.js
69
- RUN test -f /app/frontend/dist/index.html
70
-
71
- WORKDIR /app
72
- ENV NODE_ENV=production
73
- ENV PORT=7860
74
- ENV DATA_DIR=/app/data
75
- ENV FRONTEND_DIR=/app/frontend/dist
76
- ENV TRUST_ANY_ORIGIN=true
77
- ENV OWNER_PASSWORD=admin123
78
-
79
- # === DATA INTEGRITY & CORRUPTION RECOVERY ===
80
- # Create a recovery script that runs before the app starts
81
- RUN cat > /app/check-and-repair-data.sh <<'REPAIR'
82
- #!/usr/bin/env sh
83
- set -e
84
-
85
- DATA_DIR="${DATA_DIR:-/app/data}"
86
-
87
- echo "[integrity] Starting pre-startup data checks..."
88
-
89
- # Ensure data directory structure exists
90
- mkdir -p "$DATA_DIR"
91
-
92
- # Critical check: top-level entries (users, uploads, extensions) must be
93
- # directories. HF Spaces storage remounts have been observed to leave these
94
- # as stray files instead. If any of them is a file, quarantine it before
95
- # trying to mkdir, or every startup will fail with "File exists".
96
- for entry in users uploads extensions; do
97
- entry_path="$DATA_DIR/$entry"
98
- if [ -e "$entry_path" ] && [ ! -d "$entry_path" ]; then
99
- echo "[integrity] $entry_path exists but is not a directory (corrupted remount)"
100
- echo "[integrity] Quarantining to ${entry_path}.corrupted-$(date +%s)"
101
- mv "$entry_path" "${entry_path}.corrupted-$(date +%s)"
102
- fi
103
- done
104
-
105
- USERS_DIR="$DATA_DIR/users"
106
-
107
- # If users directory exists, scan for corrupted entries
108
- if [ -d "$USERS_DIR" ]; then
109
- echo "[integrity] Checking user directories for corruption..."
110
-
111
- find "$USERS_DIR" -maxdepth 1 -type f 2>/dev/null | while read -r corrupt_file; do
112
- # There should be NO files directly in users/ — only directories
113
- echo "[integrity] Found stray file in users/: $corrupt_file"
114
- echo "[integrity] Moving to quarantine: ${corrupt_file}.corrupted"
115
- mv "$corrupt_file" "${corrupt_file}.corrupted"
116
- done
117
-
118
- # Check each user's subdirectory for storage file/directory collision
119
- find "$USERS_DIR" -maxdepth 2 -name "storage" -type f 2>/dev/null | while read -r storage_file; do
120
- user_dir=$(dirname "$storage_file")
121
- echo "[integrity] Found file where directory expected: $storage_file"
122
- echo "[integrity] This was caused by HF Spaces storage remount. Removing corrupt file..."
123
- rm -f "$storage_file"
124
- echo "[integrity] Directory will be auto-created on first use"
125
- done
126
- fi
127
-
128
- # Pre-create critical directories to prevent race conditions.
129
- # These run with '|| true' as a last-resort safety net: the loop above should
130
- # have already cleared any file/directory collisions, but if something
131
- # unexpected still blocks mkdir, log it instead of crashing the container —
132
- # the app's own provisioning code can then surface a clearer error.
133
- mkdir -p "$DATA_DIR/users" || echo "[integrity] WARNING: could not create $DATA_DIR/users, check manually"
134
- mkdir -p "$DATA_DIR/uploads" || echo "[integrity] WARNING: could not create $DATA_DIR/uploads, check manually"
135
- mkdir -p "$DATA_DIR/extensions" || echo "[integrity] WARNING: could not create $DATA_DIR/extensions, check manually"
136
-
137
- # Verify SQLite database integrity if it exists
138
- DB_PATH="$DATA_DIR/lumiverse.db"
139
- if [ -e "$DB_PATH" ]; then
140
- echo "[integrity] Inspecting $DB_PATH..."
141
- ls -la "$DB_PATH" 2>&1 || true
142
- file "$DB_PATH" 2>&1 || true
143
-
144
- if [ ! -f "$DB_PATH" ]; then
145
- echo "[integrity] $DB_PATH exists but is not a regular file (corrupted remount)"
146
- echo "[integrity] Quarantining to ${DB_PATH}.corrupted-$(date +%s)"
147
- mv "$DB_PATH" "${DB_PATH}.corrupted-$(date +%s)"
148
- else
149
- echo "[integrity] Running SQLite integrity check..."
150
- if ! sqlite3 "$DB_PATH" "PRAGMA integrity_check;" 2>&1 | grep -q "ok"; then
151
- echo "[integrity] WARNING: Database integrity check failed (disk I/O error or corruption)."
152
- echo "[integrity] NOT deleting or overwriting the database automatically — that risks data loss."
153
- echo "[integrity] Backing up as-is to ${DB_PATH}.bak-$(date +%s) for manual inspection."
154
- cp "$DB_PATH" "${DB_PATH}.bak-$(date +%s)" 2>&1 || echo "[integrity] Backup copy also failed — underlying mount may be unhealthy."
155
- echo "[integrity] The app will attempt to start anyway; if it cannot open the database,"
156
- echo "[integrity] this usually means the persistent storage mount itself needs to be"
157
- echo "[integrity] detached and reattached from the Space's Settings page."
158
- else
159
- echo "[integrity] Database integrity check passed"
160
- fi
161
- fi
162
- else
163
- echo "[integrity] No existing database found at $DB_PATH (fresh install or not yet created)"
164
- fi
165
-
166
- echo "[integrity] Pre-startup checks complete"
167
- REPAIR
168
-
169
- RUN chmod +x /app/check-and-repair-data.sh
170
-
171
- # Startup script that runs integrity check BEFORE the app
172
- RUN cat > /app/start.sh <<'SH'
173
- #!/usr/bin/env sh
174
- set -eu
175
- export DATA_DIR="${DATA_DIR:-/app/data}"
176
-
177
- # Run data integrity checks first
178
- /app/check-and-repair-data.sh
179
-
180
- # Then start the app
181
- exec bun run scripts/runner.ts
182
- SH
183
-
184
- RUN chmod +x /app/start.sh
185
-
186
- USER root
187
- # Create data directory with proper permissions
188
- RUN mkdir -p /app/data && chown -R bun:bun /app/data
189
-
190
- # Pre-create user directory to prevent first-run permission issues
191
- RUN mkdir -p /app/data/users && chown -R bun:bun /app/data/users
192
-
193
- EXPOSE 7860
194
- VOLUME /app/data
195
-
196
- USER bun
197
- CMD ["/app/start.sh"]
 
1
+ import os, sys, json, time, threading, subprocess, glob, html
2
+ from http.server import BaseHTTPRequestHandler, HTTPServer
3
+ from huggingface_hub import HfApi, hf_hub_download
4
+
5
+ DATA = "/data"
6
+ BUCKET = os.environ.get("BUCKET", "")
7
+ MAX_PARAMS_B = 1.5
8
+ MAX_GB = 3.0
9
+ TIMEOUT = 900
10
+ api = HfApi()
11
+
12
+ STATUS = {"current": "starting", "queue": 0}
13
+ CACHE = {"t": 0, "rows": [], "counts": {}}
14
+
15
+
16
+ # ---------- worker (separate process) ----------
17
+ def worker(path):
18
+ from llama_cpp import Llama
19
+ from datasets import load_dataset
20
+ ds = load_dataset("allenai/ai2_arc", "ARC-Easy", split="test").shuffle(seed=0).select(range(25))
21
+ llm = Llama(model_path=path, n_ctx=512, n_threads=2, verbose=False)
22
+ correct, t0, toks = 0, time.time(), 0
23
+ for ex in ds:
24
+ opts = "\n".join(f"{l}. {t}" for l, t in zip(ex["choices"]["label"], ex["choices"]["text"]))
25
+ out = llm(f"Question: {ex['question']}\n{opts}\nAnswer:", max_tokens=2, temperature=0)
26
+ toks += out["usage"]["prompt_tokens"] + out["usage"]["completion_tokens"]
27
+ if out["choices"][0]["text"].strip().upper().startswith(ex["answerKey"]):
28
+ correct += 1
29
+ print(json.dumps({"arc_easy_25": correct / 25, "tok_per_s": round(toks / (time.time() - t0), 1)}))
30
+
31
+
32
+ # ---------- storage (mounted bucket) ----------
33
+ def load_done():
34
+ p = f"{DATA}/state/done.txt"
35
+ return set(open(p).read().split()) if os.path.exists(p) else set()
36
+
37
+ def push(text, remote):
38
+ p = f"{DATA}/{remote}"
39
+ os.makedirs(os.path.dirname(p), exist_ok=True)
40
+ with open(p, "w") as f:
41
+ f.write(text)
42
+
43
+
44
+ # ---------- main loop ----------
45
+ def loop():
46
+ done = load_done()
47
+ ids = sorted(m.id for m in api.list_models(filter="gguf", limit=5000))
48
+ STATUS["queue"] = len(ids)
49
+ for mid in ids:
50
+ if mid in done:
51
+ continue
52
+ STATUS["current"] = mid
53
+ result, path = {"model": mid, "ts": time.time()}, None
54
+ try:
55
+ info = api.model_info(mid, files_metadata=True)
56
+ gguf = getattr(info, "gguf", None) or {}
57
+ params_b = (gguf.get("total") or 0) / 1e9
58
+ files = [s for s in info.siblings if s.rfilename.endswith(".gguf")
59
+ and s.size and s.size / 1e9 <= MAX_GB]
60
+ if not params_b:
61
+ result["status"] = "skipped_no_param_metadata"
62
+ elif params_b > MAX_PARAMS_B:
63
+ result.update(status="skipped_too_big", params_b=round(params_b, 2))
64
+ elif not files:
65
+ result["status"] = "skipped_no_usable_file"
66
+ else:
67
+ f = min(files, key=lambda s: s.size)
68
+ path = hf_hub_download(mid, f.rfilename)
69
+ result.update(file=f.rfilename, params_b=round(params_b, 2))
70
+ p = subprocess.run([sys.executable, __file__, "--worker", path],
71
+ capture_output=True, text=True, timeout=TIMEOUT)
72
+ if p.returncode == 0:
73
+ result.update(json.loads(p.stdout.strip().splitlines()[-1]), status="ok")
74
+ else:
75
+ result.update(status="failed", error=p.stderr[-500:])
76
+ except Exception as e:
77
+ result.update(status="failed", error=str(e)[:500])
78
+ finally:
79
+ if path and os.path.exists(path):
80
+ os.remove(os.path.realpath(path))
81
+ try:
82
+ push(json.dumps(result), f"results/{mid.replace('/', '__')}.json")
83
+ done.add(mid)
84
+ push("\n".join(sorted(done)), "state/done.txt")
85
+ except Exception as e:
86
+ print("write failed:", e)
87
+ STATUS["current"] = "finished"
88
+
89
+
90
+ # ---------- status page (reads full history from the mount) ----------
91
+ def refresh():
92
+ if time.time() - CACHE["t"] < 60:
93
+ return
94
+ rows, counts = [], {"ok": 0, "failed": 0, "skipped": 0}
95
+ for f in glob.glob(f"{DATA}/results/*.json"):
96
+ try:
97
+ r = json.load(open(f))
98
+ except Exception:
99
+ continue
100
+ s = r.get("status", "")
101
+ if s == "ok":
102
+ counts["ok"] += 1
103
+ rows.append(r)
104
+ elif s == "failed":
105
+ counts["failed"] += 1
106
+ else:
107
+ counts["skipped"] += 1
108
+ rows.sort(key=lambda r: (-r["arc_easy_25"], -r["tok_per_s"]))
109
+ CACHE.update(t=time.time(), rows=rows[:200], counts=counts)
110
+
111
+ class H(BaseHTTPRequestHandler):
112
+ def do_GET(self):
113
+ refresh()
114
+ c = CACHE["counts"]
115
+ body = "".join(
116
+ f"<tr><td>{html.escape(r['model'])}</td><td>{r['params_b']}B</td>"
117
+ f"<td>{r['arc_easy_25']:.2f}</td><td>{r['tok_per_s']}</td>"
118
+ f"<td>{html.escape(r.get('file', ''))}</td></tr>" for r in CACHE["rows"])
119
+ link = f'<a href="https://huggingface.co/buckets/{BUCKET}">bucket</a>' if BUCKET else "bucket"
120
+ page = f"""<html><body style="font-family:sans-serif;max-width:900px;margin:2em auto">
121
+ <h2>Tiny model leaderboard</h2>
122
+ <p>Now: <b>{html.escape(STATUS['current'])}</b><br>
123
+ ok {c.get('ok', 0)} · failed {c.get('failed', 0)} · skipped {c.get('skipped', 0)}
124
+ · queue {STATUS['queue']}</p>
125
+ <p>Raw results: {link} (top 200 shown, refreshed every 60s)</p>
126
+ <table border=1 cellpadding=4><tr><th>model</th><th>params</th><th>ARC-E (25)</th>
127
+ <th>tok/s</th><th>file</th></tr>{body}</table></body></html>"""
128
+ self.send_response(200); self.send_header("Content-Type", "text/html"); self.end_headers()
129
+ self.wfile.write(page.encode())
130
+ def log_message(self, *a): pass
131
+
132
+
133
+ if __name__ == "__main__":
134
+ if len(sys.argv) > 2 and sys.argv[1] == "--worker":
135
+ worker(sys.argv[2])
136
+ else:
137
+ os.makedirs(f"{DATA}/results", exist_ok=True)
138
+ threading.Thread(target=loop, daemon=True).start()
139
+ HTTPServer(("0.0.0.0", 7860), H).serve_forever()