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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_empty(self.appid, "BaiduFanyi APPID")
  36. self.check_empty(self.secret_key, "BaiduFanyi Secret Key")
  37. self.check_valid_value(self.trans_type, "Translate type", ['translate', 'fieldtranslate'])
  38. self.check_valid_value(self.trans_type, "Translate domain",
  39. ['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
  40. 'news', 'law', 'contract'])
  41. self.check_valid_value(self.source_lang, "Source language",
  42. ['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
  43. 'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
  44. 'hu', 'cht', 'vie'])
  45. self.check_valid_value(self.target_lang, "Target language",
  46. ['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
  47. 'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
  48. 'hu', 'cht', 'vie'])
  49. self.check_valid_value(self.domain, "Translate field",
  50. ['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
  51. 'news', 'law', 'contract'])
  52. class BaiduFanyi(ComponentBase, ABC):
  53. component_name = "BaiduFanyi"
  54. def _run(self, history, **kwargs):
  55. ans = self.get_input()
  56. ans = " - ".join(ans["content"]) if "content" in ans else ""
  57. if not ans:
  58. return BaiduFanyi.be_output("")
  59. try:
  60. source_lang = self._param.source_lang
  61. target_lang = self._param.target_lang
  62. appid = self._param.appid
  63. salt = random.randint(32768, 65536)
  64. secret_key = self._param.secret_key
  65. if self._param.trans_type == 'translate':
  66. sign = md5((appid + ans + salt + secret_key).encode('utf-8')).hexdigest()
  67. url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
  68. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  69. response = requests.post(url=url, headers=headers).json()
  70. if response.get('error_code'):
  71. BaiduFanyi.be_output("**Error**:" + response['error_msg'])
  72. return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
  73. elif self._param.trans_type == 'fieldtranslate':
  74. domain = self._param.domain
  75. sign = md5((appid + ans + salt + domain + secret_key).encode('utf-8')).hexdigest()
  76. 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
  77. headers = {"Content-Type": "application/x-www-form-urlencoded"}
  78. response = requests.post(url=url, headers=headers).json()
  79. if response.get('error_code'):
  80. BaiduFanyi.be_output("**Error**:" + response['error_msg'])
  81. return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
  82. except Exception as e:
  83. BaiduFanyi.be_output("**Error**:" + str(e))