/csa-translation

Primary LanguagePythonApache License 2.0Apache-2.0

Azure Cognitive Services Translation demo

from Microsoft Learn:

Try the latest version of Azure Translator. In this quickstart, you'll get started using the Translator service to translate text using a programming language of your choice or the REST API. For this project, we recommend using the free pricing tier (F0), while you're learning the technology, and later upgrading to a paid tier for production.

Run

  1. Create a file .env at the root folder (follow instructions on how to retrieve the keys in the link above):
    TRANSLATOR_KEY=xxxxxxxxxxxxxxxxxxxxxxxxx
    TRANSLATOR_ENDPOINT=https://api.cognitive.microsofttranslator.com
    TRANSLATOR_LOCATION=YOUR_TRANSLATOR_SERVICE_LOCATION
  2. Install dependencies:
    pip install -r requirements.txt
  3. Run the sample code:
    python main.py

Alternatively, you can run it from bash command line:

source .env
# Pass secret key and region using headers to a custom endpoint
curl -X POST "$TRANSLATOR_ENDPOINT/translate?api-version=3.0&from=en&to=fr&to=es&to=pt" \
-H "Ocp-Apim-Subscription-Key: $TRANSLATOR_KEY" \
-H "Ocp-Apim-Subscription-Region: $TRANSLATOR_LOCATION" \
-H "Content-Type: application/json" \
-d "[{'Text':'Hello'}]" -v | jq

the result will look like this:

[
  {
    "translations": [
      {
        "text": "Bonjour",
        "to": "fr"
      },
      {
        "text": "Hola",
        "to": "es"
      },
      {
        "text": "Olá",
        "to": "pt"
      }
    ]
  }
]