Update Dockerfile
Browse files- Dockerfile +139 -197
Dockerfile
CHANGED
|
@@ -1,197 +1,139 @@
|
|
| 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 |
-
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|