Download generate.py from AlmostNobody/Tinygpt-python-teacher: direct link, hf CLI and curl.
- Browser
- Download file 729 Bytes
-
https://huggingface.co/AlmostNobody/Tinygpt-python-teacher/resolve/main/generate.py
- Command line
-
hf download hf://AlmostNobody/Tinygpt-python-teacher/generate.py
-
curl -L -o generate.py https://huggingface.co/AlmostNobody/Tinygpt-python-teacher/resolve/main/generate.py
729 Bytes
| """Ask TinyGPT a question: python generate.py "How does a while loop work?" """ | |
| import sys | |
| import torch | |
| from model import load_model | |
| weights = "tinygpt.pt" # change if your file has another name | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model, chars = load_model(weights, device) | |
| stoi = {c: i for i, c in enumerate(chars)} | |
| itos = {i: c for i, c in enumerate(chars)} | |
| question = " ".join(sys.argv[1:]) or "How does a while loop work?" | |
| prompt = f"### Question:\n{question}\n### Answer:\n" | |
| idx = torch.tensor([[stoi[c] for c in prompt if c in stoi]], device=device) | |
| out = model.generate(idx, 400, temperature=0.5)[0].tolist() | |
| text = "".join(itos[i] for i in out)[len(prompt):] | |
| print(text.split("<END>")[0]) | |