translators icon indicating copy to clipboard operation
translators copied to clipboard

[Bug]: KeyError: 'data' when using baidu

Open Old-Farmer opened this issue 1 year ago • 2 comments

Debug Tips

  • [X] I'm sure I've read this project's Issues of README.

What happened?

I run this code.

import translators

proxies = {"https": "http://127.0.0.1:7890/"}
# proxies = {}

text = translators.translate_text(
    query_text="i have a pen",
    from_language="en",
    to_language="zh",
    translator="baidu",
    proxies=proxies,
)
print(text)

I am sure this proxy is right.

Then I get this output:

Using region Central and Western District server backend.

Traceback (most recent call last):
  File "/home/shixinchai/mine/projects/Utils/dict/./new.py", line 536, in <module>
    text = translators.translate_text(
  File "/home/shixinchai/.local/lib/python3.10/site-packages/translators/server.py", line 5294, in translate_text
    return self.translators_dict[translator](query_text=query_text, from_language=from_language, to_language=to_language, **kwargs)
  File "/home/shixinchai/.local/lib/python3.10/site-packages/translators/server.py", line 104, in _wrapper
    return func(*args, **kwargs)
  File "/home/shixinchai/.local/lib/python3.10/site-packages/translators/server.py", line 273, in _wrapper
    return func(*args, **{**kwargs, **{'query_text': query_text}})
  File "/home/shixinchai/.local/lib/python3.10/site-packages/translators/server.py", line 718, in baidu_api
    return data if is_detail_result else '\n'.join([item['dst'] for item in data['data']])
KeyError: 'data'

However, if I set from_language as "auto", everything works fine.

APP Version

translators v5.8.7

Python Version

3.10

Runtime Environment

Linux Ubuntu

Country/Region

Central and Western District

Relevant log output

No response

Screenshots

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Old-Farmer avatar Nov 05 '23 05:11 Old-Farmer

@Old-Farmer It's not a proxy problem, you try setting from_language to auto and it should be fine. Then you try to use longer sentences and then change the from_language and it doesn't seem to be a problem. Therefore, I do not know how this problem is handled within the translation service. By the way, pip install --upgrade translators==5.8.8

UlionTse avatar Nov 06 '23 10:11 UlionTse

Try changing self._baidu = BaiduV1() # V2 to V2. V1 doesn't work anymore.

Touch-Night avatar Jan 03 '24 17:01 Touch-Night