Git Product home page Git Product logo

Comments (11)

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024 2

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")

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024 2

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

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024 1

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)

from translate.

eggplants avatar eggplants commented on May 26, 2024

Ref: #23 (comment)

from translate.

eggplants avatar eggplants commented on May 26, 2024
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'>

from translate.

eggplants avatar eggplants commented on May 26, 2024

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) ====================

from translate.

eggplants avatar eggplants commented on May 26, 2024

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'>

from translate.

ZhymabekRoman avatar ZhymabekRoman commented on May 26, 2024

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("Ленин")

from translate.

eggplants avatar eggplants commented on May 26, 2024

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())

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

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)

from translate.

Animenosekai avatar Animenosekai commented on May 26, 2024

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

from translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.