Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Text Translation] Updating the SDK from the latest TypeSpec #35450

Merged
merged 33 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions sdk/translation/azure-ai-translation-text/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Release History

## 1.0.0b2 (Unreleased)
## 1.0.0 (Unreleased)

### Features Added
- Added support for AAD authentication.
- Added support for Entra Id authentication.

### Breaking Changes

- All calls to the client using parameter 'content' have been changed to use parameter 'request_body'.
- All calls to the client using parameter 'content' have been changed to use parameter 'body'.
- Users can call methods using just a string type instead of complex objects.
- get_languages methods were changed to get_supported_languages.
- sent_len property was renamed to sentences_lengths.
- from_parameter parameter was renamed to source_language.
- score parameter was renamed to confidence.
- from_script parameter was renamed to source_language_script.
- to_script parameter was renamed to _target_langauge_script.

### Bugs Fixed

Expand Down
37 changes: 21 additions & 16 deletions sdk/translation/azure-ai-translation-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ az cognitiveservices account keys list --resource-group <your-resource-group-nam

#### Create a `TextTranslationClient` using an API key and Region credential

Once you have the value for the API key and Region, create an `TranslatorCredential`. This will allow you to
Once you have the value for the API key and Region, create an `AzureKeyCredential`. This will allow you to
update the API key without creating a new client.

With the value of the `endpoint`, `credential` and a `region`, you can create the [TextTranslationClient][client_sample]:

<!-- SNIPPET: sample_text_translation_client.create_text_translation_client_with_credential -->

```python
credential = TranslatorCredential(apikey, region)
text_translator = TextTranslationClient(credential=credential, endpoint=endpoint)
credential = AzureKeyCredential(apikey)
text_translator = TextTranslationClient(credential=credential, endpoint=endpoint, region=region)
```

<!-- END SNIPPET -->
Expand Down Expand Up @@ -92,7 +92,7 @@ Gets the set of languages currently supported by other operations of the Transla

```python
try:
response = text_translator.get_languages()
response = text_translator.get_supported_languages()
MikeyMCZ marked this conversation as resolved.
Show resolved Hide resolved

print(
f"Number of supported languages for translate operation: {len(response.translation) if response.translation is not None else 0}"
Expand Down Expand Up @@ -143,14 +143,14 @@ try:
target_languages = ["cs", "es", "de"]
input_text_elements = ["This is a test"]

response = text_translator.translate(request_body=input_text_elements, to=target_languages)
response = text_translator.translate(body=input_text_elements, to=target_languages)
translation = response[0] if response else None

if translation:
detected_language = translation.detected_language
if detected_language:
print(
f"Detected languages of the input text: {detected_language.language} with score: {detected_language.score}."
f"Detected languages of the input text: {detected_language.language} with score: {detected_language.confidence}."
)
for translated_text in translation.translations:
print(f"Text was translated to: '{translated_text.to}' and the result is: '{translated_text.text}'.")
Expand All @@ -177,12 +177,15 @@ Converts characters or letters of a source language to the corresponding charact
```python
try:
language = "zh-Hans"
from_script = "Hans"
to_script = "Latn"
source_language_script = "Hans"
target_language_script = "Latn"
input_text_elements = ["这是个测试。"]

response = text_translator.transliterate(
request_body=input_text_elements, language=language, from_script=from_script, to_script=to_script
body=input_text_elements,
language=language,
source_language_script=source_language_script,
target_language_script=target_language_script,
)
transliteration = response[0] if response else None

Expand Down Expand Up @@ -216,21 +219,23 @@ try:
input_text_elements = ["The answer lies in machine translation. This is a test."]

response = text_translator.translate(
request_body=input_text_elements, to=target_languages, include_sentence_length=include_sentence_length
body=input_text_elements, to=target_languages, include_sentence_length=include_sentence_length
)
translation = response[0] if response else None

if translation:
detected_language = translation.detected_language
if detected_language:
print(
f"Detected languages of the input text: {detected_language.language} with score: {detected_language.score}."
f"Detected languages of the input text: {detected_language.language} with score: {detected_language.confidence}."
)
for translated_text in translation.translations:
print(f"Text was translated to: '{translated_text.to}' and the result is: '{translated_text.text}'.")
if translated_text.sent_len:
print(f"Source Sentence length: {translated_text.sent_len.src_sent_len}")
print(f"Translated Sentence length: {translated_text.sent_len.trans_sent_len}")
if translated_text.sentences_lengths:
print(f"Source Sentence length: {translated_text.sentences_lengths.src_sentences_lengths}")
print(
f"Translated Sentence length: {translated_text.sentences_lengths.translated_sentences_lengths}"
)

except HttpResponseError as exception:
if exception.error is not None:
Expand All @@ -257,7 +262,7 @@ try:
input_text_elements = ["fly"]

response = text_translator.lookup_dictionary_entries(
request_body=input_text_elements, from_parameter=source_language, to=target_language
body=input_text_elements, source_language=source_language, to=target_language
)
dictionary_entry = response[0] if response else None

Expand Down Expand Up @@ -293,7 +298,7 @@ try:
input_text_elements = [DictionaryExampleTextItem(text="fly", translation="volar")]

response = text_translator.lookup_dictionary_examples(
content=input_text_elements, from_parameter=source_language, to=target_language
content=input_text_elements, source_language=source_language, to=target_language
)
dictionary_entry = response[0] if response else None

Expand Down
2 changes: 1 addition & 1 deletion sdk/translation/azure-ai-translation-text/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "python",
"TagPrefix": "python/translation/azure-ai-translation-text",
"Tag": "python/translation/azure-ai-translation-text_afde2bdc8c"
"Tag": "python/translation/azure-ai-translation-text_35ab9367d7"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from ._patch import TextTranslationClient, TranslatorCredential, TranslatorAADCredential
from ._patch import TextTranslationClient
from ._version import VERSION

__version__ = VERSION


from ._patch import patch_sdk as _patch_sdk

__all__ = [
"TranslatorCredential",
"TranslatorAADCredential",
"TextTranslationClient",
]

Expand Down
Loading
Loading