Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Intermediate ⏱ 50 min

🎙️ Build a Voice Assistant with Whisper and Gemini

🎯 What You'll Build

A voice assistant that records your speech, transcribes it locally with Whisper, and responds intelligently using the free Gemini API.

📋 What You'll Need

1

Record audio from the microphone

Use sounddevice to capture 5 seconds of audio and save it as a WAV file.

import sounddevice as sd
from scipy.io.wavfile import write
import numpy as np

SAMPLE_RATE = 16000
DURATION    = 5  # seconds

print("Recording... speak now!")
audio = sd.rec(int(DURATION * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1, dtype='int16')
sd.wait()
write("recording.wav", SAMPLE_RATE, audio)
print("Done recording.")
Recording... speak now!
Done recording.
2

Transcribe with Whisper (local, free)

Whisper runs entirely on your machine — no API key needed.

import whisper

model = whisper.load_model("base")  # tiny / base / small / medium / large
result = model.transcribe("recording.wav")
text = result["text"].strip()
print(f"You said: {text}")
You said: What is the capital of Japan?
3

Get a response from Gemini

Send the transcribed text to Gemini and print the reply.

import google.generativeai as genai

genai.configure(api_key="YOUR_GEMINI_API_KEY")
model = genai.GenerativeModel("gemini-1.5-flash")

response = model.generate_content(text)
print(f"\nAssistant: {response.text}")
Assistant: The capital of Japan is Tokyo.
4

Combine into a voice loop

Run it in a continuous loop until the user says "stop".

import sounddevice as sd
from scipy.io.wavfile import write
import whisper, google.generativeai as genai

genai.configure(api_key="YOUR_GEMINI_API_KEY")
chat_model  = genai.GenerativeModel("gemini-1.5-flash")
whisper_mdl = whisper.load_model("base")
SR = 16000

print("Voice Assistant ready. Say 'stop' to exit.\n")
while True:
    audio = sd.rec(int(5 * SR), samplerate=SR, channels=1, dtype='int16')
    sd.wait()
    write("rec.wav", SR, audio)
    text = whisper_mdl.transcribe("rec.wav")["text"].strip()
    print(f"You: {text}")
    if "stop" in text.lower():
        break
    reply = chat_model.generate_content(text).text
    print(f"Bot: {reply}\n")
💡 Tip: Add text-to-speech with pyttsx3 (`pip install pyttsx3`) to make the bot speak its replies aloud — one extra line after getting the response.

🎉 You Did It!

You built a fully working voice assistant using two free tools. Transcription runs locally, the AI response costs nothing on Gemini's free tier.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.