-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Kuchenmampfer
wants to merge
3
commits into
maubot:master
Choose a base branch
from
Kuchenmampfer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍