UlionTse / translators

🌏🌍🌎Translators🌎🌍🌏 is a library that aims to bring free, multiple, enjoyable translations to individuals and students in Python. Translators是一个旨在用Python为个人和学生带来免费、多样、愉快翻译的库。
https://pypi.org/project/translators/
GNU General Public License v3.0
1.62k stars 189 forks source link

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

Open Old-Farmer opened 10 months ago

Old-Farmer commented 10 months ago

Debug Tips

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

UlionTse commented 10 months ago

@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

Touch-Night commented 8 months ago

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