Reputation: 75
I wanted to use pydeepl in order to translate some sentences on python. I have installed pydeepl and reproduced the same code as the main page of this library:
import pydeepl
sentence = 'I like turtles.'
from_language = 'EN'
to_language = 'ES'
translation = pydeepl.translate(sentence, to_language, from_lang=from_language)
print(translation)
Unfortunately I get this error:
TranslationError: DeepL call resulted in a unknown result.
Can anyone help please? Thanks in advance!
Upvotes: 1
Views: 1492
Reputation: 281
EDIT: DeepL Python Library MrGuemez is partially right, requests only go through an official DeepL api key now. That being said, they have both paid and free versions.
The DeepL free API has a 500,000 character/Mo. limit, whereas the paid version has a flat fee of $5/Mo., with an additional $20/20,000,000 characters. If you are worried about exceeding costs in the paid version, DeepL offers a cost control setting that you can set to make sure you don't exceed a certain monetary ceiling. Not only is there now a free version of the API, there is an official DeepL Python Library:
Run a simple
pip install deepl
and you can get started very easily: if you don't care about hard-coding your auth_key you can declare a translator object as such:
translator = deepl.Translator("DEEPL_AUTH_KEY")
if you don't want to hard code it you can set it as an environmental variable this way: translator = deepl.Translator(os.getenv("DEEPL_AUTH_KEY"))
)
You can then easily translate string text in like this:
# (Taken from the documentation)
# Translate text into a target language, in this case, French
result = translator.translate_text("Hello, world!", target_lang="FR")
print(result) # "Bonjour, le monde !"
and multiple strings like this:
# (Taken from the documentation)
# 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"
print(result[1].text) # "How are you?"
print(result[1].detected_source_lang) # "ES"
If you have a need to translate full documents, you can also pass those in:
translator.translate_document_from_filepath(
"path/to/write/to/WhatABeautifulDay.docx", # Translated File
"path/to/original/CheBellissimaGiornata.docx", # Original File
target_lang="EN-US"
)
Just as a side note, the DeepL "EN" option is deprecated, and you must now use "EN-US" or "EN-GB" in your request.
OUTDATED (Still works for translating strings)
To use the api key in python you must structure a query as such:
import requests
raw_returned_data = requests.post(
url="https://api.deepl.com/v2/translate",
data={
"target_lang": "EN",
"auth_key": auth_key, # where auth key is your api key
"text": data # you can pass in a hard coded string or variable
},
)
And to view the response:
returned_data = raw_returned_data.json()["translations"][0]["text"]
Upvotes: 1
Reputation: 126
It looks like the API is ONLY available if you pay, not free anymore...
Upvotes: 1