Reputation: 1
I'm using Azure Cognitive Services for text translation and need to send data in the following JSON format:
data = [{"text":"Good Morning"},{"text":"Good night"}]
When I use the Azure REST API directly, I can successfully send this input, and it works as expected. However, when I try to send the same data using the Azure client libraries, I'm unable to do so.
Is there a way to send this specific JSON input using Azure client libraries for translation? If so, what would be the correct method to achieve this?
#This is my code.
from azure.ai.translation.text import TextTranslationClient
client = TextTranslationClient(endpoint, credential)
result = client.translate(to=["de"], input=data)
when i execute this it gives me error - string index out of range
Upvotes: 0
Views: 59
Reputation: 10455
How to Send JSON Input to Azure Cognitive Services Translation API Using Azure Client Libraries?
The error occurs when you try to access a character in a string using an index that's outside the valid range. In a string, indexes start at "0" and go up to one less than the total number of characters in the string.
Error:
In my environment, I got the same error when accessing the response with particular range (out of index).
I tried with below code and got the expected results successfully with Json input format.
Code:
import os
import json
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.text import TextTranslationClient
key = "xxxxxxx0"
region="eastus"
credential = AzureKeyCredential(key)
text_translator = TextTranslationClient(credential=credential, region=region)
to_language = ["de"]
data = [{"text":"Good Morning"},{"text":"Good night"}]
response = text_translator.translate(body=data, to_language=to_language)
if response:
for translation in response:
for translation_text in translation.translations:
print(translation_text.text)
Output:
Guten Morgen
Gute Nacht
Reference: Azure Text Translation client library for Python | Microsoft Learn
Upvotes: 0