
We’ve generated audio and transcribed files. Now, let’s do it live with a Python Voice Assistant.
We will build a script that:
- Listens to your microphone in real-time.
- Transcribes the audio using the high-accuracy Whisper model (running locally).
- Checks the text for specific command keywords (e.g., “calculate”, “search”).
Step 1: Installation
We need the SpeechRecognition library (which handles the microphone) and openai-whisper. Note: You need ffmpeg installed on your system for audio processing.
pip install SpeechRecognition openai-whisper pyaudio
Install ffmpeg separately โ Whisper requires it and fails without it:
- macOS:
brew install ffmpeg - Ubuntu:
sudo apt install ffmpeg - Windows: download from ffmpeg.org and add to PATH
Step 2: The Code
import speech_recognition as sr
import whisper
import os
# 1. Load the Whisper model (small is fast and accurate enough)
print("Loading Whisper model...")
model = whisper.load_model("base")
def execute_command(text):
text = text.lower()
print(f"Command received: {text}")
if "open calculator" in text:
print("Opening Calculator...")
os.system("calc") # Windows only (use 'open -a Calculator' for Mac)
elif "stop listening" in text:
return False
return True
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Adjusting for ambient noise... Please wait.")
recognizer.adjust_for_ambient_noise(source)
print("Listening... (Say 'Stop Listening' to exit)")
while True:
try:
# Listen for audio
audio = recognizer.listen(source)
# Save to temp file for Whisper
with open("temp.wav", "wb") as f:
f.write(audio.get_wav_data())
# Transcribe using Whisper
result = model.transcribe("temp.wav")
text = result["text"]
if not execute_command(text):
break
except sr.UnknownValueError:
print("Could not understand audio")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
listen()Step 3: Run It
Speak clearly into your microphone: “Open Calculator”. The script will record you, process the audio with Whisper, and launch the app. You now have the framework to automate anything on your computer with your voice.
Key Takeaways
- Build a Python Voice Assistant that listens, transcribes audio with the Whisper model, and checks for command keywords.
- Install necessary libraries like SpeechRecognition and openai-whisper, and ensure ffmpeg is set up for audio processing.
- Run the script by speaking commands like ‘Open Calculator’ to automate tasks on your computer using voice.
- Be aware of common errors like invalid input device or missing ffmpeg in PATH to troubleshoot effectively.





