Projects · · 5 min read

Adding Voice Commands to My Python Chatbot with Gemini

A step‑by‑step look at how I gave my Python chatbot the ability to listen, using Whisper, Gemini and Supabase – all on a free tier.

A laptop on a wooden desk with a microphone, code window showing Whisper transcription and a chat interface

When I first thought about letting people talk to my Python chatbot instead of typing, I searched for a simple phrase: add voice commands to python chatbot. The idea felt natural after I had already added real‑time streaming and sentiment analysis, but I wasn’t sure which tools would work on my modest laptop in Pundri. In this post I walk through exactly how I wired a microphone, turned speech into text with Whisper, fed the transcript to Gemini, and stored the conversation in Supabase. I keep the code tiny, the costs zero, and the setup reproducible, so anyone can follow along.

Why I wanted to add voice commands to my Python chatbot

Typing on a phone keyboard feels clunky, especially when you’re juggling a coffee and a grocery list. I wanted a hands‑free way to ask my assistant for quick facts, set reminders, or just have a casual chat while I’m in the kitchen. Voice also opens the door to accessibility – friends with limited mobility could benefit. Since I already had a text‑based chatbot that talks to Gemini, adding a speech front‑end seemed like the next logical step, and it gave me a chance to experiment with open‑source speech models without paying for a cloud API.

Setting up the speech‑to‑text pipeline

I chose OpenAI’s Whisper model because it runs locally on a CPU and supports many languages, including Hindi. The installation is straightforward with pip, and the inference time on my 8 GB laptop is acceptable for short utterances. Here’s the minimal code I use to capture audio from the default microphone and get a transcription:

import whisper, sounddevice as sd, numpy as np

model = whisper.load_model("base")

def record(seconds=3, fs=16000):
    print("Listening…")
    audio = sd.rec(int(seconds * fs), samplerate=fs, channels=1, dtype="float32")
    sd.wait()
    return np.squeeze(audio)

def transcribe(audio):
    result = model.transcribe(audio, language="en")
    return result["text"].strip()

if __name__ == "__main__":
    wav = record()
    print("You said:", transcribe(wav))

The function `record` captures a few seconds of audio, and `transcribe` hands it to Whisper. I kept the model size small (base) to avoid long warm‑up times. If you have a more powerful machine you can swap in the "small" or "medium" variant for higher accuracy.

Sending the transcript to Gemini for understanding

Once I have the text, I treat it exactly like any other user message and send it to Gemini through the Claude Code extension I already use for other parts of the bot. The prompt I send includes a short system message that tells Gemini the user is speaking and that short, informal language is fine. The response comes back as a normal chat reply, which I then pass to the text‑to‑speech step.

If you haven’t seen my Gemini integration before, you can read about it in how I added real‑time chat streaming to my Python chatbot and in the guide on setting up a Python AI development environment. Those posts explain how I configure the Claude Code API key, handle rate limits, and keep the conversation context in Supabase.

Connecting the voice input to my Supabase backend

I store each conversation turn in a Supabase table called `messages`. The schema has `id`, `user_id`, `role` (user or assistant), `content`, and `created_at`. Adding the voice turn is no different from a typed turn – I just insert the Whisper transcript as the `content` field. This approach lets me replay any session later, run analytics, or even fine‑tune a model on my own data.

from supabase import create_client

url = "https://YOUR-PROJECT.supabase.co"
key = "public-anon-key"
supabase = create_client(url, key)

def save_message(user_id, role, text):
    data = {
        "user_id": user_id,
        "role": role,
        "content": text,
    }
    supabase.table("messages").insert(data).execute()

# Example usage after transcription
user_text = transcribe(record())
save_message("krish", "user", user_text)
assistant_reply = gemini_chat(user_text)
save_message("krish", "assistant", assistant_reply)

Because Supabase gives me instant Realtime updates, the front‑end can listen for new `assistant` rows and play them back using a simple text‑to‑speech library like pyttsx3. I added a tiny UI in VS Code’s Live Server that shows the conversation and plays the audio automatically. If you’re curious about the realtime part, my post on adding real‑time chat streaming goes into detail.

Testing, tweaking, and what I learned

The first version felt a bit robotic – Whisper sometimes mis‑heard regional accents, and Gemini would answer with overly formal language. I fixed the former by adding a small noise‑reduction filter using `scipy.signal`. The latter was solved by adding a short user‑style example in the prompt: “Speak like a friendly neighbor from Haryana.” This tiny prompt tweak made the replies sound much more natural to my ears.

Performance is still the biggest bottleneck. Whisper on CPU takes about 2 seconds for a 3‑second clip, which is acceptable for casual use but not for fast back‑and‑forth. If you need lower latency, you can run Whisper on a cheap GPU instance or switch to a cloud STT service for the recording phase. The rest of the pipeline – Gemini, Supabase – is already running on free tiers, so the overall cost stays near zero.

You can try the whole thing yourself by cloning the repo I mentioned in the code blocks and following the steps. I’ve also added a link to the live demo on my site, so you can hear the bot speak back without setting up anything: my projects.

Takeaway

Adding voice commands to a Python chatbot is surprisingly straightforward when you combine Whisper for speech‑to‑text, Gemini for language understanding, and Supabase for storage. The biggest lesson for me was that a good prompt can smooth out many of the rough edges that come from noisy audio. Start small, keep the pipeline modular, and you’ll have a hands‑free assistant that works on a laptop in a small town just as well as on a cloud server.

Frequently asked questions

What library can I use for speech‑to‑text in Python?

I used OpenAI’s Whisper model because it runs locally on a CPU, supports many languages, and can be installed with pip. It provides good accuracy for short utterances without any cloud cost.

Do I need a GPU to run Whisper for a chatbot?

A GPU speeds up Whisper considerably, but the "base" model works on a regular laptop CPU. Expect about 2 seconds of processing for a three‑second clip, which is fine for casual use.

How do I store voice conversations in Supabase?

Treat the transcribed text like any other chat message. Insert it into a `messages` table with columns for user ID, role, content and timestamp. Supabase Realtime then pushes new rows to the front‑end automatically.

Can I make the assistant speak back to the user?

Yes. After Gemini returns a text reply, you can feed it to a text‑to‑speech library such as pyttsx3 or gTTS, then play the audio in the UI. This completes the full voice‑in‑voice‑out loop.

Related posts