All guides

Build a Chat Page Around a Model Function with Gradio

Wrap a Python function that takes message and history in gr.ChatInterface, test it locally, check the history format, and stop streaming with the Stop button.

AINavWritten / Updated Sources checked
On this page

At a glance

If you already have a Python function that returns text for a user's input, whether it wraps a real model or is just a placeholder, Gradio's gr.ChatInterface can turn it into a chat page with very little code. This guide covers one task: write the chat function, wrap it in ChatInterface, launch it, verify that the message and history arrive as expected, and stop a streaming reply. The example uses a synthetic placeholder with no real data, so you can confirm the page and parameters first and then swap in your own model call.

Workflow

  1. Signature layer

    The callback in this basic configuration accepts two arguments, message followed by history; their names can differ. Extra required arguments need corresponding additional_inputs, which this guide does not use.

  2. History format layer

    Confirm history is a list of dictionaries with role and content, and that the simplest return value is a string.

  3. Page behavior layer

    After launch, send several turns and check whether replies change with the turn count and whether Stop appears when streaming.

Each stage cites official sources.

Before you start

  • A working Python environment and a terminal where you can run commands. This guide requires Python 3.10 or higher.
  • Gradio upgraded as the documentation advises: pip install --upgrade gradio.
  • A script file such as chat_demo.py to hold the function and launch code.
  • Two or three test questions with no private content; do not paste real customer or personal content.

1. Upgrade Gradio

In a terminal, run the official command pip install --upgrade gradio. Finish this before continuing so that parameters in an older version do not differ from the documentation.

2. Write the chat function

In chat_demo.py, define reply(message, history). message is the user's latest text as a string, and history is the list of earlier turns. The simplest function just returns a string as the assistant reply.

3. Wrap with ChatInterface and launch

Write gr.ChatInterface(fn=reply, title=..., description=...).launch(). The title and description appear above the chat box so visitors know what the page is for. Save, run the script in the terminal, and open http://localhost:7860 in a browser with the default launch settings.

4. Chat for several turns

Send three placeholder messages in the input box. Check that each reply reflects the number of earlier user turns, which confirms that history reaches the function and not only message.

5. Switch to streaming if needed

Complete the basic multi-turn check first. For streaming, save the complete slow_echo example below as streaming_demo.py. Stop the original script, run python streaming_demo.py, then send a sufficiently long synthetic message at the default local address. Each yield replaces the previous partial response. Click Stop before generation finishes and check that the generator stops.

6. Add examples and finish

Pass a list of strings to the examples parameter; they show as buttons before any message is sent. When the page works, interrupt the process in the terminal running the script to stop the service, then replace the placeholder logic with your own model call.

Original example

This synthetic sample calls no model:

python
import gradio as gr

def reply(message, history):
    turns = len([h for h in history if h["role"] == "user"]) + 1
    return f"Turn {turns}: you wrote {len(message)} characters."

gr.ChatInterface(
    fn=reply,
    title="Sample chat",
    description="Synthetic demo, no real data",
    examples=["Hello", "Say that again"],
).launch()

Run it and send "Hello" and then "Say that again". The second reply should show a turn number one higher than the first, which shows the history was passed in. Then replace the return line with your own model-calling code. Separate streaming exercise file streaming_demo.py:

python
import time
import gradio as gr

def slow_echo(message, history):
    for i in range(len(message)):
        time.sleep(0.3)
        yield "You typed: " + message[:i + 1]

gr.ChatInterface(fn=slow_echo).launch()

Completion checks

  • The first parameter receives only the latest message string, not the whole history.
  • After several turns, the reply changes with the history.
  • When streaming, a Stop button appears and clicking it halts output.
  • Example buttons appear before the first message; this guide does not specify their click-to-submit behavior.
  • The page contains no real names, accounts or internal material.

Limits

  • This covers only basic gr.ChatInterface use: no multimodal upload, additional inputs or outputs, saved chat history, or feedback collection.
  • The placeholder is not a real model. Reply quality, latency and cost depend on the service you connect later, and the documentation makes no promise about them.
  • For heavily customized chat layouts the documentation points to the lower-level Blocks API, which is a separate task.
  • The gr.load_chat route for OpenAI-compatible endpoints is outside this flow; read the official notes before using it.

Verification scope

This guide is based on reading the official documentation. I did not log in, install Gradio, run the sample code, execute any product API, upload files or produce generated output; the code is an illustrative sample written to follow the documented pattern and has not been run.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. Gradio · Official sources
    View details
  2. Gradio · Official sources
    View details