7

I am making an assistant that uses gTTS and Google Speech but this error appears. It is fine with speech recognition as it can recognize without faults. I tested with print function however when I want text-to-speech, this bug comes. ...

import speech_recognition as sr
from time import ctime
import time
import playsound
import os
import random
from gtts import gTTS
import webbrowser

r = sr.Recognizer()


def record_audio(ask=False):
    with sr.Microphone() as source:
        if ask:
            watson_speak(ask)
        audio = r.listen(source)
        voice_data = ''
        try:
            voice_data = r.recognize_google(audio)
        except sr.UnknownValueError:
            watson_speak("Sorry, I did not catch that")
        except sr.RequestError:
            watson_speak("I am offline right now")
        return voice_data


def watson_speak(audio_string):
    tts = gTTS(text=audio_string, lang='en')
    r = random.randint(1, 10000000)
    audio_file = 'audio-' + str(r) + '.mp3'
    tts.save(audio_file)
    playsound.playsound(audio_file)
    print(audio_string)
    os.remove(audio_file)


def respond(voice_data):
    if 'what is your name' in voice_data:
        watson_speak("My name is Watson")
    if 'what time is it' in voice_data:
        watson_speak(ctime())
    if 'search' in voice_data:
        search = record_audio("What do you want to search for?")
        url = "https://duckduckgo.com/?t=ffnt&q=" + search
        webbrowser.get().open(url)
        watson_speak("Here is what I found for " + search)
    if 'find location' in voice_data:
        location = record_audio("What is the location?")
        url = "https://google.nl/maps/place/" + location + "/&"
        webbrowser.get().open(url)
        watson_speak("Here is the location of " + location)
    if 'exit' in voice_data:
        exit()


time.sleep(1)
watson_speak("How can I help you?")
while 1:
    voice_data = record_audio()
    respond(voice_data)

... Not sure what I did wrong. Some guidance would be greatly appreciated. It keeps asking for a token seed which I am not sure about.

sh karri
  • 99
  • 1
  • 6

7 Answers7

2

Do both these upgrades

pip3 install gTTS-token --upgrade
pip3 install gTTS --upgrade

Worked for me

1

You should consider checking your gTTS-token package version. If you installed the package via pip try this line in a command prompt:

pip install gTTS-token --upgrade

This was at least in my case the fix for this kind of error message

user2463728
  • 309
  • 3
  • 12
1

It seems that Google has updated and switched to another way of generating speech.

This issue is currently open on GitHub, you can check for updates here.

It will probably take a while for this to get fixed as the devs need to understand what changes were done by Google.

Mohammed Shoaib
  • 344
  • 4
  • 11
  • 1
    The ISSUE has been resolved and it seems updating go **gtts version 2.1.2** and newest resolved the problem. – GuiFalourd Feb 08 '21 at 14:55
1

You may need to reinstall gTTS

sudo pip uninstall gTTS
sudo pip install gTTS

Try updating gTTS as well as requests. This helps. If still you get error and you have both anaconda as well as python in your local, try to debug ~/.local/lib/python3.6/site-packages, if upgrading gTTS is reflecting your package and you are using the same.

thepunitsingh
  • 713
  • 1
  • 12
  • 30
Rajat Jain
  • 11
  • 1
0

I also got this error sometimes but sometimes the code run perfectly, may be it is problem with gtts or pyaudio. I recommend you to use pyttsx library which is i think much better than gtts as i working on it and gives me the best results.

you can check the installation of pyttsx here

Dharman
  • 30,962
  • 25
  • 85
  • 135
rahul
  • 53
  • 4
0

you need to update gTTs-token package For that use below command :

pip install gTTS-token --upgrade

this will update the current package

buddemat
  • 4,552
  • 14
  • 29
  • 49
0

Uninstall and reinstalling of gTTS worked for me.