{"id":13289028,"url":"https://github.com/Azure-Samples/function-python-ai-textsummarize","last_synced_at":"2025-03-10T06:33:55.767Z","repository":{"id":144908217,"uuid":"600282468","full_name":"Azure-Samples/function-python-ai-textsummarize","owner":"Azure-Samples","description":"This sample shows how to take text documents as a input via BlobTrigger, does Text Summarization processing using the AI Congnitive Language service, and then outputs to another text document using BlobOutput binding.  Uses Azure Functions Python v2 programming model.  ","archived":false,"fork":false,"pushed_at":"2024-04-29T09:11:20.000Z","size":61,"stargazers_count":16,"open_issues_count":1,"forks_count":12,"subscribers_count":14,"default_branch":"main","last_synced_at":"2024-07-29T17:04:34.690Z","etag":null,"topics":["azd","azd-templates","azure","azure-function","cognitive-services"],"latest_commit_sha":null,"homepage":"","language":"Bicep","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-11T02:23:46.000Z","updated_at":"2024-06-15T14:14:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd83606d-1507-46bd-9fe7-a0ec967f5c4d","html_url":"https://github.com/Azure-Samples/function-python-ai-textsummarize","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-textsummarize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-textsummarize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-textsummarize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-python-ai-textsummarize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/function-python-ai-textsummarize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242805666,"owners_count":20187996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azd","azd-templates","azure","azure-function","cognitive-services"],"created_at":"2024-07-29T17:00:18.151Z","updated_at":"2025-03-10T06:33:55.207Z","avatar_url":"https://github.com/Azure-Samples.png","language":"Bicep","readme":"---\npage_type: sample\nlanguages:\n- azdeveloper\n- python\n- bicep\nproducts:\n- azure\n- azure-functions\n- ai-services\n- azure-cognitive-search\nurlFragment: function-python-ai-textsummarize\nname: Azure Functions - Text Summarization using AI Cognitive Language Service (Python v2 Function)\ndescription: This sample shows how to take text documents as a input via BlobTrigger, does Text Summarization \u0026 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.  \n---\n\u003c!-- YAML front-matter schema: https://review.learn.microsoft.com/en-us/help/contribute/samples/process/onboarding?branch=main#supported-metadata-fields-for-readmemd --\u003e\n\n# Azure Functions\n## Text Summarization using AI Cognitive Language Service (Python v2 Function)\n\nThis sample shows how to take text documents as a input via BlobTrigger, does Text Summarization \u0026 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.  \n\n[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/Azure-Samples/function-python-ai-textsummarize)\n\n## Run on your local environment\n\n### Pre-reqs\n1) [Python 3.8+](https://www.python.org/) required \n2) [Azure Functions Core Tools](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v4%2Cmacos%2Ccsharp%2Cportal%2Cbash#install-the-azure-functions-core-tools)\n3) [Azurite](https://github.com/Azure/Azurite)\n\nThe easiest way to install Azurite is using a Docker container or the support built into Visual Studio:\n```bash\ndocker run -d -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite\n```\n\n4) Once you have your Azure subscription, run the following in a new terminal window to create all the AI Language and other resources needed:\n```bash\nazd provision\n```\n\nTake note of the value of `TEXT_ANALYTICS_ENDPOINT` which can be found in `./.azure/\u003cenv name from azd provision\u003e/.env`.  It will look something like:\n```bash\nTEXT_ANALYTICS_ENDPOINT=\"https://\u003cunique string\u003e.cognitiveservices.azure.com/\"\n```\n\nAlternatively you can [create a Language resource](https://portal.azure.com/#create/Microsoft.CognitiveServicesTextAnalytics) in the Azure portal to get your key and endpoint. After it deploys, click Go to resource and view the Endpoint value.\n\n5) [Azure Storage Explorer](https://azure.microsoft.com/en-us/products/storage/storage-explorer/) or storage explorer features of [Azure Portal](https://portal.azure.com)\n6) 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.  \n```json\n{\n    \"IsEncrypted\": false,\n    \"Values\": {\n        \"AzureWebJobsStorage\": \"UseDevelopmentStorage=true\",\n        \"FUNCTIONS_WORKER_RUNTIME\": \"python\",\n        \"TEXT_ANALYTICS_ENDPOINT\": \"\u003cinsert from step 4\u003e\"\n    }\n}\n```\n\n\n### Using Visual Studio\n1) Open `text_summarization.sln` using Visual Studio 2022 or later.\n2) Press Run (`F5`) to run in the debugger\n3) Open Storage Explorer, Storage Accounts -\u003e Emulator -\u003e Blob Containers -\u003e and create a container `unprocessed-text` if it does not already exists\n4) Copy any .txt document file with text into the `unprocessed-text` container\n\nYou 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.\n\n### Using VS Code\n1) Open the root folder in VS Code:\n\n```bash\ncode .\n```\n2) Ensure `local.settings.json` exists already using steps above\n3) Run and Debug by pressing `F5`\n4) Open Storage Explorer, Storage Accounts -\u003e Emulator -\u003e Blob Containers -\u003e and create a container `unprocessed-text` if it does not already exists\n5) Copy any .txt document file with text into the `unprocessed-text` container\n\nYou 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.\n\n### Using Functions Core Tools CLI\n0) Ensure `local.settings.json` exists already using steps above\n1) Open a new terminal and do the following:\n\n```bash\ncd text_summarization\nfunc start\n```\n2) Open Storage Explorer, Storage Accounts -\u003e Emulator -\u003e Blob Containers -\u003e and create a container `test-samples-trigger` if it does not already exists\n3) Copy any .txt document file with text into the `test-samples-trigger` container\n\nYou 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.\n\n## Deploy to Azure\n\nThe easiest way to deploy this app is using the [Azure Developer CLI](https://aka.ms/azd).  If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.\n\nTo provision and deploy:\n1) Open a new terminal and do the following from root folder:\n```bash\nazd up\n```\n\n## Understand the Code\n\nThe main operation of the code starts with the `summarize_function` function in [function_app.py](./text_summarize/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.  \n\n```python\n@app.function_name(name=\"summarize_function\")\n@app.blob_trigger(arg_name=\"myblob\", path=\"unprocessed-text/{name}\",\n                  connection=\"AzureWebJobsStorage\", source=\"EventGrid\")\n@app.blob_output(arg_name=\"outputblob\", path=\"processed-text/{name}-output.txt\", connection=\"AzureWebJobsStorage\")\ndef test_function(myblob: func.InputStream, outputblob: func.Out[str]):\n   logging.info(f\"Triggered item: {myblob.name}\\n\")\n\n   document = [myblob.read().decode('utf-8')]\n   summarized_text = ai_summarize_txt(document)\n   logging.info(f\"\\n *****Summary***** \\n{summarized_text}\");\n   outputblob.set(summarized_text)\n```\n\nThe `ai_summarize_txt` helper function does the heavy lifting for summary extraction and sentiment analysis using the `TextAnalyticsClient` SDK from the [AI Language Services](https://learn.microsoft.com/en-us/azure/ai-services/language-service/):\n\n```python\ndef ai_summarize_txt(document):\n\n    poller = text_analytics_client.begin_extract_summary(document)\n    extract_summary_results = poller.result()\n\n    summarized_text = \"\"\n    document_results = poller.result()\n    for result in extract_summary_results:\n        if result.kind == \"ExtractiveSummarization\":\n            summarized_text= \"Summary extracted: \\n{}\".format(\n                \" \".join([sentence.text for sentence in result.sentences]))\n            print(summarized_text)\n            logging.info(f\"Returning summarized text:  \\n{summarized_text}\")\n        elif result.is_error is True:\n            print(\"...Is an error with code '{}' and message '{}'\".format(\n                result.error.code, result.error.message\n            ))\n            logging.error(f\"Error with code '{result.error.code}' and message '{result.error.message}'\")\n\n    # Perform sentiment analysis on document summary\n    sentiment_result = text_analytics_client.analyze_sentiment([summarized_text])[0]\n    print(f\"\\nSentiment: {sentiment_result.sentiment}\")\n    print(f\"Positive Score: {sentiment_result.confidence_scores.positive}\")\n    print(f\"Negative Score: {sentiment_result.confidence_scores.negative}\")\n    print(f\"Neutral Score: {sentiment_result.confidence_scores.neutral}\")\n\n    summary_with_sentiment = summarized_text + f\"\\nSentiment: {sentiment_result.sentiment}\\n\"\n\n    return summary_with_sentiment\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-textsummarize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-textsummarize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-python-ai-textsummarize/lists"}