Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add libretranslate support #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# translate
A [maubot](https://github.com/maubot/maubot) to translate words using Google Translate (DeepL is planned too)
A [maubot](https://github.com/maubot/maubot) to translate words using Google Translate, DeepL or LibreTranslate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still no DeepL support, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, the provider code is there, but I have not tested if it is fully functional, so maybe you are right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I tested deepl and ran into some issues I was not able to figure out how to fix. So I reverted it's status in the README to planned. Thanks for the catch 👍


## Usage

Expand All @@ -19,15 +19,11 @@ You can also use the alias `tr`:
!tr en ru Hello world.

The first parameter (source language) can be set to `auto` or omitted entirely
to let Google Translate detect the source language. Additionally, you can reply
to let the bot detect the source language. Additionally, you can reply
to a message with `!tr <from> <to>` (no text) to translate the message you
replied to.

## supported languages:

- de: (german)
- en: (english)
- zh: (chinese)
- ...

Full list of supported languages: https://cloud.google.com/translate/docs/languages
This depends on the translation provider you choose. For google, a list of
supported languages can be found at https://cloud.google.com/translate/docs/languages
5 changes: 4 additions & 1 deletion base-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Translation provider settings
provider:
id: google
args: {}
args: {
url: 'translate.example.com', # required if you use libretranslate, else irrelevant
api_key: 'loremipsumdolorsitamen1234' # optional if you use libretranslate, else irrelevant
}
auto_translate:
- room_id: '!roomid:example.com'
main_language: en
Expand Down
11 changes: 6 additions & 5 deletions translate/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class TranslatorBot(Plugin):

async def start(self) -> None:
await super().start()
self.on_external_config_update()
await self.on_external_config_update()

def on_external_config_update(self) -> None:
async def on_external_config_update(self) -> None:
self.translator = None
self.config.load_and_update()
self.auto_translate = self.config.load_auto_translate()
try:
self.translator = self.config.load_translator()
self.translator = await self.config.load_translator()
except TranslationProviderError:
self.log.exception("Error loading translator")

Expand Down Expand Up @@ -84,7 +84,8 @@ def is_acceptable(lang: str) -> bool:
async def command_handler(self, evt: MessageEvent, language: Optional[Tuple[str, str]],
text: str) -> None:
if not language:
await evt.reply("Usage: !translate [from] <to> [text or reply to message]")
await evt.reply("No supported target language detected. "
"Usage: !translate [from] <to> [text or reply to message]")
return
if not self.config["response_reply"]:
evt.disable_reply = True
Expand All @@ -95,7 +96,7 @@ async def command_handler(self, evt: MessageEvent, language: Optional[Tuple[str,
reply_evt = await self.client.get_event(evt.room_id, evt.content.get_reply_to())
text = reply_evt.content.body
if not text:
await evt.reply("Usage: !translate [from] <to> [text or reply to message]")
await evt.reply("Nothing to translate detected. Usage: !translate [from] <to> [text or reply to message]")
return
result = await self.translator.translate(text, to_lang=language[1], from_lang=language[0])
await evt.reply(result.text)
2 changes: 2 additions & 0 deletions translate/provider/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AbstractTranslationProvider(ABC):
@abstractmethod
def __init__(self, args: Dict) -> None:
pass
async def post_init(self) -> None:
pass

@abstractmethod
async def translate(self, text: str, to_lang: str, from_lang: str = "auto") -> Result:
Expand Down
70 changes: 70 additions & 0 deletions translate/provider/libretranslate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# translate - A maubot plugin to translate words.
# Copyright (C) 2022 Tammes Burghard
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import re
from typing import Dict

from aiohttp import ClientSession, client_exceptions
from yarl import URL

from . import AbstractTranslationProvider, Result


class LibreTranslate(AbstractTranslationProvider):
headers: Dict[str, str] = {"Content-Type": "application/json"}

def __init__(self, args: Dict) -> None:
super().__init__(args)
if args.get("url") is None:
raise ValueError("Please specify the url of your preferred libretranslate instance in provider.args.url")
else:
self._base_url = args.get("url")
if not re.match(r"^https?://", self._base_url):
self._base_url = "https://" + self._base_url
self.url: URL = URL(self._base_url + "/translate")
self.api_key = args.get("api_key")
self.supported_languages = None

async def post_init(self):
try:
async with ClientSession() as sess:
resp = await sess.get(self._base_url + "/languages")
self.supported_languages = {lang["code"]: lang["name"] for lang in await resp.json()}
except client_exceptions.ClientError:
raise ValueError(f"This url ({self._base_url}) does not point to a compatible libretranslate instance. "
f"Please change it")

async def translate(self, text: str, to_lang: str, from_lang: str = "auto") -> Result:
if not from_lang:
from_lang = "auto"
async with ClientSession() as sess:
data=json.dumps({"q": text, "source": from_lang, "target": to_lang,
"format": "text", "api_key": self.api_key})
resp = await sess.post(self.url, data=data, headers=self.headers)
if resp.status == 403:
raise ValueError("Request forbidden. You did probably configure an incorrect api key.")
data = await resp.json()
return Result(text=data["translatedText"],
source_language=data["detectedLanguage"]["language"] if from_lang == "auto" else from_lang)

def is_supported_language(self, code: str) -> bool:
return code.lower() in self.supported_languages.keys()

def get_language_name(self, code: str) -> str:
return self.supported_languages[code]


make_translation_provider = LibreTranslate
6 changes: 4 additions & 2 deletions translate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("auto_translate")
helper.copy("response_reply")

def load_translator(self) -> AbstractTranslationProvider:
async def load_translator(self) -> AbstractTranslationProvider:
try:
provider = self["provider.id"]
mod = import_module(f".{provider}", "translate.provider")
make = mod.make_translation_provider
except (KeyError, AttributeError, ImportError) as e:
raise TranslationProviderError("Failed to load translation provider") from e
try:
return make(self["provider.args"])
translation_provider = make(self["provider.args"])
await translation_provider.post_init()
return translation_provider
except Exception as e:
raise TranslationProviderError("Failed to initialize translation provider") from e

Expand Down