BUILD A LOCAL CHATBOT · MODULE 4 — PERSONALITY, GUARDRAILS, A REAL UI · bc-12 Real Python, every page

Module 4.12

Putting a face on it

Everything up to here has run in a terminal. Wrap bc-05's exact chat loop in Gradio — a Python library for quick web UIs — and it becomes something that looks and feels like a finished chatbot. No new AI mechanism is added here. Only a UI.

→ opens automatically at http://127.0.0.1:7860

Your Local Chatbot

Running entirely on this machine — no API key, no network call.

My name is Alex.
I'm Alex, a helpful AI assistant designed to assist users in navigating the world of technology...
What's my name?
My name is Alex, and I'm a helpful AI assistant...

The script

bc-05's loop, wrapped in a UI

bc-12-put-a-face-on-it.py
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")

def respond(message, history):
    # history is a list of {"role":.., "content":..} dicts Gradio keeps for us —
    # this is exactly bc-05's chat loop, fed by a UI instead of a list you append to
    messages = history + [{"role": "user", "content": message}]
    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
    outputs = model.generate(**inputs, max_new_tokens=100)
    return tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)

demo = gr.ChatInterface(fn=respond, type="messages", title="Your Local Chatbot")
demo.launch()

gr.ChatInterface handles the message box, the send button, and the growing message list on screen — your respond() function is the only new code, and it's bc-05's loop almost unchanged. Download bc-12-put-a-face-on-it.py ⬇ (needs pip install gradio first).

Try this

Run bc-12-put-a-face-on-it.py ⬇, open the printed local URL, and have the exact conversation shown above. Predict first: same replies as the mock above, or different? This model samples somewhat deterministically with greedy decoding, but small variations by hardware/library version are normal.

So what

A UI is presentation, not a new mechanism — everything underneath is still bc-01 through bc-11.