7/29/2023 0 Comments Azure microsoft translator![]() post (url, params =params, json =body ) 'from' : source_language, # Optional, will auto-detect in most cases def translate (self, text, source_language, target_language ) : The translate method takes the text to translate and calls the API to translate from the source language to the target language, and then parses the response to return just the translated text. class TranslatorClient ( ) :ĭef _init_ (self, cog_service_key, cog_service_region ) : Ocp-Apim-Subscription-Region is optional if using the Translator resource, but including it lets this code also work with a multi-service Cognitive Services resource. Microsoft uses the oddly named Ocp-Apim-Subscription-Key header for it’s API key. In the _init_ method, we set up the authentication headers for the Translate API, and set up a re-usable session. The TranslatorClient handles the requests to the Translator API. ![]() So with this version of the NotionClient, we can conveniently get the title of a page as text, or, retrieve the response for any kind of page property. Title is it’s own kind of property, so I’ve made a method to call the API and get the plain text out of the title property. default_headers = "ĭifferent properties return different kinds of objects as a response. Notion versions it’s API via the Notion-Version header, and authorizes it using the Authorization header with a bearer token. Re-using sessions like this is good for performance. In the _init_ method we set up our standard headers, and set up a session we’ll use for all requests for this class. NotionClient Class #Īs mentioned, NotionClient will handle requests to the API. With this structure, the TranslatorClient class knows nothing about Notion and the NotionClient class knows nothing about translation, and it should be fairly straightforward to substitute another translation API or use the translation and Notion clients for other purposes in a larger application. There’s then a main function which glues those bits together, and a bit of code that handles running things as a script. This code is organized into 3 classes: NotionClient handles requests to the Notion API, along with turning the responses into convenient formats for other classes, TranslatorClient handles requests to the Microsoft Translator API, and NotionTranslator glues these two pieces together. Do something with the translated title (in this case, we’ll just print it out).Send the text to the translation API and parse the response.In order to translate a page title from Notion, there’s 3 main steps: Optionally, you can create a separate parent page for the translated output as well (otherwise it will use the original page. env file should now look something like: COG_SERVICE_KEY=a12.Ĭreate a test page and connect it to the integration. ![]() Getting Started with the Notion API Using PythonĪdd the Internal Integration Token to your. COG_SERVICE_KEY=c42.įor more information on setting up an integration and connecting a page, you can check out env file as COG_SERVICE_KEY and COG_SERVICE_REGION. From there, copy one of the keys and set the key and region in your environment or a. Once you’ve created the resource, you’ll need to go to the Keys and Endpoint tab. You can hit the “Go to resource” button from here, or, you can find it via the Search bar at the top. Once you create the resource, it will take around 30s or so to complete, and then you’ll get the complete screen. If this isn’t an option, try a different region. Choose the “Free F0” pricing tier to avoid being charged.Select a valid subscription (Yours will probably be Pay as You Go), and create a new resource group - you can call it something like translator-resources.Click on the link, or use the Search bar to search for Translator. Create a Translator resource in the Azure Portal.gitignore if you’re making a git repository). env file should not be shared as it stores secret data (put it in your. ![]() This tutorial will use some environment variables to store API keys securely, and we use python-dotenv to load them, but if you prefer, you can set environment variables directly. Instructions below use venv but you can use your preferred approach. It’s good practice to create separate Python environments for different projects.This may require a credit card, but this tutorial can be completed with free resources. You’ll need to create a Microsoft Azure account for this tutorial if you don’t already have one. Part 2 covers translating some common block types into a new page. In the second part, we’ll expand this and translate page content into a new page. In Part 1 (this post), we will set up a script to print out the translated title. In this tutorial, we’ll work towards a non-trivial sample of linking Notion with another API, the Azure Cognitive Services Translator API, and create a script that can automatically translate a Notion page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |