A voice assistant that records your speech, transcribes it locally with Whisper, and responds intelligently using the free Gemini API.
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.
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?
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.
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")
You built a fully working voice assistant using two free tools. Transcription runs locally, the AI response costs nothing on Gemini's free tier.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.