icoa-cli 2.19.683 → 2.19.684
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/ai4ctf.js +1 -1
- package/dist/commands/ctf4ai-demo.js +1 -1
- package/dist/commands/ctf4vla.js +1 -1
- package/dist/commands/exam.js +1 -1
- package/dist/commands/gpufortask.js +1 -1
- package/dist/commands/ipynb.js +1 -1
- package/dist/commands/kaggle.js +1 -1
- package/dist/lib/editor-spawn.js +1 -1
- package/dist/lib/gpu.js +1 -1
- package/dist/lib/gputask-session.js +1 -0
- package/dist/lib/kaggle.js +1 -1
- package/dist/lib/kernel-protocol.js +1 -1
- package/dist/lib/kernel.js +35 -9
- package/dist/lib/notebook-doc.js +1 -1
- package/package.json +1 -1
package/dist/lib/kaggle.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{sprintTaskForComp as e}from"./sprint-meta.js";export function parseKaggleTaskList(e){const t=e??{},s=(t.data??t).tasks;if(!Array.isArray(s))return[];const a=[];for(const e of s){if(!e||"object"!=typeof e)continue;const t=e;"string"==typeof t.comp_id&&""!==t.comp_id&&a.push({comp_id:t.comp_id,title:"string"==typeof t.title?t.title:t.comp_id,metric:"string"==typeof t.metric?t.metric:"accuracy",description:"string"==typeof t.description&&""!==t.description?t.description:void 0,kaggle_url:"string"==typeof t.kaggle_url?t.kaggle_url:null,licence:"string"==typeof t.licence?t.licence:void 0,attribution:"string"==typeof t.attribution?t.attribution:void 0,data_base:"string"==typeof t.data_base&&""!==t.data_base?t.data_base:null,id_col:"string"==typeof t.id_col?t.id_col:void 0,target_col:"string"==typeof t.target_col?t.target_col:void 0,n_train:"number"==typeof t.n_train?t.n_train:void 0,n_test:"number"==typeof t.n_test?t.n_test:void 0,judge:"string"==typeof t.judge&&""!==t.judge?t.judge:void 0,board_event:"string"==typeof t.board_event&&""!==t.board_event?t.board_event:null,needs_gpu:!0===t.needs_gpu})}return a}export function dataFileUrls(e){const t=e.data_base;if("string"!=typeof t||""===t)return null;const s=t.replace(/\/$/,"");return{train:`${s}/train.csv`,test:`${s}/test.csv`,sample_submission:`${s}/sample_submission.csv`}}export function buildStarterCells(t,s){const a=s.id_col??"id",i=s.target_col??"target";var n;n=s.metric,/rmse|mse|mae|error/i.test(n);const o="icoa-holdout"===s.judge,r=e=>({kind:"markdown",source:e,result:null}),l=e=>({kind:"code",source:e.join("\n"),result:null}),c=[`# ${s.title}`,"",...s.description?[s.description,""]:[],`Kaggle-style practice — metric: **${s.metric}**.`,"","- `train.csv` — labelled training data",`- \`test.csv\` — rows to predict (no \`${i}\` column)`,"- `sample_submission.csv` — the exact format Kaggle expects","","Run all cells top to bottom: a baseline model trains on your CPU and","`submission.csv` appears next to the data. Then improve the model cell",...o?["and re-run — `icoa kaggle submit` scores it and ranks you on the board."]:["and re-run — your score is whatever Kaggle says it is."],"","**AI help:** split your terminal (`Ctrl-b %`), run `icoa` in the right pane","and type `ai4ioai` for a live Gemma 3n E4B. Ask it for feature ideas or bug fixes —","then verify every suggestion with the val score. A tip that lowers it, drop it.",...s.licence?["",`**Data licence:** ${s.licence}`]:[],...s.attribution?[`**Attribution:** ${s.attribution}`]:[]].join("\n"),p=l(["# setup — load the data (leave as is)","import os","import numpy as np","import pandas as pd","from pandas.api.types import is_numeric_dtype","from sklearn.feature_extraction.text import TfidfVectorizer","from sklearn.model_selection import train_test_split","from sklearn.metrics import accuracy_score, mean_squared_error","","# same code everywhere: in a GPU session these env vars point at the","# box's own dataset copy + workspace — locally they are unset.",`DIR = os.environ.get("ICOA_KAGGLE_DATA") or r"${t}"`,'OUT = os.environ.get("ICOA_KAGGLE_OUT") or DIR',...s.data_base?[`DATA_BASE = "${s.data_base.replace(/\/$/,"")}"`,'if not os.path.exists(DIR + "/train.csv"):'," import urllib.request"," os.makedirs(DIR, exist_ok=True)",' for f in ("train.csv", "test.csv", "sample_submission.csv"):',' urllib.request.urlretrieve(DATA_BASE + "/" + f, DIR + "/" + f)']:[],'train = pd.read_csv(DIR + "/train.csv")','test = pd.read_csv(DIR + "/test.csv")','sub_fmt = pd.read_csv(DIR + "/sample_submission.csv")',"","# 🔴 The submission column names are NOT always the training column names.","# On tasks imported from other platforms the two genuinely differ: train.csv",'# labels the answer "class" or "result" while sample_submission.csv calls it','# "answer". Hard-coding the submission name here used to raise KeyError on the',"# very first cell. So trust the DATA: whatever column train has and test lacks","# is the thing to predict.",`SUB_ID, SUB_TARGET = "${a}", "${i}" # names the submission file wants`,"only_train = [c for c in train.columns if c not in test.columns]","TARGET = only_train[-1] if only_train else SUB_TARGET","shared = [c for c in train.columns if c in test.columns]","# Only treat a column as the row id when the submission actually names it AND",'# both files carry it. Falling back to "first shared column" silently spent a',"# real feature as an id on tasks whose id lives only in test.csv.","ID = SUB_ID if SUB_ID in shared else None",'assert TARGET in train.columns, f"cannot find the label column in {list(train.columns)}"',"","# Regression or classification? Decided from the labels, not from metadata —","# several tasks ship continuous labels, and handing those to a classifier",'# raises "Unknown label type: continuous".',"REG = is_numeric_dtype(train[TARGET]) and train[TARGET].nunique() > 20",'print(f"train {train.shape} · test {test.shape} · predicting {TARGET!r} "'," f\"({'regression' if REG else 'classification'})\")","# 前几行 —— 看一眼真实数据长什么样,比看列名有用得多","print(train.head(3).to_string(max_colwidth=24)[:700])"]),u=e(s.comp_id),d=u?.media,m="audio"===d?.kind?"scipy.io.wavfile.read(path) -> numpy, then scipy.signal for a spectrogram":"image"===d?.kind?"PIL.Image.open(path) -> numpy":"weights"===d?.kind?"torch.load(path, weights_only=True) — the model itself is the input here":"unpack the archive inside the pack, then read what is in it",h=l(["# FEATURES — turn raw columns into numbers a model can use (improve THIS cell).","# Numbers: fill gaps with the median. Category (<= 20 distinct): one-hot.","# Free text: TF-IDF — on a text task the sentences ARE the signal, and dropping",'# them as "high-cardinality" leaves nothing to train on (that used to crash the','# whole notebook with "No objects to concatenate").',"# only columns present in BOTH files — a train-only column cannot be a feature","# at prediction time, and indexing test with it raises KeyError.","feat = [c for c in shared if c not in (ID, TARGET)]","Xtr_raw, Xte_raw = train[feat].copy(), test[feat].copy()","num = [c for c in feat if is_numeric_dtype(Xtr_raw[c])]","cat = [c for c in feat if c not in num and Xtr_raw[c].nunique() <= 20]","# Everything else is vectorised as text. Requiring a long average length here","# used to throw away high-cardinality SHORT strings — ingredient names, single","# words, category codes — which on several tasks are the entire signal, leaving","# the notebook with zero features.","txt = [c for c in feat if c not in num and c not in cat]","for c in num:"," med = Xtr_raw[c].median()"," Xtr_raw[c] = Xtr_raw[c].fillna(med)"," Xte_raw[c] = Xte_raw[c].fillna(med)","blocks_tr, blocks_te = [], []","if num or cat:"," d_tr = pd.get_dummies(Xtr_raw[num + cat], columns=cat, dummy_na=True)"," d_te = pd.get_dummies(Xte_raw[num + cat], columns=cat, dummy_na=True)",' d_tr, d_te = d_tr.align(d_te, join="left", axis=1, fill_value=0)'," blocks_tr.append(d_tr.to_numpy(dtype=float))"," blocks_te.append(d_te.to_numpy(dtype=float))","for c in txt:"," # fillna BEFORE astype(str): a real NaN reaching the vectoriser raises",' # "np.nan is an invalid document", and astype alone does not catch it.',' tr_txt = Xtr_raw[c].fillna("").astype(str)',' te_txt = Xte_raw[c].fillna("").astype(str)'," # min_df=1: on a short-token column (one ingredient per row) min_df=2 can"," # filter the vocabulary down to nothing."," long_col = tr_txt.str.len().mean() >= 12"," # Try the good settings, then a permissive fallback, then give up on THIS"," # column only. A column whose vocabulary prunes to nothing used to abort the",' # whole run with "After pruning, no terms remain".',' for kw in ({"min_df": 2 if long_col else 1,',' "analyzer": "word" if long_col else "char_wb"},',' {"min_df": 1, "analyzer": "char_wb"}):'," try:"," vec = TfidfVectorizer(max_features=3000, ngram_range=(1, 2), **kw)"," b_tr = vec.fit_transform(tr_txt).toarray()"," blocks_tr.append(b_tr)"," blocks_te.append(vec.transform(te_txt).toarray())"," break"," except ValueError as e:"," last = e"," else:",' print(f" skipped text column {c!r}: {last}")',"if not blocks_tr:"," raise SystemExit(",' f"No usable feature columns. The shared columns are {feat} — none of them "',' f"is numeric, categorical or text.\\n"',...d?[` "The signal for this task is in ${d.kind} files, not in the CSVs.\\n"`,` " 1. fetch them (${d.mb}MB): teleload ${d.pack}\\n"`,...d.glob?[` " files land at <pack dir>/${d.glob}\\n"`]:[],` " 2. replace this cell: ${m}\\n"`,' " build one feature row per file, then keep the rest of the notebook.\\n"']:[' f"Two kinds of task land here, and both are real work rather than a bug:\\n"',' f" 1. audio/image — train.csv only lists sample ids and labels and the "',' f"signal is in media files. Read them with scipy.io.wavfile (audio) or "',' f"PIL (images) and turn each file into a feature row.\\n"',' f" 2. train.csv and test.csv describe different things (a retrieval or "',' f"matching task). Read both files, work out what links a test row to a "',' f"train row, and build that mapping yourself.\\n"'],...d?[' f"columns — train={list(train.columns)} test={list(test.columns)}"']:[' f"Either way: replace this cell. train={list(train.columns)} "',' f"test={list(test.columns)}"']," )","X = np.hstack(blocks_tr)","X_test = np.hstack(blocks_te)","y = train[TARGET]","X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=0)",'print(f"{X.shape[1]} model-ready features "',' f"({len(num)} numeric, {len(cat)} one-hot, {len(txt)} TF-IDF text)")']),f=l(["# EDA — look at the labels before you model. Produces ONE figure:","# left = what you are predicting (class balance / value spread)","# right = what the FEATURES cell will have to work with","# icoa ipynb saves it as a .png and opens your image viewer (type `open` to reopen).","import matplotlib.pyplot as plt","try:"," fig, ax = plt.subplots(1, 2, figsize=(11, 4))"," if REG:",' vals = pd.to_numeric(train[TARGET], errors="coerce").dropna()'," ax[0].hist(vals, bins=30)",' ax[0].set_title(f"{TARGET} — {len(vals)} values, mean {vals.mean():.3g}")'," else:"," vc = train[TARGET].astype(str).value_counts().head(12)"," ax[0].bar(range(len(vc)), vc.values)"," ax[0].set_xticks(range(len(vc)))",' ax[0].set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' # An imbalanced target is the single most common reason a "good"'," # accuracy means nothing — show the majority share right in the title."," top = vc.iloc[0] / len(train)",' ax[0].set_title(f"{TARGET} — {train[TARGET].nunique()} classes · biggest {top:.0%}")',' kinds = {"numeric": 0, "category": 0, "text": 0}'," for c in [c for c in shared if c not in (ID, TARGET)]:"," if is_numeric_dtype(train[c]):",' kinds["numeric"] += 1'," elif train[c].nunique() <= 20:",' kinds["category"] += 1'," else:",' kinds["text"] += 1'," miss = train.isna().mean()"," miss = miss[miss > 0].sort_values(ascending=False).head(10)"," if len(miss) > 0:"," ax[1].barh(range(len(miss)), miss.values)"," ax[1].set_yticks(range(len(miss)))"," ax[1].set_yticklabels([s[:16] for s in miss.index])",' ax[1].set_title("missing rate (top columns)")'," else:"," ax[1].bar(list(kinds), list(kinds.values()))",' ax[1].set_title(f"{sum(kinds.values())} usable columns · no missing values")'," plt.tight_layout()"," plt.show()",' print("columns by kind:", kinds)',"except Exception as e:"," # EDA is never allowed to stop the run — `run all` halts on the first"," # error, and a plotting hiccup must not cost the student their baseline.",' print("EDA skipped:", type(e).__name__, e)']),g=l(["# MODEL — a working baseline. Improve THIS cell to climb the board.","from sklearn.linear_model import LogisticRegression, Ridge","# REG was decided from the labels back in setup.","model = Ridge() if REG else LogisticRegression(max_iter=2000)"]),_=l(["# validate — quick local score on a held-out fifth of train","model.fit(X_tr, y_tr)","p_val = model.predict(X_val)","if REG:",' print("val RMSE:", round(mean_squared_error(y_val, p_val) ** 0.5, 4))',"else:"," acc = accuracy_score(y_val, p_val)",' print("val accuracy:", round(acc, 4))'," if acc < 0.3:"," # A near-zero baseline is the expected result on several of these tasks,"," # not a broken notebook — say so, or the student reads it as a bug.",' print(" A score this low usually means the linear baseline cannot "',' "represent the pattern at all (combination rules, sequences, "',' "spelling). That IS the task: build features that expose it, "',' "or switch to a model that can. The chain itself works — "',' "submission.csv is written either way.")']),b=l(["# submission — retrain on ALL data, write submission.csv (Kaggle format)","model.fit(X, y)","pred = model.predict(X_test)",'out = OUT + "/submission.csv"',"# Copy the layout of sample_submission.csv and swap in the predictions, rather","# than rebuilding the file from column names. Some tasks want more than two","# columns (e.g. subtaskID,datapointID,answer) and a hand-built 2-column frame","# is rejected by the grader even when every prediction is right.","if len(sub_fmt) == len(pred):"," sub = sub_fmt.copy()"," sub[sub.columns[-1]] = pred","else:"," keys = {}"," if ID is not None and ID in test.columns:"," keys[SUB_ID] = test[ID]"," keys[SUB_TARGET] = pred"," sub = pd.DataFrame(keys)",' print(f"NOTE: sample_submission has {len(sub_fmt)} rows but test has "',' f"{len(pred)} — built the file from column names instead; "',' f"check it against the task page before submitting.")',"sub.to_csv(out, index=False)",'print("wrote", out, "·", list(sub.columns))',o?`print("score it: icoa kaggle submit ${s.comp_id}")`:'print("format check: icoa kaggle check")']),y=o?["## Submit to ICOA (scored on the server)","",`1. In the ICOA CLI run \`icoa kaggle submit ${s.comp_id}\``,"2. The server scores `submission.csv` against the secret test labels"," and your rank lands on the live board (`icoa board`).","3. Improve the MODEL cell, re-run, submit again — best score counts."]:"nitro"===s.judge?["## Where this one gets scored","","This task comes from **judge.nitro-ai.org** and the answer key stays on","their server, so ICOA cannot score it for you. That does not make it a","practice stub — it is a real task, just judged elsewhere.","",`1. \`icoa kaggle check ${s.comp_id}\` — checks the FORMAT of`," `submission.csv` (headers, row count, ids). Format errors are the most"," common way a correct answer still scores zero.","2. Watch the **val score** printed above — that is your real feedback loop"," during the sprint, and it needs no account anywhere.","3. Want the official number? Register at judge.nitro-ai.org and submit"," there. Otherwise compare against the official solution, or ask"," `ai4ioai` to review your approach."]:s.kaggle_url?["## Submit on Kaggle (manual, your own account)","",`1. Open ${s.kaggle_url}`,"2. **Submit Predictions** → upload `submission.csv`","3. Read your leaderboard score, come back, improve the MODEL cell, repeat."]:["## Submit on Kaggle (manual, your own account)","","_This dataset has no live Kaggle page linked yet._","The loop is identical: upload `submission.csv`, read the score, improve","the MODEL cell, repeat. Meanwhile the **val score** printed above is a","real signal you can work against with no account at all."];return[r(c),p,f,h,g,_,b,r(y.join("\n"))]}export function parseSubmitResponse(e){const t=e&&"object"==typeof e?e:{},s=t.data??t,a=s&&"object"==typeof s?s:{};return"number"==typeof a.score&&"string"==typeof a.metric?{ok:!0,score:a.score,metric:a.metric,rank:"number"==typeof a.rank_at_time?a.rank_at_time:null,fieldSize:"number"==typeof a.field_size?a.field_size:null,deduped:!0===a.deduped,boardEvent:"string"==typeof a.board_event?a.board_event:null,used:"number"==typeof a.submissions_used?a.submissions_used:null,cap:"number"==typeof a.submissions_cap?a.submissions_cap:null}:"string"==typeof a.reason&&""!==a.reason?{ok:!1,reason:a.reason,error:"string"==typeof a.error?a.error:void 0,problems:Array.isArray(a.problems)?a.problems.filter(e=>"string"==typeof e):void 0}:{ok:!1,reason:"network"}}export function validateSubmissionText(e,t){const s=[],a=e.trim().split(/\r?\n/),i=t.trim().split(/\r?\n/),n=a[0]??"",o=i[0]??"";n.trim()!==o.trim()&&s.push(`header mismatch: expected "${o}", got "${n}"`);const r=a.slice(1).filter(e=>""!==e.trim()),l=i.slice(1).filter(e=>""!==e.trim());r.length!==l.length&&s.push(`row count mismatch: expected ${l.length}, got ${r.length}`);const c=e=>new Set(e.map(e=>(e.split(",")[0]??"").trim())),p=c(r),u=c(l),d=[...u].filter(e=>!p.has(e)),m=[...p].filter(e=>!u.has(e));if(d.length>0||m.length>0){const e=[];d.length>0&&e.push(`${d.length} missing (e.g. ${d[0]})`),m.length>0&&e.push(`${m.length} unknown (e.g. ${m[0]})`),s.push(`id mismatch vs sample_submission: ${e.join(", ")}`)}for(let e=0;e<r.length;e++)if(r[e].split(",").some(e=>""===e.trim())){s.push(`empty cell in data row ${e+1}`);break}return{ok:0===s.length,problems:s}}export function buildMediaStarterCells(e,t){const s=e=>({kind:"markdown",source:e,result:null}),a=e=>({kind:"code",source:e.join("\n"),result:null}),i=t.media?.kind??"archive",n=t.title??"ICOA sprint task",o=[`# ${t.seq?`${t.seq} · `:""}${n}`,"",...t.topic_cn?[t.topic_cn,""]:[],"This task ships **data, not a baseline** — the CSV starter cannot help here",`because the signal lives in the ${i} files you just pulled.`,"","Run all cells: they inventory the pack and draw one picture of what is inside,","so you start from evidence instead of from the folder name. The last cell says","what you have to build — that part is the task.","","**Reading the files:** on your laptop the notebook env has numpy / scipy /","pandas / scikit-learn / torch / transformers / Pillow — but NOT librosa,","torchaudio or soundfile. The GPU contest box has librosa and torchaudio too."].join("\n"),r=a(["# inventory — what is actually in this pack (counts, sizes, a few real names)","import os","import numpy as np",`DIR = os.environ.get("ICOA_PACK_DIR") or r"${e}"`,"by_ext, total, sample = {}, 0, {}","for root, _dirs, files in os.walk(DIR):"," for f in files:",' ext = os.path.splitext(f)[1].lower() or "(no ext)"'," p = os.path.join(root, f)"," by_ext[ext] = by_ext.get(ext, 0) + 1"," total += os.path.getsize(p)"," sample.setdefault(ext, p)",'print(f"{DIR}\\n{sum(by_ext.values())} files · {total/1048576:.0f} MB")',"for ext, n in sorted(by_ext.items(), key=lambda kv: -kv[1])[:8]:",' print(f" {ext:>10} {n:>6} e.g. {os.path.relpath(sample[ext], DIR)}")',"# 🔴 a leftover .zip means the pack still needs unpacking before you can read it",'zips = [p for e, p in sample.items() if e == ".zip"]',"if zips:",' print("\\nNOTE: there is still a .zip in here — unpack it first:")',' print(f" import zipfile; zipfile.ZipFile(r\\"{zips[0]}\\").extractall(DIR)")']),l=["# EDA — one picture of what is inside. icoa ipynb saves it as a .png and","# opens your image viewer (`open` reopens it).","import matplotlib.pyplot as plt","try:"];"image"===i?l.push(" from PIL import Image"," imgs = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".jpg", ".jpeg", ".png"):'," imgs.append(os.path.join(root, f))"," if len(imgs) >= 9:"," break"," fig, axes = plt.subplots(3, 3, figsize=(7, 7))"," for ax, p in zip(axes.ravel(), imgs[:9]):"," ax.imshow(Image.open(p))"," ax.set_title(os.path.basename(p)[:18], fontsize=8)"," for ax in axes.ravel():",' ax.axis("off")',' plt.suptitle(f"{len(imgs)}+ images · first 9")'):"weights"===i?l.push(" import torch"," wfiles = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".pt", ".pth"):'," wfiles.append(os.path.join(root, f))"," fig, ax = plt.subplots(1, max(1, min(2, len(wfiles))), figsize=(10, 4), squeeze=False)"," for i, p in enumerate(wfiles[:2]):"," # weights_only=True: never unpickle arbitrary objects from a data file",' obj = torch.load(p, map_location="cpu", weights_only=True)',' t = obj if hasattr(obj, "flatten") else list(obj.values())[0]'," v = np.asarray(t, dtype=float).ravel()"," ax[0][i].hist(v, bins=60)",' ax[0][i].set_title(f"{os.path.basename(p)} · shape {tuple(np.shape(t))}", fontsize=9)',' print(os.path.basename(p), "shape", tuple(np.shape(t)))'):l.push(" import pandas as pd"," csvs = []"," for root, _d, files in os.walk(DIR):"," for f in files:",' if f.lower().endswith(".csv"):'," csvs.append(os.path.join(root, f))"," csvs.sort(key=os.path.getsize, reverse=True)"," df = pd.read_csv(csvs[0])"," print(os.path.basename(csvs[0]), df.shape)"," print(df.head(3).to_string(max_colwidth=24)[:600])"," # the last low-cardinality column is the usual label; if there is none,"," # plot the row-length spread so the picture still says something true"," cand = [c for c in df.columns if df[c].nunique() <= 30]"," fig, ax = plt.subplots(figsize=(8, 4))"," if cand:"," vc = df[cand[-1]].astype(str).value_counts().head(12)"," ax.bar(range(len(vc)), vc.values)"," ax.set_xticks(range(len(vc)))",' ax.set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' ax.set_title(f"{cand[-1]} — {df[cand[-1]].nunique()} values")'," else:"," ax.hist([len(str(x)) for x in df[df.columns[-1]]], bins=30)",' ax.set_title(f"length of {df.columns[-1]}")'),l.push(" plt.tight_layout()"," plt.show()","except Exception as e:"," # never stop the run on a plotting problem — `run all` halts on first error",' print("EDA skipped:", type(e).__name__, e)');const c=["## Your job","","There is no baseline to beat here — you build the first one. The shape of it:","","1. **turn each file into a row of numbers** — that is the whole trick","image"===i?" (`PIL.Image.open` → resize → `np.asarray` → flatten, or a pretrained encoder)":"weights"===i?" (`torch.load(..., weights_only=True)` → the tensors themselves ARE the features)":" (read the CSVs / unpack the archive, then vectorise the text columns)","2. keep a held-out fifth of the data and print a val score every time","3. write your predictions in exactly the format the task page asks for","","`add` makes a new code cell · `add md` a note cell · `run all` runs everything ·","a cell that raises offers `ai N`, which sends that cell and its traceback to the AI."].join("\n");return[s(o),r,a(l),s(c)]}
|
|
1
|
+
import{sprintTaskForComp as e}from"./sprint-meta.js";export function parseKaggleTaskList(e){const t=e??{},s=(t.data??t).tasks;if(!Array.isArray(s))return[];const i=[];for(const e of s){if(!e||"object"!=typeof e)continue;const t=e;"string"==typeof t.comp_id&&""!==t.comp_id&&i.push({comp_id:t.comp_id,title:"string"==typeof t.title?t.title:t.comp_id,metric:"string"==typeof t.metric?t.metric:"accuracy",description:"string"==typeof t.description&&""!==t.description?t.description:void 0,kaggle_url:"string"==typeof t.kaggle_url?t.kaggle_url:null,licence:"string"==typeof t.licence?t.licence:void 0,attribution:"string"==typeof t.attribution?t.attribution:void 0,data_base:"string"==typeof t.data_base&&""!==t.data_base?t.data_base:null,id_col:"string"==typeof t.id_col?t.id_col:void 0,target_col:"string"==typeof t.target_col?t.target_col:void 0,n_train:"number"==typeof t.n_train?t.n_train:void 0,n_test:"number"==typeof t.n_test?t.n_test:void 0,judge:"string"==typeof t.judge&&""!==t.judge?t.judge:void 0,board_event:"string"==typeof t.board_event&&""!==t.board_event?t.board_event:null,needs_gpu:!0===t.needs_gpu})}return i}export function dataFileUrls(e){const t=e.data_base;if("string"!=typeof t||""===t)return null;const s=t.replace(/\/$/,"");return{train:`${s}/train.csv`,test:`${s}/test.csv`,sample_submission:`${s}/sample_submission.csv`}}export function buildStarterCells(t,s){const i=s.id_col??"id",a=s.target_col??"target";var n;n=s.metric,/rmse|mse|mae|error/i.test(n);const o="icoa-holdout"===s.judge,r=e=>({kind:"markdown",source:e,result:null}),l=e=>({kind:"code",source:e.join("\n"),result:null}),c=[`# ${s.title}`,"",...s.description?[s.description,""]:[],`Kaggle-style practice — metric: **${s.metric}**.`,"","- `train.csv` — labelled training data",`- \`test.csv\` — rows to predict (no \`${a}\` column)`,"- `sample_submission.csv` — the exact format Kaggle expects","","Run all cells top to bottom: a baseline model trains on your CPU and","`submission.csv` appears next to the data. Then improve the model cell",...o?["and re-run — `icoa kaggle submit` scores it and ranks you on the board."]:["and re-run — your score is whatever Kaggle says it is."],"","**AI help:** split your terminal (`Ctrl-b %`), run `icoa` in the right pane","and type `ai4ioai` for a live Gemma 3n E4B. Ask it for feature ideas or bug fixes —","then verify every suggestion with the val score. A tip that lowers it, drop it.",...s.licence?["",`**Data licence:** ${s.licence}`]:[],...s.attribution?[`**Attribution:** ${s.attribution}`]:[]].join("\n"),p=l(["# setup — load the data (leave as is)","import os","import numpy as np","import pandas as pd","from pandas.api.types import is_numeric_dtype","from sklearn.feature_extraction.text import TfidfVectorizer","from sklearn.model_selection import train_test_split","from sklearn.metrics import accuracy_score, mean_squared_error","","# same code everywhere: in a GPU session these env vars point at the","# box's own dataset copy + workspace — locally they are unset.",`DIR = os.environ.get("ICOA_KAGGLE_DATA") or r"${t}"`,'OUT = os.environ.get("ICOA_KAGGLE_OUT") or DIR',...s.data_base?[`DATA_BASE = "${s.data_base.replace(/\/$/,"")}"`,'if not os.path.exists(DIR + "/train.csv"):'," import urllib.request"," os.makedirs(DIR, exist_ok=True)",' for f in ("train.csv", "test.csv", "sample_submission.csv"):',' urllib.request.urlretrieve(DATA_BASE + "/" + f, DIR + "/" + f)']:[],'train = pd.read_csv(DIR + "/train.csv")','test = pd.read_csv(DIR + "/test.csv")','sub_fmt = pd.read_csv(DIR + "/sample_submission.csv")',"","# 🔴 The submission column names are NOT always the training column names.","# On tasks imported from other platforms the two genuinely differ: train.csv",'# labels the answer "class" or "result" while sample_submission.csv calls it','# "answer". Hard-coding the submission name here used to raise KeyError on the',"# very first cell. So trust the DATA: whatever column train has and test lacks","# is the thing to predict.",`SUB_ID, SUB_TARGET = "${i}", "${a}" # names the submission file wants`,"only_train = [c for c in train.columns if c not in test.columns]","TARGET = only_train[-1] if only_train else SUB_TARGET","shared = [c for c in train.columns if c in test.columns]","# Only treat a column as the row id when the submission actually names it AND",'# both files carry it. Falling back to "first shared column" silently spent a',"# real feature as an id on tasks whose id lives only in test.csv.","ID = SUB_ID if SUB_ID in shared else None",'assert TARGET in train.columns, f"cannot find the label column in {list(train.columns)}"',"","# Regression or classification? Decided from the labels, not from metadata —","# several tasks ship continuous labels, and handing those to a classifier",'# raises "Unknown label type: continuous".',"REG = is_numeric_dtype(train[TARGET]) and train[TARGET].nunique() > 20",'print(f"train {train.shape} · test {test.shape} · predicting {TARGET!r} "'," f\"({'regression' if REG else 'classification'})\")","# 前几行 —— 看一眼真实数据长什么样,比看列名有用得多","print(train.head(3).to_string(max_colwidth=24)[:700])"]),u=e(s.comp_id),d=u?.media,m="audio"===d?.kind?"scipy.io.wavfile.read(path) -> numpy, then scipy.signal for a spectrogram":"image"===d?.kind?"PIL.Image.open(path) -> numpy":"weights"===d?.kind?"torch.load(path, weights_only=True) — the model itself is the input here":"unpack the archive inside the pack, then read what is in it",h=l(["# FEATURES — turn raw columns into numbers a model can use (improve THIS cell).","# Numbers: fill gaps with the median. Category (<= 20 distinct): one-hot.","# Free text: TF-IDF — on a text task the sentences ARE the signal, and dropping",'# them as "high-cardinality" leaves nothing to train on (that used to crash the','# whole notebook with "No objects to concatenate").',"# only columns present in BOTH files — a train-only column cannot be a feature","# at prediction time, and indexing test with it raises KeyError.","feat = [c for c in shared if c not in (ID, TARGET)]","Xtr_raw, Xte_raw = train[feat].copy(), test[feat].copy()","num = [c for c in feat if is_numeric_dtype(Xtr_raw[c])]","cat = [c for c in feat if c not in num and Xtr_raw[c].nunique() <= 20]","# Everything else is vectorised as text. Requiring a long average length here","# used to throw away high-cardinality SHORT strings — ingredient names, single","# words, category codes — which on several tasks are the entire signal, leaving","# the notebook with zero features.","txt = [c for c in feat if c not in num and c not in cat]","for c in num:"," med = Xtr_raw[c].median()"," Xtr_raw[c] = Xtr_raw[c].fillna(med)"," Xte_raw[c] = Xte_raw[c].fillna(med)","blocks_tr, blocks_te = [], []","if num or cat:"," d_tr = pd.get_dummies(Xtr_raw[num + cat], columns=cat, dummy_na=True)"," d_te = pd.get_dummies(Xte_raw[num + cat], columns=cat, dummy_na=True)",' d_tr, d_te = d_tr.align(d_te, join="left", axis=1, fill_value=0)'," blocks_tr.append(d_tr.to_numpy(dtype=float))"," blocks_te.append(d_te.to_numpy(dtype=float))","for c in txt:"," # fillna BEFORE astype(str): a real NaN reaching the vectoriser raises",' # "np.nan is an invalid document", and astype alone does not catch it.',' tr_txt = Xtr_raw[c].fillna("").astype(str)',' te_txt = Xte_raw[c].fillna("").astype(str)'," # min_df=1: on a short-token column (one ingredient per row) min_df=2 can"," # filter the vocabulary down to nothing."," long_col = tr_txt.str.len().mean() >= 12"," # Try the good settings, then a permissive fallback, then give up on THIS"," # column only. A column whose vocabulary prunes to nothing used to abort the",' # whole run with "After pruning, no terms remain".',' for kw in ({"min_df": 2 if long_col else 1,',' "analyzer": "word" if long_col else "char_wb"},',' {"min_df": 1, "analyzer": "char_wb"}):'," try:"," vec = TfidfVectorizer(max_features=3000, ngram_range=(1, 2), **kw)"," b_tr = vec.fit_transform(tr_txt).toarray()"," blocks_tr.append(b_tr)"," blocks_te.append(vec.transform(te_txt).toarray())"," break"," except ValueError as e:"," last = e"," else:",' print(f" skipped text column {c!r}: {last}")',"if not blocks_tr:"," raise SystemExit(",' f"No usable feature columns. The shared columns are {feat} — none of them "',' f"is numeric, categorical or text.\\n"',...d?[` "The signal for this task is in ${d.kind} files, not in the CSVs.\\n"`,` " 1. fetch them (${d.mb}MB): teleload ${d.pack}\\n"`,...d.glob?[` " files land at <pack dir>/${d.glob}\\n"`]:[],` " 2. replace this cell: ${m}\\n"`,' " build one feature row per file, then keep the rest of the notebook.\\n"']:[' f"Two kinds of task land here, and both are real work rather than a bug:\\n"',' f" 1. audio/image — train.csv only lists sample ids and labels and the "',' f"signal is in media files. Read them with scipy.io.wavfile (audio) or "',' f"PIL (images) and turn each file into a feature row.\\n"',' f" 2. train.csv and test.csv describe different things (a retrieval or "',' f"matching task). Read both files, work out what links a test row to a "',' f"train row, and build that mapping yourself.\\n"'],...d?[' f"columns — train={list(train.columns)} test={list(test.columns)}"']:[' f"Either way: replace this cell. train={list(train.columns)} "',' f"test={list(test.columns)}"']," )","X = np.hstack(blocks_tr)","X_test = np.hstack(blocks_te)","y = train[TARGET]","X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=0)",'print(f"{X.shape[1]} model-ready features "',' f"({len(num)} numeric, {len(cat)} one-hot, {len(txt)} TF-IDF text)")']),f=l(["# EDA — look at the labels before you model. Produces ONE figure:","# left = what you are predicting (class balance / value spread)","# right = what the FEATURES cell will have to work with","# icoa ipynb saves it as a .png and opens your image viewer (type `open` to reopen).","import matplotlib.pyplot as plt","try:"," fig, ax = plt.subplots(1, 2, figsize=(11, 4))"," if REG:",' vals = pd.to_numeric(train[TARGET], errors="coerce").dropna()'," ax[0].hist(vals, bins=30)",' ax[0].set_title(f"{TARGET} — {len(vals)} values, mean {vals.mean():.3g}")'," else:"," vc = train[TARGET].astype(str).value_counts().head(12)"," ax[0].bar(range(len(vc)), vc.values)"," ax[0].set_xticks(range(len(vc)))",' ax[0].set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' # An imbalanced target is the single most common reason a "good"'," # accuracy means nothing — show the majority share right in the title."," top = vc.iloc[0] / len(train)",' ax[0].set_title(f"{TARGET} — {train[TARGET].nunique()} classes · biggest {top:.0%}")',' kinds = {"numeric": 0, "category": 0, "text": 0}'," for c in [c for c in shared if c not in (ID, TARGET)]:"," if is_numeric_dtype(train[c]):",' kinds["numeric"] += 1'," elif train[c].nunique() <= 20:",' kinds["category"] += 1'," else:",' kinds["text"] += 1'," miss = train.isna().mean()"," miss = miss[miss > 0].sort_values(ascending=False).head(10)"," if len(miss) > 0:"," ax[1].barh(range(len(miss)), miss.values)"," ax[1].set_yticks(range(len(miss)))"," ax[1].set_yticklabels([s[:16] for s in miss.index])",' ax[1].set_title("missing rate (top columns)")'," else:"," ax[1].bar(list(kinds), list(kinds.values()))",' ax[1].set_title(f"{sum(kinds.values())} usable columns · no missing values")'," plt.tight_layout()"," plt.show()",' print("columns by kind:", kinds)',"except Exception as e:"," # EDA is never allowed to stop the run — `run all` halts on the first"," # error, and a plotting hiccup must not cost the student their baseline.",' print("EDA skipped:", type(e).__name__, e)']),g=l(["# MODEL — a working baseline. Improve THIS cell to climb the board.","from sklearn.linear_model import LogisticRegression, Ridge","# REG was decided from the labels back in setup.","model = Ridge() if REG else LogisticRegression(max_iter=2000)"]),_=l(["# validate — quick local score on a held-out fifth of train","model.fit(X_tr, y_tr)","p_val = model.predict(X_val)","if REG:",' print("val RMSE:", round(mean_squared_error(y_val, p_val) ** 0.5, 4))',"else:"," acc = accuracy_score(y_val, p_val)",' print("val accuracy:", round(acc, 4))'," if acc < 0.3:"," # A near-zero baseline is the expected result on several of these tasks,"," # not a broken notebook — say so, or the student reads it as a bug.",' print(" A score this low usually means the linear baseline cannot "',' "represent the pattern at all (combination rules, sequences, "',' "spelling). That IS the task: build features that expose it, "',' "or switch to a model that can. The chain itself works — "',' "submission.csv is written either way.")']),b=l(["# submission — retrain on ALL data, write submission.csv (Kaggle format)","model.fit(X, y)","pred = model.predict(X_test)",'out = OUT + "/submission.csv"',"# Copy the layout of sample_submission.csv and swap in the predictions, rather","# than rebuilding the file from column names. Some tasks want more than two","# columns (e.g. subtaskID,datapointID,answer) and a hand-built 2-column frame","# is rejected by the grader even when every prediction is right.","if len(sub_fmt) == len(pred):"," sub = sub_fmt.copy()"," sub[sub.columns[-1]] = pred","else:"," keys = {}"," if ID is not None and ID in test.columns:"," keys[SUB_ID] = test[ID]"," keys[SUB_TARGET] = pred"," sub = pd.DataFrame(keys)",' print(f"NOTE: sample_submission has {len(sub_fmt)} rows but test has "',' f"{len(pred)} — built the file from column names instead; "',' f"check it against the task page before submitting.")',"sub.to_csv(out, index=False)",'print("wrote", out, "·", list(sub.columns))',o?`print("score it: icoa kaggle submit ${s.comp_id}")`:'print("format check: icoa kaggle check")']),y=o?["## Submit to ICOA (scored on the server)","",`1. In the ICOA CLI run \`icoa kaggle submit ${s.comp_id}\``,"2. The server scores `submission.csv` against the secret test labels"," and your rank lands on the live board (`icoa board`).","3. Improve the MODEL cell, re-run, submit again — best score counts."]:"nitro"===s.judge?["## Where this one gets scored","","This task comes from **judge.nitro-ai.org** and the answer key stays on","their server, so ICOA cannot score it for you. That does not make it a","practice stub — it is a real task, just judged elsewhere.","",`1. \`icoa kaggle check ${s.comp_id}\` — checks the FORMAT of`," `submission.csv` (headers, row count, ids). Format errors are the most"," common way a correct answer still scores zero.","2. Watch the **val score** printed above — that is your real feedback loop"," during the sprint, and it needs no account anywhere.","3. Want the official number? Register at judge.nitro-ai.org and submit"," there. Otherwise compare against the official solution, or ask"," `ai4ioai` to review your approach."]:s.kaggle_url?["## Submit on Kaggle (manual, your own account)","",`1. Open ${s.kaggle_url}`,"2. **Submit Predictions** → upload `submission.csv`","3. Read your leaderboard score, come back, improve the MODEL cell, repeat."]:["## Submit on Kaggle (manual, your own account)","","_This dataset has no live Kaggle page linked yet._","The loop is identical: upload `submission.csv`, read the score, improve","the MODEL cell, repeat. Meanwhile the **val score** printed above is a","real signal you can work against with no account at all."];return[r(c),p,f,h,g,_,b,r(y.join("\n"))]}export function parseSubmitResponse(e){const t=e&&"object"==typeof e?e:{},s=t.data??t,i=s&&"object"==typeof s?s:{};return"number"==typeof i.score&&"string"==typeof i.metric?{ok:!0,score:i.score,metric:i.metric,rank:"number"==typeof i.rank_at_time?i.rank_at_time:null,fieldSize:"number"==typeof i.field_size?i.field_size:null,deduped:!0===i.deduped,sameCode:!0===i.same_code,submissionId:"string"==typeof i.submission_id?i.submission_id:null,leaderboard:"string"==typeof i.leaderboard?i.leaderboard:null,boardEvent:"string"==typeof i.board_event?i.board_event:null,used:"number"==typeof i.submissions_used?i.submissions_used:null,cap:"number"==typeof i.submissions_cap?i.submissions_cap:null}:"string"==typeof i.reason&&""!==i.reason?{ok:!1,reason:i.reason,error:"string"==typeof i.error?i.error:void 0,problems:Array.isArray(i.problems)?i.problems.filter(e=>"string"==typeof e):void 0}:{ok:!1,reason:"network"}}export function validateSubmissionText(e,t){const s=[],i=e.trim().split(/\r?\n/),a=t.trim().split(/\r?\n/),n=i[0]??"",o=a[0]??"";n.trim()!==o.trim()&&s.push(`header mismatch: expected "${o}", got "${n}"`);const r=i.slice(1).filter(e=>""!==e.trim()),l=a.slice(1).filter(e=>""!==e.trim());r.length!==l.length&&s.push(`row count mismatch: expected ${l.length}, got ${r.length}`);const c=e=>new Set(e.map(e=>(e.split(",")[0]??"").trim())),p=c(r),u=c(l),d=[...u].filter(e=>!p.has(e)),m=[...p].filter(e=>!u.has(e));if(d.length>0||m.length>0){const e=[];d.length>0&&e.push(`${d.length} missing (e.g. ${d[0]})`),m.length>0&&e.push(`${m.length} unknown (e.g. ${m[0]})`),s.push(`id mismatch vs sample_submission: ${e.join(", ")}`)}for(let e=0;e<r.length;e++)if(r[e].split(",").some(e=>""===e.trim())){s.push(`empty cell in data row ${e+1}`);break}return{ok:0===s.length,problems:s}}export function buildMediaStarterCells(e,t){const s=e=>({kind:"markdown",source:e,result:null}),i=e=>({kind:"code",source:e.join("\n"),result:null}),a=t.media?.kind??"archive",n=t.title??"ICOA sprint task",o=[`# ${t.seq?`${t.seq} · `:""}${n}`,"",...t.topic_cn?[t.topic_cn,""]:[],"This task ships **data, not a baseline** — the CSV starter cannot help here",`because the signal lives in the ${a} files you just pulled.`,"","Run all cells: they inventory the pack and draw one picture of what is inside,","so you start from evidence instead of from the folder name. The last cell says","what you have to build — that part is the task.","","**Reading the files:** on your laptop the notebook env has numpy / scipy /","pandas / scikit-learn / torch / transformers / Pillow — but NOT librosa,","torchaudio or soundfile. The GPU contest box has librosa and torchaudio too."].join("\n"),r=i(["# inventory — what is actually in this pack (counts, sizes, a few real names)","import os","import numpy as np",`DIR = os.environ.get("ICOA_PACK_DIR") or r"${e}"`,"by_ext, total, sample = {}, 0, {}","for root, _dirs, files in os.walk(DIR):"," for f in files:",' ext = os.path.splitext(f)[1].lower() or "(no ext)"'," p = os.path.join(root, f)"," by_ext[ext] = by_ext.get(ext, 0) + 1"," total += os.path.getsize(p)"," sample.setdefault(ext, p)",'print(f"{DIR}\\n{sum(by_ext.values())} files · {total/1048576:.0f} MB")',"for ext, n in sorted(by_ext.items(), key=lambda kv: -kv[1])[:8]:",' print(f" {ext:>10} {n:>6} e.g. {os.path.relpath(sample[ext], DIR)}")',"# 🔴 a leftover .zip means the pack still needs unpacking before you can read it",'zips = [p for e, p in sample.items() if e == ".zip"]',"if zips:",' print("\\nNOTE: there is still a .zip in here — unpack it first:")',' print(f" import zipfile; zipfile.ZipFile(r\\"{zips[0]}\\").extractall(DIR)")']),l=["# EDA — one picture of what is inside. icoa ipynb saves it as a .png and","# opens your image viewer (`open` reopens it).","import matplotlib.pyplot as plt","try:"];"image"===a?l.push(" from PIL import Image"," imgs = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".jpg", ".jpeg", ".png"):'," imgs.append(os.path.join(root, f))"," if len(imgs) >= 9:"," break"," fig, axes = plt.subplots(3, 3, figsize=(7, 7))"," for ax, p in zip(axes.ravel(), imgs[:9]):"," ax.imshow(Image.open(p))"," ax.set_title(os.path.basename(p)[:18], fontsize=8)"," for ax in axes.ravel():",' ax.axis("off")',' plt.suptitle(f"{len(imgs)}+ images · first 9")'):"weights"===a?l.push(" import torch"," wfiles = []"," for root, _d, files in os.walk(DIR):"," for f in sorted(files):",' if os.path.splitext(f)[1].lower() in (".pt", ".pth"):'," wfiles.append(os.path.join(root, f))"," fig, ax = plt.subplots(1, max(1, min(2, len(wfiles))), figsize=(10, 4), squeeze=False)"," for i, p in enumerate(wfiles[:2]):"," # weights_only=True: never unpickle arbitrary objects from a data file",' obj = torch.load(p, map_location="cpu", weights_only=True)',' t = obj if hasattr(obj, "flatten") else list(obj.values())[0]'," v = np.asarray(t, dtype=float).ravel()"," ax[0][i].hist(v, bins=60)",' ax[0][i].set_title(f"{os.path.basename(p)} · shape {tuple(np.shape(t))}", fontsize=9)',' print(os.path.basename(p), "shape", tuple(np.shape(t)))'):l.push(" import pandas as pd"," csvs = []"," for root, _d, files in os.walk(DIR):"," for f in files:",' if f.lower().endswith(".csv"):'," csvs.append(os.path.join(root, f))"," csvs.sort(key=os.path.getsize, reverse=True)"," df = pd.read_csv(csvs[0])"," print(os.path.basename(csvs[0]), df.shape)"," print(df.head(3).to_string(max_colwidth=24)[:600])"," # the last low-cardinality column is the usual label; if there is none,"," # plot the row-length spread so the picture still says something true"," cand = [c for c in df.columns if df[c].nunique() <= 30]"," fig, ax = plt.subplots(figsize=(8, 4))"," if cand:"," vc = df[cand[-1]].astype(str).value_counts().head(12)"," ax.bar(range(len(vc)), vc.values)"," ax.set_xticks(range(len(vc)))",' ax.set_xticklabels([s[:14] for s in vc.index], rotation=45, ha="right")',' ax.set_title(f"{cand[-1]} — {df[cand[-1]].nunique()} values")'," else:"," ax.hist([len(str(x)) for x in df[df.columns[-1]]], bins=30)",' ax.set_title(f"length of {df.columns[-1]}")'),l.push(" plt.tight_layout()"," plt.show()","except Exception as e:"," # never stop the run on a plotting problem — `run all` halts on first error",' print("EDA skipped:", type(e).__name__, e)');const c=["## Your job","","There is no baseline to beat here — you build the first one. The shape of it:","","1. **turn each file into a row of numbers** — that is the whole trick","image"===a?" (`PIL.Image.open` → resize → `np.asarray` → flatten, or a pretrained encoder)":"weights"===a?" (`torch.load(..., weights_only=True)` → the tensors themselves ARE the features)":" (read the CSVs / unpack the archive, then vectorise the text columns)","2. keep a held-out fifth of the data and print a val score every time","3. write your predictions in exactly the format the task page asks for","","`add` makes a new code cell · `add md` a note cell · `run all` runs everything ·","a cell that raises offers `ai N`, which sends that cell and its traceback to the AI."].join("\n");return[s(o),r,i(l),s(c)]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=new Set(["ready","stream","result","display","error","done","file","file_error","fatal"]);export function parseKernelEvent(t){const a=t.trim();if(!a)return null;let r;try{r=JSON.parse(a)}catch{return null}if(!r||"object"!=typeof r)return null;const n=r.type;return"string"==typeof n&&e.has(n)?r:null}export function foldCellOutputs(e){const t=[];let a=!0,r=null;for(const n of e)switch(n.type){case"stream":{const e=t[t.length-1];e&&"stream"===e.kind&&e.name===n.name?e.text+=n.text:t.push({kind:"stream",name:n.name,text:n.text});break}case"result":t.push({kind:"result",text:n.text,...n.data?{data:n.data}:{}});break;case"display":t.push({kind:"display",data:n.data});break;case"error":a=!1,t.push({kind:"error",ename:n.ename,evalue:n.evalue,traceback:n.traceback});break;case"done":r=n.count;break;case"file":case"file_error":default:break;case"fatal":a=!1,t.push({kind:"error",ename:"KernelError",evalue:n.message,traceback:[n.message]})}return{outputs:t,ok:a,execCount:r}}
|
|
1
|
+
const e=new Set(["ready","stream","result","display","error","done","file","file_error","put_ok","fatal"]);export function parseKernelEvent(t){const a=t.trim();if(!a)return null;let r;try{r=JSON.parse(a)}catch{return null}if(!r||"object"!=typeof r)return null;const n=r.type;return"string"==typeof n&&e.has(n)?r:null}export function foldCellOutputs(e){const t=[];let a=!0,r=null;for(const n of e)switch(n.type){case"stream":{const e=t[t.length-1];e&&"stream"===e.kind&&e.name===n.name?e.text+=n.text:t.push({kind:"stream",name:n.name,text:n.text});break}case"result":t.push({kind:"result",text:n.text,...n.data?{data:n.data}:{}});break;case"display":t.push({kind:"display",data:n.data});break;case"error":a=!1,t.push({kind:"error",ename:n.ename,evalue:n.evalue,traceback:n.traceback});break;case"done":r=n.count;break;case"file":case"file_error":case"put_ok":default:break;case"fatal":a=!1,t.push({kind:"error",ename:"KernelError",evalue:n.message,traceback:[n.message]})}return{outputs:t,ok:a,execCount:r}}
|
package/dist/lib/kernel.js
CHANGED
|
@@ -7,14 +7,25 @@ def emit(o):
|
|
|
7
7
|
def main():
|
|
8
8
|
# Self-contained kernelspec INSIDE the venv (found via sys.prefix; isolated,
|
|
9
9
|
# wiped with the venv). Points the kernel at this very interpreter.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
# On a shared box the venv is root-owned and the seat user cannot write into
|
|
11
|
+
# sys.prefix (committee sandbox, 2026-10-23) — fall back to the user's own
|
|
12
|
+
# Jupyter data dir, which jupyter_client searches by default.
|
|
13
|
+
spec = {"argv": [sys.executable, "-m", "ipykernel_launcher", "-f", "{connection_file}"],
|
|
14
|
+
"display_name": "ICOA aienv", "language": "python"}
|
|
15
|
+
candidates = [os.path.join(sys.prefix, "share", "jupyter", "kernels", "icoa-aienv"),
|
|
16
|
+
os.path.join(os.path.expanduser("~"), ".local", "share", "jupyter", "kernels", "icoa-aienv")]
|
|
17
|
+
err = None
|
|
18
|
+
for specdir in candidates:
|
|
19
|
+
try:
|
|
20
|
+
os.makedirs(specdir, exist_ok=True)
|
|
21
|
+
with open(os.path.join(specdir, "kernel.json"), "w") as f:
|
|
22
|
+
json.dump(spec, f)
|
|
23
|
+
err = None
|
|
24
|
+
break
|
|
25
|
+
except Exception as e:
|
|
26
|
+
err = e
|
|
27
|
+
if err is not None:
|
|
28
|
+
emit({"type": "fatal", "message": "could not write kernelspec: %s" % err}); os._exit(1)
|
|
18
29
|
|
|
19
30
|
from jupyter_client.manager import KernelManager
|
|
20
31
|
transport = "ipc" if os.name == "posix" else "tcp"
|
|
@@ -50,6 +61,21 @@ def main():
|
|
|
50
61
|
except Exception as e:
|
|
51
62
|
emit({"type": "file_error", "id": fid, "message": str(e)})
|
|
52
63
|
continue
|
|
64
|
+
if req.get("cmd") == "put":
|
|
65
|
+
# Upload over the same pipe (committee sandbox upload verb, same-folder
|
|
66
|
+
# sync): small files only, written under the kernel's cwd unless absolute.
|
|
67
|
+
pid = req.get("id"); ppath = req.get("path", ""); pdata = req.get("data", "")
|
|
68
|
+
try:
|
|
69
|
+
raw = base64.b64decode(pdata)
|
|
70
|
+
d = os.path.dirname(ppath)
|
|
71
|
+
if d:
|
|
72
|
+
os.makedirs(d, exist_ok=True)
|
|
73
|
+
with open(ppath, "wb") as fh:
|
|
74
|
+
fh.write(raw)
|
|
75
|
+
emit({"type": "put_ok", "id": pid, "size": len(raw)})
|
|
76
|
+
except Exception as e:
|
|
77
|
+
emit({"type": "file_error", "id": pid, "message": str(e)})
|
|
78
|
+
continue
|
|
53
79
|
cid = req.get("id"); code = req.get("code", "")
|
|
54
80
|
mid = kc.execute(code)
|
|
55
81
|
count = None
|
|
@@ -92,4 +118,4 @@ def main():
|
|
|
92
118
|
os._exit(0)
|
|
93
119
|
|
|
94
120
|
main()
|
|
95
|
-
`;function s(e){return`'${e.replace(/'/g,"'\\''")}'`}export function buildRemoteBridgeCommand(e,t={},i){const r=Buffer.from(KERNEL_BRIDGE_PY,"utf-8").toString("base64"),n=["exec","env",...Object.entries(t).map(([e,t])=>`${e}=${s(t)}`),s(e),"-c",s(`exec(__import__("base64").b64decode("${r}").decode())`)].join(" ");return i?`mkdir -p ${s(i)} && cd ${s(i)} && ${n}`:n}export class NotebookKernel{proc=null;buf="";errTail="";nextId=1;ready=!1;pending=null;pendingFetch=null;onReady=null;onExit=null;opts;bridgePath;constructor(e){this.opts=e,this.bridgePath=e.venvRoot?i(e.venvRoot,"icoa-kernel-bridge.py"):null}start(){let i;if(this.buf="",this.errTail="",this.ready=!1,this.pending=null,this.pendingFetch=null,this.nextId=1,this.opts.spawnSpec)i=e(this.opts.spawnSpec.command,this.opts.spawnSpec.args,{stdio:["pipe","pipe","pipe"]});else{if(!this.opts.venvPython||!this.bridgePath)return Promise.reject(new Error("kernel needs venvPython+venvRoot or a spawnSpec"));t(this.bridgePath,KERNEL_BRIDGE_PY),i=e(this.opts.venvPython,[this.bridgePath],{stdio:["pipe","pipe","pipe"]})}this.proc=i,i.stdout.setEncoding("utf-8"),i.stdout.on("data",e=>this.onStdout(e)),i.stderr.setEncoding("utf-8"),i.stderr.on("data",e=>{this.errTail=(this.errTail+e).slice(-600)}),i.on("exit",()=>{this.ready=!1,this.onExit?.()}),i.on("error",()=>{this.ready=!1,this.proc=null,this.onExit?.()});const
|
|
121
|
+
`;function s(e){return`'${e.replace(/'/g,"'\\''")}'`}export function buildRemoteBridgeCommand(e,t={},i){const r=Buffer.from(KERNEL_BRIDGE_PY,"utf-8").toString("base64"),n=["exec","env",...Object.entries(t).map(([e,t])=>`${e}=${s(t)}`),s(e),"-c",s(`exec(__import__("base64").b64decode("${r}").decode())`)].join(" ");return i?`mkdir -p ${s(i)} && cd ${s(i)} && ${n}`:n}export class NotebookKernel{proc=null;buf="";errTail="";nextId=1;ready=!1;pending=null;pendingFetch=null;onReady=null;onExit=null;opts;bridgePath;constructor(e){this.opts=e,this.bridgePath=e.venvRoot?i(e.venvRoot,"icoa-kernel-bridge.py"):null}start(){let i;if(this.shuttingDown=!1,this.buf="",this.errTail="",this.ready=!1,this.pending=null,this.pendingFetch=null,this.nextId=1,this.opts.spawnSpec)i=e(this.opts.spawnSpec.command,this.opts.spawnSpec.args,{stdio:["pipe","pipe","pipe"]});else{if(!this.opts.venvPython||!this.bridgePath)return Promise.reject(new Error("kernel needs venvPython+venvRoot or a spawnSpec"));t(this.bridgePath,KERNEL_BRIDGE_PY),i=e(this.opts.venvPython,[this.bridgePath],{stdio:["pipe","pipe","pipe"]})}this.proc=i,i.stdout.setEncoding("utf-8"),i.stdout.on("data",e=>this.onStdout(e)),i.stderr.setEncoding("utf-8"),i.stderr.on("data",e=>{this.errTail=(this.errTail+e).slice(-600)}),i.on("exit",()=>{const e=this.ready;this.ready=!1,this.onExit?.();const t=this.pending;t&&(this.pending=null,t.resolve(r(t.events.concat([{type:"fatal",message:"kernel connection lost"},{type:"done",id:t.id,count:null}]))));const i=this.pendingFetch;i&&(this.pendingFetch=null,i.reject(new Error("kernel connection lost"))),e&&!this.shuttingDown&&this.onUnexpectedExit?.()}),i.on("error",()=>{this.ready=!1,this.proc=null,this.onExit?.()});const n=this.opts.readyTimeoutMs??6e4;return new Promise((e,t)=>{const i=e=>new Error(this.errTail?`${e} — ${this.errTail.trim()}`:e),r=setTimeout(()=>t(i("kernel did not become ready in time")),n);this.onReady=()=>{clearTimeout(r),e()},this.onExit=()=>{clearTimeout(r),this.ready||t(i("kernel process exited before ready"))}})}execute(e){if(!this.proc||!this.ready)return Promise.reject(new Error("kernel not ready"));if(this.pending)return Promise.reject(new Error("a cell is already executing"));const t=this.nextId++;return new Promise(i=>{this.pending={id:t,events:[],resolve:i},this.proc?.stdin.write(`${JSON.stringify({id:t,code:e})}\n`)})}fetchFile(e){if(!this.proc||!this.ready)return Promise.reject(new Error("kernel not ready"));if(this.pendingFetch)return Promise.reject(new Error("a fetch is already in flight"));const t=this.nextId++;return new Promise((i,r)=>{this.pendingFetch={id:t,resolve:i,reject:r},this.proc?.stdin.write(`${JSON.stringify({cmd:"fetch",id:t,path:e})}\n`)})}putFile(e,t){if(!this.proc||!this.ready)return Promise.reject(new Error("kernel not ready"));if(this.pendingFetch)return Promise.reject(new Error("a transfer is already in flight"));if(t.length>20971520)return Promise.reject(new Error("file exceeds the 20MB transfer cap"));const i=this.nextId++;return new Promise((r,n)=>{this.pendingFetch={id:i,resolve:e=>r(e.length),reject:n},this.proc?.stdin.write(`${JSON.stringify({cmd:"put",id:i,path:e,data:t.toString("base64")})}\n`)})}onUnexpectedExit=null;shuttingDown=!1;shutdown(){this.shuttingDown=!0;const e=this.proc;return e?new Promise(t=>{this.onExit=()=>t();try{e.stdin.write(`${JSON.stringify({cmd:"shutdown"})}\n`)}catch{e.kill()}setTimeout(()=>{this.proc&&(this.proc.kill(),t())},5e3)}):Promise.resolve()}onStdout(e){this.buf+=e;let t=this.buf.indexOf("\n");for(;-1!==t;){const e=this.buf.slice(0,t);this.buf=this.buf.slice(t+1),this.handleEvent(n(e)),t=this.buf.indexOf("\n")}}handleEvent(e){if(!e)return;if("ready"===e.type)return this.ready=!0,void this.onReady?.();if("file"===e.type||"file_error"===e.type||"put_ok"===e.type){const t=this.pendingFetch;if(!t||e.id!==t.id)return;return this.pendingFetch=null,void("file"===e.type?t.resolve(Buffer.from(e.data,"base64")):"put_ok"===e.type?t.resolve(Buffer.alloc(e.size)):t.reject(new Error(e.message)))}const t=this.pending;if(t){if("done"===e.type){if(e.id!==t.id)return;const i=r(t.events.concat(e));return this.pending=null,void t.resolve(i)}t.events.push(e)}}}
|
package/dist/lib/notebook-doc.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function parseIpynb(t){let e;try{e=JSON.parse(t)}catch{throw new Error("not valid JSON")}const
|
|
1
|
+
export function parseIpynb(t){let e;try{e=JSON.parse(t)}catch{throw new Error("not valid JSON")}const n=e?.cells;if(!Array.isArray(n))throw new Error("not a Jupyter notebook (no cells array)");const r=[];for(const t of n){if(!t||"object"!=typeof t)continue;const e=t.cell_type;if("code"!==e&&"markdown"!==e)continue;const n=t.source,o={kind:e,source:(Array.isArray(n)?n.join(""):"string"==typeof n?n:"").replace(/\s+$/,""),result:null};if("code"===e){const e=t.outputs;Array.isArray(e)&&e.length>0&&(o.rawOutputs=e);const n=t.execution_count;o.rawExecCount="number"==typeof n?n:null}r.push(o)}return r}export function parsePercentPy(t){const e=[];let n=[];const r=()=>{const t=n.join("\n").replace(/^\n+/,"").replace(/\s+$/,"");t&&e.push({kind:"code",source:t,result:null}),n=[]};for(const e of t.split("\n"))/^#\s*%%/.test(e)?r():n.push(e);return r(),e}function t(t){if(t.result){const e=[];for(const n of t.result.outputs)"stream"===n.kind?e.push({output_type:"stream",name:n.name,text:n.text}):"result"===n.kind?e.push({output_type:"execute_result",execution_count:t.result.execCount,data:n.data??{"text/plain":n.text},metadata:{}}):"display"===n.kind?e.push({output_type:"display_data",data:n.data,metadata:{}}):e.push({output_type:"error",ename:n.ename,evalue:n.evalue,traceback:n.traceback});return{outputs:e,execution_count:t.result.execCount}}return{outputs:t.rawOutputs??[],execution_count:t.rawExecCount??null}}export function serializeIpynb(e){const n={nbformat:4,nbformat_minor:5,metadata:{kernelspec:{display_name:"Python 3",language:"python",name:"python3"},language_info:{name:"python",version:"3.12"}},cells:e.map(e=>"markdown"===e.kind?{cell_type:"markdown",metadata:{},source:e.source}:{cell_type:"code",metadata:{},source:e.source,...t(e)})};return`${JSON.stringify(n,null,1)}\n`}export function statusChar(t){return"markdown"===t.kind?"md":null===t.result?"○":t.result.ok?"✓":"✗"}export function cellPreview(t,e){const n=(t.source.split("\n").find(t=>""!==t.trim())??"").trim();return n.length<=e?n:`${n.slice(0,Math.max(0,e-1))}…`}export function parseRunRange(t,e){const n=t.trim().toLowerCase();if(!n)return null;if("all"===n)return Array.from({length:e},(t,e)=>e+1);const r=n.match(/^(\d+)(?:-(\d+))?$/);if(!r)return null;const o=Number(r[1]),s=r[2]?Number(r[2]):o;return o<1||s<o||s>e?null:Array.from({length:s-o+1},(t,e)=>o+e)}export function mergeReloadedCells(t,e){let n=0,r=0;return{cells:e.map((e,o)=>{const s=t[o];return s&&s.kind===e.kind&&s.source===e.source?(s.result&&n++,{...e,result:s.result}):(r++,e)}),kept:n,changed:r}}const e=['# Whole notebook · each "# %%" line starts a cell · save and close to apply','# new cell: add a line "# %%" (or "# %% markdown") · delete: remove a cell with its "# %%" line'],n=/^# %%(?:\s*\[(\d+)\])?(?:\s*(?:\[(markdown|md)\]|(markdown|md)))?\s*$/i;export function toWholeEditText(t){const n=[e.join("\n")];return t.forEach((t,e)=>{if("markdown"===t.kind){const r=t.source.split("\n").map(t=>""===t?"#":`# ${t}`).join("\n");n.push(`# %% [${e+1}] markdown\n${r}`)}else n.push(`# %% [${e+1}]\n${t.source}`)}),`${n.join("\n\n")}\n`}export function fromWholeEditText(t,r){const o=t.replace(/\r\n/g,"\n").split("\n");let s=0;for(;s<o.length&&(""===o[s].trim()||e.includes(o[s]));)s++;const a=[];let l=null;for(;s<o.length;s++){const t=o[s].match(n);t?(l={tag:t[1]?Number(t[1]):null,kind:t[2]||t[3]?"markdown":"code",body:[]},a.push(l)):(l||(l={tag:null,kind:"code",body:[]},a.push(l)),l.body.push(o[s]))}const u=new Set,c=[],i=[],d=[],p=[];for(const t of a){let e=t.body;"markdown"===t.kind&&(e=e.map(t=>t.startsWith("# ")?t.slice(2):t.startsWith("#")?t.slice(1):t));const n=e.join("\n").replace(/^\n+/,"").replace(/\s+$/,"");if(!n)continue;const o=c.length+1,s=null!==t.tag&&t.tag>=1&&t.tag<=r.length&&!u.has(t.tag)?r[t.tag-1]:null;s&&null!==t.tag&&u.add(t.tag),s&&s.kind===t.kind&&s.source===n?(c.push({...s}),p.push(t.tag)):(c.push({kind:t.kind,source:n,result:null}),(s?i:d).push(o))}return{cells:c,changed:i,added:d,deleted:r.length-u.size,reordered:p.some((t,e)=>e>0&&t<p[e-1]),same:c.length===r.length&&0===i.length&&0===d.length&&p.every((t,e)=>t===e+1)}}export function normalizePathArg(t,e,n=process.platform){let r=t.trim();return r.length>=2&&(r.startsWith("'")&&r.endsWith("'")||r.startsWith('"')&&r.endsWith('"'))?r=r.slice(1,-1):"win32"!==n&&(r=r.replace(/\\(.)/g,"$1")),"~"===r?e:r.startsWith("~/")?`${e}/${r.slice(2)}`:r}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "icoa-cli",
|
|
3
|
-
"version": "2.19.
|
|
3
|
+
"version": "2.19.684",
|
|
4
4
|
"description": "ICOA CLI — AI-native competition runtime of the International Cyber Olympiad in AI: learn, practise, and compete in AI & cybersecurity from the terminal (AI4CTF · CTF4AI · CTF4EAI)",
|
|
5
5
|
"author": "Charlie Zhu",
|
|
6
6
|
"type": "module",
|