Skip to content

Commit b2f147a

Browse files
committed
update compatibility
1 parent c4d06d7 commit b2f147a

File tree

2 files changed

+81
-33
lines changed
  • articles/ai-services/translator/document-translation/quickstarts/includes/sdk

2 files changed

+81
-33
lines changed

articles/ai-services/translator/document-translation/quickstarts/includes/sdk/csharp.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: laujan
55
manager: nitinme
66
ms.service: azure-ai-translator
77
ms.topic: include
8-
ms.date: 06/14/2024
8+
ms.date: 06/17/2024
99
ms.author: lajanuar
1010
recommendations: false
1111
---
@@ -165,7 +165,7 @@ Here's a snippet of the expected output:
165165

166166
## Synchronous translation code sample
167167

168-
You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.pdf) for this quickstart. The source language is English.
168+
You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.docx) for this quickstart. The source language is English.
169169

170170
```csharp
171171

@@ -188,7 +188,7 @@ class Program {
188188
using Stream fileStream = File.OpenRead(filePath);
189189

190190
// MultipartFormFileData (string name, System.IO.Stream content, string contentType);
191-
var sourceDocument = new MultipartFormFileData(Path.GetFileName(filePath), fileStream, "text/html");
191+
var sourceDocument = new MultipartFormFileData(Path.GetFileName(filePath), fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
192192

193193
DocumentTranslateContent content = new DocumentTranslateContent(sourceDocument);
194194

@@ -363,7 +363,7 @@ Here's a snippet of the expected output:
363363

364364
## Synchronous translation code sample
365365

366-
You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.pdf) for this quickstart. The source language is English.
366+
You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.docx) for this quickstart. The source language is English.
367367

368368
```csharp
369369

@@ -382,11 +382,11 @@ class Program {
382382

383383
try
384384
{
385-
string filePath = @"C:\{folder}\document.txt"
385+
string filePath = @"C:\{folder}\document-translation-sample.docx"
386386
using Stream fileStream = File.OpenRead(filePath);
387387

388388
// MultipartFormFileData (string name, System.IO.Stream content, string contentType);
389-
var sourceDocument = new MultipartFormFileData(Path.GetFileName(filePath), fileStream, "text/html");
389+
var sourceDocument = new MultipartFormFileData(Path.GetFileName(filePath), fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
390390

391391
DocumentTranslateContent content = new DocumentTranslateContent(sourceDocument);
392392

articles/ai-services/translator/document-translation/quickstarts/includes/sdk/python.md

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: laujan
55
manager: nitinme
66
ms.service: azure-ai-translator
77
ms.topic: include
8-
ms.date: 05/15/2024
8+
ms.date: 06/17/2024
99
ms.author: lajanuar
1010
recommendations: false
1111
---
@@ -24,16 +24,16 @@ Install the latest version of the Document Translation client library:
2424
pip install azure-ai-translation-document==1.1.0b1
2525
```
2626

27-
### Translate a document or batch files
27+
### Translate batch files
2828

2929
1. For this project, you need a **source document** uploaded to your **source container**. You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.pdf) for this quickstart. The source language is English.
3030

3131
1. In your Python application file, create variables for your resource key and custom endpoint. For more information, *see* [Retrieve your key and custom domain endpoint](../../../how-to-guides/use-rest-api-programmatically.md#retrieve-your-key-and-custom-domain-endpoint).
3232

3333
```python
34-
key = "<your-key>"
35-
endpoint = "<your-custom-endpoint>"
36-
34+
key = "{your-api-key}"
35+
endpoint = "{your-document-translation-endpoint}"
36+
3737
```
3838

3939
1. Initialize a `DocumentTranslationClient` object that contains your `endpoint` and `key` parameters.
@@ -60,7 +60,7 @@ Install the latest version of the Document Translation client library:
6060
* **targetUri** The SAS URI, with a SAS token appended as a query string,for the target container to which the translated documents are written.
6161
* **targetLanguageCode**. The language code for the translated documents. You can find language codes on our [Language support](../../../../language-support.md) page.
6262

63-
### Code Sample
63+
## Asynchronous translation code sample
6464

6565
> [!IMPORTANT]
6666
> Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../../../../key-vault/general/overview.md). For more information, *see* Azure AI services [security](../../../../../../ai-services/security-features.md).
@@ -74,45 +74,45 @@ from azure.core.credentials import AzureKeyCredential
7474
from azure.ai.translation.document import DocumentTranslationClient
7575

7676
# create variables for your resource key, custom endpoint, sourceUrl, targetUrl, and targetLanguage
77-
key = "<your-key>"
78-
endpoint = "<your-custom-endpoint>"
79-
sourceUri = "<your-container-sourceUrl>"
80-
targetUri = "<your-container-targetUrl>"
81-
targetLanguage = "<target-language-code>"
77+
key = '{your-api-key}'
78+
endpoint = '{your-document-translation-endpoint}'
79+
sourceUri = '<your-container-sourceUrl>'
80+
targetUri = '<your-container-targetUrl>'
81+
targetLanguage = '<target-language-code>'
8282

8383

84-
# initialize a new instance of the DocumentTranslationClient object to interact with the Document Translation feature
84+
# initialize a new instance of the DocumentTranslationClient object to interact with the asynchronous Document Translation feature
8585
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
8686

8787
# include source and target locations and target language code for the begin translation operation
8888
poller = client.begin_translation(sourceUri, targetUri, targetLanguage)
8989
result = poller.result()
9090

91-
print("Status: {}".format(poller.status()))
92-
print("Created on: {}".format(poller.details.created_on))
93-
print("Last updated on: {}".format(poller.details.last_updated_on))
91+
print('Status: {}'.format(poller.status()))
92+
print('Created on: {}'.format(poller.details.created_on))
93+
print('Last updated on: {}'.format(poller.details.last_updated_on))
9494
print(
95-
"Total number of translations on documents: {}".format(
95+
'Total number of translations on documents: {}'.format(
9696
poller.details.documents_total_count
9797
)
9898
)
9999

100-
print("\nOf total documents...")
101-
print("{} failed".format(poller.details.documents_failed_count))
102-
print("{} succeeded".format(poller.details.documents_succeeded_count))
100+
print('\nOf total documents...')
101+
print('{} failed'.format(poller.details.documents_failed_count))
102+
print('{} succeeded'.format(poller.details.documents_succeeded_count))
103103

104104
for document in result:
105-
print("Document ID: {}".format(document.id))
106-
print("Document status: {}".format(document.status))
107-
if document.status == "Succeeded":
108-
print("Source document location: {}".format(document.source_document_url))
105+
print('Document ID: {}'.format(document.id))
106+
print('Document status: {}'.format(document.status))
107+
if document.status == 'Succeeded':
108+
print('Source document location: {}'.format(document.source_document_url))
109109
print(
110-
"Translated document location: {}".format(document.translated_document_url)
110+
'Translated document location: {}'.format(document.translated_document_url)
111111
)
112-
print("Translated to language: {}\n".format(document.translated_to))
112+
print('Translated to language: {}\n'.format(document.translated_to))
113113
else:
114114
print(
115-
"Error Code: {}, Message: {}\n".format(
115+
'Error Code: {}, Message: {}\n'.format(
116116
document.error.code, document.error.message
117117
)
118118
)
@@ -130,4 +130,52 @@ Here's a snippet of the expected output:
130130

131131
:::image type="content" source="../../../../media/quickstarts/python-output-document.png" alt-text="Screenshot of the Python output in the terminal window. ":::
132132

133-
That's it! You just created a program to translate documents in a storage container using the Python client library.
133+
## Synchronous translation code sample
134+
135+
You can download our [document translation sample document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/Translator/document-translation-sample.docx) for this quickstart. The source language is English.
136+
137+
```python
138+
import os
139+
from azure.core.credentials import AzureKeyCredential
140+
from azure.ai.translation.document import SingleDocumentTranslationClient
141+
from azure.ai.translation.document.models import DocumentTranslateContent
142+
143+
144+
def sample_single_document_translation():
145+
146+
# create variables for your resource api key, document translation endpoint, and target language
147+
key = "<your-api-key>"
148+
endpoint = "<your-document-translation-endpoint>"
149+
target_language = "{target-language-code}"
150+
151+
# initialize a new instance of the SingleDocumentTranslationClient object to interact with the synchronous Document Translation feature
152+
client = SingleDocumentTranslationClient(endpoint, AzureKeyCredential(key))
153+
154+
# absolute path to your document
155+
file_path = "C:/{your-file-path}/document-translation-sample.docx"
156+
file_name = os.path.path.basename(file_path)
157+
file_type = (
158+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
159+
)
160+
print(f"File for translation: {file_name}")
161+
162+
with open(file_name, "r") as file:
163+
file_contents = file.read()
164+
165+
document_content = (file_name, file_contents, file_type)
166+
document_translate_content = DocumentTranslateContent(document=document_content)
167+
168+
response_stream = client.document_translate(
169+
body=document_translate_content, target_language=target_language
170+
)
171+
translated_response = response_stream.decode("utf-8-sig") # type: ignore[attr-defined]
172+
print(f"Translated response: {translated_response}")
173+
174+
175+
if __name__ == "__main__":
176+
sample_single_document_translation()
177+
178+
179+
```
180+
181+
That's it! You just created a program to translate documents asynchronously and synchronously using the Python client library.

0 commit comments

Comments
 (0)