-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiRequestManager.cs
More file actions
444 lines (374 loc) · 16.8 KB
/
ApiRequestManager.cs
File metadata and controls
444 lines (374 loc) · 16.8 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using Observatory.Framework;
using System.Text;
using System.Text.Json;
using System.Xml;
using System.Security.Cryptography;
using System.Collections.Concurrent;
namespace Observatory.Herald
{
class ApiRequestManager
{
private HttpClient httpClient;
private DirectoryInfo cacheLocation;
private int cacheSize;
private Action<Exception, string> ErrorLogger;
private ConcurrentDictionary<string, CacheData> cacheIndex;
private Api Api {
get
{
if (settings.API.TryGetValue(settings.SelectedAPI ?? "Patreon", out var selectedApi))
{
return (Api)selectedApi;
}
else
{
return Api.Patreon;
}
}
}
private HeraldSettings settings;
internal ApiRequestManager(
HeraldSettings settings, HttpClient httpClient, string cacheFolder, Action<Exception, String> errorLogger)
{
this.settings = settings;
// Api = settings.API.TryGetValue(settings.SelectedAPI ?? "Patreon", out var selectedApi) ? (Api)selectedApi : Api.Patreon;
this.httpClient = httpClient;
cacheSize = Math.Max(settings.CacheSize, 1);
cacheLocation = new DirectoryInfo(cacheFolder);
ReadCache();
ErrorLogger = errorLogger;
if (!Directory.Exists(cacheLocation.FullName))
{
Directory.CreateDirectory(cacheLocation.FullName);
}
settings.Voices = PopulateVoiceSettingOptions();
}
internal async Task<string> GetNewUserId()
{
var requestTask = httpClient.GetAsync(settings.PatreonApiEndpoint + "NewUserId");
requestTask.Wait(5000);
if (requestTask.IsFaulted)
throw new Exception("Error retrieving new user ID.", requestTask.Exception);
using var response = await requestTask;
if (response.IsSuccessStatusCode)
{
var userId = await response.Content.ReadAsStringAsync();
settings.UserID = userId;
return userId;
}
else
{
throw new Exception("Unable to retrieve new user ID.", new Exception(response.StatusCode.ToString() + ": " + response.ReasonPhrase));
}
}
internal async Task<string> GetAudioFile(string ssml, string rawText, string voice, string style, string rate)
{
ssml = AddVoiceToSsml(ssml, voice, style, rate);
using var sha = SHA256.Create();
var ssmlHash = BitConverter.ToString(
sha.ComputeHash(Encoding.UTF8.GetBytes(ssml))
).Replace("-", string.Empty);
var audioFilename = cacheLocation + ssmlHash + ".mp3";
FileInfo audioFileInfo = null;
if (File.Exists(audioFilename))
{
audioFileInfo = new FileInfo(audioFilename);
if (audioFileInfo.Length == 0)
{
File.Delete(audioFilename);
audioFileInfo = null;
}
}
audioFileInfo ??= Api switch
{
Api.Patreon => await GetAudioFromPatreon(ssml, audioFilename),
Api.Azure => await GetAudioFromAzure(ssml, audioFilename),
Api.OpenAI => await GetAudioFromOpenAI(rawText, audioFilename),
_ => throw new NotImplementedException("No valid API selected."),
};
UpdateAndPruneCache(audioFileInfo);
return audioFilename;
}
private async Task<FileInfo> GetAudioFromPatreon(string ssml, string audioFilename)
{
using StringContent request = new(ssml)
{
Headers = {
{ "User-Id", settings.UserID }
}
};
var requestTask = httpClient.PostAsync(settings.PatreonApiEndpoint + "AzureVoice/Speak", request);
requestTask.Wait(5000);
if (requestTask.IsFaulted)
throw new Exception("Error retrieving voice audio from Observatory API.", requestTask.Exception);
using var response = await requestTask;
if (response.IsSuccessStatusCode)
{
using FileStream fileStream = new FileStream(audioFilename, FileMode.CreateNew);
response.Content.ReadAsStream().CopyTo(fileStream);
fileStream.Close();
}
else if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("Not Authorised to use Observatory API, authenticate with Patreon first.");
}
else
{
throw new Exception("Unable to retrieve audio data from Observatory API.", new Exception(response.StatusCode.ToString() + ": " + response.ReasonPhrase));
}
return new FileInfo(audioFilename);
}
private async Task<FileInfo> GetAudioFromAzure(string ssml, string audioFilename)
{
using StringContent request = new(ssml, Encoding.UTF8, "application/ssml+xml");
var url = $"https://{settings.AzureRegion}.tts.speech.microsoft.com/cognitiveservices/v1";
using HttpRequestMessage httpRequest = new(HttpMethod.Post, url);
httpRequest.Headers.Add("User-Agent", $"ObservatoryHerald{settings.SettingsVersion} (+https://github.com/Xjph/ObservatoryCore)");
httpRequest.Headers.Add("Ocp-Apim-Subscription-Key", settings.SubscriptionKey);
httpRequest.Headers.Add("X-Microsoft-OutputFormat", "audio-16khz-128kbitrate-mono-mp3");
httpRequest.Headers.Add("X-Microsoft-ProjectName", "ObservatoryHerald");
httpRequest.Content = request;
var requestTask = httpClient.SendAsync(httpRequest);
requestTask.Wait(5000);
if (requestTask.IsFaulted)
throw new PluginException("Herald", "Error retrieving voice audio from Azure.", requestTask.Exception);
using var response = await requestTask;
if (response.IsSuccessStatusCode)
{
using FileStream fileStream = new FileStream(audioFilename, FileMode.CreateNew);
response.Content.ReadAsStream().CopyTo(fileStream);
fileStream.Close();
}
else
{
throw new PluginException("Herald", "Unable to retrieve audio data from Azure.", new Exception(response.StatusCode.ToString() + ": " + response.ReasonPhrase));
}
return new FileInfo(audioFilename);
}
private async Task<FileInfo> GetAudioFromOpenAI(string text, string audioFilename)
{
Dictionary<string, string> openAiPayload = new()
{
{ "model", settings.OpenAIModel },
{ "input", text },
{ "voice", settings.OpenAIVoice },
{ "response_format", "mp3" }
};
using StringContent request = new(JsonSerializer.Serialize(openAiPayload))
{
Headers = {
{ "Authorization", "Bearer " + settings.OpenAIKey },
{ "Content-Type", "application/json" }
}
};
var requestTask = httpClient.PostAsync(settings.OpenAIEndpoint, request);
requestTask.Wait(5000);
if (requestTask.IsFaulted)
throw new PluginException("Herald", "Error retrieving voice audio from OpenAI.", requestTask.Exception);
using var response = await requestTask;
if (response.IsSuccessStatusCode)
{
using FileStream fileStream = new FileStream(audioFilename, FileMode.CreateNew);
response.Content.ReadAsStream().CopyTo(fileStream);
fileStream.Close();
}
else
{
throw new PluginException("Herald", "Unable to retrieve audio data from OpenAI.", new Exception(response.StatusCode.ToString() + ": " + response.ReasonPhrase));
}
return new FileInfo(audioFilename);
}
private static string AddVoiceToSsml(string ssml, string voiceName, string styleName, string rate)
{
XmlDocument ssmlDoc = new();
ssmlDoc.LoadXml(ssml);
var ssmlNamespace = ssmlDoc.DocumentElement.NamespaceURI;
XmlNamespaceManager ssmlNs = new(ssmlDoc.NameTable);
ssmlNs.AddNamespace("ssml", ssmlNamespace);
ssmlNs.AddNamespace("mstts", "http://www.w3.org/2001/mstts");
ssmlNs.AddNamespace("emo", "http://www.w3.org/2009/10/emotionml");
var voiceNode = ssmlDoc.SelectSingleNode("/ssml:speak/ssml:voice", ssmlNs);
voiceNode.Attributes.GetNamedItem("name").Value = voiceName;
if (!string.IsNullOrWhiteSpace(rate))
{
var prosodyNode = ssmlDoc.CreateElement("ssml", "prosody", ssmlNamespace);
prosodyNode.SetAttribute("rate", rate);
prosodyNode.InnerXml = voiceNode.InnerXml;
voiceNode.InnerXml = prosodyNode.OuterXml;
}
if (!string.IsNullOrWhiteSpace(styleName))
{
var expressAsNode = ssmlDoc.CreateElement("mstts", "express-as", "http://www.w3.org/2001/mstts");
expressAsNode.SetAttribute("style", styleName);
expressAsNode.InnerXml = voiceNode.InnerXml;
voiceNode.InnerXml = expressAsNode.OuterXml;
}
return ssmlDoc.OuterXml;
}
private Dictionary<string, object> PopulateVoiceSettingOptions()
{
Dictionary<string, object> voices = new();
using var request = new HttpRequestMessage(HttpMethod.Get, settings.PatreonApiEndpoint + "AzureVoice/List");
var response = httpClient.Send(request);
if (response.IsSuccessStatusCode)
{
var voiceJson = response.Content.ReadAsStringAsync().Result;
var voiceDoc = JsonDocument.Parse(voiceJson);
var englishSpeakingVoices = from v in voiceDoc.RootElement.EnumerateArray()
where v.GetProperty("Locale").GetString().StartsWith("en-")
|| (v.TryGetProperty("SecondaryLocaleList", out var l) && l.EnumerateArray().Any(s => s.GetString().StartsWith("en-")))
select v;
foreach(var voice in englishSpeakingVoices)
{
string demonym = GetDemonymFromLocale(voice.GetProperty("Locale").GetString());
voices.TryAdd(
demonym + " - " + voice.GetProperty("LocalName").GetString(),
voice);
if (voice.TryGetProperty("StyleList", out var styles))
foreach (var style in styles.EnumerateArray())
{
voices.TryAdd(
demonym + " - " + voice.GetProperty("LocalName").GetString() + " - " + style.GetString(),
voice);
}
}
}
else
{
throw new PluginException("Herald", "Unable to retrieve available voices.", new Exception(response.StatusCode.ToString() + ": " + response.ReasonPhrase));
}
return voices;
}
private static readonly Dictionary<string, string> LocaleDemonymMap = new()
{
{ "en-AU", "Australian" },
{ "en-CA", "Canadian" },
{ "en-GB", "British" },
{ "en-HK", "Hong Konger" },
{ "en-IE", "Irish" },
{ "en-IN", "Indian" },
{ "en-KE", "Kenyan" },
{ "en-NG", "Nigerian" },
{ "en-NZ", "Kiwi" },
{ "en-PH", "Filipino" },
{ "en-SG", "Singaporean" },
{ "en-TZ", "Tanzanian" },
{ "en-US", "American" },
{ "en-ZA", "South African" },
{ "de-DE", "German" },
{ "fr-FR", "French" },
{ "it-IT", "Italian" },
{ "ja-JP", "Japanese" },
{ "ko-KR", "Korean" },
{ "es-ES", "Spanish" },
{ "es-MX", "Mexican" },
{ "pt-BR", "Brazilian" },
{ "pt-PT", "Portuguese" },
{ "zh-CN", "Chinese" },
{ "zh-HK", "Hong Konger" },
{ "zh-TW", "Taiwanese" }
};
private static string GetDemonymFromLocale(string locale)
{
return LocaleDemonymMap.TryGetValue(locale, out var demonym) ? demonym : locale;
}
private void ReadCache()
{
string cacheIndexFile = cacheLocation + "CacheIndex.json";
if (File.Exists(cacheIndexFile))
{
var indexFileContent = File.ReadAllText(cacheIndexFile);
try
{
cacheIndex = JsonSerializer.Deserialize<ConcurrentDictionary<string, CacheData>>(indexFileContent);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
cacheIndex = new();
ErrorLogger(ex, "deserializing CacheIndex.json");
}
}
else
{
cacheIndex = new();
}
// Re-index orphaned files in event of corrupted or lost index.
var cacheFiles = cacheLocation.GetFiles("*.mp3");
foreach (var file in cacheFiles.Where(file => !cacheIndex.ContainsKey(file.Name)))
{
cacheIndex.TryAdd(file.Name, new(file.CreationTime, 0));
};
}
private void UpdateAndPruneCache(FileInfo currentFile)
{
var cacheFiles = cacheLocation.GetFiles("*.mp3");
if (cacheIndex.ContainsKey(currentFile.Name))
{
cacheIndex[currentFile.Name] = new(
cacheIndex[currentFile.Name].Created,
cacheIndex[currentFile.Name].HitCount + 1
);
}
else
{
cacheIndex.TryAdd(currentFile.Name, new(DateTime.UtcNow, 1));
}
var indexedCacheSize = cacheFiles
.Where(f => cacheIndex.ContainsKey(f.Name))
.Sum(f => f.Length);
while (indexedCacheSize > cacheSize * 1024 * 1024)
{
var staleFile = (from file in cacheIndex
orderby file.Value.HitCount, file.Value.Created
select file.Key).First();
if (staleFile == currentFile.Name)
break;
cacheIndex.TryRemove(staleFile, out _);
}
}
internal async void CommitCache()
{
string cacheIndexFile = cacheLocation + "CacheIndex.json";
System.Diagnostics.Stopwatch stopwatch = new();
stopwatch.Start();
// Race condition isn't a concern anymore, but should check this anyway to be safe.
// (Maybe someone is poking at the file with notepad?)
while (!IsFileWritable(cacheIndexFile) && stopwatch.ElapsedMilliseconds < 1000)
await Task.Factory.StartNew(() => System.Threading.Thread.Sleep(100));
// 1000ms should be more than enough for a conflicting title or detail to complete,
// if we're still waiting something else is locking the file, just give up.
if (stopwatch.ElapsedMilliseconds < 1000)
{
File.WriteAllText(cacheIndexFile, JsonSerializer.Serialize(cacheIndex));
// Purge cache from earlier versions, if still present.
var legacyCache = cacheLocation.GetFiles("*.wav");
Array.ForEach(legacyCache, file => File.Delete(file.FullName));
}
stopwatch.Stop();
}
private static bool IsFileWritable(string path)
{
try
{
using FileStream fs = File.OpenWrite(path);
fs.Close();
return true;
}
catch
{
return false;
}
}
private class CacheData
{
public CacheData(DateTime Created, int HitCount)
{
this.Created = Created;
this.HitCount = HitCount;
}
public DateTime Created { get; set; }
public int HitCount { get; set; }
}
}
}