Skip to content

How-ToGPU

How to Build Your Own Local AI Chat App with Gradio and Ollama

Build a private, streaming AI chat app on your own PC in about a dozen lines of Python: Ollama runs the model on your GPU and Gradio's ChatInterface supplies the chat window. Includes a one-line version, extras and safe sharing.

By , Systems & Technical Editor

5 min read

How to Build Your Own Local AI Chat App with Gradio and Ollama

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.

Step 6: Useful extras

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.

The bottom line

Install Gradio and the Ollama Python library into a uv environment with uv pip install gradio ollama, write a function that sends the history to Ollama with stream=True and yields the growing reply, wrap it in gr.ChatInterface, and run it to get a private streaming chat app at http://localhost:7860. Use gradio app.py for hot reload while you edit, save_history=True for saved conversations, and only open it up with server_name="0.0.0.0" plus a password, or a one-week share=True link, when you really need to.

Sources

  1. 01Gradio documentation, Quickstart
  2. 02Gradio documentation, How to Create a Chatbot with Gradio
  3. 03Gradio documentation, Sharing Your App
  4. 04Gradio documentation, Environment Variables
  5. 05Gradio documentation, Blocks (launch parameters)
  6. 06GitHub, ollama/ollama-python (README)
  7. 07Ollama documentation, OpenAI compatibility
  8. 08uv documentation, Using Python environments

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

How to Choose an AI Upscaling Model: Using OpenModelDB to Find the Right One

How-To

How to Choose an AI Upscaling Model: Using OpenModelDB to Find the Right One

AI upscalers are only as good as the model you use. We explain how to use OpenModelDB to find the right one: subject and purpose tags, scale, architecture and app compatibility, file formats for PyTorch, ONNX and NCNN, licences, and how to shortlist and test models on your own images.

Marcus Thorne5 min

chaiNNer vs Upscayl: Which Free AI Image Upscaler Should You Use?

Versus

chaiNNer vs Upscayl: Which Free AI Image Upscaler Should You Use?

chaiNNer and Upscayl are two free, open source AI image upscalers you can run on your own PC. We compare ease of use, GPU support, model support, batch and video processing, how actively each is maintained, and licensing to help you choose.

Callum Reid6 min

How to Batch Upscale Images With chaiNNer: Free, Local AI Upscaling for Any GPU

How-To

How to Batch Upscale Images With chaiNNer: Free, Local AI Upscaling for Any GPU

chaiNNer is a free, open source, node-based app for running AI upscaling models locally. We cover which framework to install for NVIDIA, AMD and Intel graphics cards, downloading a model, building a simple upscale chain, turning it into a folder batch job and fixing black output and other common errors.

Marcus Thorne7 min

Best Monitor Control Apps for Windows in 2026: Ditch the OSD Buttons

Best Picks

Best Monitor Control Apps for Windows in 2026: Ditch the OSD Buttons

Most big monitor brands offer a free Windows app that replaces fiddly OSD buttons with mouse controls, plus extras such as layouts, per-app modes and firmware updates. Using each maker's own pages, we pick five: Dell Display and Peripheral Manager, ASUS DisplayWidget Center, LG OnScreen Control, BenQ Display Pilot 2 and Gigabyte OSD Sidekick.

Hannah Vance5 min