roneneldan/TinyStories
Viewer β’ Updated β’ 2.14M β’ 297k β’ 1.46k
A compact, decoder-only autoregressive language model built from scratch in pure PyTorch and trained on Microsoft Research's TinyStories dataset.
import os
import torch
from huggingface_hub import hf_hub_download
# 1. Download model files from Hugging Face
repo_id = "mallelamanoj75/tinystories-minigpt"
ckpt_path = hf_hub_download(repo_id=repo_id, filename="best_model.pt")
vocab_path = hf_hub_download(repo_id=repo_id, filename="vocab.json")
merges_path = hf_hub_download(repo_id=repo_id, filename="merges.txt")
model_py = hf_hub_download(repo_id=repo_id, filename="model.py")
tokenizer_py = hf_hub_download(repo_id=repo_id, filename="tokenizer_util.py")
# 2. Load model
from model import TinyStoriesGPT, GPTConfig
from tokenizer_util import StoryTokenizer
os.makedirs("tokenizer_data", exist_ok=True)
import shutil
shutil.copy(vocab_path, "tokenizer_data/vocab.json")
shutil.copy(merges_path, "tokenizer_data/merges.txt")
tok = StoryTokenizer("tokenizer_data")
checkpoint = torch.load(ckpt_path, map_location="cpu", weights_only=False)
model = TinyStoriesGPT(checkpoint["config"])
model.load_state_dict(checkpoint["state_dict"])
model.eval()
# 3. Generate a story!
prompt = "Once upon a time, Lily found a sparkling green stone"
input_ids = torch.tensor([tok.encode(prompt)], dtype=torch.long)
output_ids = model.generate(input_ids, max_new_tokens=80, temperature=0.8, top_k=30, top_p=0.9)
print(tok.decode(output_ids[0].tolist()))
Prompt: "One sunny morning, Mia and her mom went to the big park"
"One sunny morning, Mia and her mom went to the big park with her mom. They saw a big, red ball and a swing.
"Look, this is a big slide!" Mia said. "But I want to swing very high and fast."
"No, I want to play!" Mia said. "Let's play!"
They ran to the swing and jumped in the slide. They were happy. They had fun.
[End of Story]"