Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ and this project adheres to
([#647])
- Hub model card datasets: only a Hub dataset id or page gets a location, a
page is named by its id; a URL naming a file warns that the repository is
read ([#650])
read ([#650], [#653])

[#591]: https://github.com/bact/pitloom/pull/591
[#598]: https://github.com/bact/pitloom/pull/598
Expand Down Expand Up @@ -121,6 +121,7 @@ and this project adheres to
[#647]: https://github.com/bact/pitloom/pull/647
[#650]: https://github.com/bact/pitloom/pull/650
[#651]: https://github.com/bact/pitloom/pull/651
[#653]: https://github.com/bact/pitloom/pull/653

## [0.25.1] - 2026-11-28

Expand Down
6 changes: 3 additions & 3 deletions docs/ai-model-formats-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ can hold it, Pitloom does not read it. **--**: the format has no such field.
array of names or links; a link is also the download location) and
llama.cpp's numbered `general.dataset.<id>.*` entries
(`general.dataset.count`). A Hub dataset page,
`https://huggingface.co/datasets/<id>`, as a numbered entry's
`repo_url` or an array element, names the dataset by its Hub id
(`Method: hub_dataset_id`), as a Hub model card's datasets are named.
`https://huggingface.co/datasets/<id>` (or on `hf.co`, `www.`), as a
numbered entry's `repo_url` or an array element, names the dataset by
its Hub id (`Method: hub_dataset_id`), as a Hub model card's datasets are named.
llama.cpp writes a card's Hub id `owner/name` as `name` = a title of
the name part (`OpenHermes 2.5`), `organization` = a title of the owner,
a `version` guessed from the name, and the model page URL
Expand Down
12 changes: 7 additions & 5 deletions docs/ai-model-formats-hugging-face.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ loom model https://huggingface.co/mistralai/Mistral-7B-v0.1
loom model Qwen/Qwen3-235B-A22B
```

The `www.huggingface.co` and `hf.co` forms, `http://` and any case of the host
are read as `https://huggingface.co/...`, with one `INFO:` line saying so
The `www.huggingface.co` and `hf.co` forms, `http://` and any ASCII case of
the host are read as `https://huggingface.co/...` (a Unicode lookalike such
as `huggıngface.co` is no Hub host), with one `INFO:` line saying so
([`target-ambiguous/hf-url`](message-codes-targets.md#target-ambiguoushf-url)):
`INFO: URL=<url>: read as Hugging Face model <target> [target-ambiguous/hf-url]`.
A page URL of a model (`https://hf.co/owner/name/tree/main`, or another
Expand Down Expand Up @@ -196,9 +197,10 @@ which Pitloom does not do. To get a model file's fields, download it and run

Each dataset the model card lists (`datasets:`, else the Hub's
`dataset:*` tags) that is a Hub dataset id is located at its page,
`https://huggingface.co/datasets/<id>`; one listed as that page is named by
its id. Any other entry (a plain name, `wikipedia + bookcorpus`, an id whose
owner is a reserved Hub name (see
`https://huggingface.co/datasets/<id>`; one listed as that page (on any
Hub host, in any ASCII case: `hf.co`, `www.huggingface.co`) is named by
its id. Any other entry (a plain name, `wikipedia + bookcorpus`, an id
whose owner is a reserved Hub name (see
[What each format holds](ai-model-formats-fields.md#what-each-format-holds)),
another URL) keeps its text as the name and gets no download location. A
local model card's `datasets:` (`--enrich`, `loom enrich`) names a dataset
Expand Down
5 changes: 3 additions & 2 deletions docs/message-codes-model-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ each `<name>`, after at most one prefix: the index lists the prefixes and
cannot carry the location. Said once per dataset.
- **SBOM effect**: thinner: the SBOM says less than reading the input would.
- **Pitloom did**: Wrote the fragment without the location; the model's SBOM
made with enrichment (--enrich, generate_model_sbom(enrich=True)) records it.
made with enrichment (loom model --enrich, generate_model_sbom(enrich=True))
records it.
- **Tell the user**: the merged SBOM lacks the dataset's location from the model
card; generating the model's SBOM with enrichment, as the line says, records
it. Do not run it for them.
- **Lines**:

```text
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; use --enrich to record it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; loom model --enrich records it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; generate_model_sbom(enrich=True) records it [field-not-recordable/fragment]
```

Expand Down
5 changes: 3 additions & 2 deletions skills/sbom-enrich/references/messages-model-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ each `<name>`, after at most one prefix: the index lists the prefixes and
cannot carry the location. Said once per dataset.
- **SBOM effect**: thinner: the SBOM says less than reading the input would.
- **Pitloom did**: Wrote the fragment without the location; the model's SBOM
made with enrichment (--enrich, generate_model_sbom(enrich=True)) records it.
made with enrichment (loom model --enrich, generate_model_sbom(enrich=True))
records it.
- **Tell the user**: the merged SBOM lacks the dataset's location from the model
card; generating the model's SBOM with enrichment, as the line says, records
it. Do not run it for them.
- **Lines**:

```text
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; use --enrich to record it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; loom model --enrich records it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; generate_model_sbom(enrich=True) records it [field-not-recordable/fragment]
```

Expand Down
5 changes: 3 additions & 2 deletions skills/sbom-generate/references/messages-model-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ each `<name>`, after at most one prefix: the index lists the prefixes and
cannot carry the location. Said once per dataset.
- **SBOM effect**: thinner: the SBOM says less than reading the input would.
- **Pitloom did**: Wrote the fragment without the location; the model's SBOM
made with enrichment (--enrich, generate_model_sbom(enrich=True)) records it.
made with enrichment (loom model --enrich, generate_model_sbom(enrich=True))
records it.
- **Tell the user**: the merged SBOM lacks the dataset's location from the model
card; generating the model's SBOM with enrichment, as the line says, records
it. Do not run it for them.
- **Lines**:

```text
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; use --enrich to record it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; loom model --enrich records it [field-not-recordable/fragment]
WARNING: DATASET='<dataset>': an enrichment fragment cannot carry its download location from the model card; generate_model_sbom(enrich=True) records it [field-not-recordable/fragment]
```

Expand Down
28 changes: 14 additions & 14 deletions src/pitloom/core/ai_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,10 @@ def source_metadata(

@dataclass
class AiModelFormatInfo:
"""Physical model file and format/framework metadata.
"""Physical model file and format/framework metadata: *how* the model is
stored on disk, rather than what the model does."""

Groups the file-level and toolchain fields that describe *how* the model
is stored on disk, rather than what the model does.
"""

# Physical model file name (basename only, no directory path)
file_name: str | None = None
file_name: str | None = None # physical file's basename, no directory

# Canonical distribution path of the file inside the project
file_path_relative: str | None = None
Expand All @@ -290,8 +286,7 @@ class AiModelFormatInfo:
# is not among its document's files (assemble.spdx3.ai.model_lookup_key).
physical_path: str | None = None

# Format enum value (e.g. AiModelFormat.GGUF)
model_format: AiModelFormat = AiModelFormat.UNKNOWN
model_format: AiModelFormat = AiModelFormat.UNKNOWN # e.g. AiModelFormat.GGUF

# Version of the model file format (e.g. "v2" for Keras v2, "1.0" for NumPy 1.0)
format_version: str | None = None
Expand All @@ -300,8 +295,7 @@ class AiModelFormatInfo:
# (e.g. "keras", "pytorch", "llama.cpp")
framework: str | None = None

# Version of the framework/library used to produce the model
# (e.g. "2.15.0" for Keras 2.15.0, "2.7.1" for PyTorch 2.7.1)
# Version of the framework that produced the model ("2.15.0" for Keras)
framework_version: str | None = None

# The tool that wrote the file and its version, as the file names them
Expand All @@ -311,11 +305,17 @@ class AiModelFormatInfo:
writing_tool: str | None = None
writing_tool_version: str | None = None

# Set by pitloom.extract.ai_model.limits.settle_read_text: see shown_path
logged_path: str | None = None

@property
def shown_path(self) -> str | None:
"""The model file as a warning names it: its physical path, else
its path in the package, else its file name; ``None`` with none."""
return self.physical_path or self.file_path_relative or self.file_name or None
"""The model file as a warning names it: as read, loggable, before a
lone surrogate is written as text (:attr:`logged_path`), so every line
gives one ``FILE=``; else its physical path, else its path in the
package, else its file name; ``None`` with none."""
paths = (self.physical_path, self.file_path_relative, self.file_name)
return self.logged_path or next(filter(None, paths), None)


@dataclass
Expand Down
25 changes: 19 additions & 6 deletions src/pitloom/core/hub_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
"HUB_NOT_OWNERS",
"HUB_ONE_PART_URL_RE",
"HUB_SITE_PAGES",
"HUB_URL_FLAGS",
"first_part_reserved",
"hub_area_url_short",
"hub_url_dot_segment",
"is_hub_git_area",
"is_hub_host",
"is_hub_model_id",
]

#: The Hub's hosts (``huggingface.co``, its ``www.`` form and the ``hf.co``
#: short name), as a pattern to match in any case.
HUB_HOST_PATTERN: Final = r"(?:www\.)?(?:huggingface|hf)\.co"
_HUB_HOST: Final = re.compile(HUB_HOST_PATTERN, re.IGNORECASE)
#: short name), as a pattern that matches them in any ASCII case, its flags
#: inline: a Unicode lookalike (``huggıngface.co``, with U+0131) is none.
HUB_HOST_PATTERN: Final = r"(?ai:(?:www\.)?(?:huggingface|hf)\.co)"
#: The flags of every pattern matching a Hub URL: any case, ASCII only (with
#: Unicode case folding, ``ı`` matches ``i`` and ``ſ`` matches ``s``).
HUB_URL_FLAGS: Final = re.IGNORECASE | re.ASCII
_HUB_HOST: Final = re.compile(HUB_HOST_PATTERN)
#: A Hub URL of one path part (a trailing ``/``, a query or a fragment
#: aside), the part its group: ``/gpt2``. Use it with ``fullmatch``.
HUB_ONE_PART_URL_RE: Final = re.compile(
rf"https?://{HUB_HOST_PATTERN}/([^/?#]+)/?(?:[?#].*)?", re.IGNORECASE
rf"https?://{HUB_HOST_PATTERN}/([^/?#]+)/?(?:[?#].*)?", HUB_URL_FLAGS
)

#: The areas whose repositories are Git repositories, not models: a Space,
Expand All @@ -63,6 +69,13 @@
HUB_SITE_PAGES: Final = HUB_NOT_OWNERS - HUB_GIT_AREAS


def is_hub_host(host: str) -> bool:
"""Whether *host* is one of the Hub's hosts (:data:`HUB_HOST_PATTERN`),
in any ASCII case. *host* is a URL's whole ``netloc`` or its ``hostname``: a
netloc with a port or credentials is no Hub host."""
return _HUB_HOST.fullmatch(host) is not None


def _ascii_folded(part: str) -> str:
"""*part* in lower case when it is all ASCII, else as given, so it
matches none of the (ASCII) names here: ``str.lower`` folds the Kelvin
Expand Down Expand Up @@ -123,7 +136,7 @@ def hub_area_url_short(url: str) -> bool:
short. *url* is one ``urlsplit`` parses."""
parts = urlsplit(url.strip())
segments = [part for part in parts.path.split("/") if part]
if _HUB_HOST.fullmatch(parts.hostname or "") is None or not segments:
if not is_hub_host(parts.hostname or "") or not segments:
return False
if not is_hub_git_area(segments[0]):
return False
Expand All @@ -138,6 +151,6 @@ def hub_url_dot_segment(url: str) -> bool:
``/spaces/./o`` the short ``/spaces/o``). Every Hub route refuses one
before reading its parts. *url* is one ``urlsplit`` parses."""
parts = urlsplit(url.strip())
if _HUB_HOST.fullmatch(parts.hostname or "") is None:
if not is_hub_host(parts.hostname or ""):
return False
return any(unquote(part) in (".", "..") for part in parts.path.split("/"))
18 changes: 10 additions & 8 deletions src/pitloom/core/hub_dataset_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# SPDX-License-Identifier: Apache-2.0

"""A Hugging Face Hub dataset's id, as a model card or a GGUF file names a
dataset: a bare id, or the dataset's page ``https://huggingface.co/datasets/<id>``.
dataset: a bare id, or the dataset's page ``https://huggingface.co/datasets/<id>``
on any Hub host (:func:`~pitloom.core.hub_areas.is_hub_host`).
One rule for every reader, so none locates a dataset at a page the Hub
cannot have.

Expand All @@ -20,7 +21,7 @@

from pitloom.core.dataset_metadata import DatasetMetadata
from pitloom.core.dataset_names import dataset_key, distinct_dataset_names
from pitloom.core.hub_areas import first_part_reserved
from pitloom.core.hub_areas import first_part_reserved, is_hub_host
from pitloom.core.hub_repo_id import is_hub_repo_id
from pitloom.core.internal_error import reraise_internal
from pitloom.core.provenance import provenance_segment
Expand All @@ -45,7 +46,6 @@
CARD_DATASETS_FIELD: Final = "model card YAML (datasets)"
#: The Hub's dataset pages: the prefix of ``<prefix><id>``.
HUB_DATASETS_PREFIX: Final = "https://huggingface.co/datasets/"
_HUB_HOST: Final = "huggingface.co"


def is_hub_dataset_id(name: str) -> bool:
Expand All @@ -59,10 +59,12 @@ def is_hub_dataset_id(name: str) -> bool:

def hub_dataset_page(url: str) -> str | None:
"""The Hub id an ``http(s)://huggingface.co/datasets/<id>`` *url* names
(:func:`is_hub_dataset_id`); ``None`` for any other text: another host
or port, credentials, a query, a fragment, a deeper path, a reserved
owner, text ``urlsplit`` refuses, or a white-space or non-printable
character anywhere (``urlsplit`` drops a tab or a newline), as
(:func:`is_hub_dataset_id`), on any Hub host in any case
(:func:`~pitloom.core.hub_areas.is_hub_host`: ``hf.co``, ``www.``);
``None`` for any other text: another host or a port, credentials, a
query, a fragment, a deeper path, a reserved owner, text ``urlsplit``
refuses, or a white-space or non-printable character anywhere
(``urlsplit`` drops a tab or a newline), as
:func:`~pitloom.extract.dataset.values.stated_url` refuses one."""
if any(c.isspace() or not c.isprintable() for c in url):
return None
Expand All @@ -73,7 +75,7 @@ def hub_dataset_page(url: str) -> str | None:
return None
if (
parts.scheme not in ("http", "https") # urlsplit lower-cases it
or parts.netloc.lower() != _HUB_HOST
or not is_hub_host(parts.netloc)
or parts.query
or parts.fragment
):
Expand Down
21 changes: 15 additions & 6 deletions src/pitloom/extract/_scanner_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import logging
import os
import re
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -65,13 +66,21 @@ def scrub(text: str, path: Path | None, stable_path: str) -> str:
is written (escaped, in quotes, when it holds a control character).
Replaced after *text* is made one line: the stable path keeps its own
white space, and a control character in it cannot turn the whole text
into one escaped literal that is no longer the reader's line."""
into one escaped literal that is no longer the reader's line. A path
the reader already wrote as a ``FILE=`` value is replaced with its
quotes, so it is not quoted twice."""
text = one_line(text)
if path is not None:
stable = FIELDS["file"].write(stable_path)
for form in _path_forms(path):
text = text.replace(form, stable)
return text
if path is None:
return text
stable = FIELDS["file"].write(stable_path)
forms = _path_forms(path)
# A value the reader wrote as a field (quoted) is replaced whole, and in
# one pass, so neither it nor the stable path is quoted again.
spellings = {FIELDS["file"].write(form) for form in forms} | set(forms)
found = re.compile(
"|".join(map(re.escape, sorted(spellings, key=len, reverse=True)))
)
return found.sub(lambda _: stable, text)


def detail(exc: BaseException, path: Path | None, stable_path: str) -> str:
Expand Down
8 changes: 7 additions & 1 deletion src/pitloom/extract/ai_model/limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pitloom.core.untrusted_text import escape_lone_surrogates_in
from pitloom.extract.ai_model.formats import Limits
from pitloom.extract.ai_model.unrecordable import notice_unrecordable
from pitloom.logging_config import loggable
from pitloom.messages.files import Cap, warn_over_cap
from pitloom.messages.text import warn_lone_surrogates

Expand Down Expand Up @@ -209,7 +210,12 @@ def settle_read_text(meta: AiModelMetadata, fmt: str, where: str) -> None:
(:func:`~pitloom.extract.ai_model.unrecordable.notice_unrecordable`).
Called once per model, before anything is derived from its text (an
id, a name): by :func:`cap_and_warn` for a model file, by
``read_huggingface`` for a Hugging Face model."""
``read_huggingface`` for a Hugging Face model. A model file's path is
kept as read for its later warnings first
(:attr:`~pitloom.core.ai_metadata.AiModelFormatInfo.logged_path`)."""
shown = meta.format_info.shown_path
if shown is not None:
meta.format_info.logged_path = loggable(shown)
if escape_lone_surrogates_in(meta)[1]:
warn_lone_surrogates(log, file=where, model_format=fmt)
warn_name_cut(meta, fmt, where)
Expand Down
7 changes: 4 additions & 3 deletions src/pitloom/extract/remote/_hub_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
HUB_GIT_AREAS,
HUB_HOST_PATTERN,
HUB_ONE_PART_URL_RE,
HUB_URL_FLAGS,
first_part_reserved,
hub_url_dot_segment,
is_hub_git_area,
Expand Down Expand Up @@ -71,19 +72,19 @@
]

# Match full HF model URLs: https://huggingface.co/owner/name[/anything], also
# as http, www.huggingface.co or hf.co, in any case of the scheme and host. A
# as http, www.huggingface.co or hf.co, in any ASCII case of the scheme and host. A
# Space, a dataset or a kernel (``HUB_GIT_AREAS``) is a Git repository, not a
# model.
_HF_URL_RE = re.compile(
rf"https?://{HUB_HOST_PATTERN}/"
rf"(?!(?:{'|'.join(sorted(map(re.escape, HUB_GIT_AREAS)))})/)"
r"([^/?#]+/[^/?#]+?)(?:[/?#].*)?$",
re.IGNORECASE,
HUB_URL_FLAGS,
)
CANONICAL_PREFIX = "https://huggingface.co/"
#: The first path part of a Hub URL, whatever follows it.
_HUB_PATH_RE: Final = re.compile(
rf"https?://{HUB_HOST_PATTERN}/([^/?#]*)", re.IGNORECASE
rf"https?://{HUB_HOST_PATTERN}/([^/?#]*)", HUB_URL_FLAGS
)

#: The first part of a model page's path after the model id
Expand Down
Loading
Loading