Build your own AI model, hands-on. Reel lo chusina 6 steps, nee laptop lo.
Tick the boxes as you go: your progress is saved on this device.
Creator: Augyanix · Series: AI with Augyanix · From: Day 5 "How an AI model is built" · Cost: ₹0 (free tools only)
| Step | What happens | Placement student example | In this playbook |
|---|---|---|---|
| 1. Collection of data | Gather lots of text = training data | Collecting notes | Your own input.txt |
| 2. Cleaning | Remove spam, repeats, junk | Throwing away useless pages | A 10-line Python cleaner |
| 3. Tokenization | Cut text into small pieces = tokens | Breaking chapters into points | prepare.py + tiktoken demo |
| 4. Pre-training | Guess the next token, check with the actual token, adjust weights | Checking with the answer key | train.py |
| 5. Fine-tuning | Teach it to answer with Q&A examples = fine-tuning data | Mock interviews | Project 2 (TRL) |
| 6. Testing | Exam with questions it never saw = test data | The final exam | Validation loss + unseen questions |
| ✓ Final model | All weights saved in one file | Placement ready! | ckpt.pt / model.safetensors |
Rendu options. Beginners ki Option A (Google Colab) best: install emi ledu, free GPU kuda vastundi.
Colab = Google's free online Python notebook. Code Google computer lo run avtundi, nee laptop lo kaadu.
Colab rules to remember:
Colab lo unte, ee cells ni order lo okkokkati run cheyyi. Each cell = one + Code box. What each step means is explained in the projects below.
playbook_colab.ipynb → set T4 GPU → run the cells one by one. On your phone (Instagram browser)? Open this page on a laptop for the notebook: Colab works best there.Cell 1 · setup (Colab already has PyTorch):
!git clone https://github.com/karpathy/nanoGPT.git %cd nanoGPT !pip install -q tiktoken
Cell 2 · Step 1, your data. Run it, click Choose files, pick your raw.txt. No file? Skip this cell: Shakespeare is downloaded automatically.
from google.colab import files files.upload() # saves raw.txt inside the nanoGPT folder
Cell 3 · Step 2, cleaning (only if you uploaded raw.txt):
%%writefile clean.py
lines = open("raw.txt", encoding="utf-8").read().splitlines()
seen, keep = set(), []
for line in lines:
line = line.strip()
if len(line) < 20: continue # too short = junk
if "http" in line: continue # links / spam
if line in seen: continue # repeat lines
seen.add(line)
keep.append(line)
open("data/shakespeare_char/input.txt", "w", encoding="utf-8").write("\n".join(keep))
print(len(lines), "lines in,", len(keep), "lines kept")!python clean.py
Cell 4 · Step 3, tokenization:
!python data/shakespeare_char/prepare.py
Cell 5 · Step 4, pre-training on the T4 GPU. It prints the loss every few steps; this can take a while. The model file is saved at every check (every 250 steps, when val loss improves). Want to stop early? Press ■ only after you see a step 250: ... val loss line, otherwise there is no model file yet.
!python train.py config/train_shakespeare_char.py --compile=False
Cell 6 · Step 6, testing: generate text from your model:
!python sample.py --out_dir=out-shakespeare-char
Cell 7 · ✓ Final model: download your weights file (ckpt.pt) before the runtime disconnects:
from google.colab import files
files.download("out-shakespeare-char/ckpt.pt")Start a new notebook (or Runtime → Restart session), turn on T4 GPU again, then:
Cell 1 · setup:
!pip install -q trl
Cell 2 · fine-tuning data: upload your qa.jsonl (format in Project 2 below):
from google.colab import files files.upload()
Cell 3 · fine-tune + save (Step 5):
from datasets import load_dataset
from trl import SFTTrainer, SFTConfig
data = load_dataset("json", data_files="qa.jsonl", split="train")
data = data.train_test_split(test_size=0.2, seed=42) # 20 % kept aside = test data
trainer = SFTTrainer(
model="HuggingFaceTB/SmolLM2-135M-Instruct",
train_dataset=data["train"],
eval_dataset=data["test"],
args=SFTConfig(output_dir="my-model", num_train_epochs=3, report_to="none"),
)
trainer.train()
trainer.save_model("my-model")Cell 4 · testing (Step 6), before vs after:
from transformers import pipeline
for name in ["HuggingFaceTB/SmolLM2-135M-Instruct", "my-model"]:
chat = pipeline("text-generation", model=name)
out = chat([{"role": "user", "content": "How should I prepare for an HR round?"}], max_new_tokens=80)
print(name, "→", out[0]["generated_text"][-1]["content"])Cell 5 · ✓ Final model: zip and download it:
!zip -r my-model.zip my-model
from google.colab import files
files.download("my-model.zip")If Colab asks for a "WANDB" or "HF_TOKEN" key: you don't need one for this playbook. Click Cancel / skip; report_to="none" already turns off logging.
Time: one weekend. Machine: any laptop (CPU is fine, just slower).
We use nanoGPT by Andrej Karpathy: small, readable code. Note: the author now marks nanoGPT as old/deprecated and points to his newer nanochat (that one needs big rented GPUs). For learning the 6 steps on a laptop, nanoGPT is still the simplest.
Rule: never collect private data of other people or paid content you don't own.
lines = open("raw.txt", encoding="utf-8").read().splitlines()
seen, keep = set(), []
for line in lines:
line = line.strip()
if len(line) < 20: continue # too short = junk
if "http" in line: continue # links / spam
if line in seen: continue # repeat lines
seen.add(line)
keep.append(line)
open("data/shakespeare_char/input.txt", "w", encoding="utf-8").write("\n".join(keep))
print(len(lines), "lines in,", len(keep), "lines kept")import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
ids = enc.encode("I am ready for the interview")
print(ids, [enc.decode([i]) for i in ids])python train.py config/train_shakespeare_char.py --device=cpu --compile=False --eval_iters=20 --log_interval=1 --block_size=64 --batch_size=12 --n_layer=4 --n_head=4 --n_embd=128 --max_iters=2000 --lr_decay_iters=2000 --dropout=0.0
Apple Silicon Mac: use --device=mps instead of --device=cpu (faster).
Time: 1-2 hours. Machine: free Colab T4 GPU recommended.
Instead of training from zero, take a small open model and teach it YOUR style of answers with your own Q&A examples. We use SmolLM2-135M-Instruct (Apache-2.0 license, tiny) and Hugging Face TRL.
{"messages": [{"role": "user", "content": "Tell me about yourself"}, {"role": "assistant", "content": "Hi, I'm a final-year CS student. I love building small Python projects..."}]}
{"messages": [{"role": "user", "content": "Placements ki tips?"}, {"role": "assistant", "content": "1. Basics revise cheyyi. 2. Mock interviews cheyyi. 3. Resume ready chesko."}]}from datasets import load_dataset
from trl import SFTTrainer, SFTConfig
data = load_dataset("json", data_files="qa.jsonl", split="train")
data = data.train_test_split(test_size=0.2, seed=42) # 20 % kept aside = test data
trainer = SFTTrainer(
model="HuggingFaceTB/SmolLM2-135M-Instruct",
train_dataset=data["train"],
eval_dataset=data["test"],
args=SFTConfig(output_dir="my-model", num_train_epochs=3, report_to="none"),
)
trainer.train()
trainer.save_model("my-model")from transformers import pipeline
for name in ["HuggingFaceTB/SmolLM2-135M-Instruct", "my-model"]:
chat = pipeline("text-generation", model=name)
out = chat([{"role": "user", "content": "How should I prepare for an HR round?"}], max_new_tokens=80)
print(name, "→", out[0]["generated_text"][-1]["content"])batch_size / block_size, or switch to Colab GPU.