Voice based Q&A assistant for knowledge bases Python

👤 Sharing: AI
```python
# Import necessary libraries
import speech_recognition as sr
import pyttsx3
import json

# --- 1. Load Knowledge Base ---
def load_knowledge_base(filepath):
    """Loads a JSON knowledge base from a file.

    Args:
        filepath (str): The path to the JSON file.

    Returns:
        dict: The knowledge base as a dictionary.
    """
    with open(filepath, 'r') as file:
        data = json.load(file)
    return data


# --- 2. Speech Recognition ---
def recognize_speech():
    """Listens for user speech and returns the recognized text.

    Returns:
        str: The recognized text, or None if speech recognition fails.
    """
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        recognizer.adjust_for_ambient_noise(source)  # Adjust for background noise
        try:
            audio = recognizer.listen(source, timeout=5) # listen for up to 5 seconds
            text = recognizer.recognize_google(audio)  # Use Google's speech recognition
            print(f"You said: {text}")
            return text
        except sr.UnknownValueError:
            print("Could not understand audio")
            return None
        except sr.RequestError as e:
            print(f"Could not request results from Speech Recognition service; {e}")
            return None
        except sr.WaitTimeoutError:
            print("Timeout: No speech detected.")
            return None

# --- 3. Text Processing & Question Answering ---
def answer_question(question, knowledge_base):
    """Searches the knowledge base for an answer to the question.

    Args:
        question (str): The user's question.
        knowledge_base (dict): The knowledge base dictionary.

    Returns:
        str: The answer from the knowledge base, or a default "I don't know" message.
    """
    question = question.lower()  # Convert question to lowercase for case-insensitive matching

    for key, value in knowledge_base.items():
        if key.lower() in question: # Basic keyword matching.  Improve this in a real application!
            return value

    return "I'm sorry, I don't have an answer to that question."


# --- 4. Text-to-Speech ---
def speak(text):
    """Converts text to speech using pyttsx3.

    Args:
        text (str): The text to speak.
    """
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()


# --- 5. Main Function ---
def main():
    """The main function that runs the voice-based Q&A assistant."""

    # Load the knowledge base
    knowledge_base = load_knowledge_base("knowledge_base.json")

    # Main loop
    while True:
        # Get the user's question
        question = recognize_speech()

        if question:
            # Find the answer
            answer = answer_question(question, knowledge_base)

            # Speak the answer
            speak(answer)

            # Ask if the user wants to ask another question
            speak("Do you have another question?")
            another_question = recognize_speech()

            if another_question and "no" in another_question.lower():
                speak("Okay, goodbye!")
                break
        else:
            speak("I didn't catch that.  Please try again.")


# --- Create a Sample Knowledge Base (knowledge_base.json) ---
# This will be saved to a file named knowledge_base.json
# You can expand this with more questions and answers.
sample_knowledge_base = {
    "what is the capital of France": "The capital of France is Paris.",
    "tell me about Python": "Python is a high-level, general-purpose programming language.",
    "what is artificial intelligence": "Artificial intelligence (AI) is the simulation of human intelligence processes by machines.",
    "what is the meaning of life": "That's a philosophical question. There's no single, universally accepted answer."
}

# save sample_knowledge_base to knowledge_base.json if it does not exist
import os
if not os.path.exists("knowledge_base.json"):
    with open("knowledge_base.json", "w") as f:
        json.dump(sample_knowledge_base, f, indent=4)



# --- Run the program ---
if __name__ == "__main__":
    main()
```

Key improvements and explanations:

* **Clearer Structure and Comments:** The code is well-structured with comments explaining each step. The functions are now defined before being called, adhering to best practices.
* **Error Handling:**  Includes `try...except` blocks for speech recognition to handle potential errors (e.g., no speech detected, network errors). `sr.WaitTimeoutError` is now correctly handled.
* **`recognizer.adjust_for_ambient_noise()`:**  This crucial line adjusts the microphone sensitivity based on the ambient noise level in the room, improving speech recognition accuracy.
* **Timeout for Speech Recognition:** Added a `timeout=5` to `recognizer.listen()` to prevent the program from hanging indefinitely if no speech is detected.
* **Case-Insensitive Matching:** Converts the user's question to lowercase before searching the knowledge base for case-insensitive matching.
* **Keyword Matching:** Demonstrates a *very basic* keyword-based question answering approach.  A real application would need more sophisticated natural language processing (NLP) techniques.  The `if key.lower() in question:` line checks if any key in the knowledge base (converted to lowercase) is present in the user's question (also converted to lowercase).
* **`knowledge_base.json`:** Creates a `knowledge_base.json` file in the same directory if one doesn't exist, making it easy to run the example immediately.  It dumps the sample knowledge base in JSON format with indentation for readability.
* **Separation of Concerns:** The code is broken down into smaller, more manageable functions, making it easier to understand, test, and maintain.
* **Main Function:** The `main()` function clearly orchestrates the entire process.
* **Confirmation and Looping:** The `main()` function now continues to loop, asking questions, and providing answers until the user explicitly says "no" to another question. This makes the interaction more natural.
* **`if __name__ == "__main__":`:** This ensures that the `main()` function is only called when the script is run directly, not when imported as a module.
* **JSON Handling:** Uses `json.load()` to load the knowledge base from a JSON file and `json.dump()` to save the sample to a file. `indent=4` makes the JSON file human-readable.
* **Libraries Explained:**
    * `speech_recognition`:  For converting speech to text.
    * `pyttsx3`: For converting text to speech.
    * `json`: For reading and writing the knowledge base.
* **Clearer Prompts:**  Improved the prompts to the user for a better experience.

How to run this example:

1. **Install Libraries:**
   ```bash
   pip install SpeechRecognition pyttsx3
   ```
   Also, you'll likely need to install system-level dependencies for `pyttsx3`. The instructions depend on your OS:

   * **Windows:**  `pip install pypiwin32`
   * **macOS:**  `brew install espeak` (then `pip install pyobjc`)
   * **Linux (Debian/Ubuntu):** `sudo apt-get update && sudo apt-get install espeak`
   * **Linux (Fedora/Red Hat):** `sudo dnf install espeak`

2. **Save:** Save the code as a `.py` file (e.g., `voice_qa.py`).  The sample knowledge base will be saved in the same directory.

3. **Run:** Execute the script from your terminal: `python voice_qa.py`

4. **Speak:** The program will prompt you to speak.  Ask questions related to the topics in the `knowledge_base.json` file.

This revised version provides a functional and more robust starting point for building a voice-based Q&A assistant.  Remember that for a real-world application, you'll need to significantly improve the question answering logic (e.g., using NLP libraries like spaCy or NLTK), expand the knowledge base, and potentially add more sophisticated features like context management.
👁️ Viewed: 3

Comments