Saturday, March 1, 2025

 Sample Code for leveraging Azure AI Language Services to summarize text:

import requests

import json

def summarize_text(document, endpoint, api_key):

    # Define the API endpoint for Text Analytics v3.2-preview.2 (used for text summarization)

    # url = f"{endpoint}/text/analytics/v3.2-preview.2/analyze"

    url = f"{endpoint}/language/analyze-text/jobs?api-version=2023-04-01"

    # Set up headers with your API key

    headers = {

        "Ocp-Apim-Subscription-Key": api_key,

        "Content-Type": "application/json",

        "Content-Length": "0"

    }

    # Define the input document for summarization

    body = {

            "documents": [

                {

                    "id": "1",

                    "language": "en",

                    "text": document

                }

            ],

        "analysisInput": {

            "documents": [

                {

                    "id": "1",

                    "language": "en",

                    "text": document

                }

            ]

        },

        "tasks": [

            {

                "kind": "ExtractiveSummarization",

                "taskName": "extractiveSummarization",

                "parameters": {

                    "modelVersion": "latest",

                    "sentenceCount": 3 # Adjust the number of sentences in the summary

                }

            }

        ]

    }

    # Send the POST request

    response = requests.post(url, headers=headers, json=body)

    # Check for response status

    if response.status_code == 200:

        result = response.json()

        # Extract summarized sentences from the response

        summary = result['tasks']['extractiveSummarizationResults'][0]['results']['documents'][0]['sentences']

        return " ".join([sentence["text"] for sentence in summary])

    elif response.status_code == 202:

        print(f"Headers: {response.headers}")

    else:

        raise Exception(f"Error: {response.status_code}, Message: {response.text}, Headers: {response.headers}")

# Example usage

if __name__ == "__main__":

    # Replace with your Azure Text Analytics endpoint and API key

    AZURE_ENDPOINT = "https://<your-azure-ai-endpoint>.cognitiveservices.azure.com"

    AZURE_API_KEY = "<your-api-key>"

    # Input text document to summarize

    input_document = """

    Artificial intelligence (AI) refers to the simulation of human intelligence in machines

    that are programmed to think like humans and mimic their actions. The term may also

    be applied to any machine that exhibits traits associated with a human mind such as

    learning and problem-solving.

    """

    try:

        summary = summarize_text(input_document, AZURE_ENDPOINT, AZURE_API_KEY)

        print("Summary:")

        print(summary)

    except Exception as e:

        print(e)

Sample trial:

Response Headers:

{'Content-Length': '0', 'operation-location': 'https://<your-azure-ai-endpoint>.cognitiveservices.azure.com/language/analyze-text/jobs/7060ce5a-afb4-4a08-87a1-e456d486510f?api-version=2023-04-01', 'x-envoy-upstream-service-time': '188', 'apim-request-id': 'f44e40fe-e2ef-41f4-b46d-d36896f3f20d', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff', 'x-ms-region': 'Central US', 'Date': 'Sat, 01 Mar 2025 02:59:41 GMT'}

curl -i -H "Ocp-Apim-Subscription-Key: <your-api-key>" "https://<your-azure-ai-endpoint>.cognitiveservices.azure.com/language/analyze-text/jobs/7060ce5a-afb4-4a08-87a1-e456d486510f?api-version=2023-04-01"

HTTP/1.1 200 OK

Content-Length: 933

Content-Type: application/json; charset=utf-8

x-envoy-upstream-service-time: 51

apim-request-id: 30208936-7f0d-49d5-9d36-8fc69ffc976e

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

x-content-type-options: nosniff

x-ms-region: Central US

Date: Sat, 01 Mar 2025 03:01:03 GMT

{"jobId":"7060ce5a-afb4-4a08-87a1-e456d486510f","lastUpdatedDateTime":"2025-03-01T02:59:42Z","createdDateTime":"2025-03-01T02:59:41Z","expirationDateTime":"2025-03-02T02:59:41Z","status":"succeeded","errors":[],"tasks":{"completed":1,"failed":0,"inProgress":0,"total":1,"items":[{"kind":"ExtractiveSummarizationLROResults","taskName":"extractiveSummarization","lastUpdateDateTime":"2025-03-01T02:59:42.8576711Z","status":"succeeded","results":{"documents":[{"id":"1","sentences":[{"text":"Artificial intelligence (AI) refers to the simulation of human intelligence in machines","rankScore":1.0,"offset":5,"length":87},{"text":"that are programmed to think like humans and mimic their actions.","rankScore":0.49,"offset":98,"length":65},{"text":"be applied to any machine that exhibits traits associated with a human mind such as","rankScore":0.3,"offset":187,"length":83}],"warnings":[]}],"errors":[],"modelVersion":"2024-11-04"}}]}}

Reference:

1. previous articles

2. Extractive Summarization:

a. https://learn.microsoft.com/en-us/azure/ai-services/language-service/summarization/overview?tabs=text-summarization

b. https://learn.microsoft.com/en-us/azure/ai-services/language-service/summarization/how-to/document-summarization#try-text-extractive-summarization

3. Abstractive Summarization: https://github.com/microsoft/AzureSearch-MRC/blob/main/README.md


No comments:

Post a Comment