"""What do the two models' own cards, and the library, say about them?

    python model_cards.py

The page says a few things about its two models that it did not measure: who
made them, under which licence, what they were trained to do. This prints the
lines of each model's card on Hugging Face that those sentences rest on, and
what the transformers library says about the documents= argument. It needs the
internet once, to fetch each card.
"""
import sys
from pathlib import Path

from huggingface_hub import hf_hub_download
from transformers import PreTrainedTokenizerBase

PAGE_FILES = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PAGE_FILES))
import answer  # noqa: E402  (the page's own file)

# For each model: the beginnings of the lines and of the sentences worth showing.
LINES = {
    answer.EMBEDDER: ["language:", "license:"],
    answer.MODEL: ["license:", "- **Developers:**", "- **Release Date**", "- **License:**",
                   "* Question-answering", "* Retrieval Augmented Generation"],
}
SENTENCES = {
    answer.EMBEDDER: ["This is a [sentence-transformers]", "The total number of sentence pairs",
                      "We use a contrastive learning objective",
                      "By default, input text longer"],
    answer.MODEL: ["**Supported Languages:**", "Granite 4.0 Nano instruct models feature"],
}


def line_from(text, start):
    """The line of the card that begins with these words."""
    for line in text.splitlines():
        if line.strip().startswith(start):
            return line.strip()
    return f"NOT IN THE CARD ANY MORE: {start}"


def sentence_from(text, start):
    """The sentence of the card that begins with these words, on one line."""
    place = text.find(start)
    if place == -1:
        return f"NOT IN THE CARD ANY MORE: {start}"
    rest = " ".join(text[place:place + 700].split())
    if ". " in rest[len(start):]:
        return rest[:rest.index(". ", len(start)) + 1]
    return rest


def main():
    for name in [answer.EMBEDDER, answer.MODEL]:
        card = Path(hf_hub_download(name, "README.md")).read_text(encoding="utf-8")
        print(f"huggingface.co/{name}, its card ({len(card):,} characters)")
        for start in LINES[name]:
            print("    " + line_from(card, start))
        for start in SENTENCES[name]:
            print("    " + sentence_from(card, start))
        print()

    doc = PreTrainedTokenizerBase.apply_chat_template.__doc__
    place = doc.find("documents (")
    print("transformers, in the documentation of apply_chat_template:")
    print("    " + " ".join(doc[place:doc.find("chat_template (", place)].split()))


if __name__ == "__main__":
    main()
