Home/Lab/Gita Fine-tuning Demo
AI/MLEnterprise AILLMsOpen Source

Fine-tuning a Language Model: A Working Demo

What it is, when to use it, and why RAG might serve you better. Built with GPT-2, Bhagavad Gita text, and a Colab T4 GPU.


The Demo

Ask the same question to base GPT-2 and the Gita model

Side-by-side responses reveal exactly what fine-tuning changes — and what it doesn't.

Live on Streamlit Cloud

Models load from HuggingFace Hub. Generation runs on CPU — allow 15–30 s for the first response.

Open Demo →

Opens in a new tab · Streamlit Cloud · free tier


What I Built

The pipeline, start to finish

Data: The full text of the Bhagavad Gita from Project Gutenberg (#2388, Sir Edwin Arnold translation). Pre-processed into 100-word chunks, split 80/20 for training and validation.

Training: GPT-2 (124M parameters) fine-tuned using HuggingFace Transformers Trainer API. 3 epochs on a Google Colab T4 GPU — about 30 minutes total. The notebook is fully reproducible.

The side-by-side comparison is the whole point. Base GPT-2 drifts into generic internet text within a sentence. The fine-tuned model stays in the vocabulary of the Gita — dharma, karma, Arjuna — because that's the only text it saw during fine-tuning.

What the demo honestly shows: fine-tuning changes style and vocabulary. It does not give a model factual understanding. For accurate Q&A on a document, RAG is the right call.


Key Learnings

Fine-tuning vs RAG — the decision framework

Most practitioners reach for fine-tuning when RAG would serve them better. Here's when each wins.

DimensionFine-tuningRAG
What it teachesStyle and toneNothing — retrieves facts
Factual accuracyCan hallucinateGrounded in source text
Source citationsNoYes
CostOne-time training costInference + vector DB cost
Data updatesRequires retrainingUpdate the index
LatencyFast (no retrieval)Adds retrieval overhead
Best forStyle transfer, format enforcementQ&A, fact lookup, live data

When fine-tuning IS the right choice

📋
Format enforcement
SAP change-request outputs must follow a specific XML schema. Fine-tuning on examples teaches the model that schema. RAG cannot enforce format.
✍️
Style transfer
Legal contracts written in your firm's specific voice. A fine-tuned model internalises that style at scale.
Latency-critical tasks
No retrieval overhead. A fine-tuned model responds in one forward pass — decisive for embedded or mobile apps.
🔒
Proprietary task types
Internal framework code review with no public documentation to retrieve from. The training data IS the knowledge.
🏥
Domain vocabulary
Medical, legal, or financial terminology where the base model generates plausible but wrong jargon.
The architecture that actually works
RAG layer          → retrieves the right passage for the question
Fine-tuned model   → formats and explains it in a specific voice
Structured output  → cited, formatted, on-brand

RAG handles what to say. Fine-tuning handles how to say it. Neither alone is sufficient for enterprise Q&A.


Resources

Everything open source

GitHub Repo
Full code, notebook, and data pipeline
Fine-tuned Model (HuggingFace)
rupeshpanda/gita-text-generation-gpt2
Training Notebook (Colab)
Reproducible, runs free on T4 GPU
Live Demo (Streamlit)
Side-by-side base vs fine-tuned
Experimenting with AI in your enterprise?

Let's talk about what fine-tuning, RAG, and agentic systems can actually do for your organisation.

Get in touch