-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
331 lines (305 loc) · 13.3 KB
/
MainWindow.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Azure;
using Azure.AI.OpenAI;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using System.Reflection;
namespace audio_powered_gpt
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Task audioBackgroundTask = null;
private SpeechRecognizer speechRecognizer = null;
public MainWindow()
{
this.InitializeComponent();
this.SpeechKey.Password = "AZURE_COGNITIVE_SERVICES_API_KEY";
this.SpeechRegion.Text = "westus2";
this.GptKey.Password = "AZURE_OPENAI_API_KEY";
this.GptEndpoint.Text = "https://RESOURCE.openai.azure.com/";
this.GptModel.Text = "DEPLOYMENT_OR_MODEL_NAME";
this.ChangeToStartButton();
this.ChangeReponseToTextView();
}
public void ConfigsLoad(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
try
{
Dictionary<string, string> configs = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(dlg.FileName));
this.SpeechKey.Password = configs?.GetValueOrDefault("SpeechKey") ?? string.Empty;
this.SpeechRegion.Text = configs?.GetValueOrDefault("SpeechRegion") ?? string.Empty;
this.GptKey.Password = configs?.GetValueOrDefault("GptKey") ?? string.Empty;
this.GptEndpoint.Text = configs?.GetValueOrDefault("GptEndpoint") ?? string.Empty;
this.GptModel.Text = configs?.GetValueOrDefault("GptModel") ?? string.Empty;
this.ShowToast($"Loaded: {dlg.FileName}");
}
catch (Exception exception)
{
this.ShowToast($"Error: {exception.Message}");
}
}
}
public void ConfigsSave(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
Dictionary<string, string> configs = new Dictionary<string, string>
{
{ "SpeechKey", this.SpeechKey.Password },
{ "SpeechRegion", this.SpeechRegion.Text },
{ "GptKey", this.GptKey.Password },
{ "GptEndpoint", this.GptEndpoint.Text },
{ "GptModel", this.GptModel.Text }
};
string json = JsonSerializer.Serialize(configs);
File.WriteAllText(dlg.FileName, json);
this.ShowToast($"Saved: {dlg.FileName}");
}
}
public void AboutVersion(object sender, RoutedEventArgs e)
{
this.ShowToast($"Version: {Assembly.GetExecutingAssembly().GetName().Version}");
}
public void AboutSource(object sender, RoutedEventArgs e)
{
string destinationurl = "https://github.com/der3318/audio-powered-gpt";
Process.Start(new ProcessStartInfo(destinationurl)
{
UseShellExecute = true,
});
}
public async void StartStop(object sender, RoutedEventArgs e)
{
lock (this.StartStopBtn)
{
if (!this.StartStopBtn.IsEnabled)
{
return;
}
this.StartStopBtn.IsEnabled = false;
}
if (this.IsRunning())
{
await (this.speechRecognizer?.StopContinuousRecognitionAsync() ?? Task.CompletedTask);
await (this.audioBackgroundTask ?? Task.CompletedTask);
this.speechRecognizer?.Dispose();
this.speechRecognizer = null;
this.audioBackgroundTask = null;
this.ChangeToStartButton();
}
else if (this.Mode.SelectedIndex == 0) // Translate to Chinese
{
this.ResponseConsole.Text = string.Empty;
this.ResponseMdConsole.Markdown = string.Empty;
this.audioBackgroundTask = this.AudioBackgroundTask(
false,
this.SpeechKey.Password,
this.SpeechRegion.Text,
this.GptKey.Password,
this.GptEndpoint.Text,
this.GptModel.Text);
this.ChangeToStopButton();
}
else if (this.Mode.SelectedIndex == 1) // Interactive Speech
{
this.ResponseConsole.Text = string.Empty;
this.ResponseMdConsole.Markdown = string.Empty;
this.audioBackgroundTask = this.AudioBackgroundTask(
true,
this.SpeechKey.Password,
this.SpeechRegion.Text,
this.GptKey.Password,
this.GptEndpoint.Text,
this.GptModel.Text);
this.ChangeToStopButton();
}
else if (this.Mode.SelectedIndex == 2) // Interactive Text
{
this.ResponseConsole.Text = string.Empty;
this.ResponseMdConsole.Markdown = string.Empty;
await this.GptBackgroundTask(this.PromotConsole.Text, this.GptKey.Password, this.GptEndpoint.Text, this.GptModel.Text);
}
this.StartStopBtn.IsEnabled = true;
}
public void TextMarkdown(object sender, RoutedEventArgs e)
{
lock (this.TextMarkdownBtn)
{
if (this.IsUsingMarkdown())
{
this.ChangeReponseToTextView();
}
else
{
this.ChangeReponseToMarkdownView();
}
}
}
private bool IsRunning()
{
return this.StartStopBtn.Content.ToString().Contains("Stop");
}
private void ChangeToStartButton()
{
this.StartStopBtn.Content = "▶️ Start";
this.StartStopBtn.Background = Brushes.LightGreen;
}
private void ChangeToStopButton()
{
this.StartStopBtn.Content = "⏹️ Stop";
this.StartStopBtn.Background = Brushes.LightCoral;
}
private bool IsUsingMarkdown()
{
return this.TextMarkdownBtn.Content.ToString().Contains("Text");
}
private void ChangeReponseToTextView()
{
this.ResponseConsole.Visibility = Visibility.Visible;
this.ResponseMdConsole.Visibility = Visibility.Collapsed;
this.TextMarkdownBtn.Content = "📰 Switch to Markdown View";
this.UpdateLayout();
}
private void ChangeReponseToMarkdownView()
{
this.ResponseConsole.Visibility = Visibility.Collapsed;
this.ResponseMdConsole.Visibility = Visibility.Visible;
this.TextMarkdownBtn.Content = "🗒 Switch to Text View";
this.UpdateLayout();
}
private void AppendToTextBox(TextBox textbox, string text)
{
const int MaxLoggedLineCnt = 100;
textbox.Text += $"⏱️ {DateTime.Now}\n{text}\n";
textbox.ScrollToEnd();
while (textbox.LineCount > MaxLoggedLineCnt)
{
textbox.Text = textbox.Text[(textbox.Text.IndexOf("\n", StringComparison.OrdinalIgnoreCase) + 1)..];
}
}
private void RenderMarkdown(string text)
{
this.ResponseMdConsole.Markdown = $"{text}";
}
private void ShowToast(string text)
{
new ToastContentBuilder().AddText(text).Show(toast =>
{
toast.ExpirationTime = DateTime.Now.AddSeconds(3);
});
}
private async Task<int> AudioBackgroundTask(bool interactiveMode, string speechKey, string speechRegion, string gptKey, string gptEndpoint, string gptModel)
{
TaskCompletionSource<int> stopRecognition = new TaskCompletionSource<int>();
try
{
SpeechConfig speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
AutoDetectSourceLanguageConfig autoDetect = AutoDetectSourceLanguageConfig.FromLanguages(new string[] { "en-US", "ja-JP", "ko-KR", "zh-TW" });
AudioConfig audioConfig = AudioConfig.FromDefaultMicrophoneInput();
this.speechRecognizer = new SpeechRecognizer(speechConfig, autoDetect, audioConfig);
this.speechRecognizer.Recognized += (s, e) =>
{
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
Trace.WriteLine(e.Result.Text);
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.PromotConsole, e.Result.Text);
});
if (e.Result.Text.Trim(' ', '\r', '\n').Length > 1)
{
string prompt = interactiveMode ? $"{e.Result.Text}" : $"{e.Result.Text} 翻譯成中文";
_ = this.GptBackgroundTask(prompt, gptKey, gptEndpoint, gptModel);
}
}
if (e.Result.Reason == ResultReason.NoMatch)
{
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.PromotConsole, "NOMATCH: Speech could not be recognized.");
});
}
};
this.speechRecognizer.Canceled += (s, e) =>
{
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.PromotConsole, $"CANCELED: Reason = {e.Reason}.");
});
stopRecognition.TrySetResult(0);
};
this.speechRecognizer.SessionStopped += (s, e) =>
{
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.PromotConsole, "STOPPED: Recognition session ended.");
});
stopRecognition.TrySetResult(0);
};
await this.speechRecognizer.StartContinuousRecognitionAsync();
}
catch (Exception e)
{
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.PromotConsole, $"EXCEPTION: Message = {e.Message}.\n{e.StackTrace}");
});
stopRecognition.TrySetResult(0);
}
return await stopRecognition.Task;
}
private async Task GptBackgroundTask(string prompt, string gptKey, string gptEndpoint, string gptModel)
{
try
{
OpenAIClient client = new OpenAIClient(new Uri(gptEndpoint), new AzureKeyCredential(gptKey));
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(
gptModel,
new ChatCompletionsOptions()
{
Messages = { new ChatMessage(ChatRole.System, prompt), },
Temperature = (float)0.7,
MaxTokens = 500,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});
ChatCompletions completions = response.Value;
string message = completions.Choices.FirstOrDefault()?.Message.Content ?? string.Empty;
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.ResponseConsole, message);
this.RenderMarkdown(message);
});
this.ShowToast(message);
}
catch (Exception e)
{
this.Dispatcher.Invoke(() =>
{
this.AppendToTextBox(this.ResponseConsole, $"EXCEPTION: Message = {e.Message}.\n{e.StackTrace}");
this.RenderMarkdown($"EXCEPTION: Message = {e.Message}.\n{e.StackTrace}");
});
}
}
}
}