page_type | languages | products | urlFragment | name | description | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
|
function-python-ai-textsummarize |
Azure Functions - Text Summarization using AI Cognitive Language Service (Python v2 Function) |
This sample shows how to take text documents as a input via BlobTrigger, does Text Summarization & Sentiment Score processing using the AI Congnitive Language service, and then outputs to another text document using BlobOutput binding. Deploys to Flex Consumption hosting plan of Azure Functions. |
This sample shows how to take text documents as a input via BlobTrigger, does Text Summarization & Sentiment Score processing using the AI Congnitive Language service, and then outputs to another text document using BlobOutput binding. Deploys to Flex Consumption hosting plan of Azure Functions.
- Python 3.8+ required
- Azure Functions Core Tools
- Azurite
The easiest way to install Azurite is using a Docker container or the support built into Visual Studio:
docker run -d -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
- Once you have your Azure subscription, run the following in a new terminal window to create all the AI Language and other resources needed:
azd provision
Take note of the value of TEXT_ANALYTICS_ENDPOINT
which can be found in ./.azure/<env name from azd provision>/.env
. It will look something like:
TEXT_ANALYTICS_ENDPOINT="https://<unique string>.cognitiveservices.azure.com/"
Alternatively you can create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource and view the Endpoint value.
- Azure Storage Explorer or storage explorer features of Azure Portal
- Add this
local.settings.json
file to the./text_summarization
folder to simplify local development. Optionally fill in the AI_URL and AI_SECRET values per step 4. This file will be gitignored to protect secrets from committing to your repo.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python",
"TEXT_ANALYTICS_ENDPOINT": "<insert from step 4>"
}
}
- Open
text_summarization.sln
using Visual Studio 2022 or later. - Press Run (
F5
) to run in the debugger - Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container
unprocessed-text
if it does not already exists - Copy any .txt document file with text into the
unprocessed-text
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the processed-text
blob container.
- Open the root folder in VS Code:
code .
- Ensure
local.settings.json
exists already using steps above - Run and Debug by pressing
F5
- Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container
unprocessed-text
if it does not already exists - Copy any .txt document file with text into the
unprocessed-text
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the processed-text
blob container.
- Ensure
local.settings.json
exists already using steps above - Open a new terminal and do the following:
cd text_summarization
func start
- Open Storage Explorer, Storage Accounts -> Emulator -> Blob Containers -> and create a container
test-samples-trigger
if it does not already exists - Copy any .txt document file with text into the
test-samples-trigger
container
You will see AI analysis happen in the Terminal standard out. The analysis will be saved in a .txt file in the test-samples-output
blob container.
The easiest way to deploy this app is using the Azure Developer CLI. If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.
To provision and deploy:
- Open a new terminal and do the following from root folder:
azd up
The main operation of the code starts with the summarize_function
function in function_app.py. The function is triggered by a Blob uploaded event using BlobTrigger with EventGrid, your code runs to do the processing with AI, and then the output is returned as another blob file simply by returning a value and using the BlobOutput binding.
@app.function_name(name="summarize_function")
@app.blob_trigger(arg_name="myblob", path="unprocessed-text/{name}",
connection="AzureWebJobsStorage", source="EventGrid")
@app.blob_output(arg_name="outputblob", path="processed-text/{name}-output.txt", connection="AzureWebJobsStorage")
def test_function(myblob: func.InputStream, outputblob: func.Out[str]):
logging.info(f"Triggered item: {myblob.name}\n")
document = [myblob.read().decode('utf-8')]
summarized_text = ai_summarize_txt(document)
logging.info(f"\n *****Summary***** \n{summarized_text}");
outputblob.set(summarized_text)
The ai_summarize_txt
helper function does the heavy lifting for summary extraction and sentiment analysis using the TextAnalyticsClient
SDK from the AI Language Services:
def ai_summarize_txt(document):
poller = text_analytics_client.begin_extract_summary(document)
extract_summary_results = poller.result()
summarized_text = ""
document_results = poller.result()
for result in extract_summary_results:
if result.kind == "ExtractiveSummarization":
summarized_text= "Summary extracted: \n{}".format(
" ".join([sentence.text for sentence in result.sentences]))
print(summarized_text)
logging.info(f"Returning summarized text: \n{summarized_text}")
elif result.is_error is True:
print("...Is an error with code '{}' and message '{}'".format(
result.error.code, result.error.message
))
logging.error(f"Error with code '{result.error.code}' and message '{result.error.message}'")
# Perform sentiment analysis on document summary
sentiment_result = text_analytics_client.analyze_sentiment([summarized_text])[0]
print(f"\nSentiment: {sentiment_result.sentiment}")
print(f"Positive Score: {sentiment_result.confidence_scores.positive}")
print(f"Negative Score: {sentiment_result.confidence_scores.negative}")
print(f"Neutral Score: {sentiment_result.confidence_scores.neutral}")
summary_with_sentiment = summarized_text + f"\nSentiment: {sentiment_result.sentiment}\n"
return summary_with_sentiment