[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
c9ff52ba04
commit
7b5bb24900
8 changed files with 167 additions and 87 deletions
|
|
@ -51,7 +51,9 @@ def resolve_latest_tag(repo: str) -> str:
|
|||
final_url = response.geturl()
|
||||
marker = "/releases/tag/"
|
||||
if marker not in final_url:
|
||||
raise SystemExit(f"FAIL: could not resolve latest release of {repo} (landed on {final_url})")
|
||||
raise SystemExit(
|
||||
f"FAIL: could not resolve latest release of {repo} (landed on {final_url})"
|
||||
)
|
||||
return final_url.rsplit(marker, 1)[1].strip("/")
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -49,14 +49,14 @@ CURATED = [
|
|||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--src", required=True, help="Studio 'Sloth emojis' dir")
|
||||
parser.add_argument("--dest", required=True, help="output dir (static/sloth)")
|
||||
parser = argparse.ArgumentParser(description = __doc__)
|
||||
parser.add_argument("--src", required = True, help = "Studio 'Sloth emojis' dir")
|
||||
parser.add_argument("--dest", required = True, help = "output dir (static/sloth)")
|
||||
args = parser.parse_args()
|
||||
|
||||
os.makedirs(args.dest, exist_ok=True)
|
||||
os.makedirs(args.dest, exist_ok = True)
|
||||
installed = 0
|
||||
for index, name in enumerate(CURATED, start=1):
|
||||
for index, name in enumerate(CURATED, start = 1):
|
||||
source = os.path.join(args.src, name)
|
||||
target = os.path.join(args.dest, "%02d.png" % index)
|
||||
if not os.path.isfile(source):
|
||||
|
|
|
|||
|
|
@ -38,14 +38,14 @@ def colab_cell_magic_fix(lines):
|
|||
for i, line in enumerate(lines):
|
||||
stripped = line.strip()
|
||||
if stripped == "" or stripped.startswith("#"):
|
||||
skipped.append(line) # blank or comment (incl. #@title)
|
||||
skipped.append(line) # blank or comment (incl. #@title)
|
||||
continue
|
||||
# First real line. Only act if it is a cell magic that is not yet on
|
||||
# top (i.e. something was skipped before it).
|
||||
if stripped.startswith("%%") and i > 0:
|
||||
return [line] + skipped + lines[i + 1:]
|
||||
return lines # already on top, or not a magic
|
||||
return lines # all blank/comment -> nothing to do
|
||||
return [line] + skipped + lines[i + 1 :]
|
||||
return lines # already on top, or not a magic
|
||||
return lines # all blank/comment -> nothing to do
|
||||
except Exception:
|
||||
return lines
|
||||
|
||||
|
|
@ -62,4 +62,4 @@ def register_ipython():
|
|||
ip.input_transformers_cleanup.append(colab_cell_magic_fix)
|
||||
ip._unsloth_colab_fix = True
|
||||
except Exception as e: # never break a kernel because of the helper
|
||||
print(f"[unsloth-nb] colab-compat hook skipped: {e!r}", file=sys.stderr)
|
||||
print(f"[unsloth-nb] colab-compat hook skipped: {e!r}", file = sys.stderr)
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ except Exception as _e: # never break a kernel because of the helper
|
|||
# failure here never disables the transformers-sidecar hook above and vice versa.
|
||||
try:
|
||||
import unsloth_colab_compat
|
||||
|
||||
unsloth_colab_compat.register_ipython()
|
||||
except Exception as _e: # never break a kernel because of the helper
|
||||
import sys
|
||||
|
|
|
|||
|
|
@ -34,9 +34,14 @@ def _logging_enabled() -> bool:
|
|||
`[unsloth-nb] activated transformers sidecar ...` line noisy. Set
|
||||
UNSLOTH_ENABLE_LOGGING=1 to surface it (and other [unsloth-nb] diagnostics)."""
|
||||
return os.environ.get("UNSLOTH_ENABLE_LOGGING", "").strip().lower() not in (
|
||||
"", "0", "false", "no", "off",
|
||||
"",
|
||||
"0",
|
||||
"false",
|
||||
"no",
|
||||
"off",
|
||||
)
|
||||
|
||||
|
||||
# Model-name -> minimum transformers tier, ported from Studio's
|
||||
# transformers_version.py (substring match on the lowered model id). Used as a
|
||||
# fallback when a notebook does not pin transformers but names a new-family model.
|
||||
|
|
|
|||
|
|
@ -60,9 +60,9 @@ def _strip_lines(lines):
|
|||
or None if there was nothing to strip."""
|
||||
for i, line in enumerate(lines):
|
||||
if line.lstrip().lower().startswith(_INTRO_PREFIX):
|
||||
out = lines[:i] + lines[i + 1:]
|
||||
out = lines[:i] + lines[i + 1 :]
|
||||
if i < len(out) and out[i].strip() == "":
|
||||
out = out[:i] + out[i + 1:]
|
||||
out = out[:i] + out[i + 1 :]
|
||||
return out
|
||||
return None
|
||||
|
||||
|
|
@ -77,7 +77,7 @@ def _strip_intro(nb):
|
|||
return False
|
||||
src = cell.get("source")
|
||||
if isinstance(src, str):
|
||||
lines = src.splitlines(keepends=True)
|
||||
lines = src.splitlines(keepends = True)
|
||||
as_str = True
|
||||
elif isinstance(src, list):
|
||||
lines = list(src)
|
||||
|
|
@ -104,7 +104,8 @@ def _clean_widgets(nb):
|
|||
if not isinstance(outs, list):
|
||||
continue
|
||||
kept = [
|
||||
o for o in outs
|
||||
o
|
||||
for o in outs
|
||||
if not (isinstance(o, dict) and _WIDGET_VIEW_MIME in (o.get("data") or {}))
|
||||
]
|
||||
if len(kept) != len(outs):
|
||||
|
|
@ -120,7 +121,7 @@ def _clean_widgets(nb):
|
|||
def strip_notebook(path):
|
||||
"""Return True if the notebook was modified and written back."""
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
with open(path, "r", encoding = "utf-8") as f:
|
||||
nb = json.load(f)
|
||||
except Exception:
|
||||
return False
|
||||
|
|
@ -133,8 +134,8 @@ def strip_notebook(path):
|
|||
|
||||
tmp = path + ".tmp"
|
||||
try:
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(nb, f, indent=1, ensure_ascii=False)
|
||||
with open(tmp, "w", encoding = "utf-8") as f:
|
||||
json.dump(nb, f, indent = 1, ensure_ascii = False)
|
||||
f.write("\n")
|
||||
os.replace(tmp, path)
|
||||
except Exception:
|
||||
|
|
@ -157,7 +158,7 @@ def _sha256(path):
|
|||
def migrate(state_path, dest):
|
||||
"""Strip owned+unedited notebooks listed in STATE and update their hashes."""
|
||||
try:
|
||||
with open(state_path, "r", encoding="utf-8") as f:
|
||||
with open(state_path, "r", encoding = "utf-8") as f:
|
||||
lines = f.read().splitlines()
|
||||
except OSError:
|
||||
return 0
|
||||
|
|
@ -165,7 +166,7 @@ def migrate(state_path, dest):
|
|||
out = []
|
||||
changed = 0
|
||||
for line in lines:
|
||||
parts = line.split(" ", 1) # "<sha256> <relpath>"
|
||||
parts = line.split(" ", 1) # "<sha256> <relpath>"
|
||||
if len(parts) != 2:
|
||||
out.append(line)
|
||||
continue
|
||||
|
|
@ -173,7 +174,7 @@ def migrate(state_path, dest):
|
|||
path = os.path.join(dest, rel)
|
||||
if rel.endswith(".ipynb") and os.path.isfile(path):
|
||||
try:
|
||||
if _sha256(path) == rec: # we own it and it is unedited
|
||||
if _sha256(path) == rec: # we own it and it is unedited
|
||||
if strip_notebook(path):
|
||||
rec = _sha256(path)
|
||||
changed += 1
|
||||
|
|
@ -184,7 +185,7 @@ def migrate(state_path, dest):
|
|||
if changed:
|
||||
tmp = state_path + ".tmp"
|
||||
try:
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
with open(tmp, "w", encoding = "utf-8") as f:
|
||||
f.write("\n".join(out) + "\n")
|
||||
os.replace(tmp, state_path)
|
||||
except OSError:
|
||||
|
|
@ -194,10 +195,10 @@ def migrate(state_path, dest):
|
|||
|
||||
|
||||
def main(argv):
|
||||
ap = argparse.ArgumentParser(description="Strip the Colab-only intro sentence.")
|
||||
ap.add_argument("--state", help="sync state file (enables migration mode)")
|
||||
ap.add_argument("--dest", help="notebooks dir (with --state)")
|
||||
ap.add_argument("paths", nargs="*", help="notebooks to strip in place")
|
||||
ap = argparse.ArgumentParser(description = "Strip the Colab-only intro sentence.")
|
||||
ap.add_argument("--state", help = "sync state file (enables migration mode)")
|
||||
ap.add_argument("--dest", help = "notebooks dir (with --state)")
|
||||
ap.add_argument("paths", nargs = "*", help = "notebooks to strip in place")
|
||||
args = ap.parse_args(argv)
|
||||
|
||||
if args.state:
|
||||
|
|
|
|||
|
|
@ -68,11 +68,11 @@ def parse_readme(readme_path):
|
|||
|
||||
filename is the urldecoded basename under nb/ (literal parens, matching disk).
|
||||
"""
|
||||
with open(readme_path, "r", encoding="utf-8") as f:
|
||||
with open(readme_path, "r", encoding = "utf-8") as f:
|
||||
text = f.read()
|
||||
|
||||
rows = []
|
||||
seen_pairs = set() # (section, filename) already emitted
|
||||
seen_pairs = set() # (section, filename) already emitted
|
||||
section = None
|
||||
for line in text.splitlines():
|
||||
m = re.match(r"^###\s+(.*)$", line)
|
||||
|
|
@ -102,7 +102,11 @@ def _ordered_sections(rows):
|
|||
return order
|
||||
|
||||
|
||||
def build_view(dest, view, amd=False):
|
||||
def build_view(
|
||||
dest,
|
||||
view,
|
||||
amd = False,
|
||||
):
|
||||
nb_dir = os.path.join(dest, "nb")
|
||||
readme = os.path.join(dest, "README.md")
|
||||
if not os.path.isdir(nb_dir):
|
||||
|
|
@ -138,23 +142,23 @@ def build_view(dest, view, amd=False):
|
|||
|
||||
# Rebuild VIEW from scratch.
|
||||
_rmtree(view)
|
||||
os.makedirs(view, exist_ok=True)
|
||||
os.makedirs(view, exist_ok = True)
|
||||
|
||||
n_links = 0
|
||||
for i, section in enumerate(order, start=1):
|
||||
for i, section in enumerate(order, start = 1):
|
||||
folder = os.path.join(view, f"{i:02d} {section}")
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
os.makedirs(folder, exist_ok = True)
|
||||
for fname in by_section[section]:
|
||||
link = os.path.join(folder, fname)
|
||||
target = os.path.join(nb_dir, fname)
|
||||
rel = os.path.relpath(target, folder) # ../../unsloth-notebooks/nb/<file>
|
||||
rel = os.path.relpath(target, folder) # ../../unsloth-notebooks/nb/<file>
|
||||
try:
|
||||
if os.path.islink(link) or os.path.exists(link):
|
||||
os.remove(link)
|
||||
os.symlink(rel, link)
|
||||
n_links += 1
|
||||
except OSError as e:
|
||||
print(f"[unsloth-nb] view: skip {fname}: {e}", file=sys.stderr)
|
||||
print(f"[unsloth-nb] view: skip {fname}: {e}", file = sys.stderr)
|
||||
return len(order), n_links
|
||||
|
||||
|
||||
|
|
@ -165,7 +169,7 @@ def _rmtree(path):
|
|||
if os.path.islink(path):
|
||||
os.remove(path)
|
||||
return
|
||||
for root, dirs, files in os.walk(path, topdown=False):
|
||||
for root, dirs, files in os.walk(path, topdown = False):
|
||||
for name in files:
|
||||
try:
|
||||
os.remove(os.path.join(root, name))
|
||||
|
|
@ -184,12 +188,16 @@ def _rmtree(path):
|
|||
|
||||
|
||||
def main(argv):
|
||||
ap = argparse.ArgumentParser(description="Build the categorized notebook view.")
|
||||
ap.add_argument("dest", help="notebooks dir (contains README.md and nb/)")
|
||||
ap.add_argument("view", nargs="?", help="output view dir (omit with --print)")
|
||||
ap.add_argument("--amd", action="store_true", help="include AMD-* notebooks")
|
||||
ap.add_argument("--print", dest="do_print", action="store_true",
|
||||
help="print section<TAB>file rows instead of building")
|
||||
ap = argparse.ArgumentParser(description = "Build the categorized notebook view.")
|
||||
ap.add_argument("dest", help = "notebooks dir (contains README.md and nb/)")
|
||||
ap.add_argument("view", nargs = "?", help = "output view dir (omit with --print)")
|
||||
ap.add_argument("--amd", action = "store_true", help = "include AMD-* notebooks")
|
||||
ap.add_argument(
|
||||
"--print",
|
||||
dest = "do_print",
|
||||
action = "store_true",
|
||||
help = "print section<TAB>file rows instead of building",
|
||||
)
|
||||
args = ap.parse_args(argv)
|
||||
|
||||
if args.do_print:
|
||||
|
|
@ -200,7 +208,7 @@ def main(argv):
|
|||
|
||||
if not args.view:
|
||||
ap.error("view dir is required unless --print is given")
|
||||
n_sections, n_links = build_view(args.dest, args.view, amd=args.amd)
|
||||
n_sections, n_links = build_view(args.dest, args.view, amd = args.amd)
|
||||
print(f"[unsloth-nb] view: {n_links} notebooks in {n_sections} folders -> {args.view}")
|
||||
return 0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue