Module 4.12
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.
Running entirely on this machine — no API key, no network call.
The script
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).
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.