Giter Club home page Giter Club logo

deepl-python's Introduction

DeepL Python Library

PyPI version Supported Python versions License: MIT

The DeepL API is a language translation API that allows other computer programs to send texts and documents to DeepL's servers and receive high-quality translations. This opens a whole universe of opportunities for developers: any translation product you can imagine can now be built on top of DeepL's best-in-class translation technology.

The DeepL Python library offers a convenient way for applications written in Python to interact with the DeepL API. We intend to support all API functions with the library, though support for new features may be added to the library after they’re added to the API.

Getting an authentication key

To use the DeepL Python Library, you'll need an API authentication key. To get a key, please create an account here. With a DeepL API Free account you can translate up to 500,000 characters/month for free.

Installation

The library can be installed from PyPI using pip:

pip install --upgrade deepl

If you need to modify this source code, install the dependencies using poetry:

poetry install

On Ubuntu 22.04 an error might occur: ModuleNotFoundError: No module named 'cachecontrol'. Use the workaround sudo apt install python3-cachecontrol as explained in this bug report.

Requirements

The library is tested with Python versions 3.6 to 3.11.

The requests module is used to perform HTTP requests; the minimum is version 2.0.

Starting in 2024, we will drop support for older Python versions that have reached official end-of-life. You can find the Python versions and support timelines here. To continue using this library, you should update to Python 3.8+.

Usage

Import the package and construct a Translator. The first argument is a string containing your API authentication key as found in your DeepL Pro Account.

Be careful not to expose your key, for example when sharing source code.

import deepl

auth_key = "f63c02c5-f056-..."  # Replace with your key
translator = deepl.Translator(auth_key)

result = translator.translate_text("Hello, world!", target_lang="FR")
print(result.text)  # "Bonjour, le monde !"

This example is for demonstration purposes only. In production code, the authentication key should not be hard-coded, but instead fetched from a configuration file or environment variable.

Translator accepts additional options, see Configuration for more information.

Translating text

To translate text, call translate_text(). The first argument is a string containing the text you want to translate, or a list of strings if you want to translate multiple texts.

source_lang and target_lang specify the source and target language codes respectively. The source_lang is optional, if it is unspecified the source language will be auto-detected.

Language codes are case-insensitive strings according to ISO 639-1, for example 'DE', 'FR', 'JA''. Some target languages also include the regional variant according to ISO 3166-1, for example 'EN-US', or 'PT-BR'. The full list of supported languages is in the API documentation.

There are additional optional arguments to control translation, see Text translation options below.

translate_text() returns a TextResult, or a list of TextResults corresponding to your input text(s). TextResult has two properties: text is the translated text, and detected_source_lang is the detected source language code.

# Translate text into a target language, in this case, French:
result = translator.translate_text("Hello, world!", target_lang="FR")
print(result.text)  # "Bonjour, le monde !"

# Translate multiple texts into British English
result = translator.translate_text(
    ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB"
)
print(result[0].text)  # "How are you?"
print(result[0].detected_source_lang)  # "JA" the language code for Japanese
print(result[1].text)  # "How are you?"
print(result[1].detected_source_lang)  # "ES" the language code for Spanish

# Translate into German with less and more Formality:
print(
    translator.translate_text(
        "How are you?", target_lang="DE", formality="less"
    )
)  # 'Wie geht es dir?'
print(
    translator.translate_text(
        "How are you?", target_lang="DE", formality="more"
    )
)  # 'Wie geht es Ihnen?'

Text translation options

In addition to the input text(s) argument, the available translate_text() arguments are:

  • source_lang: Specifies the source language code, but may be omitted to auto-detect the source language.
  • target_lang: Required. Specifies the target language code.
  • split_sentences: specify how input text should be split into sentences, default: 'on'.
    • 'on'' (SplitSentences.ON): input text will be split into sentences using both newlines and punctuation.
    • 'off' (SplitSentences.OFF): input text will not be split into sentences. Use this for applications where each input text contains only one sentence.
    • 'nonewlines' (SplitSentences.NO_NEWLINES): input text will be split into sentences using punctuation but not newlines.
  • preserve_formatting: controls automatic-formatting-correction. Set to True to prevent automatic-correction of formatting, default: False.
  • formality: controls whether translations should lean toward informal or formal language. This option is only available for some target languages, see Listing available languages.
    • 'less' (Formality.LESS): use informal language.
    • 'more' (Formality.MORE): use formal, more polite language.
  • glossary: specifies a glossary to use with translation, either as a string containing the glossary ID, or a GlossaryInfo as returned by get_glossary().
  • context: specifies additional context to influence translations, that is not translated itself. Characters in the context parameter are not counted toward billing. See the API documentation for more information and example usage.
  • tag_handling: type of tags to parse before translation, options are 'html' and 'xml'.

The following options are only used if tag_handling is 'xml':

  • outline_detection: specify False to disable automatic tag detection, default is True.
  • splitting_tags: list of XML tags that should be used to split text into sentences. Tags may be specified as an array of strings (['tag1', 'tag2']), or a comma-separated list of strings ('tag1,tag2'). The default is an empty list.
  • non_splitting_tags: list of XML tags that should not be used to split text into sentences. Format and default are the same as for splitting_tags.
  • ignore_tags: list of XML tags that containing content that should not be translated. Format and default are the same as for splitting_tags.

For a detailed explanation of the XML handling options, see the API documentation.

Translating documents

To translate documents, you may call either translate_document() using file IO objects, or translate_document_from_filepath() using file paths. For both functions, the first and second arguments correspond to the input and output files respectively.

Just as for the translate_text() function, the source_lang and target_lang arguments specify the source and target language codes.

There are additional optional arguments to control translation, see Document translation options below.

# Translate a formal document from English to German
input_path = "/path/to/Instruction Manual.docx"
output_path = "/path/to/Bedienungsanleitung.docx"
try:
    # Using translate_document_from_filepath() with file paths 
    translator.translate_document_from_filepath(
        input_path,
        output_path,
        target_lang="DE",
        formality="more"
    )

    # Alternatively you can use translate_document() with file IO objects
    with open(input_path, "rb") as in_file, open(output_path, "wb") as out_file:
        translator.translate_document(
            in_file,
            out_file,
            target_lang="DE",
            formality="more"
        )

except deepl.DocumentTranslationException as error:
    # If an error occurs during document translation after the document was
    # already uploaded, a DocumentTranslationException is raised. The
    # document_handle property contains the document handle that may be used to
    # later retrieve the document from the server, or contact DeepL support.
    doc_id = error.document_handle.id
    doc_key = error.document_handle.key
    print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}")
except deepl.DeepLException as error:
    # Errors during upload raise a DeepLException
    print(error)

translate_document() and translate_document_from_filepath() are convenience functions that wrap multiple API calls: uploading, polling status until the translation is complete, and downloading. If your application needs to execute these steps individually, you can instead use the following functions directly:

  • translate_document_upload(),
  • translate_document_get_status() (or translate_document_wait_until_done()), and
  • translate_document_download()

Document translation options

In addition to the input file, output file, source_lang and target_lang arguments, the available translate_document() and translate_document_from_filepath() arguments are:

  • formality: same as in Text translation options.
  • glossary: same as in Text translation options.
  • output_format: (translate_document() only) file extension of desired format of translated file, for example: 'pdf'. If unspecified, by default the translated file will be in the same format as the input file.

Glossaries

Glossaries allow you to customize your translations using user-defined terms. Multiple glossaries can be stored with your account, each with a user-specified name and a uniquely-assigned ID.

Creating a glossary

You can create a glossary with your desired terms and name using create_glossary(). Each glossary applies to a single source-target language pair. Note: Glossaries are only supported for some language pairs, see Listing available glossary languages for more information. The entries should be specified as a dictionary.

If successful, the glossary is created and stored with your DeepL account, and a GlossaryInfo object is returned including the ID, name, languages and entry count.

# Create an English to German glossary with two terms:
entries = {"artist": "Maler", "prize": "Gewinn"}
my_glossary = translator.create_glossary(
    "My glossary",
    source_lang="EN",
    target_lang="DE",
    entries=entries,
)
print(
    f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) "
    f"{my_glossary.source_lang}->{my_glossary.target_lang} "
    f"containing {my_glossary.entry_count} entries"
)
# Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries

You can also upload a glossary downloaded from the DeepL website using create_glossary_from_csv(). Instead of supplying the entries as a dictionary, specify the CSV data as csv_data either as a file-like object or string or bytes containing file content:

# Open the CSV file assuming UTF-8 encoding. If your file contains a BOM,
# consider using encoding='utf-8-sig' instead.
with open('/path/to/glossary_file.csv', 'r',  encoding='utf-8') as csv_file:
    csv_data = csv_file.read()  # Read the file contents as a string
    my_csv_glossary = translator.create_glossary_from_csv(
        "CSV glossary",
        source_lang="EN",
        target_lang="DE",
        csv_data=csv_data,
    )

The API documentation explains the expected CSV format in detail.

Getting, listing and deleting stored glossaries

Functions to get, list, and delete stored glossaries are also provided:

  • get_glossary() takes a glossary ID and returns a GlossaryInfo object for a stored glossary, or raises an exception if no such glossary is found.
  • list_glossaries() returns a list of GlossaryInfo objects corresponding to all of your stored glossaries.
  • delete_glossary() takes a glossary ID or GlossaryInfo object and deletes the stored glossary from the server, or raises an exception if no such glossary is found.
# Retrieve a stored glossary using the ID
glossary_id = "559192ed-8e23-..."
my_glossary = translator.get_glossary(glossary_id)

# Find and delete glossaries named 'Old glossary'
glossaries = translator.list_glossaries()
for glossary in glossaries:
    if glossary.name == "Old glossary":
        translator.delete_glossary(glossary)

Listing entries in a stored glossary

The GlossaryInfo object does not contain the glossary entries, but instead only the number of entries in the entry_count property.

To list the entries contained within a stored glossary, use get_glossary_entries() providing either the GlossaryInfo object or glossary ID:

entries = translator.get_glossary_entries(my_glossary)
print(entries)  # "{'artist': 'Maler', 'prize': 'Gewinn'}"

Using a stored glossary

You can use a stored glossary for text translation by setting the glossary argument to either the glossary ID or GlossaryInfo object. You must also specify the source_lang argument (it is required when using a glossary):

text = "The artist was awarded a prize."
with_glossary = translator.translate_text(
    text, source_lang="EN", target_lang="DE", glossary=my_glossary,
)
print(with_glossary)  # "Der Maler wurde mit einem Gewinn ausgezeichnet."

# For comparison, the result without a glossary:
without_glossary = translator.translate_text(text, target_lang="DE")
print(without_glossary)  # "Der Künstler wurde mit einem Preis ausgezeichnet."

Using a stored glossary for document translation is the same: set the glossary argument and specify the source_lang argument:

translator.translate_document(
    in_file, out_file, source_lang="EN", target_lang="DE", glossary=my_glossary,
)

The translate_document(), translate_document_from_filepath() and translate_document_upload() functions all support the glossary argument.

Checking account usage

To check account usage, use the get_usage() function.

The returned Usage object contains three usage subtypes: character, document and team_document. Depending on your account type, some usage subtypes may be invalid; this can be checked using the valid property. For API accounts:

  • usage.character.valid is True,
  • usage.document.valid and usage.team_document.valid are False.

Each usage subtype (if valid) has count and limit properties giving the amount used and maximum amount respectively, and the limit_reached property that checks if the usage has reached the limit. The top level Usage object has the any_limit_reached property to check all usage subtypes.

usage = translator.get_usage()
if usage.any_limit_reached:
    print('Translation limit reached.')
if usage.character.valid:
    print(
        f"Character usage: {usage.character.count} of {usage.character.limit}")
if usage.document.valid:
    print(f"Document usage: {usage.document.count} of {usage.document.limit}")

Listing available languages

You can request the list of languages supported by DeepL for text and documents using the get_source_languages() and get_target_languages() functions. They both return a list of Language objects.

The name property gives the name of the language in English, and the code property gives the language code. The supports_formality property only appears for target languages, and indicates whether the target language supports the optional formality parameter.

print("Source languages:")
for language in translator.get_source_languages():
    print(f"{language.name} ({language.code})")  # Example: "German (DE)"

print("Target languages:")
for language in translator.get_target_languages():
    if language.supports_formality:
        print(f"{language.name} ({language.code}) supports formality")
        # Example: "Italian (IT) supports formality"
    else:
        print(f"{language.name} ({language.code})")
        # Example: "Lithuanian (LT)"

Listing available glossary languages

Glossaries are supported for a subset of language pairs. To retrieve those languages use the get_glossary_languages() function, which returns an array of GlossaryLanguagePair objects. Each has source_lang and target_lang properties indicating that that pair of language codes is supported.

glossary_languages = translator.get_glossary_languages()
for language_pair in glossary_languages:
    print(f"{language_pair.source_lang} to {language_pair.target_lang}")
    # Example: "EN to DE", "DE to EN", etc.

You can also find the list of supported glossary language pairs in the API documentation.

Note that glossaries work for all target regional-variants: a glossary for the target language English ("EN") supports translations to both American English ("EN-US") and British English ("EN-GB").

Writing a Plugin

If you use this library in an application, please identify the application with deepl.Translator.set_app_info, which needs the name and version of the app:

translator = deepl.Translator(...).set_app_info("sample_python_plugin", "1.0.2")

This information is passed along when the library makes calls to the DeepL API. Both name and version are required. Please note that setting the User-Agent header via deepl.http_client.user_agent will override this setting, if you need to use this, please manually identify your Application in the User-Agent header.

Exceptions

All module functions may raise deepl.DeepLException or one of its subclasses. If invalid arguments are provided, they may raise the standard exceptions ValueError and TypeError.

Configuration

Logging

Logging can be enabled to see the HTTP requests sent and responses received by the library. Enable and control logging using Python's logging module, for example:

import logging

logging.basicConfig()
logging.getLogger('deepl').setLevel(logging.DEBUG)

Server URL configuration

You can override the URL of the DeepL API by specifying the server_url argument when constructing a deepl.Translator. This may be useful for testing purposes. You do not need to specify the URL to distinguish API Free and API Pro accounts, the library selects the correct URL automatically.

server_url = "http://user:pass@localhost:3000"
translator = deepl.Translator(..., server_url=server_url)

Proxy configuration

You can configure a proxy by specifying the proxy argument when constructing a deepl.Translator:

proxy = "http://user:[email protected]:3128"
translator = deepl.Translator(..., proxy=proxy)

The proxy argument is passed to the underlying requests session, see the documentation for requests; a dictionary of schemes to proxy URLs is also accepted.

Override SSL verification

You can control how requests performs SSL verification by specifying the verify_ssl option when constructing a deepl.Translator, for example to disable SSL certificate verification:

translator = deepl.Translator(..., verify_ssl=False)

This option is passed to the underlying requests session as the verify option, see the documentation for requests.

Configure automatic retries

This SDK will automatically retry failed HTTP requests (if the failures could be transient, e.g. a HTTP 429 status code). This behaviour can be configured in http_client.py, for example by default the number of retries is 5. This can be changed to 3 as follows:

import deepl

deepl.http_client.max_network_retries = 3
t = deepl.Translator(...)
t.translate_text(...)

You can configure the timeout min_connection_timeout the same way, as well as set a custom user_agent, see the next section.

Anonymous platform information

By default, we send some basic information about the platform the client library is running on with each request, see here for an explanation. This data is completely anonymous and only used to improve our product, not track any individual users. If you do not wish to send this data, you can opt-out when creating your deepl.Translator object by setting the send_platform_info flag like so:

translator = deepl.Translator(..., send_platform_info=False)

You can also customize the user_agent by setting its value explicitly before constructing your deepl.Translator object.

deepl.http_client.user_agent = 'my custom user agent'
translator = deepl.Translator(os.environ["DEEPL_AUTH_KEY"])

Command Line Interface

The library can be run on the command line supporting all API functions. Use the --help option for usage information:

python3 -m deepl --help

The CLI requires your DeepL authentication key specified either as the DEEPL_AUTH_KEY environment variable, through the keyring module, or using the --auth-key option, for example:

python3 -m deepl --auth-key=YOUR_AUTH_KEY usage

Note that the --auth-key argument must appear before the command argument. To use the keyring module, set the DEEPL_AUTH_KEY field in the service deepl to your API key. The recognized commands are:

Command Description
text translate text(s)
document translate document(s)
usage print usage information for the current billing period
languages print available languages
glossary create, list, and remove glossaries

For example, to translate text:

python3 -m deepl --auth-key=YOUR_AUTH_KEY text --to=DE "Text to be translated."

Wrap text arguments in quotes to prevent the shell from splitting sentences into words.

Issues

If you experience problems using the library, or would like to request a new feature, please open an issue.

Development

We welcome Pull Requests, please read the contributing guidelines.

Tests

Execute the tests using pytest. The tests communicate with the DeepL API using the auth key defined by the DEEPL_AUTH_KEY environment variable.

Be aware that the tests make DeepL API requests that contribute toward your API usage.

The test suite may instead be configured to communicate with the mock-server provided by deepl-mock. Although most test cases work for either, some test cases work only with the DeepL API or the mock-server and will be otherwise skipped. The test cases that require the mock-server trigger server errors and test the client error-handling. To execute the tests using deepl-mock, run it in another terminal while executing the tests. Execute the tests using pytest with the DEEPL_MOCK_SERVER_PORT and DEEPL_SERVER_URL environment variables defined referring to the mock-server.

deepl-python's People

Contributors

benjamin-eikel-deepl avatar daniel-jones-deepl avatar daniel-jones-dev avatar foehlschlaeger avatar janebbing avatar mike-winters-deepl avatar phap7 avatar seratch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deepl-python's Issues

Proxy usage?

Does deepl-python provide out of the box usage of proxies?

For example, I define my proxies config as a dictionary and pass it to deepl.Translator.

feat request: return consumed characters for each translation response

It would be great if for each translation response we'd get also the number of characters used for that specific request.

Always or maybe only if a specific parameter has been set in the request.

For example, I've just realized that if I send an HTML formatted as source text, without the tag_handling parameter will consume all characters, including from the HTML structure code (tags, attributes and their values) but if I pass the tag_handling parameter these will not be counted as used.

I know that I can make use of the separate usage statistics, but it might not be relevant if I'd like to keep track of the usage of each translation, and multiple concurrent users might use my app in the same time.

Otherwise, I have to try to count the characters myself but I might not get the same result as the exact used values.

PS: this is generally available for the API, not only Python lib

Turn off translation punctuation entirely

I really want to turn off punctuation completely on German language and others that I use. Using any of the preserve_formatting="1" or split_sentences='off' won't work.

Can't use on aws with php-python combination

I want to use this API for my university project, but can't do it with php:

This is my php code:

              <?php
                echo "alma: ";
                $original = 'rák';
                $output = passthru("python3 search.py $original");
              ?>

and in search.py

#!/usr/bin/env python
import sys
import requests
import deepl
import logging

logging.basicConfig()
logging.getLogger('deepl').setLevel(logging.DEBUG)

print "translated: \t"
translator = deepl.Translator("mykey")
result = translator.translate_text("Hello, world!", target_lang="FR")
print(result)
print '<br>'

And I got only alma: text back.

Also tried like: system("python3 -m deepl --auth-key=MYKEY text --to=DE 'Text to be translated.'"); but nothing. I didn't find anything in PHP error log.

BUT if I want to use in aws command line as was mentioned than I got the correct answers.

I also opened a stackoverflow error about this: https://stackoverflow.com/questions/69861167/how-to-parse-answers-with-deepl-api

[Feature Request?] Ability to set a preferred gender for outputs with gendered languages

Apologies if this issue is irrelevant.

Is there a way to specify a preferred gender for gendered languages such as French or Spanish in a way that the translation that comes up matches it?

For instance, with preferred gender set to feminine, from english to french:
Input: "I've come to this city"
Output: "Je suis venue dans cette ville" (instead of "Je suis venu dans cette ville")

Otherwise it would be very nice if this was added. In fact, this is kind of a feature request for DeepL as a whole, not just the API, but I didn't know where else to ask.
I guess this would take a certain time to implement, too.

list_glossaries

Hello,

I'm not able to use the "list_glossaries" function.

It's working when I do the call, but I just can't seem to do anything with the object returned?

print(dir(deeplClient))
['_DEEPL_SERVER_URL', '_DEEPL_SERVER_URL_FREE', '_HTTP_STATUS_QUOTA_EXCEEDED', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api_call', '_check_language_and_formality', '_check_valid_languages', '_client', '_raise_for_status', '_server_url', 'close', 'create_glossary', 'delete_glossary', 'get_glossary', 'get_glossary_entries', 'get_glossary_languages', 'get_source_languages', 'get_target_languages', 'get_usage', 'headers', 'list_glossaries', 'server_url', 'translate_document', 'translate_document_download', 'translate_document_from_filepath', 'translate_document_get_status', 'translate_document_upload', 'translate_text', 'translate_text_with_glossary']

glossaries = deeplClient.list_glossaries
print(dir(glossaries))
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

How am I suppose to access the list of glossaries? it's not an iterable object and I don't see any methods usefull to get info out of the object.

The glossary creation seem to work great, but I want to create them just once and then retrieve them through the list.

Best regards,
Samuel

Diacritics in UTF-8 Glossary File Not Working

I have attempted several times to upload a glossary such that diacritics in French will be handled correctly. I am sure is in UTF-8 and have verified in various ways.

A related issue is that Excel by default includes a Byte Order Mark (BOM), which I have seen is included in the first item in the glossary. Using Notepad++ I have converted the encoding of the file to exclude the BOM, but that does not correct the problem. Here is the link to the glossary file that I uploaded to DeepL using Python.

I am also attaching below:

  1. A screen grab of the "Save as" screen from Excel
  2. The results of a dump of the most recent glossary that I uploaded as well as a screen grab of the problems int the translated document
  3. The actual CSV
    SaveTestGlossaryV2
    GlossaryContents(V2)
    Sample Page of Character Encoding Problems

Sample Python Code for Uploading a Glossary

This issue is related to #44, which documented the following problem as well as a bug with processing a Byte Order Mark (BOM). The issues here is that the following code from the DeepL Python instructions does not work:

with open('/path/to/glossary_file.csv', 'r') as csv_file:
csv_data = csv_file.read() # Read the file contents as a string
my_csv_glossary = translator.create_glossary_from_csv(
"CSV glossary",
source_lang="EN",
target_lang="DE",
csv_data=csv_data,
)

The code does not work as is as documented on issue #44. Needed is the parameter that identifies the glossary file as encoded with UTF-8:

with open('/path/to/glossary_file.csv', 'r', encoding="UTF-8") as csv_file:

translate_text_with_glossary issue

Hello,

I'm getting this issue when I try to call with a glossary:

flask_app | File "/usr/local/lib/python3.8/site-packages/deepl/translator.py", line 745, in translate_text_with_glossary flask_app | source_lang=glossary.source_lang, flask_app | AttributeError: 'numpy.ndarray' object has no attribute 'source_lang'

Here is my calling code:
translation = client.translate_text_with_glossary(df['SourceTokenized'].tolist(), target_lang=targetLangDeeplCode, tag_handling='html', ignore_tags='ignore', split_sentences="0", preserve_formatting="1", formality=formality, glossary=glossaryId)

In your example you are not providing any of the optional parameters. However, I need to have these in my case.

If it can help. The same request without the glossary is working perfectly fine:
translation = client.translate_text(df['SourceTokenized'].tolist(), target_lang=targetLangDeeplCode, tag_handling='html', ignore_tags='ignore', split_sentences="0", preserve_formatting="1", formality=formality)

Thanks for the help!

Best regards,
Samuel

'deepl' has no attribute 'Translator'

I receive an Error with the last Version (deepl 1.3.1) running the example on python 3.9 - 64 bit Windows 10

translator = deepl.Translator("mykey")
AttributeError: partially initialized module 'deepl' has no attribute 'Translator' (most likely due to a circular import)

My SampleCode:

import os
import deepl

translator = deepl.Translator("mykey")
result = translator.translate_text("Hello, world!", target_lang="FR")
print(result)

Maybe a simple error, but i can't figure it out yet what i'am doing wrong. I hope somebody knows what to do. Thanks for your help.

Wrong endpoint. Use https://api.deepl.com For Deepl PRO auth key

Hi,
When I try to use api-free key to translate text it works fine, but when I try to use Pro-key I get following error:

translator.translate_text('abc', target_lang='en-us').text

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/.../venv3/lib/python3.6/site-packages/deepl/translator.py", line 685, in translate_text
    self._raise_for_status(status, content, json)
  File "/home/.../venv3/lib/python3.6/site-packages/deepl/translator.py", line 501, in _raise_for_status
    f"Authorization failure, check auth_key{message}"
deepl.exceptions.AuthorizationException: Authorization failure, check auth_key, message: Wrong endpoint. Use https://api.deepl.com

Translator calls v2 API and fails with this error. But when I tried to change api_call url from v2 to v1 it worked.
Is there any way to use my key with API v2 or change v2 call to v1 except for changing package source code?
I'm using python3.6.14 and requests==2.25.1

GSM-7 as input

Some languages can't be translated if the text entered is from an SMS (eg GSM-7 encoded), this problem is visible in the deep-L web interface (eg try to translate "KAΛHΣΠEPA!").

The problem is that to save space, SMS encoding reuses the latin characters where it can (eg KAHEP) and mixes in greek letters where necessary (ΛΣΠ)

If you know the language the text is in, the solution is straightforward. For each language, convert the relevant latin characters to their native counterparts in unicode. When you do this, the translation works, eg translate ΚΑΛΗΣΠΕΡΑ!. For Greek, this might be:

original_greek_sms = "KAΛHΣΠEPA!"
latin = "EPTYIOAHKZXBNM"
greek = "ΕΡΤΥΙΟΑΗΚΖΧΒΝΜ"

table = str.maketrans(latin, greek)
original_greek_unicode = original_greek_sms.translate(table)
print(original_greek_unicode)  # ΚΑΛΗΣΠΕΡΑ!

Not all languages need this, but every language that has latin-like characters that need separate unicode symbols would need this conversion. Until then, text that comes from an SMS in those languages will not work.

Authorization failure, check auth_key, message: Wrong endpoint

Hello,
When i'm using a free DeepL account, the auth_key works but when i'm using a pro account, the aut_key is wrong with the folloing error : Authorization failure, check auth_key, message: Wrong endpoint

here is my code :

!pip install --upgrade deepl
import deepl
import os

os.environ['API_KEY'] = 'xxxxxxxxxxxxxxxxxxxxxxxx'
translator = deepl.Translator(os.getenv('API_KEY'))

result = translator.translate_text("Hello, world!", target_lang="FR")
print(result)

I don't understand what i have to do to use my Pro account.

Thanks

Suggestion: Asynchronous Support

It really a nice thing to hear DeepL provide a Python client module.

Through the doc I found that this module seems not support async functions, and I think it would be better to use a async request module such as aiohttp to make the translation can be used asynchronously

ModuleNotFoundError during Poetry Installation in Ubuntu 22.04

Hello there,

after forking and cloning of the repository, I was trying to install the dependencies for modification of the source code based on the README file. I was using a virtual machine with Ubuntu 22.04 LTS, but I got an error related to a missing module CacheControl. Probably it is related to Ubuntu 22.04 and the poetry installation regarding to this bug report. The fix would be to install CacheControl via sudo apt install python3-cachecontrol.

Maybe, this can be added as remark to the installation section in the README file?

Here is the traceback after executing of poetry install:

Traceback (most recent call last):
  File "/usr/bin/poetry", line 5, in <module>
    from poetry.console import main
  File "/usr/lib/python3/dist-packages/poetry/console/__init__.py", line 1, in <module>
    from .application import Application
  File "/usr/lib/python3/dist-packages/poetry/console/application.py", line 7, in <module>
    from .commands.about import AboutCommand
  File "/usr/lib/python3/dist-packages/poetry/console/commands/__init__.py", line 4, in <module>
    from .check import CheckCommand
  File "/usr/lib/python3/dist-packages/poetry/console/commands/check.py", line 2, in <module>
    from poetry.factory import Factory
  File "/usr/lib/python3/dist-packages/poetry/factory.py", line 18, in <module>
    from .repositories.pypi_repository import PyPiRepository
  File "/usr/lib/python3/dist-packages/poetry/repositories/pypi_repository.py", line 11, in <module>
    from cachecontrol import CacheControl
ModuleNotFoundError: No module named 'cachecontrol'

Hopefully, I provided enough information, otherwise let me know. This is my first issue and participation in open source, so please feel free to give me any feedback.

ConnectionException due to no internet connection is not instantaneous

Hi,

In case of no internet connection, when I make a POST/GET request using requests library then it instantaneously throw below mentioned connection exception:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api-free.deepl.com', port=443): Max retries exceeded with url: /v2/translate (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001A9772E82B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

However, when I make the same request using deepl library under the same circumstances, there is a waiting time of 15 seconds before it throws the below mentioned connection exeption:

deepl.exceptions.ConnectionException: Connection failed or request timed out.

So, if I am exposing a translation endpoint using deepl then there will be a lag in response even though there is no internet connection. This lag is understandable if the endpoint is not reachable or unresponsive, but if internet connection is not available then shouldn't it be instantaneous like requests lib?

Is there anyway to check which line caused error when translating document?

I have this error which I suspect is due to the format of a few lines in my text file. But this error is very cryptic so there is no way for me to check what caused it.

Traceback (most recent call last):
File ".\testDeepL.py", line 7, in
translator.translate_document_from_filepath(
File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\deepl\translator.py", line 774, in translate_document_from_filepath
raise e
File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\deepl\translator.py", line 764, in translate_document_from_filepath
self.translate_document(
File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\deepl\translator.py", line 803, in translate_document
handle = self.translate_document_upload(
File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\deepl\translator.py", line 858, in translate_document_upload
self._raise_for_status(status, content, json)
File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\deepl\translator.py", line 479, in _raise_for_status
raise DeepLException(f"Bad request{message}")

Update the "Chinese" lang to "Chinese simplified" (or appropriate)

I used deepl api to update some values in a file and had a native speaker review the changes.

They replied with:
..."it would be better to make this version of Chinese zh-cn since it's done according to Simplified Chinese in Mainland China. That will make space for other variants, like zh-tw which is a different dialect of Chinese used in Taiwan."

Problems when NOT using Deepl_API-Key from environment

I am currently practicing with the Python library, initially only with the translation of simple text sections. For this I have disabled the following line:
#translator = deepl.Translator(os.getenv("DEEPL_AUTH_KEY"))
and instead
translator = MyAPIKey
(with the real key). This resulted in an error message:

  File "./deepl_trans.py", line 9
    translator=XXXXXXXe-XXXX-XXXX-XXXXXXXXXXXX:fx
                      ^
SyntaxError: invalid syntax

The ^character is just one position before "e". So I put the key in quotes. Then it throws this error message:

Traceback (most recent call last):
  File "./deepl_trans.py", line 11, in <module>
    result = translator.translate_text("Hello, world!", target_lang="FR")
AttributeError: 'str' object has no attribute 'translate_text'.

What do I have to do if I DON'T want to set the deepl API key as an environment variable but hard coded or as input? (As soon as I set the Api key as an environment variable the script works fine.)

Translated with www.DeepL.com/Translator (free version) ;-)

TypeError: 'type' object is not subscriptable while importing deepl

Hello there

I'm trying to use the deepl API for python like reported here. I am working on JupyterLab.

Anyway, after successfully installing it with the command pip install --upgrade deepl, i get the error (I masked my API key of course):

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0c75bb4de935> in <module>
----> 1 import deepl
      2 
      3 # Create a Translator object providing your DeepL API authentication key
      4 translator = deepl.Translator("MY_API_KEY")

~/.local/lib/python3.6/site-packages/deepl/__init__.py in <module>
     19 from . import http_client  # noqa
     20 
---> 21 from .translator import (  # noqa
     22     DocumentHandle,
     23     DocumentStatus,

~/.local/lib/python3.6/site-packages/deepl/translator.py in <module>
    346 
    347 
--> 348 class Translator:
    349     """Wrapper for the DeepL API for language translation.
    350 

~/.local/lib/python3.6/site-packages/deepl/translator.py in Translator()
   1006         return GlossaryInfo.from_json(json)
   1007 
-> 1008     def list_glossaries(self) -> list[GlossaryInfo]:
   1009         """Retrieves GlossaryInfo for all available glossaries.
   1010 

TypeError: 'type' object is not subscriptable

I already tried to restart the kernel, but that doesn't seem to solve anything.

I tried the same operation on Google Colab as well, and I got the same exact error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-0c75bb4de935> in <module>()
----> 1 import deepl
      2 
      3 # Create a Translator object providing your DeepL API authentication key
      4 translator = deepl.Translator("MY_API_KEY")

2 frames
/usr/local/lib/python3.7/dist-packages/deepl/translator.py in Translator()
   1006         return GlossaryInfo.from_json(json)
   1007 
-> 1008     def list_glossaries(self) -> list[GlossaryInfo]:
   1009         """Retrieves GlossaryInfo for all available glossaries.
   1010 

TypeError: 'type' object is not subscriptable

What do you think it could be?

translating xml - spaces deleted at end of text string before <b> bold

Hello, I am using this API to translate XML documents from Schema. Some of my text elements have bold sections and I use lxml.etree to parse and translate text and tail elements. The bold elements within text end up with the space removed in front of them.
Anything I can do to keep the spaces?

DeepLException

Hello,

There is an example of DeepLException :
except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)

but

  1. I'm not sure how to check the error "code" returned (501, 404, etc)
  2. I noticed in the source code that there is supposed to be an argument "should_retry". Should I expect this argument to be always present in my error and just have to do :
    if error.should_retry: call again
  3. Having an example how to simulate an error returned by DeepL would be practical to debug.

Best regards,
Samuel

Missing language support: Ukrainian

it is very very obvious that there is one Language missing in the entire Deepl universe!

which is Ukrainian!

why?

shall me make a official post about it, someone working to solve it..
or are 44 million ukrainians not existing for deepl.com?

Translation EN-US EN-GB don't work

It seems that these 2 languages are not translated correctly, when you type french text and use EN-US or EN-GB as target language it doesn't translate anything, the text is the same

for exemple translator.translate_text("Bonjour", target_lang='EN-GB') return "Bonjour" and it should return "Hello"

encoding

Trying to use deepl.com, can't find any answers online.

I translate to japanese and get some weird encoding back. Can't figure out how to fix, there is not documentation on this. your examples are useless.

Why is your api so weird? Why not a simple post with json? x-www-form-urlencoded? Is this 1998?

For such a simple api, it's incredibly difficult to use.

glossary

why there are only 4 ways for english->{Lang} ?
amazon, google and other services can use glossaries for any lang.
Deepl is very powerful service but missed glossaries is big problem.

source_lang problem

Hello, I just want to translate from German to French and I don't want to translate the English part in a sentence. For example, when I try to translate "Das is meine mutter, beautiful woman" in deepl translate API it returns 'c'est ma mère, belle femme', but I just want to German part of the sentence and return 'c'est ma mère, beautiful woman'.

And in some requests, all my sentences are in English, and I don't want to translate them to French because I assigned source_lang as DE and target_lang FR, but it translates English sentence to French.

Any way to get native language names from the list?

For instance, in the README it shows this snippet:

for language in translator.get_source_languages():
    print(f"{language.code} ({language.name})")  # Example: "DE (German)"

It would be wonderful if the "native" translation of their language could be shown, too.

In other words, "DE (German)" would also print "Deutsch". Most of the languages I can figure out, but not really sure what the "ZH" equivalent would be.

I'm using the free api level, so I'm hoping that could be added to that level.

Authentication based on enterprise proxy server

In browser-based usage, we have a proxy server with which we connect, eg. deepl.<enterprise domain>.ch. This grants us access to DeepL Pro without the need for a login. Is it possible to use our enterprise proxy server to access DeepL Pro API without using an authentication key?

I could not find anything on this topic in the documentation. As far as I saw, deepl.Translator always raises an exception, if no authentication key is given.

source_lang 'EN-US' and 'EN-GB' do not work

Hi,

deepl.Translator(apikey).translate_text("Berl's lime kiln", target_lang='CS', source_lang='EN-US')

results in this call in translator.py:

self._api_call( "v2/translate", data={'target_lang': 'CS', 'source_lang': 'EN-US', 'text': "Berl's lime kiln"})

which returns:

'{"message":"Value for \'source_lang\' not supported."}'

It seems as if translator.py is communicating with a server which does not support 'EN-US', same for 'EN-GB'.

I have not (yet) tried circumventing translator.py with a direct call to the API server using plain 'EN'.

David

Document_key and document_id for checking translation status is missing

We´d like to translate a large amount of files. Mostly these files are "TXT" and have up to 1 Mio. signs. (We know about the API-Limitations)

If we use this code we will get back an fully translated file:

translator.translate_document_from_filepath(
    "Instruction Manual.docx",
    "Bedienungsanleitung.docx",
    target_lang="DE",
    formality="more"
)

Problem:

In some cases, when the file is very large or the script goes into the timeout cause the API of DeepL is slow, we lose the translation and have to start our script over again.

Solution:

We would like to see the document_ID and document_id as output in the console when starting the script.
If the the script fails, we can grab and download the translation manually from the server. (See "Downloading translated documents" in DeepL-API-Docs)

Translation of Markdown (MD) files

Hi DeepL,

your Python library is great. Since I am using your library to translate some of my articles of my blog, I would like to inform you of some issues. My articles are in the markdown format (. md). When I translate markdown files I often encounter that DeepL does not preserve the formatting. Sometimes, parts of the sentence are repeated in the target language (I suppose because of a new line in the original document). I tried different settings in the Python library but none got it completely right.

Therefore, I would like to ask you to look into making translation of markdown documents better. I am sure this problem also encounter other people, and especially for technical programming documents and websites, markdown is often used. These clients would also benefit hugely from your improvements.

BTW: GitHub has a spec of their own flavor of markdown here: https://github.github.com/gfm/

Thanks! :)

Running python file display "ValueError: auth_key must not be empty"

Hello, following the issue #23 , I have a similar error when I use deepl-python in a Flask python file (this is a Flask mini-server for a web application).

Context :

My DeepL account is a free one with limited access to the DeepL REST API (500 000 characters / month)

Screenshot from 2022-04-07 13-29-07

Each time I connect to my account, I have the following error pop-up :
Screenshot from 2022-04-07 13-28-45

When I run the python file with the authorization key into it, I can't open the web-app because of the following error :

Traceback (most recent call last):
  File "~/app_2.py", line 22, in <module>
    translator = deepl.Translator(os.getenv('xxx-xxx-xxx-xxx-xxx'))
  File "~/.local/lib/python3.10/site-packages/deepl/translator.py", line 429, in __init__
    raise ValueError("auth_key must not be empty")
ValueError: auth_key must not be empty

As you can see, I use my key like for example abc-5ea-0cb-... without the prefix auth-key=

Something strange : I can use my authorization key in a terminal like below (and where is use the prefix auth-key=) :

>>> python3 -m deepl --auth-key=xxx-xxx-xxx-xxx-xxx-xxx text --to=FR "Text to be translated."
>>> Texte à traduire

Questions :

  1. Could you please confirm I use my authorization key the right way ?
  2. If so, could you please tell me how to solve my issue ?

Thank you very much for your kind help !

Inappropriate Exception for queries to Ukranian

Instead of appropriately raising DeepLException: Bad request, message: Value for 'target_lang' not supported., the request throws TooManyRequestsException: Too many requests, DeepL servers are currently experiencing high load, message: Too many requests after ~10s of sleeping. It would be much better to raise early that it is not currently supported.

In [21]: translator.translate_text("hrdinum slava", source_lang='cs', target_lang="uk")        
2022-08-30 12:32:47,545 deepl        INFO     Request to DeepL API method=POST url=https://api-free.deepl.com/v2/translate
2022-08-30 12:32:47,828 deepl        INFO     Starting retry 1 for request POST https://api-free.deepl.com/v2/translate after sleeping for 0.72 seconds. 
2022-08-30 12:32:48,711 deepl        INFO     Starting retry 2 for request POST https://api-free.deepl.com/v2/translate after sleeping for 1.39 seconds. 
2022-08-30 12:32:50,287 deepl        INFO     Starting retry 3 for request POST https://api-free.deepl.com/v2/translate after sleeping for 2.57 seconds. 
2022-08-30 12:32:52,963 deepl        INFO     Starting retry 4 for request POST https://api-free.deepl.com/v2/translate after sleeping for 4.27 seconds. 
2022-08-30 12:32:57,410 deepl        INFO     Starting retry 5 for request POST https://api-free.deepl.com/v2/translate after sleeping for 4.98 seconds. 
2022-08-30 12:33:02,605 deepl        INFO     DeepL API response status_code=429 url=https://api-free.deepl.com/v2/translate
---------------------------------------------------------------------------
TooManyRequestsException                  Traceback (most recent call last)
Input In [21], in <module>
----> 1 translator.translate_text("hrdinum slava", source_lang='cs', target_lang="uk")

File ~/Git/Supernova/venv/lib/python3.9/site-packages/deepl/translator.py:769, in Translator.translate_text(self, text, source_lang, target_lang, split_sentences, preserve_formatting, formality, glossary, tag_handling, outline_detection, non_splitting_tags, splitting_tags, ignore_tags)
    763     request_data["ignore_tags"] = join_tags(ignore_tags)
    765 status, content, json = self._api_call(
    766     "v2/translate", data=request_data
    767 )
--> 769 self._raise_for_status(status, content, json)
    771 translations = json.get("translations", [])
    772 output = []

File ~/Git/Supernova/venv/lib/python3.9/site-packages/deepl/translator.py:560, in Translator._raise_for_status(self, status_code, content, json, glossary, downloading_document)
    558     raise DeepLException(f"Bad request{message}")
    559 elif status_code == http.HTTPStatus.TOO_MANY_REQUESTS:
--> 560     raise TooManyRequestsException(
    561         "Too many requests, DeepL servers are currently experiencing "
    562         f"high load{message}"
    563     )
    564 elif status_code == http.HTTPStatus.SERVICE_UNAVAILABLE:
    565     if downloading_document:

TooManyRequestsException: Too many requests, DeepL servers are currently experiencing high load, message: Too many requests

DocumentTranslationException

Hi there, I've been using the Deepl python package to translate the entire PDFs. I had it running for some time already and most of the time it worked perfectly. Today, I was going through the logs and found some failed cases and the error logs I've collected. I couldn't figure out what went wrong so please provide some suggestions on how to address them:

#1:
Traceback (most recent call last):
  File "/opt/document_translator.py", line 528, in translate_batch_docs
    target_lang=self.target_language_code)
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 780, in translate_document_from_filepath
    raise e
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 775, in translate_document_from_filepath
    glossary=glossary,
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 839, in translate_document
    "Error occurred while translating document", handle
deepl.exceptions.DocumentTranslationException: <super: <class 'DocumentTranslationException'>, <DocumentTranslationException object>>, document request: Document ID: B54E4E5999C915EF63AACA877843C03C, key: 2B17****
!
#2:
Traceback (most recent call last):
  File "/opt/document_translator.py", line 528, in translate_batch_docs
    target_lang=self.target_language_code)
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 780, in translate_document_from_filepath
    raise e
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 775, in translate_document_from_filepath
    glossary=glossary,
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 817, in translate_document
    glossary=glossary,
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 876, in translate_document_upload
    self._raise_for_status(status, content, json)
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 522, in _raise_for_status
    "Too many requests, DeepL servers are currently experiencing "
deepl.exceptions.TooManyRequestsException: Too many requests, DeepL servers are currently experiencing high load, message: Too many non-downloaded documents!
#3:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "/opt/conda/lib/python3.6/http/client.py", line 1379, in getresponse
    response.begin()
  File "/opt/conda/lib/python3.6/http/client.py", line 311, in begin
    version, status, reason = self._read_status()
  File "/opt/conda/lib/python3.6/http/client.py", line 272, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/opt/conda/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/opt/conda/lib/python3.6/ssl.py", line 1012, in recv_into
    return self.read(nbytes, buffer)
  File "/opt/conda/lib/python3.6/ssl.py", line 874, in read
    return self._sslobj.read(len, buffer)
  File "/opt/conda/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/opt/conda/lib/python3.6/site-packages/urllib3/util/retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/opt/conda/lib/python3.6/site-packages/urllib3/packages/six.py", line 735, in reraise
    raise value
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 447, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py", line 337, in _raise_timeout
    self, url, "Read timed out. (read timeout=%s)" % timeout_value
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.deepl.com', port=443): Read timed out. (read timeout=10.886348485946655)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/deepl/http_client.py", line 168, in _internal_request
    request, stream=stream, timeout=timeout, **kwargs
  File "/opt/conda/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.deepl.com', port=443): Read timed out. (read timeout=10.886348485946655)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/document_translator.py", line 528, in translate_batch_docs
    target_lang=self.target_language_code)
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 780, in translate_document_from_filepath
    raise e
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 775, in translate_document_from_filepath
    glossary=glossary,
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 817, in translate_document
    glossary=glossary,
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 874, in translate_document_upload
    "v2/document", data=request_data, files=files
  File "/opt/conda/lib/python3.6/site-packages/deepl/translator.py", line 476, in _api_call
    **kwargs,
  File "/opt/conda/lib/python3.6/site-packages/deepl/http_client.py", line 119, in request_with_backoff
    raise exception
  File "/opt/conda/lib/python3.6/site-packages/deepl/http_client.py", line 106, in request_with_backoff
    request, stream=stream, timeout=backoff.get_timeout()
  File "/opt/conda/lib/python3.6/site-packages/deepl/http_client.py", line 184, in _internal_request
    raise ConnectionException(message, should_retry=True) from e
deepl.exceptions.ConnectionException: Request timed out: HTTPSConnectionPool(host='api.deepl.com', port=443): Read timed out. (read timeout=10.886348485946655)
!

I'm using deepl==1.3.1 and the code I use was:

class DocumentTranslator(object):
    def __init__(self):
        self.translator = deepl.Translator(auth_key=AUTO_TRANSLATION_KEY)
        self.target_language_code = "en-us"

    def translate_batch_docs(pdfs):
        for pdf in pdfs:
            translated_pdf = pdf.replace('.pdf', '_translated.pdf')
            self.translator.translate_document_from_filepath(pdf, translated_pdf, target_lang=self.target_language_code)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.