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.5KB

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