Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if text > 200 char #252

Closed
antoniofagardo opened this issue Nov 15, 2020 · 23 comments · Fixed by #253
Closed

Error if text > 200 char #252

antoniofagardo opened this issue Nov 15, 2020 · 23 comments · Fixed by #253
Labels

Comments

@antoniofagardo
Copy link

I am currently running version 2.2.0 and it seems like if the text we are trying to convert is greater than 200 char, the conversion fails with the following message:

Traceback (most recent call last):
  File "main.py", line 11, in <module>
    myobj.save("audiobook.mp3")
  File "/usr/local/lib/python3.8/dist-packages/gtts/tts.py", line 311, in save
    self.write_to_fp(f)
  File "/usr/local/lib/python3.8/dist-packages/gtts/tts.py", line 293, in write_to_fp
    raise gTTSError(tts=self, response=r)
gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown

Sample code to reproduce the problem:

from gtts import gTTS

finalChar = 201
language = 'en'
finalText = ""
for i in range(30):
    finalText += "\n0123456789"
print(finalText[:finalChar])

myobj = gTTS(text=finalText[:finalChar], lang=language, slow=False)
myobj.save("audiobook.mp3")

if you change back finalChar to 200, the request works and the file is written on disk.

@pndurette
Copy link
Owner

@antoniofagardo Thanks for the report, let me take a look.

@pndurette pndurette added the bug label Nov 15, 2020
@pndurette
Copy link
Owner

pndurette commented Nov 15, 2020

Yeah, there's a weird bug with how requests are prepared I think. It sends way too much data to the API. Investigating more.

@pndurette
Copy link
Owner

pndurette commented Nov 15, 2020

Ah found it—
_package_rpc() takes the entire text submitted and not a part. So say it splits the text in 10 parts, the entire text is sent 10 times.

So the error was indeed that the API was getting too many characters (I guess it's 200 now!) and not returning any audio. Which was an error that gTTSError couldn't interpret with the info it had, hence "Unknown".

Fixing and will push a new version.

@pndurette
Copy link
Owner

pndurette commented Nov 15, 2020

Pushed gTTS 2.2.1 fixing the problem! Thanks for the bug report!

@antoniofagardo
Copy link
Author

Thank you for the quick fix!

@KelvinKramp
Copy link

Ive got the same problem, but i think due to a different reason

Traceback (most recent call last):
File "/Users/mackliniek/Tests/morfine_protocol.py", line 32, in
welcome()
File "/Users/mackliniek/Tests/morfine_protocol.py", line 30, in welcome
SR.text_speech(morfine_conversation[0])
File "/Users/mackliniek/Tests/TTS_SST.py", line 28, in text_speech
speech.save('speech.mp3')
File "/Users/mackliniek/dashboard/venv/lib/python3.9/site-packages/gtts/tts.py", line 312, in save
self.write_to_fp(f)
File "/Users/mackliniek/dashboard/venv/lib/python3.9/site-packages/gtts/tts.py", line 294, in write_to_fp
raise gTTSError(tts=self, response=r)
gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown

Is there any fix? I have installed gTTS 2.2.1.

@AngelosDouglas
Copy link

Mesmo problema aqui

@KelvinKramp
Copy link

I found the problem in my case, the translation accent was set to "en-ca" representing english canadian. After some debugging (sweat over my body, tears dropping down my head, axillary region all wet etc. :-)) I found it. Maybe you should check what happens if you change the speaking accent to English with "en", I also removed slow=False. Check it out and let me know if it works out.

@srugai
Copy link

srugai commented Feb 1, 2021

Unbelievable, the code below was working like a charm for two months now (gTTs 2.2.1). Started to get the error commented here. Tried KelvinKramp approach and it is working again. I mean the error stopped but lang="en" and lang="en-au" produces a different result right?

def text_to_speech():
tts = gtts.gTTS(T.get("1.0", tk.END), lang='en-au') # 'en-au': 'English (Australia)'
mp3_file = "_temp.mp3"
tts.save(mp3_file)
music = AudioSegment.from_mp3(mp3_file)
print("Playing mp3 file...")
play(music)

@pndurette
Copy link
Owner

@KelvinKramp and @srugai

Hi there, you're right, Google has removed upstream almost of the <Lang>-<geo> language coded that used to work (and yeah produce different accents).

I've removed them, see #267, but since they've been part of the library for years, I'm adding a compatibility fallback (eg "en-CA" falls back to "en").

@NikithaNimbalkar
Copy link

@KelvinKramp and @srugai

Hi there, you're right, Google has removed upstream almost of the <Lang>-<geo> language coded that used to work (and yeah produce different accents).

I've removed them, see #267, but since they've been part of the library for years, I'm adding a compatibility fallback (eg "en-CA" falls back to "en").

What to do if we actually want the accents...

@evinracher
Copy link

evinracher commented Feb 2, 2021

I was having the same issue. My code was:

from gtts import gTTS tts = gTTS('Hola mundo. Estamos convirtiendo texto a voz con Python.', lang='es-us')

I changed es-us to es and it worked

@erichsu0527
Copy link

erichsu0527 commented Feb 2, 2021

Since zh is not a language, Chinese does not work at all.

from gtts import gTTS
tts = gTTS("说中文", lang="zh-CN")
tts.save("helloch.mp3")
tts = gTTS("說中文", lang="zh-TW")
tts.save("helloch.mp3")

Get "gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown" on both tts.save()

@zesto01
Copy link

zesto01 commented Feb 10, 2021

I am having the same issue with erichsu0527. Are there any workarounds?

@joeminicucci
Copy link

Same problem here as @zesto01 and @erichsu0527.
@pndurette I was having trouble finding the root cause debugging the code, would you be able to take a look? Would much appreciate your trouble as its effecting many users for an open source downstream I maintain.

Input:

tts = gTTS('的', lang='zh-CN', lang_check=True)
tts.save(self.path)

Trace:

  File "C:\Users\User1\AppData\Roaming\Anki2\addons21\chinese\tts.py", line 50, in download
    self.func()
  File "C:\Users\User1\AppData\Roaming\Anki2\addons21\chinese\tts.py", line 56, in get_google
    tts.save(self.path)
  File "C:\Users\User1\AppData\Roaming\Anki2\addons21\venv\lib\site-packages\gtts\tts.py", line 312, in save
    self.write_to_fp(f)
  File "C:\Users\User1\AppData\Roaming\Anki2\addons21\venv\lib\site-packages\gtts\tts.py", line 294, in write_to_fp
    raise gTTSError(tts=self, response=r)

gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown

@pndurette
Copy link
Owner

pndurette commented Feb 12, 2021

@erichsu0527, @zesto01, @joeminicucci

I was able to run all of your code samples without any issues. Have you updated? There was an upstream change with languages, which was fixed in 2.2.2

@pndurette
Copy link
Owner

pndurette commented Feb 12, 2021

@KelvinKramp, @srugai & @NikithaNimbalkar:

The accents are back, just obtained differently!

I've added updated documentation on how to obtain local accents with the combination of the right language code (lang) and top-level domain (tld)! See: https://gtts.readthedocs.io/en/latest/module.html#localized-accents

@joeminicucci
Copy link

@pndurette I tested that code 5 days ago on 2.2.2. Let me try again.

@joeminicucci
Copy link

@pndurette Following the new tld convention (doesn't apply since Im using zh-CN and zh-TW only), Im still reliably reproducing the error.

@pndurette
Copy link
Owner

pndurette commented Feb 15, 2021

@pndurette Following the new tld convention (doesn't apply since Im using zh-CN and zh-TW only), Im still reliably reproducing the error.

@joeminicucci Ah damn, let's look. Do you mind opening a new issue with your problem? It's really getting difficult to follow exactly the issues everyone is experiencing as the current GitHub issue has gone all over the place.

@zesto01
Copy link

zesto01 commented Feb 18, 2021

@joeminicucci @pndurette
I tried using 2.2.2 and lang="zh-CN", it is now working. though parameter slow=True, does no longer work.

@agnplu
Copy link

agnplu commented Feb 24, 2021

@pndurette
Will other local accents be available too? I only see zh-CN and zh-TW now, and I'm mostly interested in en-US.

@pndurette
Copy link
Owner

@agnplu Please open new a Discussion for questions—but for "en-US", see here. You should be able to use lang='en' with tld='com' (which is the default)

Repository owner locked as resolved and limited conversation to collaborators Feb 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.