本文介绍了GTTS的运行时错误(在大多数项目中都会发生这种情况)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个虚拟助手,为此,我正在使用gtts,但是当我尝试运行我的代码时,会遇到此错误,是否有任何解决方法?我已经尝试了一切,但没有成功,当我使用gtts时,所有项目都会出现此问题

I am trying to make a Virtual Assistant and for that i am using gtts but when i am trying to run my code am stuck with this errors, is there any way to get around? I have tried everything but did not succeed, this problem occurs in all my projects when i use gtts

错误-

Traceback (most recent call last):
  File "/Users/ashvinbhagat/Downloads/TARS-master/tars.py", line 177, in <module>
    talk("TARS activated!")
  File "/Users/ashvinbhagat/Downloads/TARS-master/tars.py", line 24, in talk
    text_to_speech.save("audio.mp3")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts/tts.py", line 111, in save
    self.write_to_fp(f)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts/tts.py", line 124, in write_to_fp
    'tk' : self.token.calculate_token(part)}
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gtts_token/gtts_token.py", line 61, in _get_token_key
    tkk_expr = re.search(".*?(TKK=.*?;)W.*?", line).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

代码-

from gtts import gTTS
import speech_recognition as sr
import re
import time
import webbrowser
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import smtplib
import requests
from pygame import mixer
import urllib.request
import urllib.parse
import json
import bs4


def talk(audio):
    "speaks audio passed as argument"

    print(audio)
    for line in audio.splitlines():
        text_to_speech = gTTS(text=audio, lang="en-uk")
        text_to_speech.save("audio.mp3")
        mixer.init()
        mixer.music.load("audio.mp3")
        mixer.music.play()


def myCommand():
    "listens for commands"
    # Initialize the recognizer
    # The primary purpose of a Recognizer instance is, of course, to recognize speech.
    r = sr.Recognizer()

    with sr.Microphone() as source:
        print("TARS is Ready...")
        r.pause_threshold = 1
        # wait for a second to let the recognizer adjust the
        # energy threshold based on the surrounding noise level
        r.adjust_for_ambient_noise(source, duration=1)
        # listens for the user's input
        audio = r.listen(source)
        print("analyzing...")

    try:
        command = r.recognize_google(audio).lower()
        print("You said: " + command + "\n")
        time.sleep(2)

    # loop back to continue to listen for commands if unrecognizable speech is received
    except sr.UnknownValueError:
        print("Your last command couldn't be heard")
        command = myCommand()

    return command


def tars(command):
    errors = ["I don't know what you mean", "Excuse me?", "Can you repeat it please?"]
    "if statements for executing commands"

    # Search on Google
    if "open google and search" in command:
        reg_ex = re.search("open google and search (.*)", command)
        search_for = command.split("search", 1)[1]
        print(search_for)
        url = "https://www.google.com/"
        if reg_ex:
            subgoogle = reg_ex.group(1)
            url = url + "r/" + subgoogle
        talk("Okay!")
        driver = webdriver.Firefox(executable_path="/path/to/geckodriver")
        driver.get("http://www.google.com")
        search = driver.find_element_by_name("q")
        search.send_keys(str(search_for))
        search.send_keys(Keys.RETURN)  # hit return after you enter search text

    # Send Email
    elif "email" in command:
        talk("What is the subject?")
        time.sleep(3)
        subject = myCommand()
        talk("What should I say?")
        message = myCommand()
        content = "Subject: {}\n\n{}".format(subject, message)

        # init gmail SMTP
        mail = smtplib.SMTP("smtp.gmail.com", 587)

        # identify to server
        mail.ehlo()

        # encrypt session
        mail.starttls()

        # login
        mail.login("username_gmail", "password_gmail")

        # send message
        mail.sendmail("FROM", "TO", content)

        # end mail connection
        mail.close()

        talk("Email sent.")

    # search in wikipedia (e.g. Can you search in wikipedia apples)
    elif "wikipedia" in command:
        reg_ex = re.search("wikipedia (.+)", command)
        if reg_ex:
            query = command.split("wikipedia", 1)[1]
            response = requests.get("https://en.wikipedia.org/wiki/" + query)
            if response is not None:
                html = bs4.BeautifulSoup(response.text, "html.parser")
                title = html.select("#firstHeading")[0].text
                paragraphs = html.select("p")
                for para in paragraphs:
                    print(para.text)
                intro = "\n".join([para.text for para in paragraphs[0:3]])
                print(intro)
                mp3name = "speech.mp3"
                language = "en"
                myobj = gTTS(text=intro, lang=language, slow=False)
                myobj.save(mp3name)
                mixer.init()
                mixer.music.load("speech.mp3")
                mixer.music.play()
    elif 'stop' in command:
        mixer.music.stop()

    # Search videos on Youtube and play (e.g. Search in youtube believer)
    elif "youtube" in command:
        talk("Ok!")
        reg_ex = re.search("youtube (.+)", command)
        if reg_ex:
            domain = command.split("youtube", 1)[1]
            query_string = urllib.parse.urlencode({"search_query": domain})
            html_content = urllib.request.urlopen(
                "http://www.youtube.com/results?" + query_string
            )
            search_results = re.findall(
                r"href=\"\/watch\?v=(.{11})", html_content.read().decode()
            )
            # print("http://www.youtube.com/watch?v=" + search_results[0])
            webbrowser.open(
                "http://www.youtube.com/watch?v={}".format(search_results[0])
            )
            pass
    #  weather forecast in your city (e.g. weather in London)
    # please create and use your own API it is free
    elif "weather in" in command:
        city = command.split("in", 1)[1]   
        #openweathermap API
        url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid=your_api_key&units=metric'.format(city)
        response = requests.get(url)
        data = response.json()
        #print(data)
        temp = data['main']['temp']
        round_temp = int(round(temp))
        talk('It is {} degree celcius in {}'.format(round_temp, city))
        time.sleep(3)


    elif "hello" in command:
        talk("Hello! I am TARS. How can I help you?")
        time.sleep(3)
    elif "who are you" in command:
        talk("I am one of four former U.S. Marine Corps tactical robots")
        time.sleep(3)
    else:
        error = random.choice(errors)
        talk(error)
        time.sleep(3)


talk("TARS activated!")

# loop to continue executing multiple commands
while True:
    time.sleep(4)
    tars(myCommand())

请帮助我,我做错了或应该怎么做?

Please help me, what i am doing wrong or what should i do?

推荐答案

这是已知问题在gTTS中.

将gTTS依赖项升级到最新版本应该可以解决问题:

Bumping your gTTS dependencies to the last version should do the trick:

pip install gtts --upgrade
pip install gtts-token --upgrade

这篇关于GTTS的运行时错误(在大多数项目中都会发生这种情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:32