-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsummarize_function.cs
135 lines (105 loc) · 5.64 KB
/
summarize_function.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Azure;
using Azure.AI.TextAnalytics;
using Azure.Core;
using Azure.Core.Diagnostics;
using Azure.Identity;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace AI_Functions
{
public class summarize_function
{
private readonly ILogger _logger;
private TokenCredential credential;
private Uri endpoint;
public summarize_function(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<summarize_function>();
var endpointUri = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT")
?? throw new InvalidOperationException("Environment variable 'TEXT_ANALYTICS_ENDPOINT' is not set.");
endpoint = new Uri(endpointUri);
credential = new DefaultAzureCredential();
}
[Function("summarize_function")]
[BlobOutput("processed-text/{name}-output.txt")]
public async Task<string> Run(
[BlobTrigger("unprocessed-text/{name}", Source = BlobTriggerSource.EventGrid )] string myTriggerItem,
FunctionContext context)
{
var logger = context.GetLogger("summarize_function");
logger.LogInformation($"Triggered Item = {myTriggerItem}");
// Create client using Entra User or Managed Identity (no longer AzureKeyCredential)
// This requires a sub domain name to be set in endpoint URL for Managed Identity support
// See https://learn.microsoft.com/en-us/azure/ai-services/authentication#authenticate-with-microsoft-entra-id
var client = new TextAnalyticsClient(endpoint, credential);
// analyze document text using Azure Cognitive Language Services
var summarizedText = await AISummarizeText(client, myTriggerItem, logger);
logger.LogInformation(Newline() + "*****Summary*****" + Newline() + summarizedText);
// Blob Output
return summarizedText;
}
private async Task<string> AISummarizeText(TextAnalyticsClient client, string document, ILogger logger)
{
// Perform Extractive Summarization
string summarizedText = "";
// Prepare analyze operation input. You can add multiple documents to this list and perform the same
// operation to all of them.
var batchInput = new List<string>
{
document
};
TextAnalyticsActions actions = new TextAnalyticsActions()
{
ExtractiveSummarizeActions = new List<ExtractiveSummarizeAction>() { new ExtractiveSummarizeAction() }
};
// Start analysis process.
ExtractiveSummarizeOperation operation = client.ExtractiveSummarize(WaitUntil.Completed, batchInput);
// View operation status.
summarizedText += $"AnalyzeActions operation has completed" + Newline();
summarizedText += $"Created On : {operation.CreatedOn}" + Newline();
summarizedText += $"Expires On : {operation.ExpiresOn}" + Newline();
summarizedText += $"Id : {operation.Id}" + Newline();
summarizedText += $"Status : {operation.Status}" + Newline();
// View operation results.
await foreach (ExtractiveSummarizeResultCollection documentsInPage in operation.Value)
{
Console.WriteLine($"Extractive Summarize, version: \"{documentsInPage.ModelVersion}\"");
Console.WriteLine();
foreach (ExtractiveSummarizeResult documentResult in documentsInPage)
{
if (documentResult.HasError)
{
Console.WriteLine($" Error!");
Console.WriteLine($" Document error code: {documentResult.Error.ErrorCode}");
Console.WriteLine($" Message: {documentResult.Error.Message}");
continue;
}
Console.WriteLine($" Extracted {documentResult.Sentences.Count} sentence(s):");
Console.WriteLine();
foreach (ExtractiveSummarySentence sentence in documentResult.Sentences)
{
Console.WriteLine($" Sentence: {sentence.Text}");
Console.WriteLine($" Rank Score: {sentence.RankScore}");
Console.WriteLine($" Offset: {sentence.Offset}");
Console.WriteLine($" Length: {sentence.Length}");
Console.WriteLine();
summarizedText += $" Sentence: {sentence.Text}" + Newline();
}
}
}
logger.LogInformation(Newline() + "*****Summary*****" + Newline() + summarizedText);
// Perform sentiment analysis on document summary
var sentimentResult = await client.AnalyzeSentimentAsync(summarizedText);
Console.WriteLine($"\nSentiment: {sentimentResult.Value.Sentiment}");
Console.WriteLine($"Positive Score: {sentimentResult.Value.ConfidenceScores.Positive}");
Console.WriteLine($"Negative Score: {sentimentResult.Value.ConfidenceScores.Negative}");
Console.WriteLine($"Neutral Score: {sentimentResult.Value.ConfidenceScores.Neutral}");
var summaryWithSentiment = summarizedText + $"Sentiment: {sentimentResult.Value.Sentiment}" + Newline();
return summaryWithSentiment;
}
private string Newline()
{
return "\r\n";
}
}
}