Creating a Document Translation Service using DeepL Pro API’s
If you are looking for a way to create a custom document translation service using DeepL pro API’s, you are in the right place. In this blog post, I will show you how to use the DeepL pro API’s to translate documents from one language to another, and how to customize the translation quality and style according to your needs.
DeepL is a company that provides high-quality machine translation services based on deep learning. They offer a free version of their service that allows you to translate texts up to 5,000 characters per request, and a pro version that gives you access to more features and higher limits. One of the features of the pro version is the ability to translate documents in various formats, such as PDF, DOCX, PPTX, etc.
To use the DeepL pro API’s, you need to have a DeepL pro account and an API key. You can sign up for a free trial or a paid subscription on their website: https://www.deepl.com/pro. Once you have your account and API key, you can start using the API’s to translate documents.
The basic workflow of using the DeepL pro API’s for document translation is as follows:
- Upload the document you want to translate to the DeepL server using the /document endpoint. You need to specify the source language and the target language, and optionally some parameters to customize the translation quality and style. You will receive a document ID as a response.
- Check the status of the document translation using the /document/{document_id} endpoint. You can poll this endpoint until the status is “done” or “error”. If there is an error, you will receive an error code and message as a response.
- Download the translated document from the DeepL server using the /document/{document_id}/result endpoint. You will receive a URL as a response, which you can use to download the translated document in the same format as the original document.
You can find more details and examples of how to use the DeepL pro API’s for document translation in their documentation: https://www.deepl.com/docs-api/translating-documents/.
In this blog post, I will show you how to use Python DeepL library and the Azure Function to implement a simple document translation service using the DeepL pro API’s.
The DeepL Python library provides a more straightforward and seamless way to integrate DeepL’s translation services into your Python applications. You can directly use Python code to make translation requests without having to handle raw HTTP requests and responses.
Create a simple front end web app for uploading the document.
Web Page (HTML and JavaScript):
Create the Frontend Webpage:
- Create an HTML page (
index.html
) that allows users to upload documents and select the target language. - Use JavaScript to capture the user’s input and send it to your Azure Function for translation.
- Download the translated document on the webpage.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DeepL Document Translation</title>
</head>
<body>
<h1>DeepL Document Translation</h1>
<input type="file" id="fileInput">
<label for="targetLanguage">Select Target Language:</label>
<select id="targetLanguage">
<option value="BG">Bulgarian (BG)</option>
<option value="CS">Czech (CS)</option>
<option value="DA">Danish (DA)</option>
<option value="DE">German (DE)</option>
<option value="EL">Greek (EL)</option>
<option value="EN-US">English (EN-US)</option>
<option value="EN-GB">English (EN-GB)</option>
<option value="ES">Spanish (ES)</option>
<option value="ET">Estonian (ET)</option>
<option value="FI">Finnish (FI)</option>
<option value="FR">French (FR)</option>
<option value="HU">Hungarian (HU)</option>
<option value="ID">Indonesian (ID)</option>
<option value="IT">Italian (IT)</option>
<option value="JA">Japanese (JA)</option>
<option value="KO">Korean (KO)</option>
<option value="LT">Lithuanian (LT)</option>
<option value="LV">Latvian (LV)</option>
<option value="NB">Norwegian Bokmål (NB)</option>
<option value="NL">Dutch (NL)</option>
<option value="PL">Polish (PL)</option>
<option value="PT">Portuguese (PT)</option>
<option value="RO">Romanian (RO)</option>
<option value="RU">Russian (RU)</option>
<option value="SK">Slovak (SK)</option>
<option value="SL">Slovenian (SL)</option>
<option value="SV">Swedish (SV)</option>
<option value="TR">Turkish (TR)</option>
<option value="UK">Ukrainian (UK)</option>
<option value="ZH">Chinese (ZH)</option>
<!-- Add more language options as needed -->
</select>
<button onclick="translateDocument()">Translate</button>
<div id="translationResult"></div>
<iframe id="documentViewer" width="100%" height="600px" frameborder="0"></iframe>
<script>
async function translateDocument() {
const fileInput = document.getElementById('fileInput');
const targetLanguage = document.getElementById('targetLanguage').value;
const translationResult = document.getElementById('translationResult');
// const apiUrl = 'https://api.deepl.com/v2/document';
const apiUrl = 'http://localhost:7071/api/deepl_translate_documents';
if (!fileInput.files.length) {
translationResult.innerHTML = 'Please select a file.';
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
formData.append('target_lang', targetLanguage);
try {
const response = await fetch(apiUrl, {
method: 'POST',
body: formData,
// headers: {
// 'Authorization': `DeepL-Pro-Auth-Key ${apiKey}`,
// },
});
if (response.ok) {
const documentBlob = await response.blob();
// const documentViewer = document.getElementById('documentViewer');
// documentViewer.src = URL.createObjectURL(documentBlob);
const a = document.createElement('a');
a.href = URL.createObjectURL(documentBlob);
// Replace with the desired filename
a.download = 'document.pdf'; // Change extension if needed
// Append the anchor element to the document and trigger the click event
document.body.appendChild(a);
a.click();
} else {
console.error('Failed to fetch document:', response.status, response.statusText);
}
// const data = await response.json();
// const translatedUrl = data.translations[0].output;
// translationResult.innerHTML = `<a href="${translatedUrl}" target="_blank">Translated Document</a>`;
} catch (error) {
console.error('Translation error:', error);
translationResult.innerHTML = 'An error occurred during translation.';
}
}
</script>
</body>
</html>
This example uses the Fetch API to make a request to the Azure Function API endpoint, fetches the document as a Blob, and then creates a temporary anchor element (<a>
) to trigger the download. The anchor element's download
attribute specifies the filename for the downloaded document.
Azure Function (Python):
- In your Azure Functions app, create a new Python function deepl_translate_documents.
- Implement the code that connects to the DeepL Pro API and performs the translation.
Install the below packages using pip:
azure-functions==1.15.0
certifi==2023.7.22
charset-normalizer==3.2.0
deepl==1.15.0
idna==3.4
requests==2.31.0
urllib3==2.0.4
import logging
import azure.functions as func
import os
import requests
import tempfile
import deepl
from deepl import DocumentTranslationException, DeepLException
auth_key = os.environ["DEEPL_API_KEY"]
translator = deepl.Translator(auth_key)
def main(req: func.HttpRequest) -> func.HttpResponse:
try:
# Check if a file was uploaded
file = req.files.get('file')
target_lang = req.form.get('target_lang')
if not file:
return func.HttpResponse("No file uploaded.", status_code=400)
if not target_lang:
return func.HttpResponse("Target Language not provided.", status_code=400)
# Create a temporary folder
with tempfile.TemporaryDirectory() as temp_folder:
temp_file_path = os.path.join(temp_folder, file.filename)
with open(temp_file_path, 'wb') as temp_file:
temp_file.write(file.read())
# Translate the document using DeepL
translated_document_path = translate_document(temp_file_path, target_lang)
# Return the translated document
with open(translated_document_path, "rb") as f:
document_content = f.read()
return func.HttpResponse(body=document_content, status_code=200, mimetype="application/pdf")
except Exception as e:
return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)
def translate_document(temp_file_path, target_lang):
# provide me a code for translating a document using deepl API's
# Translate a formal document from English to German
input_path = temp_file_path
output_path = temp_file_path + "output.pdf"
# 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=target_lang,
# 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:
response = translator.translate_document(
in_file,
out_file,
target_lang=target_lang
# formality="more"
)
return output_path
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}")
logging.error(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}")
except deepl.DeepLException as error:
# Errors during upload raise a DeepLException
print(error)
logging.error(error)
- Sign up for a DeepL Pro API account and get your API key.
- Add the DeepL API key to the Azure function environment variable.
Make sure to securely store your DeepL API key and other sensitive information. Azure Key Vault can be used for this purpose.
Please note that this is a high-level overview of the process, and you’ll need to delve into the specifics of each step, including handling document processing, implementing proper error handling, and styling your webpage.
Remember to refer to the official documentation of Azure Functions, DeepL API, and any other technologies you’re using for detailed guidance and best practices.