Python Assistant

  gtts, python, python-3.x, pyttsx3

I’m making an assistant to control/manage 3D printers. It works when it’s just a text based assistant, but since I’ve tried to add speaking and listening to the bot things aren’t working very well.

  1. Why is the assistant not speaking the responses?
  2. When I run the code with all of the listening and speaking functions the but runs much slower to the point to where it makes the bot not practical to use

Here is the main.py file

import os
import time
from known_requests import simple
from printer import print_commands
from queueCommands import *
from speaking import *
# from queueCommands import Queue_Commands
# from IG import ig

while True:
    if __name__ == '__main__':
        try:
            time.sleep(1)
            print("What can I do for you?: ")
            text = get_audio()
            print_commands(text)
            simple(text)
            add(text)
            view_queue(text)
            remove(text)
            # ig(text)
        except KeyboardInterrupt:
            print('program killed')
            exit()
        except ConnectionError:
            print('Connection error occured')
        except TimeoutError:
            print('took to long')

Speaking.py for the get audio and speak function

import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
import pyttsx3


def speak(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

The code for some basic requests:

from datetime import datetime
from datetime import date
from main import speak



def simple(text):
    if text == "what time is it":
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        speak("Current Time =", current_time)

    if text == "what is today's date":
        today = date.today()
        d2 = today.strftime("%B %d, %Y")
        speak(d2)

    if text == "hello":
        speak("Hey")

    if text == "goodbye":
        speak("goodbye")
        exit()

Source: Python Questions

LEAVE A COMMENT