From ca7996cdbd5fb3e37fc0837130d69f872bfe3a4c Mon Sep 17 00:00:00 2001 From: samerbam Date: Tue, 5 Sep 2023 11:07:38 -0400 Subject: [PATCH] basic boilerplate for translate skill --- backend/main.py | 1 + backend/skills/config.py.example | 4 ++- backend/skills/translations.py | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 backend/skills/translations.py diff --git a/backend/main.py b/backend/main.py index 0d06cd5..fa932f2 100644 --- a/backend/main.py +++ b/backend/main.py @@ -7,6 +7,7 @@ from skills.timers import Timers from skills.todos import Todos from skills.weather import Weather from skills.wolfram import Wolfram + from NLP import NLP import sys diff --git a/backend/skills/config.py.example b/backend/skills/config.py.example index efc3d92..f729990 100644 --- a/backend/skills/config.py.example +++ b/backend/skills/config.py.example @@ -1,2 +1,4 @@ # Copy & Rename this file to config.py and fill in data -ntfy_url="" # Obtained from ntfy.sh app (choose a random string of numbers/letters for better security) \ No newline at end of file +ntfy_url="" # Obtained from ntfy.sh app (choose a random string of numbers/letters for better security) +deepl_api_key="" # Obtained from https://www.deepl.com/en/docs-api +google_api_key="" #Obtained from https://cloud.google.com/translate/pricing \ No newline at end of file diff --git a/backend/skills/translations.py b/backend/skills/translations.py new file mode 100644 index 0000000..4034464 --- /dev/null +++ b/backend/skills/translations.py @@ -0,0 +1,54 @@ +import requests +from skills.config import deepl_api_key +from skills.config import google_api_key + + +class Translations: + def __init__(self): + self.trigger_phrase = "translate" + self.total_chars_translated = 0 + self.deepl_chars_translated = 0 + self.googl_chars_translated = 0 #only increment if successful translation (according to api, it only counts towards the quota if you ) + self.free_monthly_char_limit_googl = 500000 + self.free_monthly_char_limit_deepl = 500000 + self.supported_deepl_langs = ["list of supported languages"] + + + def _get_language_code(self, query): + return "FR" #TODO: Convert parse language from query and convert possibly into lang code depending on api requirements + # Return None if language not recognized + + def _clean_up_text(self, query): + return "cleaned up text" #TODO: remove characters that are not needed to translate text (apis charge per char sent, not per char translated) + + + def translate_deepl(self, text, language): + + headers = { + 'Authorization': 'DeepL-Auth-Key ' + deepl_api_key, + 'Content-Type': 'application/json', + } + + json_data = { + 'text': [text], + 'target_lang': language, + } + + response = requests.post('https://api-free.deepl.com/v2/translate', headers=headers, json=json_data) + + def translate_google(self, text, language): + pass #TODO: add in google translate api, probably using python client library for google api + + + def translate(self, text): + parsed_text = self._clean_up_text(text) #TODO: Parse text and extract language and query from it, + target_language = self._get_language_code(text) + + if target_language in self.supported_deepl_langs: + return self.translate_deepl(parsed_text, target_language) + + if target_language is not None: + return self.translate_google(text, target_language) + + return self.translate_google(text, "auto") +