Python, from Zero, for AILesson 86 of 89

Module 9 · Building the thing: a small AI program, end to end

A web interface in twenty lines: Gradio, and what launch() actually starts

In this lesson

  • 8 min read
  • 9 reading sections
  • Knowledge check
  • Lesson 7 of 10 in this chapter

This chapter’s goalAssemble a complete tool from the course’s parts (a chat loop with bounded history, injection-resistant prompt templates, a sqlite response cache, a document chunker, a local embedding search over your own notes, a free local model as the fallback provider) expose it through a Gradio interface and a FastAPI endpoint, run it on a schedule without double-processing, and gate every change behind a GitHub Actions job that runs the tests.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:39Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 11Free · no account needed
Lesson steps

From a terminal to a page

The chat loop from earlier in this module reads with input() and writes with print(). Replacing those two with a web page is what Gradio (free, pip install gradio) does:

Python
import gradio as gr

def respond(message, history):
    messages = [{"role": "system", "content": SYSTEM}]
    for turn in history:
        content = turn["content"]
        if isinstance(content, list):    # Gradio 6 sends a list of parts: [{"type": "text", "text": ...}]
            content = "".join(p.get("text", "") for p in content if p.get("type") == "text")
        messages.append({"role": turn["role"], "content": content})
    messages.append({"role": "user", "content": message})
    return provider.complete(trim(messages, 4000, count))

demo = gr.ChatInterface(respond, title="Notes assistant")
demo.launch()

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘A web interface in twenty lines: Gradio, and what launch() actually starts’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/a-web-interface-with-gradio (Accessed: 21 November 2026).