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]: Youdao reports exception: "KeyError: 'translation'" #117

Closed FunnySaltyFish closed 1 year ago

FunnySaltyFish commented 1 year ago

Debug Tips

What happened?

A bug happened!

APP Version

5.5.6

Python Version

3.9 (Default)

Runtime Environment

Linux Ubuntu

Country/Region

Server is in Beijing, but log shows: Using state server backend.

Relevant log output

Traceback (most recent call last):
  File "/home/ubuntu/api/funny_trans/utils/trans_utils.py", line 31, in translate
    res["translation"] = ts.translate_text(text, engine, source, target, if_use_cn_host=True, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 3115, in translate_text
    return self.translators_dict[translator](query_text=query_text, from_language=from_language, to_language=to_language, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 98, in _wrapper
    return func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 225, in _wrapper
    return func(*args, **{**kwargs, **{'query_text': query_text}})
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 1037, in youdao_api
    return data if is_detail_result else data['translation'][0]
KeyError: 'translation'

Screenshots

[![uTools_1678158751270.png](http://img.funnysaltyfish.fun/i/2023/03/07/6406abb136641.png)](http://img.funnysaltyfish.fun/i/2023/03/07/6406abb136641.png)

Code of Conduct

UlionTse commented 1 year ago

@FunnySaltyFish Use parameter is_detail_result=True to check whether your high frequency requests are blocked.

FunnySaltyFish commented 1 year ago

thanks for replying. A strange thing is that, the error had lasted for several days, but yesterday it worked well. I will observe for it.

UlionTse commented 1 year ago

Okay

FunnySaltyFish commented 1 year ago

Update: Now the full result of youdao is :{'errorCode': '413'}, and this code doesn't appear on their document.

FunnySaltyFish commented 1 year ago

BTW, bing somethings returns error as well:

bing, en -> zh-Hans, thanks for replying. A strange thing is that, the error had lasted for several days, but yesterday it worked well. I will observe for it.
Traceback (most recent call last):
  File "/home/ubuntu/api/funny_trans/utils/trans_utils.py", line 32, in translate
    res["translation"] = ts.translate_text(text, engine, source, target, if_use_cn_host=True, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 3115, in translate_text
    return self.translators_dict[translator](query_text=query_text, from_language=from_language, to_language=to_language, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 98, in _wrapper
    return func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 225, in _wrapper
    return func(*args, **{**kwargs, **{'query_text': query_text}})
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 1411, in bing_api
    r.raise_for_status()
  File "/home/ubuntu/.local/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://cn.bing.com/ttranslatev3?isVertical=1&&IG=40588939DD834B378E15F99E02381447&IID=translator.5028
UlionTse commented 1 year ago

Is there a problem after the first request, or a problem after several normal requests?

UlionTse commented 1 year ago

Update: Now the full result of youdao is :{'errorCode': '413'}, and this code doesn't appear on their document.

This documentation is for dedicated api and has some reference value, but the interpretation of what results are returned is not mine.

UlionTse commented 1 year ago

The API of the project is not to say how you ravage it, it's still very robust. You have to gently, slowly stroke it, love it, use it. If it is a code bug, you can post the source code and error log, let's reproduce it again, and fix it. If it is clearly a server feedback problem, you should rethink your actions. Again, this is a free interface service, and the resources of each service are limited and anti-crawler. Please solve the service problems caused by high speed and high frequency by yourself, I can do nothing. https://github.com/UlionTse/translators/issues/109#issuecomment-1427993673

FunnySaltyFish commented 1 year ago

感谢回复,我拿中文回吧。translators被运行在服务器,作为翻译程序的后端,上面的这些报错来自日志。 有道的调用代码如下,每日调用频率约为300-400次,调用时间比较分散:

obj = ts.translate_text(text, translator="youdao", source=source, target=target, is_detail_result=True, **kwargs)

在我初次反馈问题时,有道经常报错(约80%),报错为 "KeyError: "translation'",连续发现这个现象一周以上,因此提出issue;issue提出后的那一天,错误率降低到了几乎为0,因此回了第一次的回复。两天前,用户开始反馈有道不能用了,显示 413,在代码中加入log打印obj变量,发现值(即ts.translate的返回结果)为 {'errorCode:' 413},且截止到上次issue回复时几乎100%出现,因此回复第二句;到今天晚上情况又有所好转,有一部分时候有道能正常返回结果了。到刚刚,最近的20次又全部正常 image

我发有道的文档只是希望能做个参考,没有别的意思。


至于 bing 的 500 错误是最近才出现的,偶发,今天 137 次请求中有 6 次报错,它的调用代码如下:

ts.translate_text(text, "bing", source, target, if_use_cn_host=True, **kwargs)

其中一次记录到的日志如下:

error when translate: bing, auto -> zh-Hans, content-disposition
Traceback (most recent call last):
  File "/home/ubuntu/api/funny_trans/utils/trans_utils.py", line 32, in translate
    res["translation"] = ts.translate_text(text, engine, source, target, if_use_cn_host=True, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 3681, in translate_text
    return self.translators_dict[translator](query_text=query_text, from_language=from_language, to_language=to_language, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 99, in _wrapper
    return func(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 233, in _wrapper
    return func(*args, **{**kwargs, **{'query_text': query_text}})
  File "/home/ubuntu/.local/lib/python3.9/site-packages/translators/server.py", line 1617, in bing_api
    r.raise_for_status()
  File "/home/ubuntu/.local/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://cn.bing.com/ttranslatev3?isVertical=1&&IG=A4634C9A5B2140BB86C903D5EF96588C&IID=translator.5028

使用的库版本为 5.6.2.
总结来说,有道的报错时而非常频繁,时而非常低频;bing 的 500 报错则为偶发。在我手动执行测试代码时,我无法做到100%复现。上述是我现在能提供的一些信息,感谢作者维护。

UlionTse commented 1 year ago

Normally, if you're banned, it's hard to recover for a while, but your log shows that it can be recovered. Then you can consider setting the total number of requests within a period of time, as well as the frequency of the interval between requests. The frequency can be replaced by multiple translation services, and the time or frequency of updating session can be set. Under this layer of protection, in order to keep the service stable, you can request again and give the correct answer in the case of returning the wrong result. Of course, your back end should also give some room for interpretation of translation exceptions, after all, there are 26 (22) translation services available.

FunnySaltyFish commented 1 year ago

Thanks for replying, my application provides different translation engines for users, they can use different engines at the same time. So, for me, it's the best if the stability could be even better. I know that is hard, so I will also try other solutions like requesting twice you mentioned.

UlionTse commented 1 year ago

Okay.