dmMaze / BallonsTranslator

深度学习辅助漫画翻译工具, 支持一键机翻和简单的图像/文本编辑 | Yet another computer-aided comic/manga translation tool powered by deeplearning
GNU General Public License v3.0
2.37k stars 159 forks source link

Add LibreTranslate #344

Open mr-grixa opened 7 months ago

mr-grixa commented 7 months ago

I made a connection to the local Libre Translate for myself. Hastily done, the url and api key are not editable. I got the list of languages through the AI chatbot, but I didn't check everything.

from .base import *
import requests

@register_translator('LibreTranslate')
class LibreTranslator(BaseTranslator):

    concate_text = False
    params: Dict = {
    }

    def _setup_translator(self):
        self.lang_map['العربية'] = 'ar'
        self.lang_map['Azərbaycan dili'] = 'az'
        self.lang_map['中文'] = 'zh'
        self.lang_map['繁體中文'] = 'zht'
        self.lang_map['čeština'] = 'cs'
        self.lang_map['Dansk'] = 'da'
        self.lang_map['Nederlands'] = 'nl'
        self.lang_map['English'] = 'en'
        self.lang_map['Esperanto'] = 'eo'
        self.lang_map['Suomi'] = 'fi'
        self.lang_map['Français'] = 'fr'
        self.lang_map['Deutsch'] = 'de'
        self.lang_map['Ελληνικά'] = 'el'
        self.lang_map['עברית'] = 'he'
        self.lang_map['हिन्दी'] = 'hi'
        self.lang_map['magyar nyelv'] = 'hu'
        self.lang_map['Bahasa Indonesia'] = 'id'
        self.lang_map['Gaeilge'] = 'ga'
        self.lang_map['Italiano'] = 'it'
        self.lang_map['日本語'] = 'ja'
        self.lang_map['ⵜⴰⵇⴱⴰⵢⵍⵉⵜ'] = 'kab'
        self.lang_map['한국어'] = 'ko'
        self.lang_map['occitan'] = 'oc'
        self.lang_map['پارسی'] = 'fa'
        self.lang_map['Polski'] = 'pl'
        self.lang_map['Português'] = 'pt'
        self.lang_map['русский язык'] = 'ru'
        self.lang_map['slovenčina'] = 'sk'
        self.lang_map['Español'] = 'es'
        self.lang_map['Svenska'] = 'sv'
        self.lang_map['Türkçe'] = 'tr'
        self.lang_map['українська мова'] = 'uk'
        self.lang_map['Tiếng Việt'] = 'vi'

    def _translate(self, src_list: List[str]) -> List[str]:
        # URL of the LibreTranslate API server
        url = "http://localhost:5000/translate"
        # Empty list to store the translated strings
        result = []
        # Loop through the source list
        for src in src_list:
            # Make a POST request to the API with the source string and the languages
            response = requests.post(url, data={"q": src,
                                    "source": self.lang_map[self.lang_source],
                                    "target": self.lang_map[self.lang_target],
                                    "format": "text",
                                    "api_key": ""})
            # Check if the response is successful
            if response.ok:
                # Parse the JSON response and get the translated text
                translated = response.json()["translatedText"]
                # Append the translated text to the result list
                result.append(translated)
            else:
                # Handle the error
                print(f"Request failed with status code: {response.status_code}")
                print(f"Response text: {response.text}")
        # Return the result list
        return result
bropines commented 7 months ago

I made a connection to the local Libre Translate for myself. Hastily done, the url and api key are not editable. I got the list of languages through the AI chatbot, but I didn't check everything.

from .base import *
import requests

@register_translator('LibreTranslate')
class LibreTranslator(BaseTranslator):

    concate_text = False
    params: Dict = {
    }

    def _setup_translator(self):
        self.lang_map['العربية'] = 'ar'
        self.lang_map['Azərbaycan dili'] = 'az'
        self.lang_map['中文'] = 'zh'
        self.lang_map['繁體中文'] = 'zht'
        self.lang_map['čeština'] = 'cs'
        self.lang_map['Dansk'] = 'da'
        self.lang_map['Nederlands'] = 'nl'
        self.lang_map['English'] = 'en'
        self.lang_map['Esperanto'] = 'eo'
        self.lang_map['Suomi'] = 'fi'
        self.lang_map['Français'] = 'fr'
        self.lang_map['Deutsch'] = 'de'
        self.lang_map['Ελληνικά'] = 'el'
        self.lang_map['עברית'] = 'he'
        self.lang_map['हिन्दी'] = 'hi'
        self.lang_map['magyar nyelv'] = 'hu'
        self.lang_map['Bahasa Indonesia'] = 'id'
        self.lang_map['Gaeilge'] = 'ga'
        self.lang_map['Italiano'] = 'it'
        self.lang_map['日本語'] = 'ja'
        self.lang_map['ⵜⴰⵇⴱⴰⵢⵍⵉⵜ'] = 'kab'
        self.lang_map['한국어'] = 'ko'
        self.lang_map['occitan'] = 'oc'
        self.lang_map['پارسی'] = 'fa'
        self.lang_map['Polski'] = 'pl'
        self.lang_map['Português'] = 'pt'
        self.lang_map['русский язык'] = 'ru'
        self.lang_map['slovenčina'] = 'sk'
        self.lang_map['Español'] = 'es'
        self.lang_map['Svenska'] = 'sv'
        self.lang_map['Türkçe'] = 'tr'
        self.lang_map['українська мова'] = 'uk'
        self.lang_map['Tiếng Việt'] = 'vi'

    def _translate(self, src_list: List[str]) -> List[str]:
        # URL of the LibreTranslate API server
        url = "http://localhost:5000/translate"
        # Empty list to store the translated strings
        result = []
        # Loop through the source list
        for src in src_list:
            # Make a POST request to the API with the source string and the languages
            response = requests.post(url, data={"q": src,
                                    "source": self.lang_map[self.lang_source],
                                    "target": self.lang_map[self.lang_target],
                                    "format": "text",
                                    "api_key": ""})
            # Check if the response is successful
            if response.ok:
                # Parse the JSON response and get the translated text
                translated = response.json()["translatedText"]
                # Append the translated text to the result list
                result.append(translated)
            else:
                # Handle the error
                print(f"Request failed with status code: {response.status_code}")
                print(f"Response text: {response.text}")
        # Return the result list
        return result

chat gpt help? :)

mr-grixa commented 7 months ago

chat gpt help? :)

claude