Build a Local Voice AI Agent: Whisper + Ollama + Kokoro


Talk to your own machine

A voice assistant that runs entirely on your laptop is no longer a weekend research project. Three open pieces snap together into something you can talk to: Whisper turns speech into text, a local model served by Ollama writes the answer, and Kokoro reads it back out loud. Nothing leaves the machine — no API key, no monthly bill, no account.

This guide wires those three parts into one Python loop. By the end you will have a voice agent you can talk to, and a sense of where the latency comes from when a reply feels slow.



A local voice AI setup: microphone, laptop, and nothing in the cloud. Source: innoai.au

What you need

  • Python 3.10 or newer, and a working microphone
  • Ollama installed and running — if you have not set it up yet, start with our Ollama walkthrough
  • About 8 GB of free RAM, and roughly 3 GB of disk for the models
  • A quiet room helps more than a fast GPU: the whole stack runs on CPU.

Step 1: Pick a model that talks back fast

Voice is unforgiving about latency. A 70B model that writes beautiful paragraphs is the wrong tool when every word must be spoken aloud. Small 3–4B models answer in a second or two and keep replies short enough to listen to.

ollama pull qwen3:4b
ollama run qwen3:4b "Answer in one sentence: why is the sky blue?"

If the answer rambles, that is a prompting problem. Cap voice replies at two or three sentences in the system prompt. Llama 3.2 3B is a solid fallback if Qwen3 4B feels sluggish.

Step 2: Speech-to-text with faster-whisper

faster-whisper is the practical way to run Whisper locally: a CTranslate2 re-implementation that runs 2–4x faster on CPU and uses less memory than the original PyTorch package. The base.en model (142 MB) is the sweet spot — accurate enough for conversational speech, light enough to load in a second.

Whisper turns audio into text on your own machine. Source: breviu.com

pip install faster-whisper sounddevice numpy
python -c "from faster_whisper import WhisperModel; WhisperModel('base.en', device='cpu', compute_type='int8')"

The second command downloads the model on first run so the agent does not stall on it later. Whisper expects 16 kHz mono audio, which is what the recording code below produces. Model sizes run from tiny.en at 75 MB to large-v3 at 3.1 GB — see our guide to running Whisper locally for the full comparison.

Step 3: A voice with Kokoro TTS

Kokoro is an 82M-parameter open text-to-speech model that sounds far better than its size suggests, and the ONNX build runs in near real time on a laptop CPU. Version 1.0 ships 54 voices in a 28 MB file; the model itself is 326 MB. Grab both files:

curl -LO https://github.com/nazdridoy/kokoro-tts/releases/download/v1.0.0/kokoro-v1.0.onnx
curl -LO https://github.com/nazdridoy/kokoro-tts/releases/download/v1.0.0/voices-v1.0.bin
pip install kokoro-onnx soundfile

Then check the voice works before you wire it into anything:

from kokoro_onnx import Kokoro
import soundfile as sf

kokoro = Kokoro("kokoro-v1.0.onnx", "voices-v1.0.bin")
samples, sample_rate = kokoro.create(
    "Hello, I am your local voice assistant.",
    voice="af_heart", speed=1.0, lang="en-us",
)
sf.write("hello.wav", samples, sample_rate)

Play hello.wav — if it sounds like a person rather than a robot reading a manual, you are set. af_heart is a good default; the file also carries British voices (bf_emma, bf_isabella) and male voices (am_adam, am_michael). Our Kokoro TTS walkthrough covers voice selection in more detail.

Step 4: Wire it into one loop

Here is the whole agent: it records five seconds, transcribes, asks Ollama, and speaks the reply. Save it as voice_agent.py next to the Kokoro model files:

import numpy as np
import requests
import sounddevice as sd
from faster_whisper import WhisperModel
from kokoro_onnx import Kokoro
import soundfile as sf

SAMPLE_RATE = 16000
stt = WhisperModel("base.en", device="cpu", compute_type="int8")
tts = Kokoro("kokoro-v1.0.onnx", "voices-v1.0.bin")

SYSTEM = "You are a helpful voice assistant. Answer in at most two short sentences."

def listen(seconds=5):
    print("Listening...")
    audio = sd.rec(int(seconds * SAMPLE_RATE),
                   samplerate=SAMPLE_RATE, channels=1, dtype="float32")
    sd.wait()
    return audio.flatten()

def transcribe(audio):
    segments, _ = stt.transcribe(audio, language="en",
                                 beam_size=1, vad_filter=True)
    return " ".join(s.text.strip() for s in segments).strip()

def ask(text):
    r = requests.post("http://localhost:11434/api/chat", json={
        "model": "qwen3:4b",
        "messages": [{"role": "system", "content": SYSTEM},
                     {"role": "user", "content": text}],
        "stream": False,
    }, timeout=120)
    return r.json()["message"]["content"]

def speak(text):
    samples, rate = tts.create(text, voice="af_heart",
                               speed=1.0, lang="en-us")
    sd.play(samples, rate)
    sd.wait()

while True:
    heard = transcribe(listen())
    if not heard:
        continue
    print("You:", heard)
    if heard.lower() in ("goodbye", "stop", "quit"):
        speak("Goodbye.")
        break
    reply = ask(heard)
    print("Agent:", reply)
    speak(reply)

Run it with python voice_agent.py and say something. The first turn is slow while models load; after that each round takes a few seconds.

Where the time goes

Three knobs matter most. beam_size=1 switches Whisper from beam search to greedy decoding, noticeably faster on short utterances with no real accuracy cost. vad_filter=True skips silence instead of transcribing it. And the system prompt: every extra sentence the model writes is another second of speech, so the two-sentence cap is a latency feature, not just a style choice.

If replies still feel slow, the LLM is almost always the bottleneck: drop to a 3B model or move Ollama onto the GPU. If transcription lags instead, step down to tiny.en — half the size of base.en, fine for clear speech in a quiet room.

Troubleshooting

  • No microphone found: run python -c "import sounddevice as sd; print(sd.query_devices())" to list input devices. On Linux, missing PortAudio is the usual cause: sudo apt install portaudio19-dev, then reinstall sounddevice.
  • Ollama connection refused: start the daemon with ollama serve in a separate terminal, then retry.
  • Kokoro crashes on load: model and voices files must sit next to the script and their versions must match — v1.0 voices with the v1.0 model. A mismatched pair fails at startup.
  • Transcription returns empty text: check the recording level. A whisper-quiet input at 16 kHz gives Whisper nothing, and vad_filter will discard the whole clip as silence.
  • Robotic or clipped speech: keep replies short and avoid abbreviations, URLs, and long numbers — TTS reads them literally, and "w-w-w-dot" is no fun to listen to.

What to build next

A loop that listens for five seconds at a time is a starting point, not a product. Next steps: a wake word so it only listens when called, streaming TTS so speech starts before the full reply is written, and tool use so the agent can check the weather or run a command instead of only chatting. Each is a small addition to the loop above — and each one stays local, which is the whole point.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes