You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import random
  17. from abc import ABC
  18. import requests
  19. from agent.component.base import ComponentBase, ComponentParamBase
  20. from hashlib import md5
  21. class BaiduFanyiParam(ComponentParamBase):
  22. """
  23. Define the BaiduFanyi component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.appid = "xxx"
  28. self.secret_key = "xxx"
  29. self.trans_type = 'translate'
  30. self.parameters = []
  31. self.source_lang = 'auto'
  32. self.target_lang = 'auto'
  33. self.domain = 'finance'
  34. def check(self):
  35. self.check_positive_integer(self.top_n, "Top N")
  36. self.check_empty(self.appid, "BaiduFanyi APPID")
  37. self.check_empty(self.secret_key, "BaiduFanyi Secret Key")
  38. self.check_valid_value(self.trans_type, "Translate type", ['translate', 'fieldtranslate'])
  39. self.check_valid_value(self.trans_type, "Translate domain",
  40. ['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
  41. 'news', 'law', 'contract'])
  42. self.check_valid_value(self.source_lang, "Source language",
  43. ['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
  44. 'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
  45. 'hu', 'cht', 'vie'])
  46. self.check_valid_value(self.target_lang, "Target language",
  47. ['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
  48. 'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
  49. 'hu', 'cht', 'vie'])
  50. self.check_valid_value(self.domain, "Translate field",
  51. ['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
  52. 'news', 'law', 'contract'])
  53. class BaiduFanyi(ComponentBase, ABC):
  54. component_name = "BaiduFanyi"
  55. def _run(self, history, **kwargs):
  56. ans = self.get_input()
  57. ans = " - ".join(ans["content"]) if "content" in ans else ""
  58. if not ans:
  59. return BaiduFanyi.be_output("")
  60. try:
  61. source_lang = self._param.source_lang
  62. target_lang = self._param.target_lang
  63. appid = self._param.appid
  64. salt = random.randint(32768, 65536)
  65. secret_key = self._param.secret_key
  66. if self._param.trans_type == 'translate':
  67. sign = md5((appid + ans + salt + secret_key).encode('utf-8')).hexdigest()
  68. url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
  69. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  70. response = requests.post(url=url, headers=headers).json()
  71. if response.get('error_code'):
  72. BaiduFanyi.be_output("**Error**:" + response['error_msg'])
  73. return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
  74. elif self._param.trans_type == 'fieldtranslate':
  75. domain = self._param.domain
  76. sign = md5((appid + ans + salt + domain + secret_key).encode('utf-8')).hexdigest()
  77. url = 'http://api.fanyi.baidu.com/api/trans/vip/fieldtranslate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&domain=' + domain + '&sign=' + sign
  78. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  79. response = requests.post(url=url, headers=headers).json()
  80. if response.get('error_code'):
  81. BaiduFanyi.be_output("**Error**:" + response['error_msg'])
  82. return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
  83. except Exception as e:
  84. BaiduFanyi.be_output("**Error**:" + str(e))