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.

deepl.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. from abc import ABC
  17. from agent.component.base import ComponentBase, ComponentParamBase
  18. import deepl
  19. class DeepLParam(ComponentParamBase):
  20. """
  21. Define the DeepL component parameters.
  22. """
  23. def __init__(self):
  24. super().__init__()
  25. self.auth_key = "xxx"
  26. self.parameters = []
  27. self.source_lang = 'ZH'
  28. self.target_lang = 'EN-GB'
  29. def check(self):
  30. self.check_positive_integer(self.top_n, "Top N")
  31. self.check_valid_value(self.source_lang, "Source language",
  32. ['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
  33. 'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR',
  34. 'UK', 'ZH'])
  35. self.check_valid_value(self.target_lang, "Target language",
  36. ['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'ES', 'ET', 'FI', 'FR', 'HU',
  37. 'ID', 'IT', 'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT-BR', 'PT-PT', 'RO', 'RU',
  38. 'SK', 'SL', 'SV', 'TR', 'UK', 'ZH'])
  39. class DeepL(ComponentBase, ABC):
  40. component_name = "GitHub"
  41. def _run(self, history, **kwargs):
  42. ans = self.get_input()
  43. ans = " - ".join(ans["content"]) if "content" in ans else ""
  44. if not ans:
  45. return DeepL.be_output("")
  46. try:
  47. translator = deepl.Translator(self._param.auth_key)
  48. result = translator.translate_text(ans, source_lang=self._param.source_lang,
  49. target_lang=self._param.target_lang)
  50. return DeepL.be_output(result.text)
  51. except Exception as e:
  52. DeepL.be_output("**Error**:" + str(e))