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

BingTranslate.text_to_speech is not working #24

Closed
eggplants opened this issue Sep 13, 2021 · 11 comments · Fixed by #25
Closed

BingTranslate.text_to_speech is not working #24

eggplants opened this issue Sep 13, 2021 · 11 comments · Fixed by #25

Comments

@eggplants
Copy link
Contributor

In translatepy 2.0:

from translatepy.translators.bing import BingTranslate
t = BingTranslate()
r = t.text_to_speech("こんにちは")
print(r)
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/home/eggplants/prog/translate/translatepy/translators/base.py", line 442, in text_to_speech
    source_language, text_to_speech = self._text_to_speech(text, speed, gender, source_code)
  File "/home/eggplants/prog/translate/translatepy/translators/bing.py", line 196, in _text_to_speech
    source_language = self._language(text)
  File "/home/eggplants/prog/translate/translatepy/translators/bing.py", line 165, in _language
    response = self.session_manager.send("https://www.bing.com/ttranslatev3", data={'text': text, 'fromLang': "auto-detect", 'to': "en"})
  File "/home/eggplants/prog/translate/translatepy/translators/bing.py", line 123, in send
    raise BingTranslateException(status_code)
translatepy.translators.bing.BingTranslateException: 400 | Unknown error. Error code: 400

400 | Unknown error. Error code: 400
@eggplants
Copy link
Contributor Author

Ref: #23 (comment)

@ZhymabekRoman
Copy link
Contributor

ZhymabekRoman commented Sep 13, 2021

Hmm, it looks like Bing has slightly changed the response, issue fix patch:

diff --git a/translatepy/translators/bing.py b/translatepy/translators/bing.py
index d68036d..ffbf69c 100644
--- a/translatepy/translators/bing.py
+++ b/translatepy/translators/bing.py
@@ -13,7 +13,6 @@ from translatepy.utils.request import Request
 from translatepy.language import Language
 from translatepy.exceptions import UnsupportedMethod

-
 HEADERS = {
     # "Host": "www.bing.com",
     "User-Agent": pyuseragents.random(),
@@ -199,7 +198,7 @@ class BingTranslate(BaseTranslator):

         if not self.__dict__.get("_speech_token") or timestamp_now > float(self._speech_token_expiry):
             token_response = self.session_manager.send("https://www.bing.com/tfetspktok", data={})
-            token_status = token_response.get("statusCode")
+            token_status = token_response.get("statusCode", 200)

             if token_status != 200:
                 raise BingTranslateException(token_status, "Error during token request from the server")

@eggplants
Copy link
Contributor Author

git clone --depth 1 https://github.com/Animenosekai/translate && cd translate
mv translatepy/translators/bing.py translatepy/translators/bing.py.bak
sed '202s/)/, 200&/' translatepy/translators/bing.py.bak > translatepy/translators/bing.py
mv setup.py setup.py.bak
sed -E '/version =/s/"([^"]+)"/"\1b"/' setup.py.bak > setup.py
python -m pip install -U .
from translatepy.translators.bing import BingTranslate
t = BingTranslate()
r = t.text_to_speech("こんにちは")
print(type(r))
<class 'translatepy.models.TextToSpechResult'>

@eggplants
Copy link
Contributor Author

eggplants commented Sep 13, 2021

tests/test_translators.py::TestAllTranslators::test_service_text_to_speech is still being failed when the service is Bing.

git clone -q --depth 1 https://github.com/Animenosekai/translate && cd translate                                                                    ?[main]

mv translatepy/utils/request.py translatepy/utils/request.py.bak
sed '84s/"/# &/' translatepy/utils/request.py.bak > translatepy/utils/request.py

mv translatepy/translators/bing.py translatepy/translators/bing.py.bak
sed '202s/)/, 200&/' translatepy/translators/bing.py.bak > translatepy/translators/bing.py

mv setup.py setup.py.bak
sed -E '/version =/s/"([^"]+)"/"\1c"/' setup.py.bak > setup.py
python -m pip install -U .
============================= test session starts ==============================
platform linux -- Python 3.9.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /home/eggplants/.pyenv/versions/3.9.2/bin/python3
cachedir: .pytest_cache
rootdir: /home/eggplants/prog/translate
plugins: typeguard-2.12.1, torchtyping-0.1.4
collecting ... collected 8 items

tests/test_language.py::test_language PASSED                             [ 12%]
tests/test_translators.py::TestAllTranslators::test_service_translate PASSED [ 25%]
tests/test_translators.py::TestAllTranslators::test_service_transliterate PASSED [ 37%]
tests/test_translators.py::TestAllTranslators::test_service_spellcheck PASSED [ 50%]
tests/test_translators.py::TestAllTranslators::test_service_example PASSED [ 62%]
tests/test_translators.py::TestAllTranslators::test_service_dictionary PASSED [ 75%]
tests/test_translators.py::TestAllTranslators::test_service_language PASSED [ 87%]
tests/test_translators.py::TestAllTranslators::test_service_text_to_speech FAILED [100%]

=================================== FAILURES ===================================
________________ TestAllTranslators.test_service_text_to_speech ________________

self = <test_translators.TestAllTranslators object at 0x7fbf76f3ddc0>

    def test_service_text_to_speech(self):
        texts_args_list = [["What cool weater todaiy"], ["Привет"],
                           ["自动"]]
    
        for service in self.services_list:
            for args in texts_args_list:
                try:
>                   result = service.text_to_speech(*args)

tests/test_translators.py:108: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/base.py:442: in text_to_speech
    source_language, text_to_speech = self._text_to_speech(text, speed, gender, source_code)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Translator(Microsoft Bing), text = 'Привет', speed = 100
gender = 'female', source_language = 'ru'

    def _text_to_speech(self, text: str, speed: int, gender: str, source_language: str):
        if source_language == "auto-detect":
            source_language = self._language(text)
    
        timestamp_now = time.time()
    
>       if not self.__dict__.get("_speech_token") or timestamp_now > float(self._speech_token_expiry):
E       TypeError: float() argument must be a string or a number, not 'NoneType'

../../.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/bing.py:200: TypeError
=========================== short test summary info ============================
FAILED tests/test_translators.py::TestAllTranslators::test_service_text_to_speech
=================== 1 failed, 7 passed in 103.61s (0:01:43) ====================

@eggplants eggplants reopened this Sep 13, 2021
@eggplants
Copy link
Contributor Author

eggplants commented Sep 13, 2021

When executing test, it is possible that continuous execution without wait has an effect.
Below code is worked well.

from translatepy.translators.bing import BingTranslate
t = BingTranslate()
r = t.text_to_speech("Привет")
print(type(r))
<class 'translatepy.models.TextToSpechResult'>

@ZhymabekRoman
Copy link
Contributor

Thank you, yes, indeed, tests fall with an error. I can assume that the problem is getting a token from the dictionary, most likely the Bing servers started returning slightly different keys and values. I will try to fix it in the near future.

Sample code that causes the error:

from translatepy.translators.bing import BingTranslate
t = BingTranslate()
r = t.text_to_speech("Привет")
print(type(r))
r = t.text_to_speech("Ленин")

Animenosekai added a commit that referenced this issue Sep 13, 2021
@eggplants
Copy link
Contributor Author

https://github.com/Animenosekai/translate/runs/3588219963
The Bing APIs seem to be more strict than the others...
I suggest you to add random wait to test cases.

from random import ramdom
from time import sleep
def rand_sleep(base: float = 0.1, rand: float = 2.5) -> None:
    sleep(base + rand * random())

@ZhymabekRoman
Copy link
Contributor

The Bing APIs seem to be more strict than the others...

Deepl also have very strict restrictions

I suggest you to add random wait to test cases.

Will it save from the rate limit? It seems to me that it is best to pass all traffic through a proxy (or write a Tor Request adapter-class)

@Animenosekai
Copy link
Owner

https://github.com/Animenosekai/translate/runs/3588219963
The Bing APIs seem to be more strict than the others...
I suggest you to add random wait to test cases.

from random import ramdom
from time import sleep
def rand_sleep(base: float = 0.1, rand: float = 2.5) -> None:
    sleep(base + rand * random())

I found this sleep function in one of my old project

from time import sleep as _sleep
from random import random, randrange

def sleep(duration: float, deviation: float = 0):
    """
    Stops the execution for the given number of seconds, with randomness from the given deviation

    The deviation is calculated as follow:
        random() gives a number between 0 and 1
        We change it's range to 0 ~ DEVIATION
        We determine if it will be added or substracted (multiply by 1 or -1)
        We divide it by 100 to get the the result between 0 and 1 (the deviation is a percentage)
        We change it's range to 0 ~ duration
        We restrict the result to be positive
    """
    deviation = (random() * deviation) * (1 if randrange(2) == 0 else -1) / 100 * duration
    duration += deviation
    if duration < 0:
        duration = 0
    _sleep(duration)

@Animenosekai
Copy link
Owner

Animenosekai commented Sep 13, 2021

Deepl also have very strict restrictions

True, but that's why there is the Translate class

Will it save from the rate limit? It seems to me that it is best to pass all traffic through a proxy (or write a Tor Request adapter-class)

Hmmm I guess that's what the proxy_urls parameter is for in Request

@Animenosekai
Copy link
Owner

Oops GitHub automatically closed the issue after the merge...

Let me know if someone wanted to add anything, in which case I'll reopen it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants