Building your own chat app sounds like a big job, but with Gradio and Ollama it's about a dozen lines of Python. Ollama runs the language model on your graphics card; Gradio wraps a streaming chat window around it in your browser. Because both run on your PC, your conversations stay local, and because it's your code, you can change the system prompt, add buttons or bolt on file handling whenever you like. This guide builds the app on Windows, then shows how to keep it private or share it safely.
What you need
Ollama, installed and working, with at least one model downloaded. If you haven't set it up, our guide to checking Ollama is using your GPU helps confirm it's running on the graphics card rather than the CPU. Our best GPUs for local LLMs guide covers how much VRAM different model sizes need.
uv to create a Python environment. See step 1 of our uv guide for local AI. Gradio needs Python 3.10 or later, which uv can download for you.
A text editor, such as VS Code or Notepad.
Want to compare Gradio with Streamlit, Chainlit and other frameworks first? See our best Python frameworks for local AI apps.
Step 1: Download a model in Ollama
Open PowerShell and pull a model if you don't already have one. This guide uses llama3.2, the model name Gradio's own documentation uses in its Ollama example, but any chat model you've pulled will do:
ollama pull llama3.2
Run ollama list to see the exact names of the models you have; you'll need one for the code.
Step 2: Create a project and install Gradio
Make a folder, create an environment and install Gradio plus Ollama's official Python library:
mkdir C:\ai-chat
cd C:\ai-chat
uv venv --python 3.12
uv pip install gradio ollama
You don't need PyTorch or a CUDA toolkit here, because Ollama does the GPU work in its own process. That also means the model stays loaded in Ollama when you restart your app.
Step 3: The one-line version
If you just want to see it working, Gradio's chatbot guide shows a single line that connects to any OpenAI-compatible server, giving Ollama's local address as its example. Install the extra package it needs:
uv pip install openai
Then save this as quick.py:
import gradio as gr
gr.load_chat("http://localhost:11434/v1/", model="llama3.2", token="ollama").launch()
Ollama's OpenAI compatibility documentation uses ollama as the API key and notes it's required but ignored, so any placeholder will do. Run it with .venv\Scripts\python quick.py and open the address it prints. This is handy, but writing your own function gives you control over what's sent to the model, so the rest of this guide does that.
Step 4: Write your own chat app
Save the following as app.py in the same folder, changing MODEL to a name from ollama list and the system prompt to whatever you want:
import gradio as gr
from ollama import chat
MODEL = "llama3.2"
SYSTEM = "You are a helpful assistant for a UK PC builder. Use UK English."
def respond(message, history):
messages = [{"role": "system", "content": SYSTEM}]
messages += [{"role": m["role"], "content": m["content"]} for m in history]
messages.append({"role": "user", "content": message})
reply = ""
for chunk in chat(model=MODEL, messages=messages, stream=True):
reply += chunk["message"]["content"]
yield reply
gr.ChatInterface(respond, title="My local AI chat").launch()
Here's what's happening:
gr.ChatInterface builds the whole chat window. Gradio calls your function with the new message and the history, which Gradio's documentation says is a list of OpenAI-style dictionaries with role and content keys (sometimes with extra metadata keys, which is why the code copies only those two).
chat(..., stream=True) is the Ollama Python library's streaming mode, which returns the reply in chunks.
yield reply sends the growing reply back to Gradio each time. Gradio's guide explains that yielding partial responses makes a streaming chatbot, and the Submit button becomes a Stop button while it's generating.
Step 5: Run it
From the project folder, run:
.venv\Scripts\python app.py
Gradio prints a local address, normally http://localhost:7860: its documentation says it searches for an available port starting at 7860. Open it in your browser and start chatting. While you're editing the code, run .venv\Scripts\gradio app.py instead: Gradio's quickstart explains that this hot reload mode reloads the app automatically whenever you save the file.
If replies are slow, the model is probably running partly on the CPU because it doesn't fit in your VRAM. Our guides to checking VRAM usage on Windows and setting Ollama's context length can help.
gr.ChatInterface has a few options worth knowing, all from Gradio's chatbot guide:
save_history=True keeps previous conversations in a side panel. Gradio stores them in the browser's local storage, not on a server.
examples=[...] adds starter prompts as buttons before the first message.
flagging_mode="manual" adds thumbs-up and thumbs-down buttons, saving rated replies and the chat history to a CSV file in the app's working directory; useful if you're comparing models or prompts.
For example: gr.ChatInterface(respond, title="My local AI chat", save_history=True, examples=["Explain VRAM in one paragraph"]).launch().
Step 7: Keep it private, or share it carefully
Out of the box, your app is only reachable from your own PC: Gradio's server host name defaults to 127.0.0.1. You have two ways to open it up, and both deserve care.
Other devices on your home network: launch with server_name="0.0.0.0" (for example .launch(server_name="0.0.0.0", auth=("me", "a-long-password"))). Windows Firewall may ask whether to allow Python. Gradio's auth= option adds a username and password login, but Gradio notes that its built-in authentication is basic and lacks features such as multi-factor authentication or rate limiting.
Anyone on the internet: .launch(share=True) creates a public gradio.live link that forwards to your PC. Gradio says share links expire after one week and are publicly accessible, so anyone with the link can use your model. Don't use it for anything that can touch your files.
Troubleshooting
Connection refused or a model-not-found error: make sure Ollama is running and that MODEL exactly matches a name in ollama list.
gradio or python isn't recognised: run commands from the project folder with the .venv\Scripts\ prefix shown above, or activate the environment with .venv\Scripts\activate.
The app opens on a different port: if 7860 is busy (usually another Gradio app is still running), Gradio's launch documentation says it searches for the next available port, so use the address it prints. To pin a port, use .launch(server_port=7861).
Ollama runs on another machine: the Ollama Python library can connect to a different host using its Client class; see our Ollama Python guide and sharing Ollama on your local network.