{"id":669,"date":"2021-11-20T23:28:00","date_gmt":"2021-11-20T23:28:00","guid":{"rendered":"http:\/\/192.168.8.123\/?p=669"},"modified":"2022-02-11T11:16:26","modified_gmt":"2022-02-11T11:16:26","slug":"developing-ai-apps-with-cognitive-services-get-started-with-cognitive-services","status":"publish","type":"post","link":"https:\/\/exceedthecloud.com\/?p=669","title":{"rendered":"Developing AI Apps with Cognitive Services Get Started with Cognitive Services"},"content":{"rendered":"\n<p>In this exercise, you&#8217;ll get started with Cognitive Services by creating a <strong>Cognitive Services<\/strong> resource in your Azure subscription and using it from a client application. The goal of the exercise is not to gain expertise in any particular service, but rather to become familiar with a general pattern for provisioning and working with cognitive services as a developer.<\/p>\n\n\n\n<p> Prerequisites for this labs : <a rel=\"noreferrer noopener\" href=\"https:\/\/azure.microsoft.com\/en-us\/free\/\" target=\"_blank\">Azure Account<\/a> \/ <a rel=\"noreferrer noopener\" href=\"http:\/\/192.168.8.123\/index.php\/2021\/11\/20\/setting-up-windows-10-environment-for-ai-apps-with-azure-cognitive-services\/\" target=\"_blank\">Setting up Windows 10 environment for AI Apps with Azure Cognitive Services<\/a> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"clone-the-repository-for-this-course\">Clone the repository for this course<\/h2>\n\n\n\n<p>If you have not already cloned <strong>AI-102-AIEngineer<\/strong> code repository to the environment where you&#8217;re working on this lab, follow these steps to do so. Otherwise, open the cloned folder in Visual Studio Code.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Start Visual Studio Code.<\/li><li>Open the palette (SHIFT+CTRL+P) and run a <strong>Git: Clone<\/strong> command to clone the <code>https:\/\/github.com\/MicrosoftLearning\/AI-102-AIEngineer<\/code> repository to a local folder (it doesn&#8217;t matter which folder).<\/li><li>When the repository has been cloned, open the folder in Visual Studio Code.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img1-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Wait while additional files are installed to support the C# code projects in the repo.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: If you are prompted to add required assets to build and debug, select <strong>Not Now<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"provision-a-cognitive-services-resource\">Provision a Cognitive Services resource<\/h2>\n\n\n\n<p>Azure Cognitive Services are cloud-based services that encapsulate artificial intelligence capabilities you can incorporate into your applications. You can provision individual cognitive services resources for specific APIs (for example, <strong>Text Analytics<\/strong> or <strong>Computer Vision<\/strong>), or you can provision a general <strong>Cognitive Services<\/strong> resource that provides access to multiple cognitive services APIs through a single endpoint and key. In this case, you&#8217;ll use a single <strong>Cognitive Services<\/strong> resource.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Open the Azure portal at <code>https:\/\/portal.azure.com<\/code>, and sign in using the Microsoft account associated with your Azure subscription.<\/li><li>Select the <strong>\uff0bCreate a resource<\/strong> button, search for <em>cognitive services<\/em>, and create a <strong>Cognitive Services<\/strong> resource with the following settings:<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img2-3-1024x463.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li><strong>Subscription<\/strong>: <em>Your Azure subscription<\/em><\/li><li><strong>Resource group<\/strong>: <em>Choose or create a resource group (if you are using a restricted subscription, you may not have permission to create a new resource group &#8211; use the one provided)<\/em><\/li><li><strong>Region<\/strong>: <em>Choose any available region<\/em><\/li><li><strong>Name<\/strong>: <em>Enter a unique name<\/em><\/li><li><strong>Pricing tier<\/strong>: Standard S0<\/li><li>Select the required checkboxes and create the resource.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img3-2-1024x464.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Wait for deployment to complete, and then view the deployment details.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img4-2-1024x466.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Go to the resource and view its <strong>Keys and Endpoint<\/strong> page. This page contains the information that you will need to connect to your resource and use it from applications you develop. Specifically:<ul><li>An HTTP <em>endpoint<\/em> to which client applications can send requests.<\/li><li>Two <em>keys<\/em> that can be used for authentication (client applications can use either key to authenticate).<\/li><li>The <em>location<\/em> where the resource is hosted. This is required for requests to some (but not all) APIs.<\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img5-2-1024x471.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-a-rest-interface\">Use a REST Interface<\/h2>\n\n\n\n<p>The cognitive services APIs are REST-based, so you can consume them by submitting JSON requests over HTTP. In this example, you&#8217;ll explore a console application that uses the <strong>Text Analytics<\/strong> REST API to perform language detection; but the basic principle is the same for all of the APIs supported by the Cognitive Services resource.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In this exercise, you can choose to use the REST API from either <strong>C#<\/strong> or <strong>Python<\/strong>. In the steps below, perform the actions appropriate for your preferred language.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In Visual Studio Code, in the <strong>Explorer<\/strong> pane, browse to the <strong>01-getting-started<\/strong> folder and expand the <strong>C-Sharp<\/strong> or <strong>Python<\/strong> folder depending on your language preference.<\/li><li>View the contents of the <strong>rest-client<\/strong> folder, and note that it contains a file for configuration settings:<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"2\"><li><strong>C#<\/strong>: appsettings.json<\/li><li><strong>Python<\/strong>: .env<\/li><\/ul>\n\n\n\n<p>Open the configuration file and update the configuration values it contains to reflect the <strong>endpoint<\/strong> and an authentication <strong>key<\/strong> for your cognitive services resource. Save your changes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img6-2-1024x261.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Note that the <strong>rest-client<\/strong> folder contains a code file for the client application:<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li><strong>C#<\/strong>: Program.cs<\/li><li><strong>Python<\/strong>: rest-client.py<\/li><\/ul>\n\n\n\n<p>Open the code file and review the code it contains, noting the following details:<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Various namespaces are imported to enable HTTP communication<\/li><li>Code in the <strong>Main<\/strong> function retrieves the endpoint and key for your cognitive services resource &#8211; these will be used to send REST requests to the Text Analytics service.<\/li><li>The program accepts user input, and uses the <strong>GetLanguage<\/strong> function to call the Text Analytics language detection REST API for your cognitive services endpoint to detect the language of the text that was entered.<\/li><li>The request sent to the API consists of a JSON object containing the input data &#8211; in this case, a collection of <strong>document<\/strong> objects, each of which has an <strong>id<\/strong> and <strong>text<\/strong>.<\/li><li>The key for your service is included in the request header to authenticate your client application.<\/li><li>The response from the service is a JSON object, which the client application can parse.<\/li><li>Right-click the <strong>rest-client<\/strong> folder and open an integrated terminal. Then enter the following language-specific command to run the program:<\/li><\/ul>\n\n\n\n<p><strong>C#<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet run<\/code><\/pre>\n\n\n\n<p><strong>Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python rest-client.py<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>When prompted, enter some text and review the language that is detected by the service, which is returned in the JSON response. For example, try entering &#8220;Hello&#8221;, &#8220;Bonjour&#8221;, and &#8220;Hola&#8221;.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img7-2-1024x550.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img8-2-1024x555.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>When you have finished testing the application, enter &#8220;quit&#8221; to stop the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-an-sdk\">Use an SDK<\/h2>\n\n\n\n<p>You can write code that consumes cognitive services REST APIs directly, but there are software development kits (SDKs) for many popular programming languages, including Microsoft C#, Python, and Node.js. Using an SDK can greatly simplify development of applications that consume cognitive services.<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>In Visual Studio Code, in the <strong>Explorer<\/strong> pane, in the <strong>01-getting-started<\/strong> folder, expand the <strong>C-Sharp<\/strong> or <strong>Python<\/strong> folder depending on your language preference.<\/li><li>Right-click the <strong>sdk-client<\/strong> folder and open an integrated terminal. Then install the Text Analytics SDK package by running the appropriate command for your language preference:<\/li><\/ol>\n\n\n\n<p><strong>C#<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet add package Azure.AI.TextAnalytics --version 5.0.0<\/code><\/pre>\n\n\n\n<p><strong>Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install azure-ai-textanalytics==5.0.0<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img9-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>View the contents of the <strong>sdk-client<\/strong> folder, and note that it contains a file for configuration settings:<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li><strong>C#<\/strong>: appsettings.json<\/li><li><strong>Python<\/strong>: .env<\/li><\/ul>\n\n\n\n<p>Open the configuration file and update the configuration values it contains to reflect the <strong>endpoint<\/strong> and an authentication <strong>key<\/strong> for your cognitive services resource. Save your changes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img10-2-1024x240.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Note that the <strong>sdk-client<\/strong> folder contains a code file for the client application:<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li><strong>C#<\/strong>: Program.cs<\/li><li><strong>Python<\/strong>: sdk-client.py<\/li><\/ul>\n\n\n\n<p>Open the code file and review the code it contains, noting the following details:<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>The namespace for the SDK you installed is imported<\/li><li>Code in the <strong>Main<\/strong> function retrieves the endpoint and key for your cognitive services resource &#8211; these will be used with the SDK to create a client for the Text Analytics service.<\/li><li>The <strong>GetLanguage<\/strong> function uses the SDK to create a client for the service, and then uses the client to detect the language of the text that was entered.<\/li><li>Return to the integrated terminal for the <strong>sdk-client<\/strong> folder, and enter the following command to run the program:<\/li><\/ul>\n\n\n\n<p><strong>C#<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet run<\/code><\/pre>\n\n\n\n<p><strong>Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python sdk-client.py<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>When prompted, enter some text and review the language that is detected by the service. For example, try entering &#8220;Goodbye&#8221;, &#8220;Au revoir&#8221;, and &#8220;Hasta la vista&#8221;.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/02\/img11-2-1024x197.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>When you have finished testing the application, enter &#8220;quit&#8221; to stop the program.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: Some languages that require Unicode character sets may not be recognized in this simple console application.<\/p>\n\n\n\n<p><mark class=\"kt-highlight\"><strong>Reminder<\/strong>: Don\u2019t forget to delete or shutdown all unused Azure resources after your labs for cost saving<\/mark><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"more-information\">More information<\/h2>\n\n\n\n<p>For more information about Azure Cognitive Services, see the <a href=\"https:\/\/docs.microsoft.com\/azure\/cognitive-services\/what-are-cognitive-services\" target=\"_blank\" rel=\"noreferrer noopener\">Cognitive Services documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this exercise, you&#8217;ll get started with Cognitive Services by creating a Cognitive Services resource in your Azure subscription and using it from a client application. The goal of the exercise is not to gain expertise in any particular service, &hellip; <a href=\"https:\/\/exceedthecloud.com\/?p=669\">Continued<\/a><\/p>\n","protected":false},"author":1,"featured_media":870,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"kt_blocks_editor_width":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14,15,4],"tags":[7,8,57,53,55,54],"class_list":["post-669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-cognitive-services","category-practical-labs-series","tag-ai","tag-azure","tag-cognitive-service","tag-git","tag-python","tag-visual-studio-code"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2021\/11\/istockphoto-1286635624-170667a.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=669"}],"version-history":[{"count":3,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/669\/revisions"}],"predecessor-version":[{"id":2334,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/669\/revisions\/2334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/media\/870"}],"wp:attachment":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}