Text Summarization with Azure AI

859

We are living in Industry 4.0 era. AI is becoming mainstream by the day. It is permeating every aspect of our lives from Enterprise and Consumer use cases. Among them, Natural Language Processing applications have simply exploded, with the mainstreaming of LLMs in the form of ChatGPT. LLMs are our next big leap towards AGI. In a recently concluded hackathon ate our organization, we used Generative AI to create data, summarize data, extracts insight out of it and perform text classification on it. It’s a long way to go. Nonetheless, we are moving towards AGI, Kurzweil’s singularity.

There is a place for task specific models too. A classic example of this is our traditional statistical learning models like classification and regression. But, even with the state-of-the-art NLP and CV models, task specific models have their own place. They have two advantages:

  • They are trained for specific tasks. Hence, they are precise.
  • They are faster to infer.

However, they bring their own challenges too. Firstly, they are hard to train, owing to specific data needs and the required expert knowledge. Secondly, they are not transferable since the training happens on specific datasets. Lastly, they are hard to maintain as frequent updation is needed.

Also read: Explore your structured data using Natural Language with Azure Open AI

Fortunately, in the era of big tech and cloud computing, these disadvantages are taken care of. Task specific models are trained on a massive corpus of data by large organizations like Google, Microsoft etc. with massive compute at their disposal. Moreover, they are exposed to consumers via APIs/SDKs. Case in point is Azure AI services.  According to Microsoft official documentation, Azure AI services is a comprehensive suite of out-of-the-box and customizable AI tools, APIs, and models that help modernize your business processes faster.

Text Summarization with Azure AI

Let’s see an example of Text Summarization with Azure AI. Text Summarization is a part of Azure AI Language. Azure AI language summarizes two types of text i.e. Documents and Conversations. Furthermore, document summarization has two key features i.e. Extractive Summary and Abstractive summary. Let’s take hands on look at all of them. Here is the text to be summarized:

text_document = “At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, human-centric approach to learning and understanding. As Chief Technology Officer of Azure AI services, I have been working with a team of amazing scientists and engineers to turn this quest into a reality. In my role, I enjoy a unique perspective in viewing the relationship among three attributes of human cognition: monolingual text (X), audio or visual sensory signals, (Y) and multilingual (Z). At the intersection of all three, there’s magic—what we call XYZ-code as illustrated in Figure 1—a joint representation to create more powerful AI that can speak, hear, see, and understand humans better. We believe XYZ-code will enable us to fulfill our long-term vision: cross-domain transfer learning, spanning modalities and languages. The goal is to have pre-trained models that can jointly learn representations to support a broad range of downstream AI tasks, much in the way humans do today. Over the past five years, we have achieved human performance on benchmarks in conversational speech recognition, machine translation, conversational question answering, machine reading comprehension, and image captioning. These five breakthroughs provided us with strong signals toward our more ambitious aspiration to produce a leap in AI capabilities, achieving multi-sensory and multilingual learning that is closer in line with how humans learn and understand. I believe the joint XYZ-code is a foundational component of this aspiration, if grounded with external knowledge sources in the downstream AI tasks.”

Document text summarization with Azure AI

Document summarization uses Natural Language techniques to summarize documents. There are two key features/approaches viz. Extractive and Abstractive Summaries.

Extractive Summary

Extractive summarization selects and ranks sentences from a document based on their relevance to the main topic. The ranked sentences collectively form the document’s summary. Users can specify the number of sentences to include and choose between ordered or ranked extraction methods. This approach efficiently distills key information from a document for quick comprehension.

Abstractive Summary

Abstractive summarization is a technique that generates summaries by capturing the main idea of a document, even if it doesn’t necessarily use the same words. It produces summary texts for various contextual input ranges within the document. For long documents, segmentation may result in multiple groups of summary texts, each associated with its contextual input range. These ranges specify the portion of the input document used to generate each summary text.

Now comes the hands-on part. Here are the steps:

Step 1.  Install the required packages. We use python here. However, the option to use C# is available too.

Install the packages azure-ai-ml and azure-ai-textanalytics.

pip install azure-ai-ml
pip install azure-ai-textanalytics

Step 2. Import required libraries and setup the text analytics client.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
text_analytics_endpoint = "https://<your-ai-lang-service>.cognitiveservices.azure.com/"
text_analytics_key = "<your-text-analytics-key>"

text_analytics_client = TextAnalyticsClient(endpoint=text_analytics_endpoint,credential=AzureKeyCredential(text_analytics_key))
Step 3. Create the generate summary function
Here, we create a generate summary function which prints out both abstractive and extractive summaries. Here is the function:
def generateSummary(text_document,text_analytics_client):  

    document = [text_document]

    poller_abstract = text_analytics_client.begin_abstract_summary(document) #Generate Abstract Summary
    abstract_summary_results = poller_abstract.result()     

    poller_extract = text_analytics_client.begin_extract_summary(document) #Generate Extractive Summary
    extract_summary_results = poller_extract.result()   

    for result in abstract_summary_results:

        if result.kind == "AbstractiveSummarization":
            print("Summary extracted: \n{}".format(
                " ".join([summary.text for summary in result.summaries]))
            )   

        elif result.is_error is True:
            print("...Is an error with code '{}' and message '{}'".format(
                result.error.code, result.error.message
            ))

   

    for result in extract_summary_results:
         if result.kind == "ExtractiveSummarization":
            lst_sentences = [sentence.text for sentence in result.sentences]
            for sentence in lst_sentences:
                print("- {}".format(sentence))

         elif result.is_error is True:
            print("...Is an error with code '{}' and message '{}'".format(
                result.error.code, result.error.message
            ))

generateSummary(text_document,text_analytics_client)

The results of Abstractive and Extractive summaries look as follows:

Abstractive Summary Results
Microsoft has been working to advance AI beyond existing techniques by taking a more holistic, human-centric approach to learning and understanding. The Chief Technology Officer of Azure AI services, who enjoys a unique perspective in viewing the relationship among three attributes of human cognition: monolingual text, audio or visual sensory signals, and multilingual, has created XYZ-code, a joint representation to create more powerful AI that can speak, hear, see, and understand humans better. Over the past five years, Microsoft has achieved human performance on benchmarks in conversational speech recognition, machine translation, conversational question answering, machine reading comprehension, and image captioning.

Extractive Summary results

-At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, human-centric approach to learning and understanding.

– The goal is to have pre-trained models that can jointly learn representations to support a broad range of downstream AI tasks, much in the way humans do today.
– Over the past five years, we have achieved human performance on benchmarks in conversational speech recognition, machine translation, conversational question answering, machine reading comprehension, and image captioning.

Now, we move towards conversation summary

Conversation text summarization with Azure AI

Summarizing conversations could be very useful, especially in customer care scenarios. Conversations are common even in summarizing meetings. Azure AI language service can also summarize conversations. Here is a sample conversation:

Agent: “Hello, you’re chatting with Rene. How may I help you?

Customer: “Hi, I tried to set up wifi connection for Smart Brew 300 espresso machine, but it didn’t work.

Agent: “I’m sorry to hear that. Let’s see what we can do to fix this issue. Could you push the wifi connection button, hold for 3 seconds, then let me know if the power light is slowly blinking?

Customer: “Yes, I pushed the wifi connection button, and now the power light is slowly blinking.

Agent: “Great. Thank you! Now, please check in your Contoso Coffee app. Does it prompt to ask you to connect with the machine?

Customer: “No. Nothing happened.

Agent: “I see. Thanks. Let’s try if a factory reset can solve the issue. Could you please press and hold the center button for 5 seconds to start the factory reset.

Customer“I’ve tried the factory reset and followed the above steps again, but it still didn’t work.”

Agent: “I’m very sorry to hear that. Let me see if there’s another way to fix the issue. Please hold on for a minute.

To summarize this, we use REST APIs instead of using SDKs. There are two steps:
  1. Submit a Text Analysis Job using a POST request.
  2. GET the result using Job ID.

Here is the POST request to get the JOB ID:

import requests

import json

url = "https://<your-ai-lang-service>.cognitiveservices.azure.com/language/analyze-conversations/jobs?api-version=2022-10-01-preview"

payload = json.dumps({
  "displayName": "Conversation Task Example",
  "analysisInput": {
    "conversations": [
      {
        "conversationItems": [
          {
            "text": "Hello, you’re chatting with Rene. How may I help you?",
            "id": "1",
            "role": "Agent",
            "participantId": "Agent_1"
          },
          {
            "text": "Hi, I tried to set up wifi connection for Smart Brew 300 espresso machine, but it didn’t work.",
            "id": "2",
            "role": "Customer",
            "participantId": "Customer_1"
          },
          {
            "text": "I’m sorry to hear that. Let’s see what we can do to fix this issue. Could you please try the following steps for me? First, could you push the wifi connection button, hold for 3 seconds, then let me know if the power light is slowly blinking on and off every second?",
            "id": "3",
            "role": "Agent",
            "participantId": "Agent_1"
          },
          {
            "text": "Yes, I pushed the wifi connection button, and now the power light is slowly blinking.",
            "id": "4",
            "role": "Customer",
            "participantId": "Customer_1"
          },
          {
            "text": "Great. Thank you! Now, please check in your Contoso Coffee app. Does it prompt to ask you to connect with the machine? ",
            "id": "5",
            "role": "Agent",
            "participantId": "Agent_1"
          },
          {
            "text": "No. Nothing happened.",
            "id": "6",
            "role": "Customer",
            "participantId": "Customer_1"
          },
          {
            "text": "I’m very sorry to hear that. Let me see if there’s another way to fix the issue. Please hold on for a minute.",
            "id": "7",
            "role": "Agent",
            "participantId": "Agent_1"
          }
        ],
        "modality": "text",
        "id": "conversation1",
        "language": "en"
      }
    ]
  },

  "tasks": [
    {
      "taskName": "Conversation Task 1",
      "kind": "ConversationalSummarizationTask",
      "parameters": {
        "summaryAspects": [
          "narrative"
        ]
      }
    }
  ]
})

headers = {
  'Content-Type': 'application/json',
  'Ocp-Apim-Subscription-Key': '<your-text-analytics-key>'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.headers['operation-location'])
Here is the job id from response headers:
https://<your-ai-lang-service>.cognitiveservices.azure.com/language/
analyze-conversations/jobs/<job-id>?api-version=2022-10-01-preview
Use this job id in the following code:
import requests
import json

url = "https://<your-ai-lang-service>.cognitiveservices.azure.com/language/analyze-conversations/jobs/<job-id>?api-version=2022-10-01-preview"

payload = {}

headers = {
  'Content-Type': 'application/json',
  'Ocp-Apim-Subscription-Key': '<your-text-analytics-key>'
}

response = requests.request("GET", url, headers=headers, data=payload)

json_str = response.text

# Parse the JSON object
json_obj = json.loads(json_str)

# Extract the text from the JSON object
text = json_obj["tasks"]["items"][0]["results"]["conversations"][0]["summaries"][0]["text"]

# Print the extracted text
print(text)

Here is the summary of the conversation:

Customer_1 tried to set up wifi connection for Smart Brew 300 espresso machine, but it didn’t work. Agent_1 suggested trying the following steps. First, push the wifi connection button, hold for 3 seconds, and let me know if the power light is slowly blinking on and off every second. Second, check in the Contoso Coffee app to see if it prompts to ask to connect with the machine.

Conclusion

We know that in the era of LLMs, it is tempting to use them for all the tasks. But knowing specialized models could come in handy when specific models and expertise are available. Having said that, this article is only for information purposes. We do not claim any guarantees. User discretion is advised.



I am a Data Scientist with 6+ years of experience.


Leave a Reply