-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathParaphrase.xaml.cs
194 lines (169 loc) · 5.97 KB
/
Paraphrase.xaml.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using AIDevGallery.Models;
using AIDevGallery.Samples.Attributes;
using AIDevGallery.Samples.SharedCode;
using Microsoft.Extensions.AI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AIDevGallery.Samples.OpenSourceModels.LanguageModels;
[GallerySample(
Name = "Paraphrase",
Model1Types = [ModelType.LanguageModels, ModelType.PhiSilica],
Scenario = ScenarioType.TextParaphraseText,
NugetPackageReferences = [
"Microsoft.Extensions.AI"
],
Id = "9e006e82-8e3f-4401-8a83-d4c4c59cc20c",
Icon = "\uE8D4")]
internal sealed partial class Paraphrase : BaseSamplePage
{
private const int _defaultMaxLength = 1024;
private IChatClient? chatClient;
private CancellationTokenSource? cts;
private bool isProgressVisible;
public Paraphrase()
{
this.Unloaded += (s, e) => CleanUp();
this.Loaded += (s, e) => Page_Loaded(); // <exclude-line>
this.InitializeComponent();
}
protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams)
{
try
{
chatClient = await sampleParams.GetIChatClientAsync();
InputTextBox.MaxLength = _defaultMaxLength;
}
catch (Exception ex)
{
ShowException(ex);
}
sampleParams.NotifyCompletion();
}
// <exclude>
private void Page_Loaded()
{
InputTextBox.Focus(FocusState.Programmatic);
}
// </exclude>
private void CleanUp()
{
CancelParaphrase();
chatClient?.Dispose();
}
public bool IsProgressVisible
{
get => isProgressVisible;
set
{
isProgressVisible = value;
DispatcherQueue.TryEnqueue(() =>
{
OutputProgressBar.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
StopIcon.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
});
}
}
public void ParaphraseText(string text)
{
if (chatClient == null)
{
return;
}
ParaphrasedTextBlock.Text = string.Empty;
ParaphraseButton.Visibility = Visibility.Collapsed;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Paraphrasing text, please wait.", "ParaphraseWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("ParaphraseText"); // <exclude-line>
Task.Run(
async () =>
{
string systemPrompt = "You paraphrase user-provided text. " +
"Respond with only the paraphrased content and no extraneous text.";
string userPrompt = "Paraphrase this text: " + text;
cts = new CancellationTokenSource();
IsProgressVisible = true;
await foreach (var messagePart in chatClient.GetStreamingResponseAsync(
[
new ChatMessage(ChatRole.System, systemPrompt),
new ChatMessage(ChatRole.User, userPrompt)
],
null,
cts.Token))
{
DispatcherQueue.TryEnqueue(() =>
{
if (IsProgressVisible)
{
StopBtn.Visibility = Visibility.Visible;
IsProgressVisible = false;
}
ParaphrasedTextBlock.Text += messagePart;
// <exclude>
if (!contentStartedBeingGenerated)
{
NarratorHelper.Announce(InputTextBox, "Paraphrased content has started generating.", "ParaphraseGeneratedAnnouncementActivityId");
contentStartedBeingGenerated = true;
}
// </exclude>
});
}
DispatcherQueue.TryEnqueue(() =>
{
NarratorHelper.Announce(InputTextBox, "Content has finished generating.", "ParaphraseDoneAnnouncementActivityId"); // <exclude-line>
StopBtn.Visibility = Visibility.Collapsed;
ParaphraseButton.Visibility = Visibility.Visible;
});
cts?.Dispose();
cts = null;
});
}
private void ParaphraseButton_Click(object sender, RoutedEventArgs e)
{
if (this.InputTextBox.Text.Length > 0)
{
IsProgressVisible = true;
StopBtn.Visibility = Visibility.Visible;
ParaphraseButton.Visibility = Visibility.Collapsed;
ParaphraseText(InputTextBox.Text);
}
}
private void CancelParaphrase()
{
StopBtn.Visibility = Visibility.Collapsed;
IsProgressVisible = false;
ParaphraseButton.Visibility = Visibility.Visible;
cts?.Cancel();
cts?.Dispose();
cts = null;
}
private void StopBtn_Click(object sender, RoutedEventArgs e)
{
CancelParaphrase();
}
private void InputBox_Changed(object sender, TextChangedEventArgs e)
{
var inputLength = InputTextBox.Text.Length;
if (inputLength > 0)
{
if (inputLength >= _defaultMaxLength)
{
InputTextBox.Description = $"{inputLength} of {_defaultMaxLength}. Max characters reached.";
}
else
{
InputTextBox.Description = $"{inputLength} of {_defaultMaxLength}";
}
ParaphraseButton.IsEnabled = inputLength <= _defaultMaxLength;
}
else
{
InputTextBox.Description = string.Empty;
ParaphraseButton.IsEnabled = false;
}
}
}