Saturday, January 25, 2025

Develop Azure AI services applications with Azure Key Vault - Language service

Develop Azure AI services applications with Azure Key Vault:

https://learn.microsoft.com/en-us/azure/ai-services/use-key-vault?tabs=azure-cli&pivots=programming-language-csharp

C# Code:

using Azure;
using Azure.AI.TextAnalytics;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var keyVaultName = "sreekeyvault2";
            const string keySecretName = "CognitiveServicesKey";
            const string endpointSecretName = "CognitiveServicesEndpoint";
            var kvUri = $"https://{keyVaultName}.vault.azure.net";
            var keyVaultClient = new SecretClient(new Uri(kvUri),             new DefaultAzureCredential());
            Console.WriteLine($"Retrieving your secrets from {keyVaultName}.");
            var keySecret = await keyVaultClient.GetSecretAsync(keySecretName);
            var endpointSecret =
            await keyVaultClient.GetSecretAsync(endpointSecretName);
            Console.WriteLine($"Your key secret value is: {keySecret.Value.Value}");
            Console.WriteLine($"Your endpoint secret value is:
            {endpointSecret.Value.Value}");
            Console.WriteLine("Secrets retrieved successfully");
            EntityRecognitionExample(keySecret.Value.Value,
            endpointSecret.Value.Value);
            Console.ReadKey();
        }

        private static void EntityRecognitionExample(string keySecret,
        string endpointSecret)
        {
            var exampleString = "I had a wonderful trip to Seattle last week.";
            AzureKeyCredential azureKeyCredential = new AzureKeyCredential(keySecret);
            Uri endpoint = new Uri(endpointSecret);
            var languageServiceClient = new TextAnalyticsClient(endpoint,
            azureKeyCredential);
            Console.WriteLine($"Sending a Named Entity Recognition (NER) request");
            var response = languageServiceClient.RecognizeEntities(exampleString);
            Console.WriteLine("Named Entities:");
            foreach (var entity in response.Value)
            {
                Console.WriteLine($"\tText: {entity.Text},\tCategory:
                {entity.Category},\tSub-Category: {entity.SubCategory}");
                Console.WriteLine($"\t\tScore: {entity.ConfidenceScore:F2},\tLength:
                {entity.Length},\tOffset: {entity.Offset}\n");
            }
        }
    }
}

OutPut:

Friday, January 24, 2025

Azure AI services - Document intelligence - Extract Data from Forms

 Azure AI services - Document intelligence - Extract Data from Forms:

Source:
https://microsoftlearning.github.io/AI-102-AIEngineer/
https://github.com/MicrosoftLearning/AI-102-AIEngineer

1. Create Azure AI services - Document intelligence in Azure Portal
2. Run Powershell script to create Storage account
3. Train the Model
4. Test the Model 

Required Dlls;
dotnet add package Azure.Core --version 1.44.1
dotnet add package Azure.AI.FormRecognizer --version 4.1.0
dotnet add package Azure.AI.FormRecognizer --version 3.0.0
dotnet add package Tabulate.NET --version 1.0.5

2. Run Powershell script to create Storage account

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

rem Set variable values
set subscription_id=129b2bb6-asdf-asdf-83ba-85bf570bebca
set resource_group=rg1
set location=eastus
set expiry_date=2026-01-01T00:00:00Z

rem Get random numbers to create unique resource names
set unique_id=!random!!random!

rem Create a storage account in your Azure resource group
echo Creating storage...
call az storage account create --name ai102form!unique_id! --subscription !subscription_id! --resource-group !resource_group! --location !location! --sku Standard_LRS --encryption-services blob --default-action Allow --output none --allow-blob-public-access true

echo Uploading files...
rem Get storage key to create a container in the storage account
for /f "tokens=*" %%a in (
'az storage account keys list --subscription !subscription_id! --resource-group !resource_group! --account-name ai102form!unique_id! --query "[?keyName=='key1'].{keyName:keyName, permissions:permissions, value:value}"'
) do (
set key_json=!key_json!%%a
)
set key_string=!key_json:[ { "keyName": "key1", "permissions": "Full", "value": "=!
set AZURE_STORAGE_KEY=!key_string:" } ]=!
rem Create container
call az storage container create --account-name ai102form!unique_id! --name sampleforms --auth-mode key --account-key %AZURE_STORAGE_KEY% --output none
rem Upload files from your local sampleforms folder to a container called sampleforms in the storage account
rem Each file is uploaded as a blob
call az storage blob upload-batch -d sampleforms -s ./sample-forms --account-name ai102form!unique_id! --auth-mode key --account-key %AZURE_STORAGE_KEY%  --output none
rem Set a variable value for future use
set STORAGE_ACCT_NAME=ai102form!unique_id!

rem Get a Shared Access Signature (a signed URI that points to one or more storage resources) for the blobs in sampleforms  
for /f "tokens=*" %%a in (
'az storage container generate-sas --account-name ai102form!unique_id! --name sampleforms --expiry !expiry_date! --permissions rwl'
) do (
set SAS_TOKEN=%%a
set SAS_TOKEN=!SAS_TOKEN:~1,-1!
)
set URI=https://!STORAGE_ACCT_NAME!.blob.core.windows.net/sampleforms?!SAS_TOKEN!

rem Print the generated Shared Access Signature URI, which is used by Azure Storage to authorize access to the storage resource
echo -------------------------------------
echo SAS URI: !URI!


Run the code : dotnet run

OutPut:


3. Train the Model

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

// import namespaces
using Azure;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.Training;

namespace train_model
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                // Get configuration settings
                // IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                // IConfigurationRoot configuration = builder.Build();
                // string formEndpoint = configuration["FormEndpoint"];
                // string formKey = configuration["FormKey"];
                // string trainingStorageUri = configuration["StorageUri"];

                // "YOUR_FORM_RECOGNIZER_ENDPOINT"
                string formEndpoint = "https://a.cognitiveservices.azure.com/";
                // "YOUR_FORM_RECOGNIZER_KEY"
                string formKey = "1E7gEDsZ2pUAiximoBAACYeBjFXJ3w3AAALACOGu5mm";
                // "YOUR_SAS_URI"
                string trainingStorageUri = "https://8IqQY2WOeCJRHTPFg%3D";

                // Authenticate Form Training Client
                var credential = new AzureKeyCredential(formKey);
                var trainingClient = new FormTrainingClient(new Uri(formEndpoint), credential);

                // Train model
                CustomFormModel model = await trainingClient
                .StartTrainingAsync(new Uri(trainingStorageUri), useTrainingLabels: true)
                .WaitForCompletionAsync();

                // Get model info
                Console.WriteLine($"Custom Model Info:");
                Console.WriteLine($"    Model Id: {model.ModelId}");
                Console.WriteLine($"    Model Status: {model.Status}");
                Console.WriteLine($"    Training model started on: {model.TrainingStartedOn}");
                Console.WriteLine($"    Training model completed on: {model.TrainingCompletedOn}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


Run the code : dotnet run

OutPut:


4. Test the Model 

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

// import namespaces
using Azure;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using Azure.AI.FormRecognizer.Training;

namespace test_model
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                // Get configuration settings from AppSettings
                // IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                // IConfigurationRoot configuration = builder.Build();
                // string formEndpoint = configuration["FormEndpoint"];
                // string formKey = configuration["FormKey"];
                // string modelId = configuration["ModelId"];

                // "YOUR_FORM_RECOGNIZER_ENDPOINT";                 string formEndpoint = "https://a.cognitiveservices.azure.com/";
                // "YOUR_FORM_RECOGNIZER_KEY";                 string formKey = "1E7gEDsZ2pUAiximAAALACOGu5mm";
                // "YOUR_MODEL_ID";
                string modelId = "7891e019-9cc2-48a9-a9e6-08ac408484c5";

                // Authenticate Azure AI Document Intelligence Client
                var credential = new AzureKeyCredential(formKey);
                var recognizerClient = new FormRecognizerClient(new Uri(formEndpoint), credential);

                // Get form url for testing  
                string image_file = "test1.jpg";
                using (var image_data = File.OpenRead(image_file))
                {
                    // Use trained model with new form
                    RecognizedFormCollection forms = await recognizerClient
                    .StartRecognizeCustomForms(modelId, image_data)
                    .WaitForCompletionAsync();

                    foreach (RecognizedForm form in forms)
                    {
                        Console.WriteLine($"Form of type: {form.FormType}");
                        foreach (FormField field in form.Fields.Values)
                        {
                            Console.WriteLine($"Field '{field.Name}':");

                            if (field.LabelData != null)
                            {
                                Console.WriteLine($"    Label: '{field.LabelData.Text}'");
                            }

                            Console.WriteLine($"    Value: '{field.ValueData.Text}'");
                            Console.WriteLine($"    Confidence: {field.Confidence}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

OutPut:






Azure AI services - Translator - Translate Text with Azure AI Translator

 Azure AI services - Translator - Translate Text with Azure AI Translator:

C-Sharp C# Code:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
 
public class Program
{
    public static string translatorEndpoint = "https://api.cognitive.microsofttranslator.com";
    public static string cogSvcKey = "Aq3OMPInYriPClIMJJuKzpN1JGOJZZRqM3FXJ3w3AAAbACOGxJeX";
    public static string cogSvcRegion = "eastus";
 
    public static async Task Main(string[] args)
    {
        try
        {
            // Get config settings from AppSettings
            // IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            // IConfigurationRoot configuration = builder.Build();
            // cogSvcKey = configuration["CognitiveServiceKey"];
            // cogSvcRegion = configuration["CognitiveServiceRegion"];
 
            // Set console encoding to unicode
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
 
            // Analyze each text file in the reviews folder
            var folderPath = Path.GetFullPath("./reviews");
            DirectoryInfo folder = new DirectoryInfo(folderPath);
            foreach (var file in folder.GetFiles("*.txt"))
            {
                // Read the file contents
                Console.WriteLine("\n-------------\n" + file.Name);
                StreamReader sr = file.OpenText();
                var text = sr.ReadToEnd();
                sr.Close();
                Console.WriteLine("\n" + text);
 
                // Detect the language
                string language = await GetLanguage(text);
                Console.WriteLine("Language: " + language);
 
                // Translate if not already English
                if (language != "en")
                {
                    string translatedText = await Translate(text, language);
                    Console.WriteLine("\nTranslation:\n" + translatedText);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadKey();
    }
 
    private static async Task<string> GetLanguage(string text)
    {
        // Default language is English
        string language = "en";
 
        // Use the Translator detect function
        // Use the Azure AI Translator detect function
        object[] body = new object[] { new { Text = text } };
        var requestBody = JsonConvert.SerializeObject(body);
        using (var client = new HttpClient())
        {
            using (var request = new HttpRequestMessage())
            {
                // Build the request
                string path = "/detect?api-version=3.0";
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(translatorEndpoint + path);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", cogSvcKey);
                request.Headers.Add("Ocp-Apim-Subscription-Region", cogSvcRegion);
 
                // Send the request and get response
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                // Read response as a string
                string responseContent = await response.Content.ReadAsStringAsync();
 
                // Parse JSON array and get language
                JArray jsonResponse = JArray.Parse(responseContent);
                language = (string)jsonResponse[0]["language"];
            }
        }
 
        // return the language
        return language;
    }
 
    private static async Task<string> Translate(string text, string sourceLanguage)
    {
        string translation = "";
 
        // Use the Translator translate function
        // Use the Azure AI Translator translate function
        object[] body = new object[] { new { Text = text } };
        var requestBody = JsonConvert.SerializeObject(body);
        using (var client = new HttpClient())
        {
            using (var request = new HttpRequestMessage())
            {
                // Build the request
                string path = "/translate?api-version=3.0&from=" + sourceLanguage + "&to=en";
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(translatorEndpoint + path);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", cogSvcKey);
                request.Headers.Add("Ocp-Apim-Subscription-Region", cogSvcRegion);
 
                // Send the request and get response
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                // Read response as a string
                string responseContent = await response.Content.ReadAsStringAsync();
 
                // Parse JSON array and get translation
                JArray jsonResponse = JArray.Parse(responseContent);
                translation = (string)jsonResponse[0]["translations"][0]["text"];
            }
        }
 
        // Return the translation
        return translation;
    }
}
 
 Output:

Python:
from dotenv import load_dotenv
import os
import requests, json
 
def main():
    global translator_endpoint
    global cog_key
    global cog_region
 
    try:
        # Get Configuration Settings
        load_dotenv()
        # cog_key = os.getenv('COG_SERVICE_KEY')
        # cog_region = os.getenv('COG_SERVICE_REGION')
        cog_key = 'Aq3OMPInYriPClIMJJuKzpNMBAACYeBjFXJ3w3AAAbACOGxJeX'
        cog_region = 'eastus'
        translator_endpoint = 'https://api.cognitive.microsofttranslator.com'
 
        # Analyze each text file in the reviews folder
        reviews_folder = 'reviews'
        for file_name in os.listdir(reviews_folder):
            # Read the file contents
            print('\n-------------\n' + file_name)
            text = open(os.path.join(reviews_folder, file_name), encoding = 'utf8').read()
            print('\n' + text)
 
            # Detect the language
            language = GetLanguage(text)
            print('Language:', language)
 
            # Translate if not already English
            if language != 'en':
                translation = Translate(text, language)
                print("\nTranslation:\n{}".format(translation))
 
 
    except Exception as ex:
        print(ex)
 
def GetLanguage(text):
    # Default language is English
    language = 'en'
 
    # Use the Translator detect function
# Use the Azure AI Translator detect function
    path = '/detect'
    url = translator_endpoint + path
 
    # Build the request
    params = {
    'api-version': '3.0'
    }
headers = {
    'Ocp-Apim-Subscription-Key': cog_key,
        'Ocp-Apim-Subscription-Region': cog_region,
        'Content-type': 'application/json'
    }
body = [{
    'text': text
    }]
 
    # Send the request and get response
    request = requests.post(url, params=params, headers = headers, json = body)
    response = request.json()
 
    # Parse JSON array and get language
    language = response[0]["language"]
 
    # Return the language
    return language
 
def Translate(text, source_language):
    translation = ''
 
    # Use the Translator translate function
# Use the Azure AI Translator translate function
    path = '/translate'
    url = translator_endpoint + path
 
    # Build the request
    params = {
    'api-version': '3.0',
        'from': source_language,
        'to': ['en']
    }
headers = {
    'Ocp-Apim-Subscription-Key': cog_key,
        'Ocp-Apim-Subscription-Region': cog_region,
        'Content-type': 'application/json'
    }
body = [{
    'text': text
    }]
 
    # Send the request and get response
    request = requests.post(url, params=params, headers = headers, json = body)
    response = request.json()
 
    # Parse JSON array and get translation
    translation = response[0]["translations"][0]["text"]
 
    # Return the translation
    return translation
 
if __name__ == "__main__":
    main()

Output:

Source:
git clone https://github.com/MicrosoftLearning/AI-102-AIEngineer azure-ai-eng

Featured Post

Develop Azure AI services applications with Azure Key Vault - Language service

Develop Azure AI services applications with Azure Key Vault: https://learn.microsoft.com/en-us/azure/ai-services/use-key-vault?tabs=azure-cl...

Popular posts