"""Download the small TinyStories file used by steps 1 and 2.

Only the validation file (about 20 MB) is fetched, not the 2 GB training
file: counting and tokenizer practice need text, not a mountain of it.
Step 5 fetches the training file when it needs it.

Run:  python get_tinystories.py
"""
import urllib.request
from pathlib import Path

URL = ("https://huggingface.co/datasets/roneneldan/TinyStories/resolve/main/"
       "TinyStoriesV2-GPT4-valid.txt")
OUT = Path(__file__).parent / "data" / "tinystories-valid.txt"


def main():
    if OUT.exists():
        print(f"already have {OUT} ({OUT.stat().st_size / 1e6:.1f} MB)")
        return
    OUT.parent.mkdir(exist_ok=True)
    print(f"downloading {URL}")
    urllib.request.urlretrieve(URL, OUT)
    print(f"saved {OUT} ({OUT.stat().st_size / 1e6:.1f} MB)")


if __name__ == "__main__":
    main()
