basic boilerplate for translate skill

This commit is contained in:
samerbam 2023-09-05 11:07:38 -04:00
parent 858788f5d7
commit 37a746ddc1
3 changed files with 58 additions and 1 deletions

View File

@ -7,6 +7,7 @@ from skills.timers import Timers
from skills.todos import Todos from skills.todos import Todos
from skills.weather import Weather from skills.weather import Weather
from skills.wolfram import Wolfram from skills.wolfram import Wolfram
from NLP import NLP from NLP import NLP
import sys import sys

View File

@ -1,2 +1,4 @@
# Copy & Rename this file to config.py and fill in data # 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) 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

View File

@ -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")