← Back to list

πŸ€– AI Speech-Based Agent Project: Automate Setup of uv, PyAudio, and SpeechRecognition

Automate Python Environment Setup for Real-Time Speech Recognition and TTS

Dr. Shouke Wei Β· 2025-07-05 03:44 Β· 0 claps Β· 4.2 min read paywalled
#voice-agent #automate-setup #pyaudio #speech-recognition #chatbot-development
Open on Medium β†—
Wiki topics: AGT · AI Agents MM · Multimodal & Generative Media 🎡 · Music & Audio

πŸ€– AI Speech-Based Agent Project: Automate Setup of uv, PyAudio, and SpeechRecognition

Automate Python Environment Setup for Real-Time Speech Recognition and TTS

πŸ€– AI Speech-Based Agent Project: (I) Automate Setup of whisper.cpp

πŸ€– AI Speech-Based Agent Project: (II) Automate Setup of llama.cpp

πŸ€– AI Speech-Based Agent Project: Automate Setup of uv, PyAudio, and SpeechRecognition

🧩 Introduction

If you’re building an AI voice assistant, smart speaker, or chatbot that listens and talks, your Python project needs a reliable setup that includes:

  • Microphone and speaker access (PyAudio)
  • Speech-to-text capabilities (SpeechRecognition)
  • Text-to-speech output (edge-tts)
  • A fast and modern dependency manager (uv)

Manually setting all this up every time is repetitive and error-prone. In this tutorial, we’ll automate the entire setup using a simple Bash script.

❓ Why These Packages?

Here’s why the key packages are used:

These tools give your AI agent the ability to hear, understand, and speak.

πŸ’‘ Why an Automatic Bash Script?

Instead of manually running 6–10 shell commands every time:

Reproducibility and ease of onboarding are key for any AI project.

Instead of manually:

  • Installing system packages
  • Creating a virtual environment
  • Installing Python dependencies
  • Automate installation for consistency
  • Avoid forgetting dependencies
  • Ensure uv and environment setup are done properly
  • Make it easy to onboard new team members or rebuild your environment later

You can automate it all with a single script that runs in seconds. This ensures:

  • πŸ” Consistent environments
  • πŸ§ͺ Fewer setup mistakes
  • πŸ‘₯ Faster onboarding for teammates

πŸ› οΈ Step-by-Step: Build the Bash Script

Let’s break the script into parts so you understand how it works.

βœ… Step 1: Script Header

#!/bin/bash
set -e

This tells the system to run the script using Bash and stop if any command fails.

βœ… Step 2: Configuration

PROJECT_DIR="voice_project"
PYTHON_VERSION="python3.10"

Customize your project folder and desired Python version.

βœ… Step 3: Install System Dependencies

sudo apt update
sudo apt install -y portaudio19-dev

Install the required audio library (portaudio) to enable microphone access through PyAudio.

βœ… Step 4: Ensure uv is Installed

if ! command -v uv &> /dev/null; then
    curl -Ls https://astral.sh/uv/install.sh | bash
    export PATH="$HOME/.cargo/bin:$PATH"
fi

If uv isn’t installed, download and install it. This will let you manage dependencies efficiently.

βœ… Step 5: Create Project Directory

if [ ! -d "$PROJECT_DIR" ]; then
    mkdir "$PROJECT_DIR"
fi
cd "$PROJECT_DIR"

Create your project folder (if needed) and switch into it.

βœ… Step 6: Set Up the Virtual Environment

uv .venv --python "$PYTHON_VERSION"
source .venv/bin/activate

Create and activate a virtual environment using uv.

βœ… Step 7: Add Required Packages

uv add pyaudio SpeechRecognition edge-tts

Install the core packages your AI agent needs to listen and speak.

βœ… Step 8: Export a Reproducible Requirements File

uv export --without-hashes > requirements.txt

Create a requirements.txt so others (or CI/CD) can replicate the environment with pip.

πŸ“„ Full Script: install.sh

#!/bin/bash
set -e

PROJECT_DIR="voice_project"
PYTHON_VERSION="python3.10"
echo "πŸ“¦ Installing system dependencies..."
sudo apt update
sudo apt install -y portaudio19-dev
echo "πŸ§ͺ Checking if uv is installed..."
if ! command -v uv &> /dev/null; then
    echo "πŸ”§ Installing uv..."
    curl -Ls https://astral.sh/uv/install.sh | bash
    export PATH="$HOME/.cargo/bin:$PATH"
fi
echo "πŸ“ Creating project directory if not exists..."
if [ ! -d "$PROJECT_DIR" ]; then
    mkdir "$PROJECT_DIR"
fi
cd "$PROJECT_DIR"
echo "πŸŒ€ Creating virtual environment..."
uv .venv --python "$PYTHON_VERSION"
echo "πŸš€ Activating virtual environment..."
source .venv/bin/activate
echo "πŸ“¦ Installing Python packages..."
uv add pyaudio SpeechRecognition edge-tts
echo "πŸ“„ Exporting requirements.txt..."
uv export --without-hashes > requirements.txt
echo "βœ… Setup complete! You can now start coding."

πŸš€ How to Use

Follow these simple steps to automatically set up your AI speech-based project:

1. βœ… Save the Script

Create a new file named voice_recong_tts_install.sh and paste in the full script:

nano voice_recong_tts_install.sh
# Or use your preferred editor

Make the script executable:

chmod +x voice_recong_tts_install.sh

2. πŸƒβ€β™‚οΈ Run the Script

Run the script from your terminal:

./voice_recong_tts_install.sh

This will:

  • Install system dependencies (portaudio19-dev)
  • Install uv if missing
  • Create a project folder (e.g., voice_project)
  • Set up a virtual environment using uv
  • Install pyaudio, SpeechRecognition, edge-tts
  • Export a requirements.txt file

3. πŸ” Activate the Environment (Later Sessions)

Each time you return to your project, activate the environment manually:

cd voice_project
source .venv/bin/activate

4. πŸ§ͺ Test the Setup

🎀 STT Test: stt_test.py

You can now create a stt_test.py to test your microphone and speech-to-text:

import speech_recognition as sr

# Initialize recognizer
recognizer = sr.Recognizer()

# Use the default microphone as the audio source
with sr.Microphone() as source:
    print("πŸŽ™οΈ Say something...")
    recognizer.adjust_for_ambient_noise(source)  # Optional: better accuracy
    audio = recognizer.listen(source)

print("πŸ”Š Recognizing...")

# Try to recognize speech using Google Web Speech API
try:
    text = recognizer.recognize_google(audio)
    print("βœ… You said:", text)
except sr.UnknownValueError:
    print("❌ Could not understand audio.")
except sr.RequestError as e:
    print(f"⚠️ Could not request results; {e}")

▢️ How to Run

Make sure your virtual environment is activated:

source .venv/bin/activate

Then run:

python stt_test.py

Speak clearly into your microphone when prompted.

πŸ”Š TTS Test: tts_test.py

import asyncio
from edge_tts import Communicate

async def main():
    text = "Hello! I am your AI speech agent. This is a text-to-speech test."
    voice = "en-US-AriaNeural"  # You can change this to any available Microsoft voice
    communicate = Communicate(text, voice)
    await communicate.save("tts_output.mp3")
    print("βœ… Speech saved to tts_output.mp3")
asyncio.run(main())

▢️ How to Run

python tts_test.py

Then play the generated file:

# Use your system's audio player
ffplay tts_output.mp3       # If you have ffmpeg
# or
mpg123 tts_output.mp3       # On some Linux systems
# or just double-click it in your file explorer

βœ… Summary

With this one-click voice_recong_tts_install.shscript, you now have:

  • A full project directory with a virtual environment
  • Leverage uv for a fast and modern Python development workflow
  • All dependencies installed (pyaudio, speechrecognition, edge-tts)
  • Set up a clean Python project with audio and speech capabilities
  • A reproducible requirements.txt
  • A clean start for building your AI speech-based assistant
  • Avoid manual setup errors

This script is perfect for projects like voice assistants, speech-to-text apps, or conversational bots.


메타데이터
post_id
dfae2ccc2106
slug
ai-speech-based-agent-project-automate-setup-of-uv-pyaudio-and-speechrecognition-dfae2ccc2106
url
https://medium.com/@shouke.wei/ai-speech-based-agent-project-automate-setup-of-uv-pyaudio-and-speechrecognition-dfae2ccc2106
canonical_url
https://medium.com/@shouke.wei/ai-speech-based-agent-project-automate-setup-of-uv-pyaudio-and-speechrecognition-dfae2ccc2106
author_url
https://medium.com/@shouke.wei
status
ok
fetched_at
2026-08-26 15:49:19