Daily Tech Briefing
AI 科技速览
每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。
Dev.to AI · 2026/7/29 02:00:04
Build a Voice Assistant with Python and Whisper
AI 中文解读
现在你可以用Python和OpenAI的Whisper模型,在自己电脑上搭建一个完全本地运行、无需联网的语音助手!它既能保护你的隐私,又能像Siri或Alexa一样流畅对话,而且完全免费、可定制。
Whisper是OpenAI开源的语音识别模型,经过海量多语言数据训练,即使环境嘈杂、口音各异也能准确转写文字。搭配Python的录音、文字处理和语音合成库,你只需几步就能实现“说话→识别→回答”的完整流程。整个过程都在本地完成,数据不会上传到云端,响应速度也更快。
对普通人来说,这意味着你不再需要依赖第三方云服务来控制电脑、查询信息或执行脚本。比如对着麦克风说“打开我的音乐文件夹”或“帮我查一下明天的天气”,系统都能立刻响应。开发者更是可以低成本定制专属语音控制界面,从智能家居到办公自动化,应用场景非常广。随着Whisper不断优化,未来语音交互将变得更私密、更可靠,普通人也能轻松拥有“私人AI助理”。
<h1>
Build a Voice Assistant with Python and Whisper
</h1>
<p>tags: python, ai, audio, tutorial</p>
<p>tags: python, ai, audio, tutorial</p>
<p>Imagine hearing your computer respond to your voice with the same nuance and clarity as a human, all running locally on your own machine without sending a single byte of data to the cloud. That’s the power of building a Voice Assistant with Python and OpenAI’s Whisper model. While cloud-based assistants like Siri or Alexa are convenient, they come with privacy trade-offs and latency issues. By combining Whisper’s state-of-the-art speech-to-text capabilities with Python’s flexibility, you can create a <strong>private, fast, and customizable</strong> voice interface that answers your questions, controls your scripts, or even just chats with you—right from your terminal.</p>
<p>Let’s get your hands dirty and build one from scratch today.</p>
<h2>
Why Whisper and Python?
</h2>
<p>OpenAI’s Whisper is a transformer-based model trained on 680,000 hours of multilingual and multitask supervised data. Unlike older speech recognition tools that struggled with background noise or specific accents, Whisper delivers <strong>high accuracy</strong> across diverse environments [5]. It’s open-source, meaning you can run it locally, ensuring your conversations stay private.</p>
<p>Python is the natural partner for this task. With libraries like <code>SpeechRecognition</code>, <code>pyttsx3</code>, and <code>openai-whisper</code> (or its faster variant <code>faster-whisper</code>), you can stitch together the entire pipeline: capturing audio, transcribing it, processing the text with an LLM, and speaking the response back [6][7].</p>
<h2>
Setting Up Your Environment
</h2>
<p>Before writing code, you need the right tools. Start by creating a dedicated project directory and a virtual environment to keep dependencies clean.<br>
</p>
<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">mkdir </span>voice-assistant <span class="o">&&</span> <span class="nb">cd </span>voice-assistant
python <span class="nt">-m</span> venv venv
<span class="nb">source </span>venv/bin/activate <span class="c"># On Windows: venv\Scripts\activate</span>
</code></pre>
</div>
<p>Next, install the core speech processing stack. While <code>openai-whisper</code> works well, <code>faster-whisper</code> is often preferred for production due to its optimized performance and lower memory usage [2][5].<br>
</p>
<div class="highlight js-code-highlight">
<pre class="highlight shell"><code>pip <span class="nb">install </span><span class="nv">SpeechRecognition</span><span class="o">==</span>3.10.4 <span class="se">\</span>
faster-whisper <span class="se">\</span>
<span class="nv">pyttsx3</span><span class="o">==</span>2.98 <span class="se">\</span>
python-dotenv<span class="o">==</span>1.0.1
</code></pre>
</div>
<p>You’ll also need an API key if you plan to send the transcribed text to an LLM like OpenAI’s GPT or Anthropic’s Claude. Store this securely in a <code>.env</code> file to avoid hardcoding secrets:<br>
</p>
<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="c"># .env</span>
<span class="nv">ANTHROPIC_API_KEY</span><span class="o">=</span>sk-ant-api03-your-key-here
<span class="nv">OPENAI_API_KEY</span><span class="o">=</span>your-openai-key-here
</code></pre>
</div>
<h2>
The Core Logic: Speech-to-Text and Text-to-Speech
</h2>
<p>The heart of your assistant is the loop: <strong>Listen → Transcribe → Process → Speak</strong>. Let’s break down the first two steps.</p>
<h3>
Capturing and Transcribing Audio
</h3>
<p>We’ll use <code>SpeechRecognition</code> to capture microphone input and <code>faster-whisper</code> to transcribe it. The <code>recognizer</code> object handles the audio stream, while the <code>whisper</code> model converts audio waves into text.<br>
</p>
<div class=
分享
阅读原文 ↗