Create your own AI Assistant | Python | 2022

3 min read 4 hours ago
Published on Sep 28, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will learn how to build a voice-controlled AI assistant using Python. This project will enhance your programming skills while introducing you to concepts like speech recognition, web navigation, and data retrieval from sources like Wikipedia and Wolfram Alpha. By the end of this guide, you'll have a functional AI assistant that can respond to voice commands and provide information.

Step 1: Initial Setup

Before we begin coding, we need to set up our environment.

  1. Install Python: Ensure you have Python (version 3.x) installed on your computer. Download it from the official website.

  2. Install Required Libraries: Open your command line interface and run the following commands to install necessary libraries:

    pip install SpeechRecognition
    pip install pyttsx3
    pip install wikipedia-api
    pip install wolframalpha
    
  3. Create a Project Directory: Make a new folder for your project files.

Step 2: Set Up Speech Recognition

Now that we have our environment ready, we will set up the speech recognition system.

  1. Import Libraries: Start by creating a new Python file (e.g., ai_assistant.py) and import the necessary libraries:

    import speech_recognition as sr
    import pyttsx3
    
  2. Initialize Speech Recognition and Text-to-Speech Engine:

    recognizer = sr.Recognizer()
    engine = pyttsx3.init()
    
  3. Create a Function for Listening:

    def listen():
        with sr.Microphone() as source:
            print("Listening...")
            audio = recognizer.listen(source)
            return audio
    
  4. Convert Speech to Text:

    def speak(text):
        engine.say(text)
        engine.runAndWait()
    

Step 3: Web Navigation and Data Retrieval

Next, we will enhance our assistant's capabilities to fetch data from the web.

  1. Set Up Wikipedia Integration:

    import wikipediaapi
    
    wiki_wiki = wikipediaapi.Wikipedia('en')
    
  2. Create a Function to Retrieve Wikipedia Articles:

    def get_wikipedia_summary(query):
        page = wiki_wiki.page(query)
        if page.exists():
            return page.summary
        else:
            return "Sorry, I could not find that topic."
    
  3. Integrate Wolfram Alpha:

    import wolframalpha
    
    wolfram_client = wolframalpha.Client('YOUR_APP_ID')
    
  4. Create a Function for Wolfram Alpha Queries:

    def ask_wolfram(query):
        res = wolfram_client.query(query)
        return next(res.results).text
    

Step 4: Putting It All Together

Now we will combine everything into a main function that listens for commands and performs actions.

  1. Main Loop:

    def main():
        while True:
            audio = listen()
            try:
                command = recognizer.recognize_google(audio)
                print(f"You said: {command}")
    
                if "Wikipedia" in command:
                    topic = command.replace("Wikipedia", "")
                    summary = get_wikipedia_summary(topic.strip())
                    speak(summary)
    
                elif "calculate" in command:
                    calculation = command.replace("calculate", "")
                    answer = ask_wolfram(calculation.strip())
                    speak(answer)
    
            except sr.UnknownValueError:
                print("Sorry, I did not understand that.")
            except sr.RequestError:
                print("Could not request results from Google Speech Recognition service.")
    
  2. Run the Assistant: Call the main function at the bottom of your script:

    if __name__ == "__main__":
        main()
    

Conclusion

You've successfully created a basic voice-controlled AI assistant using Python. This assistant can listen to your commands and fetch information from Wikipedia and Wolfram Alpha.

Key Takeaways:

  • Set up Python and install necessary libraries.
  • Implement speech recognition and text-to-speech functionalities.
  • Fetch data from Wikipedia and Wolfram Alpha.

Next Steps:

  • Experiment with adding more features such as weather updates, reminders, or integration with other APIs.
  • Explore improving the natural language processing capabilities of your assistant for more complex interactions.