How to Build a Production-Ready Gemma 3 1B Order Generation AI Pipeline with Hugging Face Transformers, Conversation Templates, and Colab Inference

In this tutorial, we create and implement a Colab workflow Gemma 3 1B Teach using Hugging Face Transformers and HF Tokens, in a practical, reproducible, and easy-to-follow step-by-step method. We start by installing the required libraries, securely authenticating with our Hugging Face token, and uploading the token and model to an available device with the correct intuitive settings. Since then, we’ve been building reusable resources, formatting information in a dialog-style structure, and testing the model on many practical tasks such as basic generation, structured JSON-style responses, fast chaining, scaling, and deterministic summarization, so that we’re not only loading Gemma but actually working with it in a meaningful way.
import os
import sys
import time
import json
import getpass
import subprocess
import warnings
warnings.filterwarnings("ignore")
def pip_install(*pkgs):
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", *pkgs])
pip_install(
"transformers>=4.51.0",
"accelerate",
"sentencepiece",
"safetensors",
"pandas",
)
import torch
import pandas as pd
from huggingface_hub import login
from transformers import AutoTokenizer, AutoModelForCausalLM
print("=" * 100)
print("STEP 1 — Hugging Face authentication")
print("=" * 100)
hf_token = None
try:
from google.colab import userdata
try:
hf_token = userdata.get("HF_TOKEN")
except Exception:
hf_token = None
except Exception:
pass
if not hf_token:
hf_token = getpass.getpass("Enter your Hugging Face token: ").strip()
login(token=hf_token)
os.environ["HF_TOKEN"] = hf_token
print("HF login successful.")Set up the necessary environment to run the tutorial properly in Google Colab. We install the required libraries, import all dependencies, and authenticate securely with Hugging Face using our token. At the end of this section, we will configure the notebook to access the Gemma model and continue the workflow without manual setup problems.
print("=" * 100)
print("STEP 2 — Device setup")
print("=" * 100)
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
print("device:", device)
print("dtype:", dtype)
model_id = "google/gemma-3-1b-it"
print("model_id:", model_id)
print("=" * 100)
print("STEP 3 — Load tokenizer and model")
print("=" * 100)
tokenizer = AutoTokenizer.from_pretrained(
model_id,
token=hf_token,
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
token=hf_token,
torch_dtype=dtype,
device_map="auto",
)
model.eval()
print("Tokenizer and model loaded successfully.")We optimize the runtime by determining whether we are using the GPU or the CPU and choose the appropriate precision to load the model properly. We then define the Gemma 3 1 B model method and load both the tokenizer and the model from Hugging Face. In this section, we complete the implementation of the main model, which makes the notebook ready for text generation.
def build_chat_prompt(user_prompt: str):
messages = [
{"role": "user", "content": user_prompt}
]
try:
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
except Exception:
text = f"usern{user_prompt}nmodeln"
return text
def generate_text(prompt, max_new_tokens=256, temperature=0.7, do_sample=True):
chat_text = build_chat_prompt(prompt)
inputs = tokenizer(chat_text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=do_sample,
temperature=temperature if do_sample else None,
top_p=0.95 if do_sample else None,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.eos_token_id,
)
generated = outputs[0][inputs["input_ids"].shape[-1]:]
return tokenizer.decode(generated, skip_special_tokens=True).strip()
print("=" * 100)
print("STEP 4 — Basic generation")
print("=" * 100)
prompt1 = """Explain Gemma 3 in plain English.
Then give:
1. one practical use case
2. one limitation
3. one Colab tip
Keep it concise."""
resp1 = generate_text(prompt1, max_new_tokens=220, temperature=0.7, do_sample=True)
print(resp1) We create reusable formatter functions that help structure the expected dialog and handle text generation from the model. We make the indexing pipeline modular so that we can reuse the same function across different tasks in the notebook. After that, we use the first working example to verify that the model works well and produces reasonable output.
print("=" * 100)
print("STEP 5 — Structured output")
print("=" * 100)
prompt2 = """
Compare local open-weight model usage vs API-hosted model usage.
Return JSON with this schema:
{
"local": {
"pros": ["", "", ""],
"cons": ["", "", ""]
},
"api": {
"pros": ["", "", ""],
"cons": ["", "", ""]
},
"best_for": {
"local": "",
"api": ""
}
}
Only output JSON.
"""
resp2 = generate_text(prompt2, max_new_tokens=300, temperature=0.2, do_sample=True)
print(resp2)
print("=" * 100)
print("STEP 6 — Prompt chaining")
print("=" * 100)
task = "Draft a 5-step checklist for evaluating whether Gemma fits an internal enterprise prototype."
resp3 = generate_text(task, max_new_tokens=250, temperature=0.6, do_sample=True)
print(resp3)
followup = f"""
Here is an initial checklist:
{resp3}
Now rewrite it for a product manager audience.
"""
resp4 = generate_text(followup, max_new_tokens=250, temperature=0.6, do_sample=True)
print(resp4)We push the model beyond simple information by evaluating structured output generation and rapid integration. We ask Gemma to return the response in a defined format such as JSON and use the trace command to modify the previous response for a different audience. This helps us see how the model handles formatting constraints and multi-step development in a realistic workflow.
print("=" * 100)
print("STEP 7 — Mini benchmark")
print("=" * 100)
prompts = [
"Explain tokenization in two lines.",
"Give three use cases for local LLMs.",
"What is one downside of small local models?",
"Explain instruction tuning in one paragraph."
]
rows = []
for p in prompts:
t0 = time.time()
out = generate_text(p, max_new_tokens=140, temperature=0.3, do_sample=True)
dt = time.time() - t0
rows.append({
"prompt": p,
"latency_sec": round(dt, 2),
"chars": len(out),
"preview": out[:160].replace("n", " ")
})
df = pd.DataFrame(rows)
print(df)
print("=" * 100)
print("STEP 8 — Deterministic summarization")
print("=" * 100)
long_text = """
In practical usage, teams often evaluate
trade-offs among local deployment cost, latency, privacy, controllability, and raw capability.
Smaller models can be easier to deploy, but they may struggle more on complex reasoning or domain-specific tasks.
"""
summary_prompt = f"""
Summarize the following in exactly 4 bullet points:
{long_text}
"""
summary = generate_text(summary_prompt, max_new_tokens=180, do_sample=False)
print(summary)
print("=" * 100)
print("STEP 9 — Save outputs")
print("=" * 100)
report = {
"model_id": model_id,
"device": str(model.device),
"basic_generation": resp1,
"structured_output": resp2,
"chain_step_1": resp3,
"chain_step_2": resp4,
"summary": summary,
"benchmark": rows,
}
with open("gemma3_1b_text_tutorial_report.json", "w", encoding="utf-8") as f:
json.dump(report, f, indent=2, ensure_ascii=False)
print("Saved gemma3_1b_text_tutorial_report.json")
print("Tutorial complete.")We test the model across a small data benchmark to look at response, latency, and output length in a combined test. We then perform a summary deterministic function to see how the model behaves when the randomness is reduced. Finally, we save all major results to a report file, turning the notebook into a usable test setup with a short-term demo.
In conclusion, we have a complete script generation pipeline that shows how Gemma 3 1B can be used in Colab for practical testing and lightweight prototyping. We performed direct responses, compared the results across the prompting styles, measured simple delay behavior, and saved the results to a report file for later analysis. In doing so, we’ve turned the notebook into more than one demo: we’ve made it a reusable base for testing commands, testing output, and integrating Gemma into larger workflows with confidence.
Check it out Complete Coding Notebook here. Also, feel free to follow us Twitter and don’t forget to join our 120k+ ML SubReddit and Subscribe to Our newspaper. Wait! are you on telegram? now you can join us on telegram too.



