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

DeeplTranslate is not working #23

Closed
eggplants opened this issue Sep 12, 2021 · 5 comments
Closed

DeeplTranslate is not working #23

eggplants opened this issue Sep 12, 2021 · 5 comments

Comments

@eggplants
Copy link
Contributor

eggplants commented Sep 12, 2021

⭕In translatepy 1.7:

from translatepy import Translator
t = Translator().deepl_translate
r = t.translate("こんにちは", "ru", "ja")
print(r)
('JA', 'Здравствуйте')

❌In translatepy 2.0:

from translatepy.translators.deepl import DeeplTranslate
t = DeeplTranslate()
r = t.translate("こんにちは", "ru", "ja")
print(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/base.py", line 94, in translate
    source_language, translation = self._translate(text, dest_code, source_code)
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/deepl.py", line 136, in _translate
    sentences, computed_lang = self._split_into_sentences(text, destination_language, source_language)
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/deepl.py", line 127, in _split_into_sentences
    resp = self.jsonrpc.send_jsonrpc("LMT_split_into_sentences", params)
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/translators/deepl.py", line 95, in send_jsonrpc
    response = request.json()
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/site-packages/translatepy/utils/request.py", line 74, in json
    return loads(self.text, **kwargs)
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/eggplants/.pyenv/versions/3.9.2/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Expecting value: line 1 column 1 (char 0)
@ZhymabekRoman
Copy link
Contributor

This problem seems to have appeared after the commit b74bfa8

@ZhymabekRoman
Copy link
Contributor

Hotfix (workaround) for this issue - just comment line 84 in translatepy/utils/request.py:

diff --git a/translatepy/utils/request.py b/translatepy/utils/request.py
index 22f174d..60a6bd1 100644
--- a/translatepy/utils/request.py
+++ b/translatepy/utils/request.py
@@ -81,7 +81,7 @@ class Request():
             "Accept": "*/*",
             "Accept-Language": "en-US,en-GB; q=0.5",
             "Accept-Encoding": "gzip, deflate",
-            "Content-Type": "application/x-www-form-urlencoded; application/json; charset=UTF-8",
+            # "Content-Type": "application/x-www-form-urlencoded; application/json; charset=UTF-8",
             "Connection": "keep-alive"
         }
 # default headers

@eggplants
Copy link
Contributor Author

Please apply this patch to the next release.

@eggplants
Copy link
Contributor Author

git clone --depth 1 https://github.com/Animenosekai/translate && cd translate
mv translatepy/utils/request.py translatepy/utils/request.py.bak
sed '84s/"/# &/' translatepy/utils/request.py.bak > translatepy/utils/request.py
mv setup.py setup.py.bak
sed -E '/version =/s/"([^"]+)"/"\1a"/' setup.py.bak > setup.py
python -m pip install -U .
from translatepy.translators.deepl import DeeplTranslate
t = DeeplTranslate()
r = t.translate("こんにちは", "ru", "ja")
print(r)
Здравствуйте

@eggplants
Copy link
Contributor Author

This patch makes all tests passed except translatepy.translators.bing.BingTranslate().text_to_speech(word).

$ pytest -vv
============================= 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 0x7f97ecb59b50>

    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 = 'What cool weater todaiy', speed = 100
gender = 'female', source_language = 'en'

    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):
            token_response = self.session_manager.send("https://www.bing.com/tfetspktok", data={})
            token_status = token_response.get("statusCode")
    
            if token_status != 200:
>               raise BingTranslateException(token_status, "Error during token request from the server")
E               translatepy.translators.bing.BingTranslateException: None | Error during token request from the server

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

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

No branches or pull requests

2 participants