-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTranslateDynamicService.cs
668 lines (581 loc) · 24.2 KB
/
TranslateDynamicService.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Intento.SDK.Client;
using Intento.SDK.Exceptions;
using Intento.SDK.Logging;
using Intento.SDK.Translate.Converters;
using Intento.SDK.Translate.DTO;
using Intento.SDK.Translate.Options;
using Intento.SDK.Validation;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Polly;
namespace Intento.SDK.Translate
{
/// <summary>
/// Intento API
/// </summary>
internal sealed class TranslateDynamicService : ITranslateService
{
private IntentoHttpClient Client { get; }
private ILogger Logger { get; }
/// <summary>
/// Ctor
/// </summary>
/// <param name="client"></param>
/// <param name="logger"></param>
public TranslateDynamicService(IntentoHttpClient client, ILogger<TranslateDynamicService> logger)
{
Client = client;
Logger = logger;
}
/// <inheritdoc />
public Task<TranslateResponse> FulfillAsync(TranslateOptions options)
{
return FulfillAsync(options, null);
}
private async Task<TranslateResponse> FulfillAsync(TranslateOptions options,
Action<TranslateRequest> extendRequest)
{
options.ValidateAndThrow();
var request = new TranslateRequest();
var context = new TranslateContext();
request.Context = context;
var textLength = 0;
switch (options.Text)
{
// text
case null:
context.Text = new[] { "" };
break;
case IEnumerable<string> enumerable:
var array = enumerable as string[] ?? enumerable.ToArray();
var testArr = array.Select(i => i ?? "").ToArray();
context.Text = testArr;
textLength = testArr.Sum(i => i.Length);
break;
default:
var text = options.Text.ToString();
context.Text = new[] { text ?? "" };
textLength = text.Length;
break;
}
// determination of the possibility of using the syncwrapper
// maximum 10k characters
if (options.UseSyncwrapper)
{
if (textLength < 10000)
{
options.Async = !options.UseSyncwrapper;
}
else
{
options.UseSyncwrapper = false;
}
}
// to
context.To = options.To;
// from
if (!string.IsNullOrWhiteSpace(options.From))
{
context.From = options.From;
}
// format
if (!string.IsNullOrWhiteSpace(options.Format))
{
context.Format = options.Format;
}
// custom_model
if (!string.IsNullOrWhiteSpace(options.CustomModel))
{
context.Category = options.CustomModel;
}
// glossary
if (!string.IsNullOrWhiteSpace(options.Glossary))
{
context.Glossary = options.Glossary;
}
// intento glossary
if (options.IntentoGlossary is { Length: > 0 })
{
context.Glossaries = options.IntentoGlossary.Select(g => new Glossary { Id = g }).ToArray();
}
// service section
var service = new TranslateService();
request.Service = service;
// provider
if (!string.IsNullOrWhiteSpace(options.Provider))
{
service.Provider = options.Provider;
}
// async parameter
if (options.Async)
{
service.Async = true;
}
// auth parameter
service.Auth = options.Auth;
// routing
if (!string.IsNullOrEmpty(options.Routing))
{
service.Routing = options.Routing;
}
// pre-post processing parameters
if (options.PreProcessing != null || options.PostProcessing != null)
{
var processing = new TranslateProcessing();
if (options.PreProcessing != null)
{
processing.Pre = options.PreProcessing;
}
if (options.PostProcessing != null)
{
processing.Post = options.PostProcessing;
}
service.Processing = processing;
}
// failover parameters
if (options.Failover)
{
service.Failover = true;
service.FailoverList = options.FailoverList;
}
extendRequest?.Invoke(request);
var url = "ai/text/translate";
var modifiers = new List<string>();
if (options.Trace)
{
modifiers.Add("trace=true");
}
if (options.PlainText)
{
modifiers.Add("plainText=true");
}
if (modifiers.Count > 0)
{
url += $"?{string.Join("&", modifiers)}";
}
// Call to Intento API and get json result
var jsonResult =
await Client.PostAsync<TranslateRequest, TranslateResponse>(url, request,
useSyncwrapper: options.UseSyncwrapper, isTranslateRequest: true);
if (options.Async && options.WaitAsync)
{
// async operation (in terms of IntentoApi) and we need to wait result of it
var id = jsonResult.Id;
// In case of Sandbox key and some errors in parameters request to IntentoAPI may return:
// 1. id: async operation started
// 2. result of translation: Sandcox key
// 3. error: validation of arameters failed (like request to translate html with provider with no such capabilities)
if (id == null)
{
// Not a (1) - return result immediately, nothing to wait
return jsonResult;
}
var wrapperResult = await Policy
.Handle<Exception>().Or<IntentoApiException>()
.WaitAndRetryAsync(20, _ => TimeSpan.FromSeconds(1))
.ExecuteAndCaptureAsync(async () =>
{
var response = await CheckAsyncJobAsync(id);
if (response.Done)
{
return response;
}
throw new IntentoException("Job is active");
});
if (wrapperResult.FinalException != null)
{
jsonResult = new TranslateResponse
{
Error = new TranslationRequestError
{
Message = "Async operation haven't finished yet",
Data = wrapperResult.FinalException.Message
}
};
}
var wrapper = wrapperResult.Result;
if (wrapper.Done)
{
jsonResult = wrapper.Response is { Length: > 0 } ? wrapper.Response[0] : new TranslateResponse
{
Error = new TranslationRequestError
{
Message = wrapper.Error?.Reason,
Data = wrapper.Error != null ? JsonConvert.SerializeObject(wrapper.Error) : null
}
};
}
}
return jsonResult;
}
/// <inheritdoc />
public TranslateResponse Fulfill(TranslateOptions options)
{
var taskReadResult = Task.Run(async () => await FulfillAsync(options));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task FulfillFileAsync(TranslateOptions options, string sourcePath, string outputPath)
{
if (!File.Exists(sourcePath))
{
Logger.LogWarning($"File not found in path {sourcePath}");
return;
}
var fileInfo = new FileInfo(sourcePath);
using var stream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
using var result = await FulfillFileAsync(options, stream, fileInfo);
using var writer = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
await result.CopyToAsync(writer);
}
/// <inheritdoc />
public void FulfillFile(TranslateOptions options, string sourcePath, string outputPath)
{
var taskReadResult = Task.Run(async () => await FulfillFileAsync(options, sourcePath, outputPath));
taskReadResult.Start();
taskReadResult.Wait();
}
/// <inheritdoc />
public async Task<Stream> FulfillFileAsync(TranslateOptions options, Stream source, FileInfo fileInfo)
{
options.Text = ConvertToBase64String(source);
options.Async = true;
options.WaitAsync = true;
var res = await FulfillAsync(options, (request) =>
{
request.Context.Format = request.Context.OutputFileExtension =
request.Context.OutputFormat = fileInfo.Extension.Replace(".", "");
request.Context.Size = fileInfo.Length;
var userData = new TranslateUserData
{
FileName = fileInfo.Name
};
request.UserData = userData;
});
if (res.Results is not { Length: > 0 })
{
return null;
}
var fileContent = Convert.FromBase64String(res.Results[0]);
return new MemoryStream(fileContent);
}
/// <inheritdoc />
public Stream FulfillFile(TranslateOptions options, Stream source, FileInfo fileInfo)
{
var taskReadResult = Task.Run(async () => await FulfillFileAsync(options, source, fileInfo));
return taskReadResult.Result;
}
/// <inheritdoc />
public IList<Model> Models(string provider, IDictionary<string, string> credentials,
IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () =>
await ModelsAsync(provider, credentials, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Model>> ModelsAsync(string providerId, IDictionary<string, string> credentials,
IDictionary<string, string> additionalParams = null)
{
var path = $"ai/text/translate/models?provider={providerId}";
if (credentials != null)
{
if (credentials.Count != 0)
{
string json;
if (credentials.Count == 1 && credentials.ContainsKey("credential_id"))
json = credentials["credential_id"];
else
{
json = JsonConvert.SerializeObject(credentials, Formatting.None);
json = HttpUtility.UrlEncode(json);
}
path += $"&credential_id={json}";
}
}
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<ModelsResponse>(path, false, additionalParams);
return jsonResult.Models;
}
/// <inheritdoc />
public IList<Account> Accounts(string providerId = null, IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run<dynamic>(async () =>
await AccountsAsync(providerId, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Account>> AccountsAsync(string providerId = null,
IDictionary<string, string> additionalParams = null)
{
var path = "accounts" + (providerId != null ? $"?provider={providerId}" : null);
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<AccountsResult>(path, false, additionalParams);
return jsonResult.Result;
}
/// <inheritdoc />
public IList<Routing> Routing(IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () =>
await RoutingAsync(additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Routing>> RoutingAsync(IDictionary<string, string> additionalParams = null)
{
const string path = "ai/text/translate/routing";
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<RoutingResponse>(path, true, additionalParams);
return jsonResult.Routing;
}
/// <inheritdoc />
public IList<NativeGlossary> Glossaries(string providerId, string credentials,
IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run<dynamic>(async () =>
await GlossariesAsync(providerId, credentials, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<NativeGlossary>> GlossariesAsync(string providerId, string credentials,
IDictionary<string, string> additionalParams = null)
{
var path = $"ai/text/translate/glossaries?provider={providerId}";
if (!string.IsNullOrEmpty(credentials))
{
path += $"&credential_id={credentials}";
}
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<NativeGlossaryResult>(path, false, additionalParams);
return jsonResult.Response;
}
/// <inheritdoc />
public Provider Provider(string provider, IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () => await ProviderAsync(provider, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<Provider> ProviderAsync(string providerId,
IDictionary<string, string> additionalParams = null)
{
var path = $"ai/text/translate/{providerId}";
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<Provider>(path, true, additionalParams);
return jsonResult;
}
/// <inheritdoc />
public IList<Provider> Providers(string to = null, string @from = null, bool langDetect = false,
bool bulk = false,
IDictionary<string, string> filter = null)
{
var taskReadResult = Task.Run(async () =>
await ProvidersAsync(to, @from, langDetect, bulk, filter));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Provider>> ProvidersAsync(string to = null, string @from = null,
bool langDetect = false,
bool bulk = false,
IDictionary<string, string> filter = null)
{
var f = filter == null ? new Dictionary<string, string>() : new Dictionary<string, string>(filter);
if (!string.IsNullOrEmpty(to))
{
f["to"] = to;
}
if (!string.IsNullOrEmpty(from))
{
f["from"] = from;
}
if (langDetect)
{
f["lang_detect"] = "true";
}
if (bulk)
{
f["bulk"] = "true";
}
var p = f.Select(pair => $"{pair.Key}={HttpUtility.UrlEncode(pair.Value)}").ToList();
var url = "ai/text/translate";
if (p.Count != 0)
{
url += "?" + string.Join("&", p);
}
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<List<Provider>>(url, true);
var providers = new List<Provider>();
foreach (var providerInfo in jsonResult)
{
providers.Add(providerInfo);
}
return providers;
}
/// <inheritdoc />
public IList<Language> Languages()
{
var taskReadResult = Task.Run(async () => await LanguagesAsync());
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Language>> LanguagesAsync()
{
const string url = "ai/text/translate/languages";
var jsonResult = await Client.GetAsync<IList<Language>>(url);
return jsonResult.ToList();
}
/// <inheritdoc />
public IList<Language> GetSupportedLanguages()
{
var taskReadResult = Task.Run(async () => await GetSupportedLanguagesAsync());
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<Language>> GetSupportedLanguagesAsync()
{
const string url = "ai/text/transliterate/languages";
var jsonResult = await Client.GetAsync<IList<Language>>(url);
return jsonResult.ToList();
}
/// <inheritdoc />
public LanguagePairs RoutingPairs(string srtName, IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () => await RoutingPairsAsync(srtName, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<LanguagePairs> RoutingPairsAsync(string srtName, IDictionary<string, string> additionalParams = null)
{
var url = $"ai/text/translate/routing/{srtName}/pairs";
var jsonResult = await Client.GetAsync<LanguagePairs>(url, true, additionalParams: additionalParams);
return jsonResult;
}
/// <inheritdoc />
public IList<IList<string>> RoutingLanguagePairs(string providerId,
IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () => await RoutingLanguagePairsAsync(providerId, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<IList<string>>> RoutingLanguagePairsAsync(string providerId, IDictionary<string, string> additionalParams = null)
{
var routingInfo = await RoutingPairsAsync(providerId, additionalParams);
var res = new List<IList<string>>();
var pairs = routingInfo.Pairs;
if (pairs == null)
{
return res;
}
res.AddRange(pairs.Select(pair => new List<string> { pair.From, pair.To }));
return res;
}
/// <inheritdoc />
public IList<IList<string>> ProviderLanguagePairs(string providerId,
IDictionary<string, string> additionalParams = null)
{
var taskReadResult = Task.Run(async () => await ProviderLanguagePairsAsync(providerId, additionalParams));
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<IList<string>>> ProviderLanguagePairsAsync(string providerId, IDictionary<string, string> additionalParams = null)
{
var providerInfo = await ProviderAsync(providerId, additionalParams);
var res = new List<IList<string>>();
var languages = providerInfo.Languages;
var symmetric = languages.Symmetric;
if (symmetric != null)
{
foreach (var l1 in symmetric)
{
res.AddRange(from l2 in symmetric where l1 != l2 select new List<string> { l1, l2 });
}
}
var pairs = languages.Pairs;
if (pairs == null)
{
return res;
}
res.AddRange(pairs.Select(pair => new List<string> { pair.From, pair.To }));
return res;
}
/// <inheritdoc />
public IList<GlossaryDetailed> AgnosticGlossaries()
{
var taskReadResult = Task.Run<dynamic>(async () =>
await AgnosticGlossariesAsync());
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<GlossaryDetailed>> AgnosticGlossariesAsync()
{
var path = $"ai/text/glossaries/v2/typed";
// Call to Intento API and get json result
var jsonResult = await Client.GetAsync<AgnosticGlossariesResult>(path);
return jsonResult.Glossaries.ToList();
}
/// <inheritdoc />
public IList<AgnosticGlossaryType> AgnosticGlossariesTypes()
{
var taskReadResult = Task.Run<dynamic>(async () =>
await AgnosticGlossariesTypesAsync());
return taskReadResult.Result;
}
/// <inheritdoc />
public async Task<IList<AgnosticGlossaryType>> AgnosticGlossariesTypesAsync()
{
var path = $"ai/text/glossaries/v2/cs_types";
var jsonResult = await Client.GetAsync<AgnosticGlossariesTypesResult>(path);
return jsonResult.Types.ToList();
}
/// <inheritdoc />
public TranslateResponseWrapper CheckAsyncJob(string asyncId)
{
var task = Task.Run(async () => await CheckAsyncJobAsync(asyncId));
return task.Result;
}
/// <inheritdoc />
public async Task<TranslateResponseWrapper> CheckAsyncJobAsync(string asyncId)
{
Logger.LogDebug(SdkLogEvents.INVOKE_METHOD, "CheckAsyncJobAsync: {asyncId}ms", asyncId);
// async operations inside
var result = await Client.GetAsync<TranslateResponseWrapper>($"operations/{asyncId}");
return result;
}
private async Task<TranslateResponseWrapper> WaitAsync(TaskCompletionSource<bool> taskCompletion,
string asyncId, int delay = 1000)
{
await Task.WhenAny(taskCompletion.Task, Task.Delay(delay));
var response = await CheckAsyncJobAsync(asyncId);
if (response.Done)
{
return response;
}
return await WaitAsync(taskCompletion, asyncId, delay);
}
private async Task<TranslateResponseWrapper> TimeoutError()
{
await Task.Delay(20000);
return new TranslateResponseWrapper
{
Done = false,
Error = new Error
{
Reason = "Timeout of async operation"
}
};
}
private string ConvertToBase64String(Stream stream)
{
var ms = new MemoryStream();
stream.CopyTo(ms);
return Convert.ToBase64String(ms.ToArray());
}
}
}